From 9e07d1ba364ca401cc1c6f5704023b1df1006779 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Wed, 9 Feb 2011 13:27:45 +0100 Subject: Template syntax: $foo → $foo$ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Hakyll/Web/Template.hs | 19 ++++++++++++------- src/Hakyll/Web/Template/Internal.hs | 6 +++--- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/Hakyll/Web/Template.hs b/src/Hakyll/Web/Template.hs index b4f2ea5..06fa8d4 100644 --- a/src/Hakyll/Web/Template.hs +++ b/src/Hakyll/Web/Template.hs @@ -21,17 +21,22 @@ readTemplate = Template . readTemplate' readTemplate' [] = [] readTemplate' string | "$$" `isPrefixOf` string = - let (key, rest) = readIdentifier $ drop 2 string - in Escaped key : readTemplate' rest + Escaped : readTemplate' (drop 2 string) | "$" `isPrefixOf` string = - let (key, rest) = readIdentifier $ drop 1 string - in Identifier key : readTemplate' rest + case readIdentifier (drop 1 string) of + Just (key, rest) -> Identifier key : readTemplate' rest + Nothing -> Chunk "$" : readTemplate' (drop 1 string) | otherwise = let (chunk, rest) = break (== '$') string in Chunk chunk : readTemplate' rest - -- Parse an identifier into (identifier, rest) - readIdentifier = span isAlphaNum + -- Parse an identifier into (identifier, rest) if it's valid, and return + -- Nothing otherwise + readIdentifier string = + let (identifier, rest) = span isAlphaNum string + in if not (null identifier) && "$" `isPrefixOf` rest + then Just (identifier, drop 1 rest) + else Nothing -- | Substitutes @$identifiers@ in the given @Template@ by values from the given -- "Page". When a key is not found, it is left as it is. You can specify @@ -44,7 +49,7 @@ applyTemplate template page = substitute (Chunk chunk) = chunk substitute (Identifier key) = fromMaybe ('$' : key) $ M.lookup key $ toMap page - substitute (Escaped key) = '$' : key + substitute (Escaped) = "$" -- | Apply a page as it's own template. This is often very useful to fill in -- certain keys like @$root@ and @$url@. diff --git a/src/Hakyll/Web/Template/Internal.hs b/src/Hakyll/Web/Template/Internal.hs index 096c928..d0f6472 100644 --- a/src/Hakyll/Web/Template/Internal.hs +++ b/src/Hakyll/Web/Template/Internal.hs @@ -29,17 +29,17 @@ instance Writable Template where data TemplateElement = Chunk String | Identifier String - | Escaped String + | Escaped deriving (Show, Eq, Typeable) instance Binary TemplateElement where put (Chunk string) = putWord8 0 >> put string put (Identifier key) = putWord8 1 >> put key - put (Escaped key) = putWord8 2 >> put key + put (Escaped) = putWord8 2 get = getWord8 >>= \tag -> case tag of 0 -> Chunk <$> get 1 -> Identifier <$> get - 2 -> Escaped <$> get + 2 -> return Escaped _ -> error $ "Hakyll.Web.Template.Internal: " ++ "Error reading cached template" -- cgit v1.2.3