{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns        #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}

module Heist.Splices.Json (
  bindJson
) where

------------------------------------------------------------------------------
import           Control.Monad.Reader
import           Data.Aeson
import qualified Data.ByteString.Char8       as S
import qualified Data.ByteString.Lazy.Char8  as L
#if MIN_VERSION_aeson(2,0,0)
import qualified Data.Aeson.KeyMap           as KM
import qualified Data.Aeson.Key              as K
import qualified Data.Foldable.WithIndex     as FI
#else
import qualified Data.HashMap.Strict         as Map
#endif
import           Data.Map.Syntax
import           Data.Maybe
import           Data.Text                   (Text)
import qualified Data.Text                   as T
import qualified Data.Text.Encoding          as T
import qualified Data.Vector                 as V
import           Text.Blaze.Html5            ((!))
import qualified Text.Blaze.Html5            as B
import           Text.Blaze.Renderer.XmlHtml
import           Text.XmlHtml
------------------------------------------------------------------------------
import           Heist.Interpreted.Internal
import           Heist.Internal.Types.HeistState
------------------------------------------------------------------------------

                                 ------------
                                 -- public --
                                 ------------

------------------------------------------------------------------------------
-- | This splice binds convenience tags for the given JSON (or
-- JSON-convertible) value and runs the tag's child nodes using the new
-- bindings.
--
-- /Tags bound when you pass in an object/
--
-- Tags bound for an object looking like this:
--
-- > { "k_1": v_1, ..., "k_N": v_N }
--
-- @\<value:{k_i}\>@    -- treats v_i as text
-- @\<snippet:{k_i}\>@  -- treats v_i as HTML
-- @\<with:{k_i}\>@     -- explodes v_i and runs its children
--
-- @\<value var=\"foo.bar.baz\"\/>@ -- walks the JSON tree to find
-- \"foo.bar.baz\", and interprets it as a string
-- @\<snippet var=\"foo.bar.baz\"\/\>@
-- @\<with var=\"foo.bar.baz\"\>...\<with\>@
--
-- /Tags bound when you pass in anything else/
--
-- @\<value\/\>@    --  the given JSON value, as a string
-- @\<snippet\/\>@  --  the given JSON value, parsed and spliced in as HTML
--
bindJson :: (ToJSON a, Monad n) => a -> Splice n
bindJson :: a -> Splice n
bindJson = ReaderT Value (HeistT n n) [Node] -> Value -> Splice n
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT Value (HeistT n n) [Node]
forall (n :: * -> *). Monad n => JsonMonad n n [Node]
explodeTag (Value -> Splice n) -> (a -> Value) -> a -> Splice n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value
forall a. ToJSON a => a -> Value
toJSON


                                 -------------
                                 -- private --
                                 -------------

------------------------------------------------------------------------------
errorMessage :: String -> [Node]
errorMessage :: String -> [Node]
errorMessage String
s = Html -> [Node]
renderHtmlNodes (Html -> [Node]) -> Html -> [Node]
forall a b. (a -> b) -> a -> b
$
                     Html -> Html
B.strong (Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! Tag -> AttributeValue -> Attribute
B.customAttribute Tag
"class" AttributeValue
"error" (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$
                     String -> Html
forall a. ToMarkup a => a -> Html
B.toHtml String
s


------------------------------------------------------------------------------
type JsonMonad n m a = ReaderT Value (HeistT n m) a


------------------------------------------------------------------------------
withValue :: (Monad m) => Value -> JsonMonad n m a -> HeistT n m a
withValue :: Value -> JsonMonad n m a -> HeistT n m a
withValue = (JsonMonad n m a -> Value -> HeistT n m a)
-> Value -> JsonMonad n m a -> HeistT n m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip JsonMonad n m a -> Value -> HeistT n m a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT


------------------------------------------------------------------------------
boolToText :: Bool -> Text
boolToText :: Bool -> Text
boolToText Bool
b = if Bool
b then Text
"true" else Text
"false"


------------------------------------------------------------------------------
numToText :: ToJSON a => a -> Text
numToText :: a -> Text
numToText = ByteString -> Text
T.decodeUtf8 (ByteString -> Text) -> (a -> ByteString) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> ByteString
S.concat ([ByteString] -> ByteString)
-> (a -> [ByteString]) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
L.toChunks (ByteString -> [ByteString])
-> (a -> ByteString) -> a -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ByteString
forall a. ToJSON a => a -> ByteString
encode

------------------------------------------------------------------------------
findExpr :: Text -> Value -> Maybe Value
findExpr :: Text -> Value -> Maybe Value
findExpr Text
t = [Text] -> Value -> Maybe Value
go ((Char -> Bool) -> Text -> [Text]
T.split (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'.') Text
t)
  where
    go :: [Text] -> Value -> Maybe Value
go [] !Value
value     = Value -> Maybe Value
forall a. a -> Maybe a
Just Value
value
    go (Text
x:[Text]
xs) !Value
value = Value -> Maybe Value
findIn Value
value Maybe Value -> (Value -> Maybe Value) -> Maybe Value
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [Text] -> Value -> Maybe Value
go [Text]
xs
      where
#if MIN_VERSION_aeson(2,0,0)
        findIn :: Value -> Maybe Value
findIn (Object Object
obj) = Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KM.lookup (Text -> Key
K.fromText Text
x) Object
obj
#else
        findIn (Object obj) = Map.lookup x obj
#endif
        findIn (Array Array
arr)  = Maybe Int
forall b. Read b => Maybe b
tryReadIndex Maybe Int -> (Int -> Maybe Value) -> Maybe Value
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
i -> Array
arr Array -> Int -> Maybe Value
forall a. Vector a -> Int -> Maybe a
V.!? Int
i
        findIn Value
_            = Maybe Value
forall a. Maybe a
Nothing

        tryReadIndex :: Maybe b
tryReadIndex = ((b, String) -> b) -> Maybe (b, String) -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (b, String) -> b
forall a b. (a, b) -> a
fst (Maybe (b, String) -> Maybe b)
-> (Text -> Maybe (b, String)) -> Text -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(b, String)] -> Maybe (b, String)
forall a. [a] -> Maybe a
listToMaybe ([(b, String)] -> Maybe (b, String))
-> (Text -> [(b, String)]) -> Text -> Maybe (b, String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReadS b
forall a. Read a => ReadS a
reads ReadS b -> (Text -> String) -> Text -> [(b, String)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack (Text -> Maybe b) -> Text -> Maybe b
forall a b. (a -> b) -> a -> b
$ Text
x


------------------------------------------------------------------------------
asHtml :: Monad m => Text -> m [Node]
asHtml :: Text -> m [Node]
asHtml Text
t =
    case (String -> ByteString -> Either String Document
parseHTML String
"" (ByteString -> Either String Document)
-> ByteString -> Either String Document
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
T.encodeUtf8 Text
t) of
      Left String
e  -> [Node] -> m [Node]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Node] -> m [Node]) -> [Node] -> m [Node]
forall a b. (a -> b) -> a -> b
$ String -> [Node]
errorMessage (String -> [Node]) -> String -> [Node]
forall a b. (a -> b) -> a -> b
$
                 String
"Template error turning JSON into HTML: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
e
      Right Document
d -> [Node] -> m [Node]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Node] -> m [Node]) -> [Node] -> m [Node]
forall a b. (a -> b) -> a -> b
$! Document -> [Node]
docContent Document
d


