on master help sign in
Template syntax: $foo → $foo$
commit9e07d1ba364ca401cc1c6f5704023b1df1006779 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateWed 9 Feb 2011, 13:27 (16 years ago) parentee320c6
2 files changed+15−10
src/Hakyll/Web/Template.hs+12−7 src/Hakyll/Web/Template/Internal.hs+3−3
src/Hakyll/Web/Template.hs+12−7
@@ -21,17 +21,22 @@ readTemplate = Template . readTemplate'
2121 readTemplate' [] = []
2222 readTemplate' string
2323 | "$$" `isPrefixOf` string =
24− let (key, rest) = readIdentifier $ drop 2 string
25− in Escaped key : readTemplate' rest
24+ Escaped : readTemplate' (drop 2 string)
2625 | "$" `isPrefixOf` string =
27− let (key, rest) = readIdentifier $ drop 1 string
28− in Identifier key : readTemplate' rest
26+ case readIdentifier (drop 1 string) of
27+ Just (key, rest) -> Identifier key : readTemplate' rest
28+ Nothing -> Chunk "$" : readTemplate' (drop 1 string)
2929 | otherwise =
3030 let (chunk, rest) = break (== '$') string
3131 in Chunk chunk : readTemplate' rest
3232
33− -- Parse an identifier into (identifier, rest)
34− readIdentifier = span isAlphaNum
33+ -- Parse an identifier into (identifier, rest) if it's valid, and return
34+ -- Nothing otherwise
35+ readIdentifier string =
36+ let (identifier, rest) = span isAlphaNum string
37+ in if not (null identifier) && "$" `isPrefixOf` rest
38+ then Just (identifier, drop 1 rest)
39+ else Nothing
3540
3641-- | Substitutes @$identifiers@ in the given @Template@ by values from the given
3742-- "Page". When a key is not found, it is left as it is. You can specify
@@ -44,7 +49,7 @@ applyTemplate template page =
4449 substitute (Chunk chunk) = chunk
4550 substitute (Identifier key) =
4651 fromMaybe ('$' : key) $ M.lookup key $ toMap page
47− substitute (Escaped key) = '$' : key
52+ substitute (Escaped) = "$"
4853
4954-- | Apply a page as it's own template. This is often very useful to fill in
5055-- certain keys like @$root@ and @$url@.
src/Hakyll/Web/Template/Internal.hs+3−3
@@ -29,17 +29,17 @@ instance Writable Template where
2929data TemplateElement
3030 = Chunk String
3131 | Identifier String
32− | Escaped String
32+ | Escaped
3333 deriving (Show, Eq, Typeable)
3434
3535instance Binary TemplateElement where
3636 put (Chunk string) = putWord8 0 >> put string
3737 put (Identifier key) = putWord8 1 >> put key
38− put (Escaped key) = putWord8 2 >> put key
38+ put (Escaped) = putWord8 2
3939
4040 get = getWord8 >>= \tag -> case tag of
4141 0 -> Chunk <$> get
4242 1 -> Identifier <$> get
43− 2 -> Escaped <$> get
43+ 2 -> return Escaped
4444 _ -> error $ "Hakyll.Web.Template.Internal: "
4545 ++ "Error reading cached template"