@@ -14,12 +14,22 @@ import System.Directory (copyFile)
1414import Data.Maybe (fromMaybe)
1515import qualified Data.Map as M
1616
17−import Text.Hakyll.Hakyll (Hakyll, Context)
17+import Text.Hakyll.Hakyll (Hakyll, Context, askHakyll, getAdditionalContext)
1818import Text.Hakyll.File
1919import Text.Hakyll.HakyllAction
2020import Text.Hakyll.Internal.CompressCss
21−import Text.Hakyll.Internal.Render
22−import Text.Hakyll.Internal.Template (readTemplate)
21+import Text.Hakyll.Internal.Template
22+
23+-- | A pure render function.
24+pureRender :: Template -- ^ Template to use for rendering.
25+ -> Context -- ^ Renderable object to render with given template.
26+ -> Context -- ^ The body of the result will contain the render.
27+pureRender template context =
28+ -- Ignore $root when substituting here. We will only replace that in the
29+ -- final render (just before writing).
30+ let contextIgnoringRoot = M.insert "root" "$root" context
31+ body = regularSubstitute template contextIgnoringRoot
32+ in M.insert "body" body context
2333
2434-- | Render to a Page.
2535render :: FilePath -- ^ Template to use for rendering.
@@ -101,3 +111,17 @@ css source = runHakyllActionIfNeeded css'
101111 destination <- toDestination source
102112 makeDirectories destination
103113 liftIO $ writeFile destination (compressCss contents)
114+
115+-- | Write a page to the site destination. Final action after render
116+-- chains and such.
117+writePage :: HakyllAction Context ()
118+writePage = createHakyllAction $ \initialContext -> do
119+ additionalContext' <- askHakyll getAdditionalContext
120+ let url = fromMaybe (error "No url defined at write time.")
121+ (M.lookup "url" initialContext)
122+ body = fromMaybe "" (M.lookup "body" initialContext)
123+ let context = additionalContext' `M.union` M.singleton "root" (toRoot url)
124+ destination <- toDestination url
125+ makeDirectories destination
126+ -- Substitute $root here, just before writing.
127+ liftIO $ writeFile destination $ finalSubstitute (fromString body) context