module Text.Hakyll.Render
( depends
, render
, renderWith
, renderAndConcat
, renderAndConcatWith
, renderChain
, renderChainWith
, static
, css
) where
import Control.Monad (unless, mapM)
import System.Directory (copyFile)
import System.IO
import Text.Hakyll.Context (ContextManipulation)
import Text.Hakyll.Page
import Text.Hakyll.Renderable
import Text.Hakyll.File
import Text.Hakyll.CompressCSS
import Text.Hakyll.Render.Internal
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 = renderWith id
renderWith :: Renderable a
=> ContextManipulation
-> FilePath
-> a
-> IO Page
renderWith manipulation templatePath renderable = do
template <- readFile templatePath
context <- toContext renderable
return $ fromContext $ pureRenderWith manipulation template context
renderAndConcat :: Renderable a => FilePath -> [a] -> IO String
renderAndConcat = renderAndConcatWith id
renderAndConcatWith :: Renderable a
=> ContextManipulation
-> FilePath
-> [a]
-> IO String
renderAndConcatWith manipulation templatePath renderables = do
template <- readFile templatePath
contexts <- mapM toContext renderables
return $ pureRenderAndConcatWith manipulation template contexts
renderChain :: Renderable a => [FilePath] -> a -> IO ()
renderChain = renderChainWith id
renderChainWith :: Renderable a
=> ContextManipulation -> [FilePath] -> a -> IO ()
renderChainWith manipulation templatePaths renderable =
depends (getURL renderable) (getDependencies renderable ++ templatePaths) $
do templates <- mapM readFile templatePaths
context <- toContext renderable
let result = pureRenderChainWith manipulation templates context
writePage $ fromContext result
static :: FilePath -> IO ()
static source = depends destination [source] action
where
destination = toDestination source
action = do makeDirectories destination
copyFile source destination
css :: FilePath -> IO ()
css source = depends destination [source] css'
where
destination = toDestination source
css' = do contents <- readFile source
makeDirectories destination
writeFile destination (compressCSS contents)