------------------------------------------------------------------------------
snippetTag :: Monad m => JsonMonad n m [Node]
snippetTag :: JsonMonad n m [Node]
snippetTag = ReaderT Value (HeistT n m) Value
forall r (m :: * -> *). MonadReader r m => m r
ask ReaderT Value (HeistT n m) Value
-> (Value -> JsonMonad n m [Node]) -> JsonMonad n m [Node]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> JsonMonad n m [Node]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) (n :: * -> *).
(MonadTrans t, Monad m) =>
Value -> t (HeistT n m) [Node]
snip
  where
    txt :: Text -> t m [Node]
txt Text
t = m [Node] -> t m [Node]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [Node] -> t m [Node]) -> m [Node] -> t m [Node]
forall a b. (a -> b) -> a -> b
$ Text -> m [Node]
forall (m :: * -> *). Monad m => Text -> m [Node]
asHtml Text
t

    snip :: Value -> t (HeistT n m) [Node]
snip Value
Null       = Text -> t (HeistT n m) [Node]
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(MonadTrans t, Monad m) =>
Text -> t m [Node]
txt Text
""
    snip (Bool Bool
b)   = Text -> t (HeistT n m) [Node]
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(MonadTrans t, Monad m) =>
Text -> t m [Node]
txt (Text -> t (HeistT n m) [Node]) -> Text -> t (HeistT n m) [Node]
forall a b. (a -> b) -> a -> b
$ Bool -> Text
boolToText Bool
b
    snip (Number Scientific
n) = Text -> t (HeistT n m) [Node]
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(MonadTrans t, Monad m) =>
Text -> t m [Node]
txt (Text -> t (HeistT n m) [Node]) -> Text -> t (HeistT n m) [Node]
forall a b. (a -> b) -> a -> b
$ Scientific -> Text
forall a. ToJSON a => a -> Text
numToText Scientific
n
    snip (String Text
t) = Text -> t (HeistT n m) [Node]
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(MonadTrans t, Monad m) =>
Text -> t m [Node]
txt Text
t
    snip Value
