diff options
author | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2010-01-11 08:55:13 +0100 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2010-01-11 08:55:13 +0100 |
commit | 8548888d1c6e937bc7de2b8802ba9b9ee5a432f4 (patch) | |
tree | 3f265d63deb797419c4739044f25d1e99b612dee /src | |
parent | 4e3c26b01c369a3a70467ae4f925427775bb2726 (diff) | |
download | hakyll-8548888d1c6e937bc7de2b8802ba9b9ee5a432f4.tar.gz |
Fixed $$ escaping.
Diffstat (limited to 'src')
-rw-r--r-- | src/Text/Hakyll/Render.hs | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/src/Text/Hakyll/Render.hs b/src/Text/Hakyll/Render.hs index 1719e83..41f76b8 100644 --- a/src/Text/Hakyll/Render.hs +++ b/src/Text/Hakyll/Render.hs @@ -35,17 +35,27 @@ depends file dependencies action = do unless valid action -- | Substitutes `$identifiers` in the given string by values from the given --- "Context". When a key is not found, it is left as it is. -substitute :: String -> Context -> String -substitute [] _ = [] -substitute string context - | "$$" `isPrefixOf` string = "$$" ++ (substitute (tail tail') context) - | "$" `isPrefixOf` string = substitute' - | otherwise = (head string) : (substitute tail' context) +-- "Context". When a key is not found, it is left as it is. You can here +-- specify the characters used to replace escaped dollars `$$`. +substitute :: String -> String -> Context -> String +substitute _ [] _ = [] +substitute escaper string context + | "$$" `isPrefixOf` string = escaper ++ substitute' (tail tail') + | "$" `isPrefixOf` string = substituteKey + | otherwise = (head string) : (substitute' tail') where tail' = tail string (key, rest) = break (not . isAlpha) tail' replacement = fromMaybe ('$' : key) $ M.lookup key context - substitute' = replacement ++ substitute rest context + substituteKey = replacement ++ substitute' rest + substitute' str = substitute escaper str context + +-- | "substitute" for use during a chain. +regularSubstitute :: String -> Context -> String +regularSubstitute = substitute "$$" + +-- | "substitute" for the end of a chain (just before writing). +finalSubstitute :: String -> Context -> String +finalSubstitute = substitute "$" -- | Render to a Page. render :: Renderable a @@ -69,7 +79,7 @@ renderWith manipulation templatePath renderable = do -- Ignore $root when substituting here. We will only replace that in the -- final render (just before writing). let contextIgnoringRoot = M.insert "root" "$root" context - body = substitute templateString contextIgnoringRoot + body = regularSubstitute templateString contextIgnoringRoot return $ fromContext (M.insert "body" body context) -- | Render each renderable with the given template, then concatenate the @@ -117,8 +127,8 @@ writePage page = do writeFile destination body where url = getURL page -- Substitute $root here, just before writing. - body = substitute (getBody page) - (M.singleton "root" $ toRoot url) + body = finalSubstitute (getBody page) + (M.singleton "root" $ toRoot url) -- | Mark a certain file as static, so it will just be copied when the site is -- generated. |