module Text.Hakyll.Renderables
( CustomPage
, createCustomPage
, createListing
, createListingWith
, PagePath
, createPagePath
, CombinedRenderable
, combine
, combineWithUrl
) where
import qualified Data.Map as M
import Control.Arrow (second)
import Control.Monad (liftM)
import Data.Binary
import Text.Hakyll.Hakyll (Hakyll)
import Text.Hakyll.Page
import Text.Hakyll.Renderable
import Text.Hakyll.File
import Text.Hakyll.Context
import Text.Hakyll.Render
data CustomPage = CustomPage
{ customPageUrl :: String,
customPageDependencies :: [FilePath],
customPageContext :: [(String, Either String (Hakyll String))]
}
createCustomPage :: String
-> [FilePath]
-> [(String, Either String (Hakyll String))]
-> CustomPage
createCustomPage = CustomPage
createListing :: (Renderable a)
=> String
-> FilePath
-> [a]
-> [(String, String)]
-> CustomPage
createListing = createListingWith id
createListingWith :: (Renderable a)
=> ContextManipulation
-> String
-> FilePath
-> [a]
-> [(String, String)]
-> CustomPage
createListingWith manipulation url template renderables additional =
createCustomPage url dependencies context
where
dependencies = template : concatMap getDependencies renderables
context = ("body", Right concatenation) : additional'
concatenation = renderAndConcatWith manipulation [template] renderables
additional' = map (second Left) additional
instance Renderable CustomPage where
getDependencies = customPageDependencies
getUrl = return . customPageUrl
toContext page = do
values <- mapM (either return id . snd) (customPageContext page)
let pairs = zip (map fst $ customPageContext page) values
return $ M.fromList $ ("url", customPageUrl page) : pairs
data PagePath = PagePath FilePath
deriving (Ord, Eq, Read, Show)
createPagePath :: FilePath -> PagePath
createPagePath = PagePath
instance Renderable PagePath where
getDependencies (PagePath path) = return path
getUrl (PagePath path) = toUrl path
toContext (PagePath path) = readPage path >>= toContext
instance Binary PagePath where
put (PagePath path) = put path
get = liftM PagePath get
data CombinedRenderable a b = CombinedRenderable a b
| CombinedRenderableWithUrl FilePath a b
deriving (Ord, Eq, Read, Show)
combine :: (Renderable a, Renderable b) => a -> b -> CombinedRenderable a b
combine = CombinedRenderable
combineWithUrl :: (Renderable a, Renderable b)
=> FilePath
-> a
-> b
-> CombinedRenderable a b
combineWithUrl = CombinedRenderableWithUrl
instance (Renderable a, Renderable b)
=> Renderable (CombinedRenderable a b) where
getDependencies (CombinedRenderable a b) =
getDependencies a ++ getDependencies b
getDependencies (CombinedRenderableWithUrl _ a b) =
getDependencies a ++ getDependencies b
getUrl (CombinedRenderable a _) = getUrl a
getUrl (CombinedRenderableWithUrl url _ _) = return url
toContext (CombinedRenderable a b) = do
c1 <- toContext a
c2 <- toContext b
return $ c1 `M.union` c2
toContext (CombinedRenderableWithUrl url a b) = do
c <- toContext (CombinedRenderable a b)
return $ M.singleton "url" url `M.union` c