_          = HeistT n m [Node] -> t (HeistT n m) [Node]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (HeistT n m [Node] -> t (HeistT n m) [Node])
-> HeistT n m [Node] -> t (HeistT n m) [Node]
forall a b. (a -> b) -> a -> b
$ do
        Node
node <- HeistT n m Node
forall (m :: * -> *) (n :: * -> *). Monad m => HeistT n m Node
getParamNode
        [Node] -> HeistT n m [Node]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Node] -> HeistT n m [Node]) -> [Node] -> HeistT n m [Node]
forall a b. (a -> b) -> a -> b
$ String -> [Node]
errorMessage (String -> [Node]) -> String -> [Node]
forall a b. (a -> b) -> a -> b
$ [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [
                     String
"error processing tag <"
                   , Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"???" (Maybe Text -> Text) -> Maybe Text -> Text
forall a b. (a -> b) -> a -> b
$ Node -> Maybe Text
tagName Node
node
                   , String
">: can't interpret JSON arrays or objects as HTML."
                   ]


------------------------------------------------------------------------------
valueTag :: Monad m => JsonMonad n m [Node]
valueTag :: JsonMonad n m [Node]
valueTag = ReaderT Value (HeistT n m) Value
forall r (m :: * -> *). MonadReader r m => m r
ask ReaderT Value (HeistT n m) Value
-> (Value -> JsonMonad n m [Node]) -> JsonMonad n m [Node]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> JsonMonad n m [Node]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) (n :: * -> *).
(MonadTrans t, Monad m, Monad (t (HeistT n m))) =>
Value -> t (HeistT n m) [Node]
go
  where
    go :: Value -> t (HeistT n m) [Node]
go Value
Null       = Text -> t (HeistT n m) [Node]
forall (m :: * -> *). Monad m => Text -> m [Node]
txt Text
""
    go (Bool Bool
b)   = Text -> t (HeistT n m) [Node]
forall (m :: * -> *). Monad m => Text -> m [Node]
txt (Text -> t (HeistT n m) [Node]) -> Text -> t (HeistT n m) [Node]
forall a b. (a -> b) -> a -> b
$ Bool -> Text
boolToText Bool
b
    go (Number Scientific
n) = Text -> t (HeistT n m) [Node]
forall (m :: * -> *). Monad m => Text -> m [Node]
txt (Text -> t (HeistT n m) [Node]) -> Text -> t (HeistT n m) [Node]
forall a b. (a -> b) -> a -> b
$ Scientific -> Text
forall a. ToJSON a => a -> Text
numToText Scientific
n
    go (String Text
t) = Text -> t (HeistT n m) [Node]
forall (m :: * -> *). Monad m => Text -> m [Node]
txt Text
t
    go Value
