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