summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/Template/Read/Hamlet.hs
blob: 7b496de54e0ed71c7a06805cd4d7f0a21a36d99b (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
-- | Read templates in the hamlet format
--
{-# LANGUAGE MultiParamTypeClasses #-}
module Hakyll.Web.Template.Read.Hamlet
    ( readHamletTemplate
    , readHamletTemplateWith
    ) where

import Text.Hamlet (HamletSettings (..), defaultHamletSettings)
import Text.Hamlet.RT

import Hakyll.Web.Template.Internal

-- | Read a hamlet template using the default settings
--
readHamletTemplate :: String -> Template
readHamletTemplate = readHamletTemplateWith defaultHamletSettings

-- | Read a hamlet template using the specified settings
--
readHamletTemplateWith :: HamletSettings -> String -> Template
readHamletTemplateWith settings string =
    let result = parseHamletRT settings string
    in case result of
        Just hamlet -> fromHamletRT hamlet
        Nothing     -> error
            "Hakyll.Web.Template.Read.Hamlet.readHamletTemplateWith: \
            \Could not parse Hamlet file"

-- | Convert a 'HamletRT' to a 'Template'
--
fromHamletRT :: HamletRT  -- ^ Hamlet runtime template
             -> Template  -- ^ Hakyll template
fromHamletRT (HamletRT sd) = Template $ map fromSimpleDoc sd
  where
    fromSimpleDoc :: SimpleDoc -> TemplateElement
    fromSimpleDoc (SDRaw chunk) = Chunk chunk
    fromSimpleDoc (SDVar [var]) = Key var
    fromSimpleDoc (SDVar _) = error
        "Hakyll.Web.Template.Read.Hamlet.fromHamletRT: \
        \Hakyll does not support '.' in identifier names when using \
        \hamlet templates."
    fromSimpleDoc _ = error
        "Hakyll.Web.Template.Read.Hamlet.fromHamletRT: \
        \Only simple $key$ identifiers are allowed when using hamlet \
        \templates."