@@ -1,8 +1,11 @@
1module Text.Hakyll.Render
1module Text.Hakyll.Render
4 , renderAndConcat
5 , renderAndConcat
5 , renderChain
7 , renderChain
@@ -15,6 +18,7 @@ import Control.Monad (unless, liftM, foldM)
15import System.Directory (copyFile)
18import System.Directory (copyFile)
16import System.IO
19import System.IO
21+import Text.Hakyll.Context (ContextManipulation)
18import Text.Hakyll.Page
22import Text.Hakyll.Page
19import Text.Hakyll.Renderable
23import Text.Hakyll.Renderable
20import Text.Hakyll.File
24import Text.Hakyll.File
@@ -34,21 +38,41 @@ render :: Renderable a
34 => FilePath -- ^ Template to use for rendering.
38 => FilePath -- ^ Template to use for rendering.
35 -> a -- ^ Renderable object to render with given template.
39 -> a -- ^ Renderable object to render with given template.
36 -> IO Page -- ^ The body of the result will contain the render.
40 -> IO Page -- ^ The body of the result will contain the render.
41+render = renderWith id
43+-- | Render to a Page. This function allows you to manipulate the context
45+renderWith :: Renderable a
46+ => ContextManipulation -- ^ Manipulation to apply on the context.
47+ -> FilePath -- ^ Template to use for rendering.
48+ -> a -- ^ Renderable object to render with given template.
49+ -> IO Page -- ^ The body of the result will contain the render.
37−render templatePath renderable = do
50+renderWith manipulation templatePath renderable = do
38 handle <- openFile templatePath ReadMode
51 handle <- openFile templatePath ReadMode
39 templateString <- liftM B.pack $ hGetContents handle
52 templateString <- liftM B.pack $ hGetContents handle
40 seq templateString $ hClose handle
53 seq templateString $ hClose handle
41− context <- toContext renderable
54+ context <- liftM manipulation $ toContext renderable
42 let body = substitute templateString context
55 let body = substitute templateString context
43 return $ fromContext (M.insert (B.pack "body") body context)
56 return $ fromContext (M.insert (B.pack "body") body context)
45-- | Render each renderable with the given template, then concatenate the
58-- | Render each renderable with the given template, then concatenate the
47renderAndConcat :: Renderable a => FilePath -> [a] -> IO B.ByteString
60renderAndConcat :: Renderable a => FilePath -> [a] -> IO B.ByteString
61+renderAndConcat = renderAndConcatWith id
63+-- | Render each renderable with the given template, then concatenate the
64+-- result. This function allows you to specify a "ContextManipulation" to
65+-- apply on every "Renderable".
66+renderAndConcatWith :: Renderable a
67+ => ContextManipulation
71+renderAndConcatWith manipulation templatePath renderables =
48−renderAndConcat templatePath renderables = foldM concatRender' B.empty renderables
72+ foldM concatRender' B.empty renderables
49 where concatRender' :: Renderable a => B.ByteString -> a -> IO B.ByteString
73 where concatRender' :: Renderable a => B.ByteString -> a -> IO B.ByteString
50 concatRender' chunk renderable = do
74 concatRender' chunk renderable = do
51− rendered <- render templatePath renderable
75+ rendered <- renderWith manipulation templatePath renderable
52 let body = getBody rendered
76 let body = getBody rendered
53 return $ B.append chunk $ body
77 return $ B.append chunk $ body
@@ -56,9 +80,15 @@ renderAndConcat templatePath renderables = foldM concatRender' B.empty renderabl
56-- also write the result to the site destination. This is the preferred way
80-- also write the result to the site destination. This is the preferred way
57-- to do general rendering.
81-- to do general rendering.
58renderChain :: Renderable a => [FilePath] -> a -> IO ()
82renderChain :: Renderable a => [FilePath] -> a -> IO ()
83+renderChain = renderChainWith id
85+-- | A more custom render chain that allows you to specify a
86+-- "ContextManipulation" which to apply on the context when it is read first.
87+renderChainWith :: Renderable a
88+ => ContextManipulation -> [FilePath] -> a -> IO ()
59−renderChain templates renderable =
89+renderChainWith manipulation templates renderable =
60 depends (getURL renderable) (getDependencies renderable ++ templates) $
90 depends (getURL renderable) (getDependencies renderable ++ templates) $
61− do initialPage <- toContext renderable
91+ do initialPage <- liftM manipulation $ toContext renderable
62 result <- foldM (flip render) (fromContext initialPage) templates
92 result <- foldM (flip render) (fromContext initialPage) templates
63 writePage result
93 writePage result