module Text.Hakyll.Internal.Render
( substitute
, regularSubstitute
, finalSubstitute
, pureRenderWith
, pureRenderAndConcatWith
, pureRenderChainWith
, writePage
) where
import qualified Data.Map as M
import Control.Monad.Reader (liftIO)
import Data.List (foldl')
import Data.Maybe (fromMaybe)
import Text.Hakyll.Context (Context, ContextManipulation)
import Text.Hakyll.Renderable
import Text.Hakyll.Page
import Text.Hakyll.File
import Text.Hakyll.Hakyll
import Text.Hakyll.Internal.Template
pureRenderWith :: ContextManipulation
-> Template
-> Context
-> Context
pureRenderWith manipulation template context =
let contextIgnoringRoot = M.insert "root" "$root" (manipulation context)
body = regularSubstitute template contextIgnoringRoot
in M.insert "body" body context
pureRenderAndConcatWith :: ContextManipulation
-> [Template]
-> [Context]
-> String
pureRenderAndConcatWith manipulation templates =
concatMap renderAndConcat
where
renderAndConcat = fromMaybe "" . M.lookup "body"
. pureRenderChainWith manipulation templates
pureRenderChainWith :: ContextManipulation
-> [Template]
-> Context
-> Context
pureRenderChainWith manipulation templates context =
let initial = manipulation context
in foldl' (flip $ pureRenderWith id) initial templates
writePage :: Page -> Hakyll ()
writePage page = do
additionalContext' <- askHakyll additionalContext
url <- getUrl page
destination <- toDestination url
let context = additionalContext' `M.union` M.singleton "root" (toRoot url)
makeDirectories destination
liftIO $ writeFile destination $ finalSubstitute (fromString $ getBody page)
context