on fb71a21 help sign in
hakyll/src/Text/Hakyll/Renderables.hs
49 lines 1.6 kB history raw
1module Text.Hakyll.Renderables
2 ( CustomPage
3 , createCustomPage
4 , PagePath
5 , createPagePath
6 ) where
8import System.FilePath (FilePath)
9import qualified Data.ByteString.Lazy.Char8 as B
10import qualified Data.Map as M
11import Text.Hakyll.Page
12import Text.Hakyll.Renderable
13import Text.Hakyll.File
15-- | A custom page.
16data CustomPage = CustomPage
17 { url :: String,
18 dependencies :: [FilePath],
19 mapping :: [(String, Either String (IO B.ByteString))]
20 }
22-- | Create a custom page.
23createCustomPage :: String -- ^ Destination of the page, relative to _site.
24 -> [FilePath] -- ^ Dependencies of the page.
25 -> [(String, Either String (IO B.ByteString))] -- ^ Key - value mapping for rendering.
26 -> CustomPage
27createCustomPage = CustomPage
29instance Renderable CustomPage where
30 getDependencies = dependencies
31 getURL = url
32 toContext page = do
33 values <- mapM (either (return . B.pack) (>>= return) . snd) (mapping page)
34 let keys = map (B.pack . fst) (mapping page)
35 return $ M.fromList $ (B.pack "url", B.pack $ url page) : zip keys values
37-- | PagePath is a class that wraps a FilePath. This is used to render Pages
38-- without reading them first through use of caching.
39data PagePath = PagePath FilePath
41-- | Create a PagePath from a FilePath.
42createPagePath :: FilePath -> PagePath
43createPagePath = PagePath
45-- We can render filepaths
46instance Renderable PagePath where
47 getDependencies (PagePath path) = return path
48 getURL (PagePath path) = toURL path
49 toContext (PagePath path) = readPage path >>= toContext