summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll/Render.hs
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2010-01-03 12:21:15 +0100
committerJasper Van der Jeugt <jaspervdj@gmail.com>2010-01-03 12:21:15 +0100
commit1fdffc6a19ffad86a3f00408a72d19082fc5c6db (patch)
tree0709ac82509751c652e1135579348ed4f37cfb6b /src/Text/Hakyll/Render.hs
parent36056f174f2e8641248e95ee52e4369514bf9b14 (diff)
downloadhakyll-1fdffc6a19ffad86a3f00408a72d19082fc5c6db.tar.gz
Added context manipulating functions and associated render functions. Also, version bump.
Diffstat (limited to 'src/Text/Hakyll/Render.hs')
-rw-r--r--src/Text/Hakyll/Render.hs42
1 files changed, 36 insertions, 6 deletions
diff --git a/src/Text/Hakyll/Render.hs b/src/Text/Hakyll/Render.hs
index 7420d1c..ac529e8 100644
--- a/src/Text/Hakyll/Render.hs
+++ b/src/Text/Hakyll/Render.hs
@@ -1,8 +1,11 @@
module Text.Hakyll.Render
( depends
, render
+ , renderWith
, renderAndConcat
+ , renderAndConcatWith
, renderChain
+ , renderChainWith
, static
, css
) where
@@ -15,6 +18,7 @@ import Control.Monad (unless, liftM, foldM)
import System.Directory (copyFile)
import System.IO
+import Text.Hakyll.Context (ContextManipulation)
import Text.Hakyll.Page
import Text.Hakyll.Renderable
import Text.Hakyll.File
@@ -34,21 +38,41 @@ render :: Renderable a
=> FilePath -- ^ Template to use for rendering.
-> a -- ^ Renderable object to render with given template.
-> IO Page -- ^ The body of the result will contain the render.
-render templatePath renderable = do
+render = renderWith id
+
+-- | Render to a Page. This function allows you to manipulate the context
+-- first.
+renderWith :: Renderable a
+ => ContextManipulation -- ^ Manipulation to apply on the context.
+ -> FilePath -- ^ Template to use for rendering.
+ -> a -- ^ Renderable object to render with given template.
+ -> IO Page -- ^ The body of the result will contain the render.
+renderWith manipulation templatePath renderable = do
handle <- openFile templatePath ReadMode
templateString <- liftM B.pack $ hGetContents handle
seq templateString $ hClose handle
- context <- toContext renderable
+ context <- liftM manipulation $ toContext renderable
let body = substitute templateString context
return $ fromContext (M.insert (B.pack "body") body context)
-- | Render each renderable with the given template, then concatenate the
-- result.
renderAndConcat :: Renderable a => FilePath -> [a] -> IO B.ByteString
-renderAndConcat templatePath renderables = foldM concatRender' B.empty renderables
+renderAndConcat = renderAndConcatWith id
+
+-- | Render each renderable with the given template, then concatenate the
+-- result. This function allows you to specify a "ContextManipulation" to
+-- apply on every "Renderable".
+renderAndConcatWith :: Renderable a
+ => ContextManipulation
+ -> FilePath
+ -> [a]
+ -> IO B.ByteString
+renderAndConcatWith manipulation templatePath renderables =
+ foldM concatRender' B.empty renderables
where concatRender' :: Renderable a => B.ByteString -> a -> IO B.ByteString
concatRender' chunk renderable = do
- rendered <- render templatePath renderable
+ rendered <- renderWith manipulation templatePath renderable
let body = getBody rendered
return $ B.append chunk $ body
@@ -56,9 +80,15 @@ renderAndConcat templatePath renderables = foldM concatRender' B.empty renderabl
-- also write the result to the site destination. This is the preferred way
-- to do general rendering.
renderChain :: Renderable a => [FilePath] -> a -> IO ()
-renderChain templates renderable =
+renderChain = renderChainWith id
+
+-- | A more custom render chain that allows you to specify a
+-- "ContextManipulation" which to apply on the context when it is read first.
+renderChainWith :: Renderable a
+ => ContextManipulation -> [FilePath] -> a -> IO ()
+renderChainWith manipulation templates renderable =
depends (getURL renderable) (getDependencies renderable ++ templates) $
- do initialPage <- toContext renderable
+ do initialPage <- liftM manipulation $ toContext renderable
result <- foldM (flip render) (fromContext initialPage) templates
writePage result