_          = HeistT n m [Node] -> t (HeistT n m) [Node]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (HeistT n m [Node] -> t (HeistT n m) [Node])
-> HeistT n m [Node] -> t (HeistT n m) [Node]
forall a b. (a -> b) -> a -> b
$ do
        Node
node <- HeistT n m Node
forall (m :: * -> *) (n :: * -> *). Monad m => HeistT n m Node
getParamNode
        [Node] -> HeistT n m [Node]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Node] -> HeistT n m [Node]) -> [Node] -> HeistT n m [Node]
forall a b. (a -> b) -> a -> b
$ String -> [Node]
errorMessage (String -> [Node]) -> String -> [Node]
forall a b. (a -> b) -> a -> b
$ [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [
                     String
"error processing tag <"
                   , Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"???" (Maybe Text -> Text) -> Maybe Text -> Text
forall a b. (a -> b) -> a -> b
$ Node -> Maybe Text
tagName Node
node
                   , String
">: can't interpret JSON arrays or objects as text."
                   ]


    txt :: Text -> m [Node]
txt Text
t = [Node] -> m [Node]
forall (m :: * -> *) a. Monad m => a -> m a
return [Text -> Node
TextNode Text
t]


------------------------------------------------------------------------------
explodeTag :: forall n. (Monad n) => JsonMonad n n [Node]
explodeTag :: JsonMonad n n [Node]
explodeTag = ReaderT Value (HeistT n n) Value
forall r (m :: * -> *). MonadReader r m => m r
ask ReaderT Value (HeistT n n) Value
-> (Value -> JsonMonad n n [Node]) -> JsonMonad n n [Node]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> JsonMonad n n [Node]
go
  where
    --------------------------------------------------------------------------
    go :: Value -> JsonMonad n n [Node]
go Value
Null       = Text -> JsonMonad n n [Node]
forall (t :: (* -> *) -> * -> *) (n :: * -> *).
(MonadTrans t, Monad n) =>
Text -> t (HeistT n n) [Node]
goText Text
""
    go (Bool Bool
b)   = Text -> JsonMonad n n [Node]
forall (t :: (* -> *) -> * -> *) (n :: * -> *).
(MonadTrans t, Monad n) =>
Text -> t (HeistT n n) [Node]
goText (Text -> JsonMonad n n [Node]) -> Text -> JsonMonad n n [Node]
forall a b. (a -> b) -> a -> b
$ Bool -> Text
boolToText Bool
b
    go (Number Scientific
n) = Text -> JsonMonad n n [Node]
forall (t :: (* -> *) -> * -> *) (n :: * -> *).
(MonadTrans t, Monad n) =>
Text -> t (HeistT n n) [Node]
goText (Text -> JsonMonad n n [Node]) -> Text -> JsonMonad n n [Node]
forall a b. (a -> b) -> a -> b
$ Scientific -> Text
forall a. ToJSON a => a -> Text
numToText Scientific
n
    go (String Text
t) = Text -> JsonMonad n n [Node]
forall (t :: (* -> *) -> * -> *) (n :: * -> *).
(MonadTrans t, Monad n) =>
Text -> t (HeistT n n) [Node]
goText Text
t
    go (Array Array
a)  = Array -> JsonMonad n n [Node]
goArray Array
a
    go (Object Object
o) = Object -> JsonMonad n n [Node]
forall (f :: * -> *).
FoldableWithIndex Key f =>
f Value -> JsonMonad n n [Node]
goObject Object
o

    --------------------------------------------------------------------------
    goText :: Text -> t (HeistT n n) [Node]
goText Text
t = HeistT n n [Node] -> t (HeistT n n) [Node]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (HeistT n n [Node] -> t (HeistT n n) [Node])
-> HeistT n n [Node] -> t (HeistT n n) [Node]
forall a b. (a -> b) -> a -> b
$ Splices (HeistT n n [Node]) -> HeistT n n [Node]
forall (n :: * -> *). Monad n => Splices (Splice n) -> Splice n
runChildrenWith (Splices (HeistT n n [Node]) -> HeistT n n [Node])
-> Splices (HeistT n n [Node]) -> HeistT n n [Node]
forall a b. (a -> b) -> a -> b
$ do
        Text
"value"   Text -> HeistT n n [Node] -> Splices (HeistT n n [Node])
forall k v. k -> v -> MapSyntax k v
## [Node] -> HeistT n n [Node]
forall (m :: * -> *) a. Monad m => a -> m a
return [Text -> Node
TextNode Text
t]
        Text
"snippet" Text -> HeistT n n [Node] -> Splices (HeistT n n [Node])
forall k v. k -> v -> MapSyntax k v
## Text -> HeistT n n [Node]
forall (m :: * -> *). Monad m => Text -> m [Node]
asHtml Text
t

    --------------------------------------------------------------------------
    goArray :: V.Vector Value -> JsonMonad n n [Node]
    goArray :: Array -> JsonMonad n n [Node]
goArray Array
a = do
        HeistT n n () -> ReaderT Value (HeistT n n) ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift HeistT n n ()
forall (m :: * -> *) (n :: * -> *). Monad m => HeistT n m ()
stopRecursion
        [Node] -> [Node]
dl <- (([Node] -> [Node])
 -> Value -> ReaderT Value (HeistT n n) ([Node] -> [Node]))
-> ([Node] -> [Node])
-> Array
-> ReaderT Value (HeistT n n) ([Node] -> [Node])
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Vector b -> m a
V.foldM ([Node] -> [Node])
-> Value -> ReaderT Value (HeistT n n) ([Node] -> [Node])
forall c.
([Node] -> c) -> Value -> ReaderT Value (HeistT n n) ([Node] -> c)
f [Node] -> [Node]
forall a. a -> a
id Array
a
        [Node] -> JsonMonad n n [Node]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Node] -> JsonMonad n n [Node]) -> [Node] -> JsonMonad n n [Node]
