summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll/Render.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Text/Hakyll/Render.hs')
-rw-r--r--src/Text/Hakyll/Render.hs30
1 files changed, 27 insertions, 3 deletions
diff --git a/src/Text/Hakyll/Render.hs b/src/Text/Hakyll/Render.hs
index 96d2ffb..a81ec2f 100644
--- a/src/Text/Hakyll/Render.hs
+++ b/src/Text/Hakyll/Render.hs
@@ -14,12 +14,22 @@ import System.Directory (copyFile)
import Data.Maybe (fromMaybe)
import qualified Data.Map as M
-import Text.Hakyll.Hakyll (Hakyll, Context)
+import Text.Hakyll.Hakyll (Hakyll, Context, askHakyll, getAdditionalContext)
import Text.Hakyll.File
import Text.Hakyll.HakyllAction
import Text.Hakyll.Internal.CompressCss
-import Text.Hakyll.Internal.Render
-import Text.Hakyll.Internal.Template (readTemplate)
+import Text.Hakyll.Internal.Template
+
+-- | A pure render function.
+pureRender :: Template -- ^ Template to use for rendering.
+ -> Context -- ^ Renderable object to render with given template.
+ -> Context -- ^ The body of the result will contain the render.
+pureRender template context =
+ -- Ignore $root when substituting here. We will only replace that in the
+ -- final render (just before writing).
+ let contextIgnoringRoot = M.insert "root" "$root" context
+ body = regularSubstitute template contextIgnoringRoot
+ in M.insert "body" body context
-- | Render to a Page.
render :: FilePath -- ^ Template to use for rendering.
@@ -101,3 +111,17 @@ css source = runHakyllActionIfNeeded css'
destination <- toDestination source
makeDirectories destination
liftIO $ writeFile destination (compressCss contents)
+
+-- | Write a page to the site destination. Final action after render
+-- chains and such.
+writePage :: HakyllAction Context ()
+writePage = createHakyllAction $ \initialContext -> do
+ additionalContext' <- askHakyll getAdditionalContext
+ let url = fromMaybe (error "No url defined at write time.")
+ (M.lookup "url" initialContext)
+ body = fromMaybe "" (M.lookup "body" initialContext)
+ let context = additionalContext' `M.union` M.singleton "root" (toRoot url)
+ destination <- toDestination url
+ makeDirectories destination
+ -- Substitute $root here, just before writing.
+ liftIO $ writeFile destination $ finalSubstitute (fromString body) context