@@ -22,16 +22,22 @@ import Text.Hakyll.Internal.Template.Template
22import Text.Hakyll.Internal.Template.Hamlet
22import Text.Hakyll.Internal.Template.Hamlet
24-- | Construct a @Template@ from a string.
24-- | Construct a @Template@ from a string.
25fromString :: String -> Template
26fromString :: String -> Template
26−fromString [] = End
27+fromString = Template . fromString'
28− | "$$" `isPrefixOf` string = EscapeCharacter (fromString $ tail tail')
29− | "$" `isPrefixOf` string = let (key, rest) = span isAlphaNum tail'
30− in Identifier key (fromString rest)
31− | otherwise = let (chunk, rest) = break (== '$') string
32− in Chunk chunk (fromString rest)
31+ | "$$" `isPrefixOf` string =
32+ EscapeCharacter : (fromString' $ tail tail')
33+ | "$" `isPrefixOf` string =
34+ let (key, rest) = span isAlphaNum tail'
35+ in Identifier key : fromString' rest
37+ let (chunk, rest) = break (== '$') string
38+ in Chunk chunk : fromString' rest
34− tail' = tail string
40+ tail' = tail string
36-- | Read a @Template@ from a file. This function might fetch the @Template@
42-- | Read a @Template@ from a file. This function might fetch the @Template@
37-- from the cache, if available.
43-- from the cache, if available.
@@ -60,15 +66,12 @@ readTemplate path = do
60-- "Context". When a key is not found, it is left as it is. You can specify
66-- "Context". When a key is not found, it is left as it is. You can specify
61-- the characters used to replace escaped dollars (@$$@) here.
67-- the characters used to replace escaped dollars (@$$@) here.
62substitute :: String -> Template -> Context -> String
68substitute :: String -> Template -> Context -> String
63−substitute escaper (Chunk chunk template) context =
69+substitute escaper template context = substitute' =<< unTemplate template
64− chunk ++ substitute escaper template context
65−substitute escaper (Identifier key template) context =
66− replacement ++ substitute escaper template context
71+ substitute' (Chunk chunk) = chunk
72+ substitute' (Identifier key) =
68− replacement = fromMaybe ('$' : key) $ M.lookup key $ unContext context
73+ fromMaybe ('$' : key) $ M.lookup key $ unContext context
69−substitute escaper (EscapeCharacter template) context =
74+ substitute' (EscapeCharacter) = escaper
70− escaper ++ substitute escaper template context
71−substitute _ End _ = []
73-- | @substitute@ for use during a chain. This will leave escaped characters as
76-- | @substitute@ for use during a chain. This will leave escaped characters as
74-- they are.
77-- they are.