on 333c19d help sign in
hakyll/src/Text/Hakyll/Render.hs
66 lines 2.2 kB history raw
1module Text.Hakyll.Render
2 ( depends,
3 render,
4 writePage,
5 renderAndConcat,
6 renderChain,
7 static,
8 staticDirectory
9 ) where
11import Text.Template hiding (render)
12import qualified Data.ByteString.Lazy.Char8 as B
13import qualified Data.Map as M
14import Control.Monad
16import System.Directory
17import System.IO
19import Text.Hakyll.Page
20import Text.Hakyll.Renderable
21import Text.Hakyll.Util
23depends :: FilePath -> [FilePath] -> IO () -> IO ()
24depends file dependencies action = do
25 valid <- isCacheValid (toDestination file) dependencies
26 unless valid action
28render :: Renderable a => FilePath -> a -> IO Page
29render templatePath renderable = do
30 handle <- openFile templatePath ReadMode
31 templateString <- liftM B.pack $ hGetContents handle
32 seq templateString $ hClose handle
33 context <- toContext renderable
34 let body = substitute templateString context
35 return $ fromContext (M.insert (B.pack "body") body context)
37writePage :: Page -> IO ()
38writePage page = do
39 let destination = toDestination $ getURL page
40 makeDirectories destination
41 B.writeFile destination (getBody page)
43renderAndConcat :: Renderable a => FilePath -> [a] -> IO B.ByteString
44renderAndConcat templatePath renderables = foldM concatRender' B.empty renderables
45 where concatRender' :: Renderable a => B.ByteString -> a -> IO B.ByteString
46 concatRender' chunk renderable = do
47 rendered <- render templatePath renderable
48 let body = getBody rendered
49 return $ B.append chunk $ body
51renderChain :: Renderable a => [FilePath] -> a -> IO ()
52renderChain templates renderable =
53 depends (getURL renderable) (getDependencies renderable ++ templates) $
54 do initialPage <- toContext renderable
55 result <- foldM (flip render) (fromContext initialPage) templates
56 writePage result
58static :: FilePath -> IO ()
59static source = do
60 makeDirectories destination
61 copyFile source destination
62 where destination = toDestination source
64staticDirectory :: FilePath -> IO ()
65staticDirectory dir =
66 getRecursiveContents dir >>= mapM_ static