1 module Text.Templating.Heist.Splices.Html where 2 3 ------------------------------------------------------------------------------ 4 import Data.Maybe 5 import Data.Text (Text) 6 import qualified Text.XmlHtml as X 7 8 ------------------------------------------------------------------------------ 9 import Text.Templating.Heist.Internal 10 import Text.Templating.Heist.Types 11 12 13 ------------------------------------------------------------------------------ 14 -- | Name for the html splice. 15 htmlTag :: Text 16 htmlTag = "html" 17 18 19 ------------------------------------------------------------------------------ 20 -- | The html splice runs all children and then traverses the returned node 21 -- forest removing all head nodes. Then it merges them all and prepends it to 22 -- the html tag's child list. 23 htmlImpl :: Monad m => Splice m 24 htmlImpl = do 25 node <- getParamNode 26 children <- runNodeList $ X.childNodes node 27 let (heads, mnode) = extractHeads $ node { X.elementChildren = children } 28 new (X.Element t a c) = X.Element t a $ 29 X.Element "head" [] heads : c 30 new n = n 31 stopRecursion 32 return [maybe node new mnode] 33 34 ------------------------------------------------------------------------------ 35 -- | Extracts all heads from a node tree. 36 extractHeads :: X.Node 37 -- ^ The root (html) node 38 -> ([X.Node], Maybe X.Node) 39 -- ^ A tuple of a list of head nodes and the original tree with 40 -- heads removed. 41 extractHeads (X.Element t a c) 42 | t == "head" = (c, Nothing) 43 | otherwise = (concat heads, Just $ X.Element t a (catMaybes mcs)) 44 where 45 (heads, mcs) = unzip $ map extractHeads c 46 extractHeads n = ([], Just n) 47