on master help sign in
Documented Text.Hakyll.Render and Text.Hakyll.Util.
0638efeJasper Van der Jeugt
src/Text/Hakyll/Render.hs+26−9
@@ -1,7 +1,7 @@
1module Text.Hakyll.Render
1module Text.Hakyll.Render
2 ( depends,
2 ( depends,
3− render,
4 writePage,
3 writePage,
4+ render,
5 renderAndConcat,
5 renderAndConcat,
6 renderChain,
6 renderChain,
7 static,
7 static,
@@ -20,12 +20,27 @@ import Text.Hakyll.Page
20import Text.Hakyll.Renderable
20import Text.Hakyll.Renderable
21import Text.Hakyll.Util
21import Text.Hakyll.Util
22
22
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.
27+ -> IO ()
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
27
31
32+-- | Write a page to the site destination.
33+writePage :: Page -> IO ()
34+writePage page = do
35+ let destination = toDestination $ getURL page
36+ makeDirectories destination
37+ B.writeFile destination (getBody page)
38+
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)
36
51
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)
42−
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
50
61
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
57
71
72+-- | Mark a certain file as static, so it will just be copied when the site is
73+-- generated.
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
63
79
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
src/Text/Hakyll/Util.hs+2
@@ -15,9 +15,11 @@ import Control.Monad
15import Data.Char
15import Data.Char
16import Data.List
16import Data.List
17
17
18+-- | Convert a relative filepath to a filepath in the destination (_site).
18toDestination :: FilePath -> FilePath
19toDestination :: FilePath -> FilePath
19toDestination path = "_site" </> path
20toDestination path = "_site" </> path
20
21
22+-- | Convert a relative filepath to a filepath in the cache (_cache).
21toCache :: FilePath -> FilePath
23toCache :: FilePath -> FilePath
22toCache path = "_cache" </> path
24toCache path = "_cache" </> path
23
25