@@ -11,10 +11,12 @@ module Text.Hakyll.Render
13import Control.Monad (unless, mapM)
13import Control.Monad (unless, mapM)
14+import Control.Monad.Reader (liftIO)
15import System.Directory (copyFile)
16import System.Directory (copyFile)
16import System.IO
17import System.IO
19+import Text.Hakyll.Hakyll (Hakyll)
18import Text.Hakyll.Context (ContextManipulation)
20import Text.Hakyll.Context (ContextManipulation)
19import Text.Hakyll.Page
21import Text.Hakyll.Page
20import Text.Hakyll.Renderable
22import Text.Hakyll.Renderable
@@ -26,17 +28,17 @@ import Text.Hakyll.Render.Internal
26-- | Execute an IO action only when the cache is invalid.
28-- | Execute an IO action only when the cache is invalid.
27depends :: FilePath -- ^ File to be rendered or created.
29depends :: FilePath -- ^ File to be rendered or created.
28 -> [FilePath] -- ^ Files the render depends on.
30 -> [FilePath] -- ^ Files the render depends on.
29− -> IO () -- ^ IO action to execute when the file is out of date.
31+ -> Hakyll () -- ^ IO action to execute when the file is out of date.
30− -> IO ()
32+ -> Hakyll ()
31depends file dependencies action = do
33depends file dependencies action = do
32− valid <- isCacheValid (toDestination file) dependencies
34+ valid <- liftIO $ isCacheValid (toDestination file) dependencies
33 unless valid action
35 unless valid action
35-- | Render to a Page.
37-- | Render to a Page.
36render :: Renderable a
38render :: Renderable a
37 => FilePath -- ^ Template to use for rendering.
39 => FilePath -- ^ Template to use for rendering.
38 -> a -- ^ Renderable object to render with given template.
40 -> a -- ^ Renderable object to render with given template.
39− -> IO Page -- ^ The body of the result will contain the render.
41+ -> Hakyll Page -- ^ The body of the result will contain the render.
40render = renderWith id
42render = renderWith id
42-- | Render to a Page. This function allows you to manipulate the context
44-- | Render to a Page. This function allows you to manipulate the context
@@ -45,15 +47,15 @@ renderWith :: Renderable a
45 => ContextManipulation -- ^ Manipulation to apply on the context.
47 => ContextManipulation -- ^ Manipulation to apply on the context.
46 -> FilePath -- ^ Template to use for rendering.
48 -> FilePath -- ^ Template to use for rendering.
47 -> a -- ^ Renderable object to render with given template.
49 -> a -- ^ Renderable object to render with given template.
48− -> IO Page -- ^ The body of the result will contain the render.
50+ -> Hakyll Page -- ^ The body of the result will contain the render.
49renderWith manipulation templatePath renderable = do
51renderWith manipulation templatePath renderable = do
50− template <- readFile templatePath
52+ template <- liftIO $ readFile templatePath
51 context <- toContext renderable
53 context <- toContext renderable
52 return $ fromContext $ pureRenderWith manipulation template context
54 return $ fromContext $ pureRenderWith manipulation template context
54-- | Render each renderable with the given template, then concatenate the
56-- | Render each renderable with the given template, then concatenate the
56−renderAndConcat :: Renderable a => FilePath -> [a] -> IO String
58+renderAndConcat :: Renderable a => FilePath -> [a] -> Hakyll String
57renderAndConcat = renderAndConcatWith id
59renderAndConcat = renderAndConcatWith id
59-- | Render each renderable with the given template, then concatenate the
61-- | Render each renderable with the given template, then concatenate the
@@ -63,41 +65,43 @@ renderAndConcatWith :: Renderable a
63 => ContextManipulation
65 => ContextManipulation
64 -> FilePath
66 -> FilePath
66− -> IO String
68+ -> Hakyll String
67renderAndConcatWith manipulation templatePath renderables = do
69renderAndConcatWith manipulation templatePath renderables = do
68− template <- readFile templatePath
70+ template <- liftIO $ readFile templatePath
69 contexts <- mapM toContext renderables
71 contexts <- mapM toContext renderables
70 return $ pureRenderAndConcatWith manipulation template contexts
72 return $ pureRenderAndConcatWith manipulation template contexts
72-- | Chain a render action for a page with a number of templates. This will
74-- | Chain a render action for a page with a number of templates. This will
73-- also write the result to the site destination. This is the preferred way
75-- also write the result to the site destination. This is the preferred way
74-- to do general rendering.
76-- to do general rendering.
75−renderChain :: Renderable a => [FilePath] -> a -> IO ()
77+renderChain :: Renderable a => [FilePath] -> a -> Hakyll ()
76renderChain = renderChainWith id
78renderChain = renderChainWith id
78-- | A more custom render chain that allows you to specify a
80-- | A more custom render chain that allows you to specify a
79-- "ContextManipulation" which to apply on the context when it is read first.
81-- "ContextManipulation" which to apply on the context when it is read first.
80renderChainWith :: Renderable a
82renderChainWith :: Renderable a
81− => ContextManipulation -> [FilePath] -> a -> IO ()
83+ => ContextManipulation -> [FilePath] -> a -> Hakyll ()
82renderChainWith manipulation templatePaths renderable =
84renderChainWith manipulation templatePaths renderable =
85+ depends (getURL renderable) dependencies render'
83− depends (getURL renderable) (getDependencies renderable ++ templatePaths) $
87+ dependencies = (getDependencies renderable) ++ templatePaths
84− do templates <- mapM readFile templatePaths
88+ render' = do templates <- liftIO $ mapM readFile templatePaths
85− context <- toContext renderable
89+ context <- toContext renderable
86− let result = pureRenderChainWith manipulation templates context
90+ let result = pureRenderChainWith manipulation templates context
87− writePage $ fromContext result
91+ writePage $ fromContext result
89-- | Mark a certain file as static, so it will just be copied when the site is
93-- | Mark a certain file as static, so it will just be copied when the site is
90-- generated.
94-- generated.
91−static :: FilePath -> IO ()
95+static :: FilePath -> Hakyll ()
92−static source = depends destination [source] action
96+static source = depends destination [source] (liftIO action)
94 destination = toDestination source
98 destination = toDestination source
95 action = do makeDirectories destination
99 action = do makeDirectories destination
96 copyFile source destination
100 copyFile source destination
98-- | Render a css file, compressing it.
102-- | Render a css file, compressing it.
99−css :: FilePath -> IO ()
103+css :: FilePath -> Hakyll ()
100−css source = depends destination [source] css'
104+css source = depends destination [source] (liftIO css')
102 destination = toDestination source
106 destination = toDestination source
103 css' = do contents <- readFile source
107 css' = do contents <- readFile source