blob: 7dfe0032fc971b306932e047e29dd5b8e2a24ecc (
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
|
--------------------------------------------------------------------------------
-- | Read templates in Hakyll's native format
module Hakyll.Web.Template.Read
( readTemplate
) where
--------------------------------------------------------------------------------
import Data.List (isPrefixOf)
--------------------------------------------------------------------------------
import Hakyll.Web.Template.Internal
--------------------------------------------------------------------------------
-- | Construct a @Template@ from a string.
readTemplate :: String -> Template
readTemplate = Template . readTemplate'
where
readTemplate' [] = []
readTemplate' string
| "$$" `isPrefixOf` string =
Escaped : readTemplate' (drop 2 string)
| "$" `isPrefixOf` string =
case readKey (drop 1 string) of
Just (key, rest) -> Key key : readTemplate' rest
Nothing -> Chunk "$" : readTemplate' (drop 1 string)
| otherwise =
let (chunk, rest) = break (== '$') string
in Chunk chunk : readTemplate' rest
-- Parse an key into (key, rest) if it's valid, and return
-- Nothing otherwise
readKey string =
let (key, rest) = span validKeyChar string
in if not (null key) && "$" `isPrefixOf` rest
then Just (key, drop 1 rest)
else Nothing
validKeyChar x = x `notElem` ['$', '\n', '\r']
|