summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/Template.hs
blob: 83fd7ebf18f29c10d5e1937f3c67b5c03990470b (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
module Hakyll.Web.Template
    ( Template
    , applyTemplate
    , applySelf
    ) where

import Data.Maybe (fromMaybe)
import qualified Data.Map as M

import Hakyll.Web.Template.Internal
import Hakyll.Web.Template.Read.Hakyll (readTemplate)
import Hakyll.Web.Page

-- | Substitutes @$identifiers@ in the given @Template@ by values from the given
-- "Page". When a key is not found, it is left as it is. You can specify
-- the characters used to replace escaped dollars (@$$@) here.
--
applyTemplate :: Template -> Page String -> Page String
applyTemplate template page =
    fmap (const $ substitute =<< unTemplate template) page
  where
    substitute (Chunk chunk) = chunk
    substitute (Identifier key) =
        fromMaybe ('$' : key) $ M.lookup key $ toMap page
    substitute (Escaped) = "$"

-- | Apply a page as it's own template. This is often very useful to fill in
-- certain keys like @$root@ and @$url@.
--
applySelf :: Page String -> Page String
applySelf page = applyTemplate (readTemplate $ pageBody page) page