forall a b. (a -> b) -> a -> b
$! [Node] -> [Node]
dl []
      where
        f :: ([Node] -> c) -> Value -> ReaderT Value (HeistT n n) ([Node] -> c)
f [Node] -> c
dl Value
jsonValue = do
            [Node]
tags <- Value -> JsonMonad n n [Node]
go Value
jsonValue
            ([Node] -> c) -> ReaderT Value (HeistT n n) ([Node] -> c)
forall (m :: * -> *) a. Monad m => a -> m a
return (([Node] -> c) -> ReaderT Value (HeistT n n) ([Node] -> c))
-> ([Node] -> c) -> ReaderT Value (HeistT n n) ([Node] -> c)
forall a b. (a -> b) -> a -> b
$! [Node] -> c
dl ([Node] -> c) -> ([Node] -> [Node]) -> [Node] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Node]
tags [Node] -> [Node] -> [Node]
forall a. [a] -> [a] -> [a]
++)

    --------------------------------------------------------------------------
    -- search the param node for attribute \"var=expr\", search the given JSON
    -- object for the expression, and if it's found run the JsonMonad action m
    -- using the restricted JSON object.
    varAttrTag :: Value -> (JsonMonad n n [Node]) -> Splice n
    varAttrTag :: Value -> JsonMonad n n [Node] -> Splice n
varAttrTag Value
v JsonMonad n n [Node]
m = do
        Node
node <- HeistT n n Node
forall (m :: * -> *) (n :: * -> *). Monad m => HeistT n m Node
getParamNode
        Splice n -> (Text -> Splice n) -> Maybe Text -> Splice n
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Node -> Splice n
forall (m :: * -> *). Monad m => Node -> m [Node]
noVar Node
node) (Node -> Text -> Splice n
hasVar Node
node) (Maybe Text -> Splice n) -> Maybe Text -> Splice n
forall a b. (a -> b) -> a -> b
$ Text -> Node -> Maybe Text
getAttribute Text
"var" Node
node
      where
        noVar :: Node -> m [Node]
