@@ -11,10 +11,12 @@ module Text.Hakyll.Render
1111 ) where
1212
1313import Control.Monad (unless, mapM)
14+import Control.Monad.Reader (liftIO)
1415
1516import System.Directory (copyFile)
1617import System.IO
1718
19+import Text.Hakyll.Hakyll (Hakyll)
1820import Text.Hakyll.Context (ContextManipulation)
1921import Text.Hakyll.Page
2022import Text.Hakyll.Renderable
@@ -26,17 +28,17 @@ import Text.Hakyll.Render.Internal
2628-- | Execute an IO action only when the cache is invalid.
2729depends :: FilePath -- ^ File to be rendered or created.
2830 -> [FilePath] -- ^ Files the render depends on.
29− -> IO () -- ^ IO action to execute when the file is out of date.
30− -> IO ()
31+ -> Hakyll () -- ^ IO action to execute when the file is out of date.
32+ -> Hakyll ()
3133depends file dependencies action = do
32− valid <- isCacheValid (toDestination file) dependencies
34+ valid <- liftIO $ isCacheValid (toDestination file) dependencies
3335 unless valid action
3436
3537-- | Render to a Page.
3638render :: Renderable a
3739 => FilePath -- ^ Template to use for rendering.
3840 -> 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.
4042render = renderWith id
4143
4244-- | Render to a Page. This function allows you to manipulate the context
@@ -45,15 +47,15 @@ renderWith :: Renderable a
4547 => ContextManipulation -- ^ Manipulation to apply on the context.
4648 -> FilePath -- ^ Template to use for rendering.
4749 -> 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.
4951renderWith manipulation templatePath renderable = do
50− template <- readFile templatePath
52+ template <- liftIO $ readFile templatePath
5153 context <- toContext renderable
5254 return $ fromContext $ pureRenderWith manipulation template context
5355
5456-- | Render each renderable with the given template, then concatenate the
5557-- result.
56−renderAndConcat :: Renderable a => FilePath -> [a] -> IO String
58+renderAndConcat :: Renderable a => FilePath -> [a] -> Hakyll String
5759renderAndConcat = renderAndConcatWith id
5860
5961-- | Render each renderable with the given template, then concatenate the
@@ -63,41 +65,43 @@ renderAndConcatWith :: Renderable a
6365 => ContextManipulation
6466 -> FilePath
6567 -> [a]
66− -> IO String
68+ -> Hakyll String
6769renderAndConcatWith manipulation templatePath renderables = do
68− template <- readFile templatePath
70+ template <- liftIO $ readFile templatePath
6971 contexts <- mapM toContext renderables
7072 return $ pureRenderAndConcatWith manipulation template contexts
7173
7274-- | Chain a render action for a page with a number of templates. This will
7375-- also write the result to the site destination. This is the preferred way
7476-- to do general rendering.
75−renderChain :: Renderable a => [FilePath] -> a -> IO ()
77+renderChain :: Renderable a => [FilePath] -> a -> Hakyll ()
7678renderChain = renderChainWith id
7779
7880-- | A more custom render chain that allows you to specify a
7981-- "ContextManipulation" which to apply on the context when it is read first.
8082renderChainWith :: Renderable a
81− => ContextManipulation -> [FilePath] -> a -> IO ()
83+ => ContextManipulation -> [FilePath] -> a -> Hakyll ()
8284renderChainWith manipulation templatePaths renderable =
83− depends (getURL renderable) (getDependencies renderable ++ templatePaths) $
84− do templates <- mapM readFile templatePaths
85− context <- toContext renderable
86− let result = pureRenderChainWith manipulation templates context
87− writePage $ fromContext result
85+ depends (getURL renderable) dependencies render'
86+ where
87+ dependencies = (getDependencies renderable) ++ templatePaths
88+ render' = do templates <- liftIO $ mapM readFile templatePaths
89+ context <- toContext renderable
90+ let result = pureRenderChainWith manipulation templates context
91+ writePage $ fromContext result
8892
8993-- | Mark a certain file as static, so it will just be copied when the site is
9094-- generated.
91−static :: FilePath -> IO ()
92−static source = depends destination [source] action
95+static :: FilePath -> Hakyll ()
96+static source = depends destination [source] (liftIO action)
9397 where
9498 destination = toDestination source
9599 action = do makeDirectories destination
96100 copyFile source destination
97101
98102-- | Render a css file, compressing it.
99−css :: FilePath -> IO ()
100−css source = depends destination [source] css'
103+css :: FilePath -> Hakyll ()
104+css source = depends destination [source] (liftIO css')
101105 where
102106 destination = toDestination source
103107 css' = do contents <- readFile source