summaryrefslogtreecommitdiff
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
parentee320c61668b532cafce7f4fd0a80ba43b3b512a (diff)
downloadhakyll-9e07d1ba364ca401cc1c6f5704023b1df1006779.tar.gz
Template syntax: $foo → $foo$
-rw-r--r--src/Hakyll/Web/Template.hs19
-rw-r--r--src/Hakyll/Web/Template/Internal.hs6
2 files changed, 15 insertions, 10 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@.
diff --git a/src/Hakyll/Web/Template/Internal.hs b/src/Hakyll/Web/Template/Internal.hs
index 096c928..d0f6472 100644
--- a/src/Hakyll/Web/Template/Internal.hs
+++ b/src/Hakyll/Web/Template/Internal.hs
@@ -29,17 +29,17 @@ instance Writable Template where
data TemplateElement
= Chunk String
| Identifier String
- | Escaped String
+ | Escaped
deriving (Show, Eq, Typeable)
instance Binary TemplateElement where
put (Chunk string) = putWord8 0 >> put string
put (Identifier key) = putWord8 1 >> put key
- put (Escaped key) = putWord8 2 >> put key
+ put (Escaped) = putWord8 2
get = getWord8 >>= \tag -> case tag of
0 -> Chunk <$> get
1 -> Identifier <$> get
- 2 -> Escaped <$> get
+ 2 -> return Escaped
_ -> error $ "Hakyll.Web.Template.Internal: "
++ "Error reading cached template"