on master help sign in
Added pure renderChain function.
commitecd00b386e2848cab19c16afdcaeae3133f39569 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateTue 12 Jan 2010, 13:09 (17 years ago) parentef7ccb1
3 files changed+22−10
hakyll.cabal+1 src/Text/Hakyll/Render.hs+8−7 src/Text/Hakyll/Render/Internal.hs+13−3
hakyll.cabal+1
@@ -47,3 +47,4 @@ library
4747 Text.Hakyll.Context
4848 Text.Hakyll.Regex
4949 Network.Hakyll.SimpleServer
50+ other-modules: Text.Hakyll.Render.Internal
src/Text/Hakyll/Render.hs+8−7
@@ -10,7 +10,7 @@ module Text.Hakyll.Render
1010 , css
1111 ) where
1212
13−import Control.Monad (unless, liftM, foldM)
13+import Control.Monad (unless, mapM, foldM)
1414
1515import System.Directory (copyFile)
1616import System.IO
@@ -49,7 +49,7 @@ renderWith :: Renderable a
4949renderWith manipulation templatePath renderable = do
5050 template <- readFile templatePath
5151 context <- toContext renderable
52− return $ pureRenderWith manipulation template context
52+ return $ fromContext $ pureRenderWith manipulation template context
5353
5454-- | Render each renderable with the given template, then concatenate the
5555-- result.
@@ -83,11 +83,12 @@ renderChain = renderChainWith id
8383-- "ContextManipulation" which to apply on the context when it is read first.
8484renderChainWith :: Renderable a
8585 => ContextManipulation -> [FilePath] -> a -> IO ()
86−renderChainWith manipulation templates renderable =
87− depends (getURL renderable) (getDependencies renderable ++ templates) $
88− do initialPage <- liftM manipulation $ toContext renderable
89− result <- foldM (flip render) (fromContext initialPage) templates
90− writePage result
86+renderChainWith manipulation templatePaths renderable =
87+ depends (getURL renderable) (getDependencies renderable ++ templatePaths) $
88+ do templates <- mapM readFile templatePaths
89+ context <- toContext renderable
90+ let result = pureRenderChainWith manipulation templates context
91+ writePage $ fromContext result
9192
9293-- | Mark a certain file as static, so it will just be copied when the site is
9394-- generated.
src/Text/Hakyll/Render/Internal.hs+13−3
@@ -4,12 +4,13 @@ module Text.Hakyll.Render.Internal
44 , regularSubstitute
55 , finalSubstitute
66 , pureRenderWith
7+ , pureRenderChainWith
78 , writePage
89 ) where
910
1011import qualified Data.Map as M
1112import Text.Hakyll.Context (Context, ContextManipulation)
12−import Data.List (isPrefixOf)
13+import Data.List (isPrefixOf, foldl')
1314import Data.Char (isAlpha)
1415import Data.Maybe (fromMaybe)
1516import Control.Parallel.Strategies (rnf, ($|))
@@ -45,14 +46,23 @@ finalSubstitute = substitute "$"
4546pureRenderWith :: ContextManipulation -- ^ Manipulation to apply on the context.
4647 -> String -- ^ Template to use for rendering.
4748 -> Context -- ^ Renderable object to render with given template.
48− -> Page -- ^ The body of the result will contain the render.
49+ -> Context -- ^ The body of the result will contain the render.
4950pureRenderWith manipulation template context =
5051 -- Ignore $root when substituting here. We will only replace that in the
5152 -- final render (just before writing).
5253 let contextIgnoringRoot = M.insert "root" "$root" (manipulation context)
5354 body = regularSubstitute template contextIgnoringRoot
5455 -- Force the body to be rendered.
55− in ($|) fromContext rnf (M.insert "body" body context)
56+ in ($|) id rnf (M.insert "body" body context)
57+
58+-- | A pure renderChain function.
59+pureRenderChainWith :: ContextManipulation
60+ -> [String]
61+ -> Context
62+ -> Context
63+pureRenderChainWith manipulation templates context =
64+ let initial = manipulation context
65+ in foldl' (flip $ pureRenderWith id) initial templates
5666
5767-- | Write a page to the site destination. Final action after render
5868-- chains and such.