diff options
author | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2009-12-10 14:18:13 +0100 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2009-12-10 14:18:13 +0100 |
commit | 16f284d7471c5de1ae7a51521924199f6f5dc768 (patch) | |
tree | 6dd0dfda52789a4c09e7e6a520714ed4746eb9d0 /src/Text/Hakyll/Render.hs | |
parent | c630522ec0f17fafa9b54d1c2e654580098ae5ae (diff) | |
download | hakyll-16f284d7471c5de1ae7a51521924199f6f5dc768.tar.gz |
Made an abstract Renderable class. Still need some cleanup now.
Diffstat (limited to 'src/Text/Hakyll/Render.hs')
-rw-r--r-- | src/Text/Hakyll/Render.hs | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/src/Text/Hakyll/Render.hs b/src/Text/Hakyll/Render.hs index 7c52d9b..b9b57b7 100644 --- a/src/Text/Hakyll/Render.hs +++ b/src/Text/Hakyll/Render.hs @@ -1,6 +1,6 @@ module Text.Hakyll.Render ( depends, - renderPage, + render, writePage, renderAndConcat, renderChain, @@ -8,7 +8,7 @@ module Text.Hakyll.Render staticDirectory ) where -import Text.Template +import Text.Template hiding (render) import qualified Data.ByteString.Lazy.Char8 as B import qualified Data.Map as M import Control.Monad @@ -17,6 +17,7 @@ import System.Directory import System.IO import Text.Hakyll.Page +import Text.Hakyll.Renderable import Text.Hakyll.Util depends :: FilePath -> [FilePath] -> IO () -> IO () @@ -24,17 +25,14 @@ depends file dependencies action = do valid <- isCacheValid (toDestination file) dependencies unless valid action -createContext :: Page -> Context -createContext = M.fromList . map packPair . M.toList - where packPair (a, b) = (B.pack a, b) - -renderPage :: FilePath -> Page -> IO Page -renderPage templatePath page = do +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 - let body = substitute templateString (createContext page) - return $ M.insert "body" body page + context <- toContext renderable + let body = substitute templateString context + return $ Page (M.insert "body" body $ M.mapKeys (B.unpack) context) writePage :: Page -> IO () writePage page = do @@ -47,15 +45,16 @@ renderAndConcat templatePath paths = foldM concatRender' B.empty paths where concatRender' :: B.ByteString -> FilePath -> IO B.ByteString concatRender' chunk path = do page <- readPage path - rendered <- renderPage templatePath page + rendered <- render templatePath page let body = getBody rendered return $ B.append chunk $ body -renderChain :: [FilePath] -> FilePath -> IO () -renderChain templates pagePath = depends (toURL pagePath) (pagePath : templates) $ - do page <- readPage pagePath - result <- foldM (flip renderPage) page templates - writePage result +renderChain :: Renderable a => [FilePath] -> a -> IO () +renderChain templates renderable = + depends (getURL renderable) (getDependencies renderable ++ templates) $ + do initialPage <- toContext renderable + result <- foldM (flip render) (Page $ M.mapKeys B.unpack initialPage) templates + writePage result static :: FilePath -> IO () static source = do |