summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/Template.hs
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2011-02-09 13:27:45 +0100
committerJasper Van der Jeugt <jaspervdj@gmail.com>2011-02-09 13:27:45 +0100
commit9e07d1ba364ca401cc1c6f5704023b1df1006779 (patch)
treee33c4897b1623c1b2efd8d4aa8fec2aceec546b1 /src/Hakyll/Web/Template.hs
parentee320c61668b532cafce7f4fd0a80ba43b3b512a (diff)
downloadhakyll-9e07d1ba364ca401cc1c6f5704023b1df1006779.tar.gz
Template syntax: $foo → $foo$
Diffstat (limited to 'src/Hakyll/Web/Template.hs')
-rw-r--r--src/Hakyll/Web/Template.hs19
1 files changed, 12 insertions, 7 deletions
diff --git a/src/Hakyll/Web/Template.hs b/src/Hakyll/Web/Template.hs
index b4f2ea5..06fa8d4 100644
--- a/src/Hakyll/Web/Template.hs
+++ b/src/Hakyll/Web/Template.hs
@@ -21,17 +21,22 @@ readTemplate = Template . readTemplate'
readTemplate' [] = []
readTemplate' string
| "$$" `isPrefixOf` string =
- let (key, rest) = readIdentifier $ drop 2 string
- in Escaped key : readTemplate' rest
+ Escaped : readTemplate' (drop 2 string)
| "$" `isPrefixOf` string =
- let (key, rest) = readIdentifier $ drop 1 string
- in Identifier key : readTemplate' rest
+ case readIdentifier (drop 1 string) of
+ Just (key, rest) -> Identifier key : readTemplate' rest
+ Nothing -> Chunk "$" : readTemplate' (drop 1 string)
| otherwise =
let (chunk, rest) = break (== '$') string
in Chunk chunk : readTemplate' rest
- -- Parse an identifier into (identifier, rest)
- readIdentifier = span isAlphaNum
+ -- Parse an identifier into (identifier, rest) if it's valid, and return
+ -- Nothing otherwise
+ readIdentifier string =
+ let (identifier, rest) = span isAlphaNum string
+ in if not (null identifier) && "$" `isPrefixOf` rest
+ then Just (identifier, drop 1 rest)
+ else Nothing
-- | 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
@@ -44,7 +49,7 @@ applyTemplate template page =
substitute (Chunk chunk) = chunk
substitute (Identifier key) =
fromMaybe ('$' : key) $ M.lookup key $ toMap page
- substitute (Escaped key) = '$' : key
+ substitute (Escaped) = "$"
-- | Apply a page as it's own template. This is often very useful to fill in
-- certain keys like @$root@ and @$url@.