noVar Node
node = [Node] -> m [Node]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Node] -> m [Node]) -> [Node] -> m [Node]
forall a b. (a -> b) -> a -> b
$ String -> [Node]
errorMessage (String -> [Node]) -> String -> [Node]
forall a b. (a -> b) -> a -> b
$
                     [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ String
"expression error: no var attribute in <"
                            , Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"???" (Maybe Text -> Text) -> Maybe Text -> Text
forall a b. (a -> b) -> a -> b
$ Node -> Maybe Text
tagName Node
node
                            , String
"> tag"
                            ]

        hasVar :: Node -> Text -> Splice n
hasVar Node
node Text
expr = Splice n -> (Value -> Splice n) -> Maybe Value -> Splice n
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Node] -> Splice n
forall (m :: * -> *) a. Monad m => a -> m a
return ([Node] -> Splice n) -> [Node] -> Splice n
forall a b. (a -> b) -> a -> b
$ String -> [Node]
errorMessage (String -> [Node]) -> String -> [Node]
forall a b. (a -> b) -> a -> b
$
                                  [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [
                                    String
"expression error: can't find \""
                                  , Text -> String
T.unpack Text
expr
                                  , String
"\" in JSON object (<"
                                  , Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"???" (Maybe Text -> Text) -> Maybe Text -> Text
forall a b. (a -> b) -> a -> b
$ Node -> Maybe Text
tagName Node
node
                                  , String
"> tag)"
                                  ])
                                 (JsonMonad n n [Node] -> Value -> Splice n
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT JsonMonad n n [Node]
m)
                                 (Text -> Value -> Maybe Value
findExpr Text
expr Value
v)

    --------------------------------------------------------------------------
    genericBindings :: JsonMonad n n (Splices (Splice n))
    genericBindings :: JsonMonad n n (Splices (Splice n))
genericBindings = ReaderT Value (HeistT n n) Value
forall r (m :: * -> *). MonadReader r m => m r
ask ReaderT Value (HeistT n n) Value
-> (Value -> JsonMonad n n (Splices (Splice n)))
-> JsonMonad n n (Splices (Splice n))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Value
v -> Splices (Splice n) -> JsonMonad n n (Splices (Splice n))
forall (m :: * -> *) a. Monad m => a -> m a
return (Splices (Splice n) -> JsonMonad n n (Splices (Splice n)))
-> Splices (Splice n) -> JsonMonad n n (Splices (Splice n))
forall a b. (a -> b) -> a -> b
$ do
        Text
"with"     Text -> Splice n -> Splices (Splice n)
forall k v. k -> v -> MapSyntax k v
## Value -> JsonMonad n n [Node] -> Splice n
varAttrTag Value
v JsonMonad n n [Node]
forall (n :: * -> *). Monad n => JsonMonad n n [Node]
explodeTag
        Text
"snippet"  Text -> Splice n -> Splices (Splice n)
forall k v. k -> v -> MapSyntax k v
## Value -> JsonMonad n n [Node] -> Splice n
varAttrTag Value
v JsonMonad n n [Node]
forall (m :: * -> *) (n :: * -> *). Monad m => JsonMonad n m [Node]
snippetTag
        Text
"value"    Text -> Splice n -> Splices (Splice n)
forall k v. k -> v -> MapSyntax k v
## Value -> JsonMonad n n [Node] -> Splice n
varAttrTag Value
v JsonMonad n n [Node]
forall (m :: * -> *) (n :: * -> *). Monad m => JsonMonad n m [Node]
valueTag


    --------------------------------------------------------------------------
    goObject :: f Value -> JsonMonad n n [Node]
goObject f Value
obj = do
        Splices (Splice n)
