on master help sign in
Escaped carries allong it's escaped value
220e4b4Jasper Van der Jeugt
src/Hakyll/Web/Template.hs+7−3
@@ -21,14 +21,18 @@ readTemplate = Template . readTemplate'
21 readTemplate' [] = []
21 readTemplate' [] = []
22 readTemplate' string
22 readTemplate' string
23 | "$$" `isPrefixOf` string =
23 | "$$" `isPrefixOf` string =
24− EscapeCharacter : (readTemplate' $ drop 2 string)
24+ let (key, rest) = readIdentifier $ drop 2 string
25+ in Escaped key : readTemplate' rest
25 | "$" `isPrefixOf` string =
26 | "$" `isPrefixOf` string =
26− let (key, rest) = span isAlphaNum $ drop 1 string
27+ let (key, rest) = readIdentifier $ drop 1 string
27 in Identifier key : readTemplate' rest
28 in Identifier key : readTemplate' rest
28 | otherwise =
29 | otherwise =
29 let (chunk, rest) = break (== '$') string
30 let (chunk, rest) = break (== '$') string
30 in Chunk chunk : readTemplate' rest
31 in Chunk chunk : readTemplate' rest
31
32
33+ -- Parse an identifier into (identifier, rest)
34+ readIdentifier = span isAlphaNum
35+
32-- | Substitutes @$identifiers@ in the given @Template@ by values from the given
36-- | Substitutes @$identifiers@ in the given @Template@ by values from the given
33-- "Page". When a key is not found, it is left as it is. You can specify
37-- "Page". When a key is not found, it is left as it is. You can specify
34-- the characters used to replace escaped dollars (@$$@) here.
38-- the characters used to replace escaped dollars (@$$@) here.
@@ -40,7 +44,7 @@ applyTemplate template page =
40 substitute (Chunk chunk) = chunk
44 substitute (Chunk chunk) = chunk
41 substitute (Identifier key) =
45 substitute (Identifier key) =
42 fromMaybe ('$' : key) $ M.lookup key $ toMap page
46 fromMaybe ('$' : key) $ M.lookup key $ toMap page
43− substitute (EscapeCharacter) = "$"
47+ substitute (Escaped key) = '$' : key
44
48
45-- | Apply a page as it's own template. This is often very useful to fill in
49-- | Apply a page as it's own template. This is often very useful to fill in
46-- certain keys like @$root@ and @$url@.
50-- certain keys like @$root@ and @$url@.
src/Hakyll/Web/Template/Internal.hs+4−4
@@ -29,16 +29,16 @@ instance Writable Template where
29data TemplateElement
29data TemplateElement
30 = Chunk String
30 = Chunk String
31 | Identifier String
31 | Identifier String
32− | EscapeCharacter
32+ | Escaped String
33 deriving (Show, Eq, Typeable)
33 deriving (Show, Eq, Typeable)
34
34
35instance Binary TemplateElement where
35instance Binary TemplateElement where
36 put (Chunk string) = putWord8 0 >> put string
36 put (Chunk string) = putWord8 0 >> put string
37 put (Identifier key) = putWord8 1 >> put key
37 put (Identifier key) = putWord8 1 >> put key
38− put (EscapeCharacter) = putWord8 2
38+ put (Escaped key) = putWord8 2 >> put key
39
39
40 get = getWord8 >>= \tag -> case tag of
40 get = getWord8 >>= \tag -> case tag of
41− 0 -> Chunk <$> get
41+ 0 -> Chunk <$> get
42 1 -> Identifier <$> get
42 1 -> Identifier <$> get
43− 2 -> return EscapeCharacter
43+ 2 -> Escaped <$> get
44 _ -> error "Error reading cached template"
44 _ -> error "Error reading cached template"