{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Heist.Interpreted.Internal where
import Blaze.ByteString.Builder
import Control.Monad
import Control.Monad.State.Strict
import qualified Data.Attoparsec.Text as AP
import Data.ByteString (ByteString)
import qualified Data.HashMap.Strict as Map
import qualified Data.HeterogeneousEnvironment as HE
import Data.Map.Syntax
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
import qualified Text.XmlHtml as X
import Heist.Common
import Heist.Internal.Types.HeistState
type Splice n = HeistT n n Template
bindSplice :: Text
-> Splice n
-> HeistState n
-> HeistState n
bindSplice :: forall (n :: * -> *).
Text -> Splice n -> HeistState n -> HeistState n
bindSplice Text
n Splice n
v HeistState n
hs = HeistState n
hs {_spliceMap = Map.insert n v (_spliceMap hs)}
bindSplices :: Splices (Splice n)
-> HeistState n
-> HeistState n
bindSplices :: forall (n :: * -> *).
Splices (Splice n) -> HeistState n -> HeistState n
bindSplices Splices (Splice n)
ss HeistState n
hs =
HeistState n
hs { _spliceMap = applySpliceMap hs _spliceMap ss }
textSplice :: Monad m => Text -> HeistT n m Template
textSplice :: forall (m :: * -> *) (n :: * -> *).
Monad m =>
Text -> HeistT n m Template
textSplice Text
t = Template -> HeistT n m Template
forall a. a -> HeistT n m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Text -> Node
X.TextNode Text
t]
runChildren :: Monad n => Splice n
runChildren :: forall (n :: * -> *). Monad n => Splice n
runChildren = Template -> Splice n
forall (n :: * -> *). Monad n => Template -> Splice n
runNodeList (Template -> Splice n) -> (Node -> Template) -> Node -> Splice n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Node -> Template
X.childNodes (Node -> Splice n) -> HeistT n n Node -> Splice n
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< HeistT n n Node
forall (m :: * -> *) (n :: * -> *). Monad m => HeistT n m Node
getParamNode
runChildrenWith :: (Monad n)
=> Splices (Splice n)
-> Splice n
runChildrenWith :: forall (n :: * -> *). Monad n => Splices (Splice n) -> Splice n
runChildrenWith Splices (Splice n)
splices = (HeistState n -> HeistState n) -> Splice n -> Splice n
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
(HeistState n -> HeistState n) -> HeistT n m a -> HeistT n m a
localHS (Splices (Splice n) -> HeistState n -> HeistState n
forall (n :: * -> *).
Splices (Splice n) -> HeistState n -> HeistState n
bindSplices Splices (Splice n)
splices) Splice n
forall (n :: * -> *). Monad n => Splice n
runChildren
runChildrenWithTrans :: (Monad n)
=> (b -> Splice n)
-> Splices b
-> Splice n
runChildrenWithTrans :: forall (n :: * -> *) b.
Monad n =>
(b -> Splice n) -> Splices b -> Splice n
runChildrenWithTrans b -> Splice n
f = Splices (Splice n) -> Splice n
forall (n :: * -> *). Monad n => Splices (Splice n) -> Splice n
runChildrenWith (Splices (Splice n) -> Splice n)
-> (Splices b -> Splices (Splice n)) -> Splices b -> Splice n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> Splice n) -> Splices b -> Splices (Splice n)
forall v1 v2 k a. (v1 -> v2) -> MapSyntaxM k v1 a -> MapSyntax k v2
mapV b -> Splice n
f
runChildrenWithTemplates :: (Monad n) => Splices Template -> Splice n
runChildrenWithTemplates :: forall (n :: * -> *). Monad n => Splices Template -> Splice n
runChildrenWithTemplates = (Template -> Splice n) -> Splices Template -> Splice n
forall (n :: * -> *) b.
Monad n =>
(b -> Splice n) -> Splices b -> Splice n
runChildrenWithTrans Template -> Splice n
forall a. a -> HeistT n n a
forall (m :: * -> *) a. Monad m => a -> m a
return
runChildrenWithText :: (Monad n) => Splices Text -> Splice n
runChildrenWithText :: forall (n :: * -> *). Monad n => Splices Text -> Splice n
runChildrenWithText = (Text -> Splice n) -> Splices Text -> Splice n
forall (n :: * -> *) b.
Monad n =>
(b -> Splice n) -> Splices b -> Splice n
runChildrenWithTrans Text -> Splice n
forall (m :: * -> *) (n :: * -> *).
Monad m =>
Text -> HeistT n m Template
textSplice
lookupSplice :: Text
-> HeistState n
-> Maybe (Splice n)
lookupSplice :: forall (n :: * -> *). Text -> HeistState n -> Maybe (Splice n)
lookupSplice Text
nm HeistState n
hs = Text -> HashMap Text (Splice n) -> Maybe (Splice n)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
Map.lookup Text
nm (HashMap Text (Splice n) -> Maybe (Splice n))
-> HashMap Text (Splice n) -> Maybe (Splice n)
forall a b. (a -> b) -> a -> b
$ HeistState n -> HashMap Text (Splice n)
forall (m :: * -> *).
HeistState m -> HashMap Text (HeistT m m Template)
_spliceMap HeistState n
hs
{-# INLINE lookupSplice #-}
addTemplate :: ByteString
-> Template
-> Maybe FilePath
-> HeistState n
-> HeistState n
addTemplate :: forall (n :: * -> *).
ByteString
-> Template -> Maybe FilePath -> HeistState n -> HeistState n
addTemplate ByteString
n Template
t Maybe FilePath
mfp HeistState n
st =
TPath -> DocumentFile -> HeistState n -> HeistState n
forall (n :: * -> *).
TPath -> DocumentFile -> HeistState n -> HeistState n
insertTemplate (ByteString -> TPath
splitTemplatePath ByteString
n) DocumentFile
doc HeistState n
st
where
doc :: DocumentFile
doc = Document -> Maybe FilePath -> DocumentFile
DocumentFile (Encoding -> Maybe DocType -> Template -> Document
X.HtmlDocument Encoding
X.UTF8 Maybe DocType
forall a. Maybe a
Nothing Template
t) Maybe FilePath
mfp
addXMLTemplate :: ByteString
-> Template
-> Maybe FilePath
-> HeistState n
-> HeistState n
addXMLTemplate :: forall (n :: * -> *).
ByteString
-> Template -> Maybe FilePath -> HeistState n -> HeistState n
addXMLTemplate ByteString
n Template
t Maybe FilePath
mfp HeistState n
st =
TPath -> DocumentFile -> HeistState n -> HeistState n
forall (n :: * -> *).
TPath -> DocumentFile -> HeistState n -> HeistState n
insertTemplate (ByteString -> TPath
splitTemplatePath ByteString
n) DocumentFile
doc HeistState n
st
where
doc :: DocumentFile
doc = Document -> Maybe FilePath -> DocumentFile
DocumentFile (Encoding -> Maybe DocType -> Template -> Document
X.XmlDocument Encoding
X.UTF8 Maybe DocType
forall a. Maybe a
Nothing Template
t) Maybe FilePath
mfp
stopRecursion :: Monad m => HeistT n m ()
stopRecursion :: forall (m :: * -> *) (n :: * -> *). Monad m => HeistT n m ()
stopRecursion = (HeistState n -> HeistState n) -> HeistT n m ()
forall (m :: * -> *) (n :: * -> *).
Monad m =>
(HeistState n -> HeistState n) -> HeistT n m ()
modifyHS (\HeistState n
st -> HeistState n
st { _recurse = False })
runNode :: Monad n => X.Node -> Splice n
runNode :: forall (n :: * -> *). Monad n => Node -> Splice n
runNode (X.Element Text
nm [(Text, Text)]
at Template
ch) = do
newAtts <- [(Text, Text)] -> HeistT n n [(Text, Text)]
forall (n :: * -> *).
Monad n =>
[(Text, Text)] -> HeistT n n [(Text, Text)]
runAttributes [(Text, Text)]
at
let n = Text -> [(Text, Text)] -> Template -> Node
X.Element Text
nm [(Text, Text)]
newAtts Template
ch
s <- liftM (lookupSplice nm) getHS
maybe (runKids newAtts) (recurseSplice n) s
where
runKids :: [(Text, Text)] -> HeistT n n Template
runKids [(Text, Text)]
newAtts = do
newKids <- Template -> HeistT n n Template
forall (n :: * -> *). Monad n => Template -> Splice n
runNodeList Template
ch
return [X.Element nm newAtts newKids]
runNode Node
n = Template -> Splice n
forall a. a -> HeistT n n a
forall (m :: * -> *) a. Monad m => a -> m a
return [Node
n]
runAttributes :: Monad n => [(Text, Text)] -> HeistT n n [(Text, Text)]
runAttributes :: forall (n :: * -> *).
Monad n =>
[(Text, Text)] -> HeistT n n [(Text, Text)]
runAttributes [(Text, Text)]
attrs = ([(Text, Text)] -> HeistT n n [(Text, Text)]
forall a. a -> HeistT n n a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Text, Text)] -> HeistT n n [(Text, Text)])
-> ([[(Text, Text)]] -> [(Text, Text)])
-> [[(Text, Text)]]
-> HeistT n n [(Text, Text)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[(Text, Text)]] -> [(Text, Text)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat) ([[(Text, Text)]] -> HeistT n n [(Text, Text)])
-> HeistT n n [[(Text, Text)]] -> HeistT n n [(Text, Text)]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ((Text, Text) -> HeistT n n [(Text, Text)])
-> [(Text, Text)] -> HeistT n n [[(Text, Text)]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Text, Text) -> HeistT n n [(Text, Text)]
forall (n :: * -> *).
Monad n =>
(Text, Text) -> HeistT n n [(Text, Text)]
runAttrSplice [(Text, Text)]
attrs
runAttrSplice :: (Monad n) => (Text, Text) -> HeistT n n [(Text, Text)]
runAttrSplice :: forall (n :: * -> *).
Monad n =>
(Text, Text) -> HeistT n n [(Text, Text)]
runAttrSplice a :: (Text, Text)
a@(Text
k,Text
v) = do
splice <- (HeistState n -> Maybe (AttrSplice n))
-> HeistT n n (Maybe (AttrSplice n))
forall (m :: * -> *) (n :: * -> *) r.
Monad m =>
(HeistState n -> r) -> HeistT n m r
getsHS (Text -> HashMap Text (AttrSplice n) -> Maybe (AttrSplice n)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
Map.lookup Text
k (HashMap Text (AttrSplice n) -> Maybe (AttrSplice n))
-> (HeistState n -> HashMap Text (AttrSplice n))
-> HeistState n
-> Maybe (AttrSplice n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HeistState n -> HashMap Text (AttrSplice n)
forall (m :: * -> *). HeistState m -> HashMap Text (AttrSplice m)
_attrSpliceMap)
maybe (liftM (:[]) $ attSubst a)
(lift . flip evalStateT HE.empty . unRT . ($ v)) splice
attSubst :: (Monad n) => (t, Text) -> HeistT n n (t, Text)
attSubst :: forall (n :: * -> *) t.
Monad n =>
(t, Text) -> HeistT n n (t, Text)
attSubst (t
n,Text
v) = do
v' <- Text -> HeistT n n Text
forall (n :: * -> *). Monad n => Text -> HeistT n n Text
parseAtt Text
v
return (n,v')
parseAtt :: (Monad n) => Text -> HeistT n n Text
parseAtt :: forall (n :: * -> *). Monad n => Text -> HeistT n n Text
parseAtt Text
bs = do
let ast :: [AttAST]
ast = case IResult Text [AttAST] -> Text -> IResult Text [AttAST]
forall i r. Monoid i => IResult i r -> i -> IResult i r
AP.feed (Parser [AttAST] -> Text -> IResult Text [AttAST]
forall a. Parser a -> Text -> Result a
AP.parse Parser [AttAST]
attParser Text
bs) Text
"" of
(AP.Done Text
_ [AttAST]
res) -> [AttAST]
res
(AP.Fail Text
_ [FilePath]
_ FilePath
_) -> []
(AP.Partial Text -> IResult Text [AttAST]
_) -> []
chunks <- (AttAST -> HeistT n n Text) -> [AttAST] -> HeistT n n [Text]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM AttAST -> HeistT n n Text
forall {m :: * -> *}. Monad m => AttAST -> HeistT m m Text
cvt [AttAST]
ast
return $ T.concat chunks
where
cvt :: AttAST -> HeistT m m Text
cvt (Literal Text
x) = Text -> HeistT m m Text
forall a. a -> HeistT m m a
forall (m :: * -> *) a. Monad m => a -> m a
return Text
x
cvt (Ident Text
x) =
(Node -> Node) -> HeistT m m Text -> HeistT m m Text
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
(Node -> Node) -> HeistT n m a -> HeistT n m a
localParamNode (Node -> Node -> Node
forall a b. a -> b -> a
const (Node -> Node -> Node) -> Node -> Node -> Node
forall a b. (a -> b) -> a -> b
$ Text -> [(Text, Text)] -> Template -> Node
X.Element Text
x [] []) (HeistT m m Text -> HeistT m m Text)
-> HeistT m m Text -> HeistT m m Text
forall a b. (a -> b) -> a -> b
$ Text -> HeistT m m Text
forall (n :: * -> *). Monad n => Text -> HeistT n n Text
getAttributeSplice Text
x
getAttributeSplice :: Monad n => Text -> HeistT n n Text
getAttributeSplice :: forall (n :: * -> *). Monad n => Text -> HeistT n n Text
getAttributeSplice Text
name = do
hs <- HeistT n n (HeistState n)
forall (m :: * -> *) (n :: * -> *).
Monad m =>
HeistT n m (HeistState n)
getHS
let noSplice = Text -> m Text
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> m Text) -> Text -> m Text
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.concat [Text
"${", Text
name, Text
"}"]
s = Text -> HeistState n -> Maybe (Splice n)
forall (n :: * -> *). Text -> HeistState n -> Maybe (Splice n)
lookupSplice Text
name HeistState n
hs
maybe noSplice (liftM (T.concat . map X.nodeText)) s
runNodeList :: Monad n => [X.Node] -> Splice n
runNodeList :: forall (n :: * -> *). Monad n => Template -> Splice n
runNodeList = (Node -> HeistT n n Template) -> Template -> HeistT n n Template
forall (m :: * -> *) b a.
(Monad m, Monoid b) =>
(a -> m b) -> [a] -> m b
mapSplices Node -> HeistT n n Template
forall (n :: * -> *). Monad n => Node -> Splice n
runNode
{-# INLINE runNodeList #-}
mAX_RECURSION_DEPTH :: Int
mAX_RECURSION_DEPTH :: Int
mAX_RECURSION_DEPTH = Int
50
recurseSplice :: Monad n => X.Node -> Splice n -> Splice n
recurseSplice :: forall (n :: * -> *). Monad n => Node -> Splice n -> Splice n
recurseSplice Node
node Splice n
splice = do
result <- (Node -> Node) -> Splice n -> Splice n
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
(Node -> Node) -> HeistT n m a -> HeistT n m a
localParamNode (Node -> Node -> Node
forall a b. a -> b -> a
const Node
node) Splice n
splice
hs <- getHS
if _recurse hs
then if _recursionDepth hs < mAX_RECURSION_DEPTH
then do modRecursionDepth (+1)
res <- runNodeList result
restoreHS hs
return res
else return result `orError` err
else do modifyHS (\HeistState n
st -> HeistState n
st { _recurse = True })
return result
where
err :: FilePath
err = [FilePath] -> FilePath
unwords
[FilePath
"Recursion limit reached in node"
,FilePath
"<"FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++(Text -> FilePath
T.unpack (Text -> FilePath) -> Text -> FilePath
forall a b. (a -> b) -> a -> b
$ Node -> Text
X.elementTag Node
node)FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++FilePath
">. You"
,FilePath
"probably have infinite splice recursion!"
]
lookupAndRun :: Monad m
=> ByteString
-> ((DocumentFile, TPath) -> HeistT n m (Maybe a))
-> HeistT n m (Maybe a)
lookupAndRun :: forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
ByteString
-> ((DocumentFile, TPath) -> HeistT n m (Maybe a))
-> HeistT n m (Maybe a)
lookupAndRun ByteString
name (DocumentFile, TPath) -> HeistT n m (Maybe a)
k = do
hs <- HeistT n m (HeistState n)
forall (m :: * -> *) (n :: * -> *).
Monad m =>
HeistT n m (HeistState n)
getHS
let mt = ByteString
-> HeistState n
-> (HeistState n -> HashMap TPath DocumentFile)
-> Maybe (DocumentFile, TPath)
forall (n :: * -> *) t.
ByteString
-> HeistState n
-> (HeistState n -> HashMap TPath t)
-> Maybe (t, TPath)
lookupTemplate ByteString
name HeistState n
hs HeistState n -> HashMap TPath DocumentFile
forall (m :: * -> *). HeistState m -> HashMap TPath DocumentFile
_templateMap
let curPath = Maybe (Maybe FilePath) -> Maybe FilePath
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe FilePath) -> Maybe FilePath)
-> Maybe (Maybe FilePath) -> Maybe FilePath
forall a b. (a -> b) -> a -> b
$ ((DocumentFile, TPath) -> Maybe FilePath)
-> Maybe (DocumentFile, TPath) -> Maybe (Maybe FilePath)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (DocumentFile -> Maybe FilePath
dfFile (DocumentFile -> Maybe FilePath)
-> ((DocumentFile, TPath) -> DocumentFile)
-> (DocumentFile, TPath)
-> Maybe FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DocumentFile, TPath) -> DocumentFile
forall a b. (a, b) -> a
fst) Maybe (DocumentFile, TPath)
mt
modifyHS (setCurTemplateFile curPath)
maybe (return Nothing) k mt
evalTemplate :: Monad n
=> ByteString
-> HeistT n n (Maybe Template)
evalTemplate :: forall (n :: * -> *).
Monad n =>
ByteString -> HeistT n n (Maybe Template)
evalTemplate ByteString
name = ByteString
-> ((DocumentFile, TPath) -> HeistT n n (Maybe Template))
-> HeistT n n (Maybe Template)
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
ByteString
-> ((DocumentFile, TPath) -> HeistT n m (Maybe a))
-> HeistT n m (Maybe a)
lookupAndRun ByteString
name
(\(DocumentFile
t,TPath
ctx) -> (HeistState n -> HeistState n)
-> HeistT n n (Maybe Template) -> HeistT n n (Maybe Template)
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
(HeistState n -> HeistState n) -> HeistT n m a -> HeistT n m a
localHS (\HeistState n
hs -> HeistState n
hs {_curContext = ctx})
((Template -> Maybe Template)
-> HeistT n n Template -> HeistT n n (Maybe Template)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Template -> Maybe Template
forall a. a -> Maybe a
Just (HeistT n n Template -> HeistT n n (Maybe Template))
-> HeistT n n Template -> HeistT n n (Maybe Template)
forall a b. (a -> b) -> a -> b
$ Template -> HeistT n n Template
forall (n :: * -> *). Monad n => Template -> Splice n
runNodeList (Template -> HeistT n n Template)
-> Template -> HeistT n n Template
forall a b. (a -> b) -> a -> b
$ Document -> Template
X.docContent (Document -> Template) -> Document -> Template
forall a b. (a -> b) -> a -> b
$ DocumentFile -> Document
dfDoc DocumentFile
t))
fixDocType :: Monad m => X.Document -> HeistT n m X.Document
fixDocType :: forall (m :: * -> *) (n :: * -> *).
Monad m =>
Document -> HeistT n m Document
fixDocType Document
d = do
dts <- (HeistState n -> [DocType]) -> HeistT n m [DocType]
forall (m :: * -> *) (n :: * -> *) r.
Monad m =>
(HeistState n -> r) -> HeistT n m r
getsHS HeistState n -> [DocType]
forall (m :: * -> *). HeistState m -> [DocType]
_doctypes
return $ d { X.docType = listToMaybe dts }
evalWithDoctypes :: Monad n
=> ByteString
-> HeistT n n (Maybe X.Document)
evalWithDoctypes :: forall (n :: * -> *).
Monad n =>
ByteString -> HeistT n n (Maybe Document)
evalWithDoctypes ByteString
name = ByteString
-> ((DocumentFile, TPath) -> HeistT n n (Maybe Document))
-> HeistT n n (Maybe Document)
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
ByteString
-> ((DocumentFile, TPath) -> HeistT n m (Maybe a))
-> HeistT n m (Maybe a)
lookupAndRun ByteString
name (((DocumentFile, TPath) -> HeistT n n (Maybe Document))
-> HeistT n n (Maybe Document))
-> ((DocumentFile, TPath) -> HeistT n n (Maybe Document))
-> HeistT n n (Maybe Document)
forall a b. (a -> b) -> a -> b
$ \(DocumentFile
t,TPath
ctx) -> do
[DocType] -> HeistT n n ()
forall (m :: * -> *) (n :: * -> *).
Monad m =>
[DocType] -> HeistT n m ()
addDoctype ([DocType] -> HeistT n n ()) -> [DocType] -> HeistT n n ()
forall a b. (a -> b) -> a -> b
$ Maybe DocType -> [DocType]
forall a. Maybe a -> [a]
maybeToList (Maybe DocType -> [DocType]) -> Maybe DocType -> [DocType]
forall a b. (a -> b) -> a -> b
$ Document -> Maybe DocType
X.docType (Document -> Maybe DocType) -> Document -> Maybe DocType
forall a b. (a -> b) -> a -> b
$ DocumentFile -> Document
dfDoc DocumentFile
t
hs <- HeistT n n (HeistState n)
forall (m :: * -> *) (n :: * -> *).
Monad m =>
HeistT n m (HeistState n)
getHS
let nodes = Document -> Template
X.docContent (Document -> Template) -> Document -> Template
forall a b. (a -> b) -> a -> b
$ DocumentFile -> Document
dfDoc DocumentFile
t
putHS (hs {_curContext = ctx})
newNodes <- runNodeList nodes
restoreHS hs
newDoc <- fixDocType $ (dfDoc t) { X.docContent = newNodes }
return (Just newDoc)
bindStrings :: Monad n
=> Splices Text
-> HeistState n
-> HeistState n
bindStrings :: forall (n :: * -> *).
Monad n =>
Splices Text -> HeistState n -> HeistState n
bindStrings Splices Text
splices = Splices (Splice n) -> HeistState n -> HeistState n
forall (n :: * -> *).
Splices (Splice n) -> HeistState n -> HeistState n
bindSplices ((Text -> Splice n) -> Splices Text -> Splices (Splice n)
forall v1 v2 k a. (v1 -> v2) -> MapSyntaxM k v1 a -> MapSyntax k v2
mapV Text -> Splice n
forall (m :: * -> *) (n :: * -> *).
Monad m =>
Text -> HeistT n m Template
textSplice Splices Text
splices)
bindString :: Monad n
=> Text
-> Text
-> HeistState n
-> HeistState n
bindString :: forall (n :: * -> *).
Monad n =>
Text -> Text -> HeistState n -> HeistState n
bindString Text
n = Text -> Splice n -> HeistState n -> HeistState n
forall (n :: * -> *).
Text -> Splice n -> HeistState n -> HeistState n
bindSplice Text
n (Splice n -> HeistState n -> HeistState n)
-> (Text -> Splice n) -> Text -> HeistState n -> HeistState n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Splice n
forall (m :: * -> *) (n :: * -> *).
Monad m =>
Text -> HeistT n m Template
textSplice
callTemplate :: Monad n
=> ByteString
-> Splices (Splice n)
-> HeistT n n Template
callTemplate :: forall (n :: * -> *).
Monad n =>
ByteString -> Splices (Splice n) -> Splice n
callTemplate ByteString
name Splices (Splice n)
splices = do
(HeistState n -> HeistState n) -> HeistT n n ()
forall (m :: * -> *) (n :: * -> *).
Monad m =>
(HeistState n -> HeistState n) -> HeistT n m ()
modifyHS ((HeistState n -> HeistState n) -> HeistT n n ())
-> (HeistState n -> HeistState n) -> HeistT n n ()
forall a b. (a -> b) -> a -> b
$ Splices (Splice n) -> HeistState n -> HeistState n
forall (n :: * -> *).
Splices (Splice n) -> HeistState n -> HeistState n
bindSplices Splices (Splice n)
splices
(Maybe Template -> Template)
-> HeistT n n (Maybe Template) -> Splice n
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (Template -> (Template -> Template) -> Maybe Template -> Template
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Template -> Template
forall a. a -> a
id) (HeistT n n (Maybe Template) -> Splice n)
-> HeistT n n (Maybe Template) -> Splice n
forall a b. (a -> b) -> a -> b
$ ByteString -> HeistT n n (Maybe Template)
forall (n :: * -> *).
Monad n =>
ByteString -> HeistT n n (Maybe Template)
evalTemplate ByteString
name
callTemplateWithText :: Monad n
=> ByteString
-> Splices Text
-> HeistT n n Template
callTemplateWithText :: forall (n :: * -> *).
Monad n =>
ByteString -> Splices Text -> HeistT n n Template
callTemplateWithText ByteString
name Splices Text
splices = ByteString -> Splices (Splice n) -> Splice n
forall (n :: * -> *).
Monad n =>
ByteString -> Splices (Splice n) -> Splice n
callTemplate ByteString
name (Splices (Splice n) -> Splice n) -> Splices (Splice n) -> Splice n
forall a b. (a -> b) -> a -> b
$ (Text -> Splice n) -> Splices Text -> Splices (Splice n)
forall v1 v2 k a. (v1 -> v2) -> MapSyntaxM k v1 a -> MapSyntax k v2
mapV Text -> Splice n
forall (m :: * -> *) (n :: * -> *).
Monad m =>
Text -> HeistT n m Template
textSplice Splices Text
splices
renderTemplate :: Monad n
=> HeistState n
-> ByteString
-> n (Maybe (Builder, MIMEType))
renderTemplate :: forall (n :: * -> *).
Monad n =>
HeistState n -> ByteString -> n (Maybe (Builder, ByteString))
renderTemplate HeistState n
hs ByteString
name = HeistT n n (Maybe (Builder, ByteString))
-> Node -> HeistState n -> n (Maybe (Builder, ByteString))
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
HeistT n m a -> Node -> HeistState n -> m a
evalHeistT HeistT n n (Maybe (Builder, ByteString))
forall {n :: * -> *}.
Monad n =>
HeistT n n (Maybe (Builder, ByteString))
tpl (Text -> Node
X.TextNode Text
"") HeistState n
hs
where tpl :: HeistT n n (Maybe (Builder, ByteString))
tpl = do mt <- ByteString -> HeistT n n (Maybe Document)
forall (n :: * -> *).
Monad n =>
ByteString -> HeistT n n (Maybe Document)
evalWithDoctypes ByteString
name
case mt of
Maybe Document
Nothing -> Maybe (Builder, ByteString)
-> HeistT n n (Maybe (Builder, ByteString))
forall a. a -> HeistT n n a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Builder, ByteString)
forall a. Maybe a
Nothing
Just Document
doc -> Maybe (Builder, ByteString)
-> HeistT n n (Maybe (Builder, ByteString))
forall a. a -> HeistT n n a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Builder, ByteString)
-> HeistT n n (Maybe (Builder, ByteString)))
-> Maybe (Builder, ByteString)
-> HeistT n n (Maybe (Builder, ByteString))
forall a b. (a -> b) -> a -> b
$ (Builder, ByteString) -> Maybe (Builder, ByteString)
forall a. a -> Maybe a
Just ((Builder, ByteString) -> Maybe (Builder, ByteString))
-> (Builder, ByteString) -> Maybe (Builder, ByteString)
forall a b. (a -> b) -> a -> b
$ (Document -> Builder
X.render Document
doc, Document -> ByteString
mimeType Document
doc)
renderWithArgs :: Monad n
=> Splices Text
-> HeistState n
-> ByteString
-> n (Maybe (Builder, MIMEType))
renderWithArgs :: forall (n :: * -> *).
Monad n =>
Splices Text
-> HeistState n -> ByteString -> n (Maybe (Builder, ByteString))
renderWithArgs Splices Text
args HeistState n
hs = HeistState n -> ByteString -> n (Maybe (Builder, ByteString))
forall (n :: * -> *).
Monad n =>
HeistState n -> ByteString -> n (Maybe (Builder, ByteString))
renderTemplate (Splices Text -> HeistState n -> HeistState n
forall (n :: * -> *).
Monad n =>
Splices Text -> HeistState n -> HeistState n
bindStrings Splices Text
args HeistState n
hs)
renderTemplateToDoc :: Monad n
=> HeistState n
-> ByteString
-> n (Maybe X.Document)
renderTemplateToDoc :: forall (n :: * -> *).
Monad n =>
HeistState n -> ByteString -> n (Maybe Document)
renderTemplateToDoc HeistState n
hs ByteString
name =
HeistT n n (Maybe Document)
-> Node -> HeistState n -> n (Maybe Document)
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
HeistT n m a -> Node -> HeistState n -> m a
evalHeistT (ByteString -> HeistT n n (Maybe Document)
forall (n :: * -> *).
Monad n =>
ByteString -> HeistT n n (Maybe Document)
evalWithDoctypes ByteString
name) (Text -> Node
X.TextNode Text
"") HeistState n
hs