start <- JsonMonad n n (Splices (Splice n))
genericBindings
#if MIN_VERSION_aeson(2,0,0)
        let bindings :: Splices (Splice n)
bindings = (Key -> Splices (Splice n) -> Value -> Splices (Splice n))
-> Splices (Splice n) -> f Value -> Splices (Splice n)
forall i (f :: * -> *) b a.
FoldableWithIndex i f =>
(i -> b -> a -> b) -> b -> f a -> b
FI.ifoldl' ((Splices (Splice n) -> Key -> Value -> Splices (Splice n))
-> Key -> Splices (Splice n) -> Value -> Splices (Splice n)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Splices (Splice n) -> Key -> Value -> Splices (Splice n)
forall (m :: * -> *) a.
Monad m =>
MapSyntaxM Text (HeistT m m [Node]) a
-> Key -> Value -> MapSyntaxM Text (HeistT m m [Node]) ()
bindKvp) Splices (Splice n)
start  f Value
obj
#else
        let bindings = Map.foldlWithKey' bindKvp start obj
#endif
        Splice n -> JsonMonad n n [Node]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Splice n -> JsonMonad n n [Node])
-> Splice n -> JsonMonad n n [Node]
forall a b. (a -> b) -> a -> b
$ Splices (Splice n) -> Splice n
forall (n :: * -> *). Monad n => Splices (Splice n) -> Splice n
runChildrenWith Splices (Splice n)
bindings

    --------------------------------------------------------------------------
    
    bindKvp :: MapSyntaxM Text (HeistT m m [Node]) a
-> Key -> Value -> MapSyntaxM Text (HeistT m m [Node]) ()
bindKvp MapSyntaxM Text (HeistT m m [Node]) a
bindings Key
k Value
v =
#if MIN_VERSION_aeson(2,0,0)
        let k' :: Text
k' = Key -> Text
K.toText Key
k
#else
        let k' = k
#endif
            newBindings :: MapSyntaxM Text (HeistT m m [Node]) ()
newBindings = do
                Text -> Text -> Text
T.append Text
"with:" Text
k'    Text -> HeistT m m [Node] -> MapSyntaxM Text (HeistT m m [Node]) ()
forall k v. k -> v -> MapSyntax k v
## Value -> JsonMonad m m [Node] -> HeistT m m [Node]
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
Value -> JsonMonad n m a -> HeistT n m a
withValue Value
v JsonMonad m m [Node]
forall (n :: * -> *). Monad n => JsonMonad n n [Node]
explodeTag
                Text -> Text -> Text
T.append Text
"snippet:" Text
k' Text -> HeistT m m [Node] -> MapSyntaxM Text (HeistT m m [Node]) ()
forall k v. k -> v -> MapSyntax k v
## Value -> JsonMonad m m [Node] -> HeistT m m [Node]
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
Value -> JsonMonad n m a -> HeistT n m a
withValue Value
v JsonMonad m m [Node]
forall (m :: * -> *) (n :: * -> *). Monad m => JsonMonad n m [Node]
snippetTag
                Text -> Text -> Text
T.append Text
"value:" Text
k'   Text -> HeistT m m [Node] -> MapSyntaxM Text (HeistT m m [Node]) ()
forall k v. k -> v -> MapSyntax k v
## Value -> JsonMonad m m [Node] -> HeistT m m [Node]
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
Value -> JsonMonad n m a -> HeistT n m a
withValue Value
v JsonMonad m m [Node]
forall (m :: * -> *) (n :: * -> *). Monad m => JsonMonad n m [Node]
valueTag
        in  MapSyntaxM Text (HeistT m m [Node]) a
bindings MapSyntaxM Text (HeistT m m [Node]) a
-> MapSyntaxM Text (HeistT m m [Node]) ()
-> MapSyntaxM Text (HeistT m m [Node]) ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MapSyntaxM Text (HeistT m m [Node]) ()
forall (m :: * -> *).
Monad m =>
MapSyntaxM Text (HeistT m m [Node]) ()
newBindings