blob: 2421b2dfd5bdeef371c0402477755d4340c6cf1e (
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
--------------------------------------------------------------------------------
-- | Read templates in Hakyll's native format
module Hakyll.Web.Template.Read
( readTemplate
) where
--------------------------------------------------------------------------------
import Control.Applicative ((<$), (<$>))
import Control.Monad (void)
import Text.Parsec
import Text.Parsec.String
--------------------------------------------------------------------------------
import Hakyll.Core.Util.Parser
import Hakyll.Web.Template.Internal
--------------------------------------------------------------------------------
readTemplate :: String -> Template
readTemplate input = case parse template "" input of
Left err -> error $ "Cannot parse template: " ++ show err
Right t -> t
--------------------------------------------------------------------------------
template :: Parser Template
template = Template <$>
(many1 $ chunk <|> escaped <|> conditional <|> for <|> partial <|> key)
--------------------------------------------------------------------------------
chunk :: Parser TemplateElement
chunk = Chunk <$> (many1 $ noneOf "$")
--------------------------------------------------------------------------------
escaped :: Parser TemplateElement
escaped = Escaped <$ (try $ string "$$")
--------------------------------------------------------------------------------
conditional :: Parser TemplateElement
conditional = try $ do
void $ string "$if("
i <- metadataKey
void $ string ")$"
thenBranch <- template
elseBranch <- optionMaybe $ try (string "$else$") >> template
void $ string "$endif$"
return $ If i thenBranch elseBranch
--------------------------------------------------------------------------------
for :: Parser TemplateElement
for = try $ do
void $ string "$for("
i <- metadataKey
void $ string ")$"
body <- template
sep <- optionMaybe $ try (string "$sep$") >> template
void $ string "$endfor$"
return $ For i body sep
--------------------------------------------------------------------------------
partial :: Parser TemplateElement
partial = try $ do
void $ string "$partial("
i <- stringLiteral
void $ string ")$"
return $ Partial i
--------------------------------------------------------------------------------
key :: Parser TemplateElement
key = try $ do
void $ char '$'
k <- metadataKey
void $ char '$'
return $ Key k
--------------------------------------------------------------------------------
stringLiteral :: Parser String
stringLiteral = do
void $ char '\"'
str <- many $ do
x <- noneOf "\""
if x == '\\' then anyChar else return x
void $ char '\"'
return str
|