summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll/Renderables.hs
blob: c79df76054833fc5d3714e9d6f4c9217ab78d617 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module Text.Hakyll.Renderables
    ( CustomPage,
      createCustomPage,
      PagePath,
      createPagePath
    ) where

import System.FilePath
import qualified Data.ByteString.Lazy.Char8 as B
import qualified Data.Map as M
import Control.Monad
import Text.Hakyll.Page
import Text.Hakyll.Renderable
import Text.Hakyll.File

-- | A custom page.
data CustomPage = CustomPage 
    { url :: String,
      dependencies :: [FilePath],
      mapping :: [(String, Either String (IO B.ByteString))]
    }

-- | Create a custom page.
createCustomPage :: String -- ^ Destination of the page, relative to _site.
                 -> [FilePath] -- ^ Dependencies of the page.
                 -> [(String, Either String (IO B.ByteString))] -- ^ Key - value mapping for rendering.
                 -> CustomPage
createCustomPage = CustomPage

instance Renderable CustomPage where
    getDependencies = dependencies
    getURL = url
    toContext page = do
        values <- mapM (either (return . B.pack) (>>= return) . snd) (mapping page)
        let keys = map (B.pack . fst) (mapping page)
        return $ M.fromList $ (B.pack "url", B.pack $ url page) : zip keys values 

-- | PagePath is a class that wraps a FilePath. This is used to render Pages
--   without reading them first through use of caching.
data PagePath = PagePath FilePath

-- | Create a PagePath from a FilePath.
createPagePath :: FilePath -> PagePath
createPagePath = PagePath

-- We can render filepaths
instance Renderable PagePath where
    getDependencies (PagePath path) = return path
    getURL (PagePath path) = toURL path
    toContext (PagePath path) = readPage path >>= toContext