summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Hakyll/Web/Template.hs10
-rw-r--r--src/Hakyll/Web/Template/Internal.hs8
2 files changed, 11 insertions, 7 deletions
diff --git a/src/Hakyll/Web/Template.hs b/src/Hakyll/Web/Template.hs
index 586d0b6..b4f2ea5 100644
--- a/src/Hakyll/Web/Template.hs
+++ b/src/Hakyll/Web/Template.hs
@@ -21,14 +21,18 @@ readTemplate = Template . readTemplate'
readTemplate' [] = []
readTemplate' string
| "$$" `isPrefixOf` string =
- EscapeCharacter : (readTemplate' $ drop 2 string)
+ let (key, rest) = readIdentifier $ drop 2 string
+ in Escaped key : readTemplate' rest
| "$" `isPrefixOf` string =
- let (key, rest) = span isAlphaNum $ drop 1 string
+ let (key, rest) = readIdentifier $ drop 1 string
in Identifier key : readTemplate' rest
| otherwise =
let (chunk, rest) = break (== '$') string
in Chunk chunk : readTemplate' rest
+ -- Parse an identifier into (identifier, rest)
+ readIdentifier = span isAlphaNum
+
-- | 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
-- the characters used to replace escaped dollars (@$$@) here.
@@ -40,7 +44,7 @@ applyTemplate template page =
substitute (Chunk chunk) = chunk
substitute (Identifier key) =
fromMaybe ('$' : key) $ M.lookup key $ toMap page
- substitute (EscapeCharacter) = "$"
+ substitute (Escaped key) = '$' : key
-- | 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 43df1db..be10881 100644
--- a/src/Hakyll/Web/Template/Internal.hs
+++ b/src/Hakyll/Web/Template/Internal.hs
@@ -29,16 +29,16 @@ instance Writable Template where
data TemplateElement
= Chunk String
| Identifier String
- | EscapeCharacter
+ | Escaped String
deriving (Show, Eq, Typeable)
instance Binary TemplateElement where
put (Chunk string) = putWord8 0 >> put string
put (Identifier key) = putWord8 1 >> put key
- put (EscapeCharacter) = putWord8 2
+ put (Escaped key) = putWord8 2 >> put key
get = getWord8 >>= \tag -> case tag of
- 0 -> Chunk <$> get
+ 0 -> Chunk <$> get
1 -> Identifier <$> get
- 2 -> return EscapeCharacter
+ 2 -> Escaped <$> get
_ -> error "Error reading cached template"