@@ -1,7 +1,7 @@
1module Text.Hakyll.Render
1module Text.Hakyll.Render
5 renderAndConcat,
5 renderAndConcat,
6 renderChain,
6 renderChain,
@@ -20,12 +20,27 @@ import Text.Hakyll.Page
20import Text.Hakyll.Renderable
20import Text.Hakyll.Renderable
21import Text.Hakyll.Util
21import Text.Hakyll.Util
23+-- | Execute an IO action only when the cache is invalid.
23−depends :: FilePath -> [FilePath] -> IO () -> IO ()
24+depends :: FilePath -- ^ File to be rendered or created.
25+ -> [FilePath] -- ^ Files the render depends on.
26+ -> IO () -- ^ IO action to execute when the file is out of date.
24depends file dependencies action = do
28depends file dependencies action = do
25 valid <- isCacheValid (toDestination file) dependencies
29 valid <- isCacheValid (toDestination file) dependencies
26 unless valid action
30 unless valid action
32+-- | Write a page to the site destination.
33+writePage :: Page -> IO ()
35+ let destination = toDestination $ getURL page
36+ makeDirectories destination
37+ B.writeFile destination (getBody page)
39+-- | Render to a Page.
28−render :: Renderable a => FilePath -> a -> IO Page
40+render :: Renderable a
41+ => FilePath -- ^ Template to use for rendering.
42+ -> a -- ^ Renderable object to render with given template.
43+ -> IO Page -- ^ The body of the result will contain the render.
29render templatePath renderable = do
44render templatePath renderable = do
30 handle <- openFile templatePath ReadMode
45 handle <- openFile templatePath ReadMode
31 templateString <- liftM B.pack $ hGetContents handle
46 templateString <- liftM B.pack $ hGetContents handle
@@ -34,12 +49,8 @@ render templatePath renderable = do
34 let body = substitute templateString context
49 let body = substitute templateString context
35 return $ fromContext (M.insert (B.pack "body") body context)
50 return $ fromContext (M.insert (B.pack "body") body context)
37−writePage :: Page -> IO ()
52+-- | Render each renderable with the given template, then concatenate the
38−writePage page = do
53+-- result.
39− let destination = toDestination $ getURL page
40− makeDirectories destination
41− B.writeFile destination (getBody page)
43renderAndConcat :: Renderable a => FilePath -> [a] -> IO B.ByteString
54renderAndConcat :: Renderable a => FilePath -> [a] -> IO B.ByteString
44renderAndConcat templatePath renderables = foldM concatRender' B.empty renderables
55renderAndConcat templatePath renderables = foldM concatRender' B.empty renderables
45 where concatRender' :: Renderable a => B.ByteString -> a -> IO B.ByteString
56 where concatRender' :: Renderable a => B.ByteString -> a -> IO B.ByteString
@@ -48,6 +59,9 @@ renderAndConcat templatePath renderables = foldM concatRender' B.empty renderabl
48 let body = getBody rendered
59 let body = getBody rendered
49 return $ B.append chunk $ body
60 return $ B.append chunk $ body
62+-- | Chain a render action for a page with a number of templates. This will
63+-- also write the result to the site destination. This is the preferred way
64+-- to do general rendering.
51renderChain :: Renderable a => [FilePath] -> a -> IO ()
65renderChain :: Renderable a => [FilePath] -> a -> IO ()
52renderChain templates renderable =
66renderChain templates renderable =
53 depends (getURL renderable) (getDependencies renderable ++ templates) $
67 depends (getURL renderable) (getDependencies renderable ++ templates) $
@@ -55,12 +69,15 @@ renderChain templates renderable =
55 result <- foldM (flip render) (fromContext initialPage) templates
69 result <- foldM (flip render) (fromContext initialPage) templates
56 writePage result
70 writePage result
72+-- | Mark a certain file as static, so it will just be copied when the site is
58static :: FilePath -> IO ()
74static :: FilePath -> IO ()
59static source = do
75static source = do
60 makeDirectories destination
76 makeDirectories destination
61 copyFile source destination
77 copyFile source destination
62 where destination = toDestination source
78 where destination = toDestination source
80+-- | Mark a whole directory as static.
64staticDirectory :: FilePath -> IO ()
81staticDirectory :: FilePath -> IO ()
65staticDirectory dir =
82staticDirectory dir =
66 getRecursiveContents dir >>= mapM_ static
83 getRecursiveContents dir >>= mapM_ static