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