@@ -35,17 +35,27 @@ depends file dependencies action = do
3535 unless valid action
3636
3737-- | Substitutes `$identifiers` in the given string by values from the given
38−-- "Context". When a key is not found, it is left as it is.
39−substitute :: String -> Context -> String
40−substitute [] _ = []
41−substitute string context
42− | "$$" `isPrefixOf` string = "$$" ++ (substitute (tail tail') context)
43− | "$" `isPrefixOf` string = substitute'
44− | otherwise = (head string) : (substitute tail' context)
38+-- "Context". When a key is not found, it is left as it is. You can here
39+-- specify the characters used to replace escaped dollars `$$`.
40+substitute :: String -> String -> Context -> String
41+substitute _ [] _ = []
42+substitute escaper string context
43+ | "$$" `isPrefixOf` string = escaper ++ substitute' (tail tail')
44+ | "$" `isPrefixOf` string = substituteKey
45+ | otherwise = (head string) : (substitute' tail')
4546 where tail' = tail string
4647 (key, rest) = break (not . isAlpha) tail'
4748 replacement = fromMaybe ('$' : key) $ M.lookup key context
48− substitute' = replacement ++ substitute rest context
49+ substituteKey = replacement ++ substitute' rest
50+ substitute' str = substitute escaper str context
51+
52+-- | "substitute" for use during a chain.
53+regularSubstitute :: String -> Context -> String
54+regularSubstitute = substitute "$$"
55+
56+-- | "substitute" for the end of a chain (just before writing).
57+finalSubstitute :: String -> Context -> String
58+finalSubstitute = substitute "$"
4959
5060-- | Render to a Page.
5161render :: Renderable a
@@ -69,7 +79,7 @@ renderWith manipulation templatePath renderable = do
6979 -- Ignore $root when substituting here. We will only replace that in the
7080 -- final render (just before writing).
7181 let contextIgnoringRoot = M.insert "root" "$root" context
72− body = substitute templateString contextIgnoringRoot
82+ body = regularSubstitute templateString contextIgnoringRoot
7383 return $ fromContext (M.insert "body" body context)
7484
7585-- | Render each renderable with the given template, then concatenate the
@@ -117,8 +127,8 @@ writePage page = do
117127 writeFile destination body
118128 where url = getURL page
119129 -- Substitute $root here, just before writing.
120− body = substitute (getBody page)
121− (M.singleton "root" $ toRoot url)
130+ body = finalSubstitute (getBody page)
131+ (M.singleton "root" $ toRoot url)
122132
123133-- | Mark a certain file as static, so it will just be copied when the site is
124134-- generated.