module Hakyll.Web.Template.Blaze
( Template
, applyTemplate
, applyTemplateList
, applyTemplateListWith
, toHtml
, safeToHtml
) where
import Hakyll (Context(..), Item, Compiler,
itemSetBody, missingField, itemBody)
import Data.Monoid (mappend)
import Data.List (intercalate)
import Text.Blaze.Html (Html)
import qualified Text.Blaze.Html as H
import Text.Blaze.Html.Renderer.String (renderHtml)
type Template m a = (String -> m String) -> Item a -> m Html
applyTemplate :: Template Compiler String
-> Context String
-> Item String
-> Compiler (Item String)
applyTemplate tpl ctx item =
tpl ctx' item
>>= return . renderHtml
>>= \body -> return $ itemSetBody body item
where
ctx' :: String -> Compiler String
ctx' key = unContext (ctx `mappend` missingField) key item
applyTemplateListWith :: String
-> Template Compiler String
-> Context String
-> [Item String]
-> Compiler String
applyTemplateListWith delimiter tpl ctx items =
mapM (applyTemplate tpl ctx) items
>>= return . intercalate delimiter . map itemBody
applyTemplateList = applyTemplateListWith ""
toHtml, safeToHtml :: String -> Html
toHtml = H.toHtml
safeToHtml = H.preEscapedToHtml