From d32307aa1cbf6718845eeeb2eb6ba3b1c7661bde Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Tue, 7 May 2013 09:28:09 +0200 Subject: Add partials to template system --- src/Hakyll/Web/Template/Internal.hs | 5 ++++- src/Hakyll/Web/Template/Read.hs | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) (limited to 'src/Hakyll/Web/Template') diff --git a/src/Hakyll/Web/Template/Internal.hs b/src/Hakyll/Web/Template/Internal.hs index 1c81670..138010e 100644 --- a/src/Hakyll/Web/Template/Internal.hs +++ b/src/Hakyll/Web/Template/Internal.hs @@ -39,6 +39,7 @@ data TemplateElement | Escaped | If String Template (Maybe Template) -- key, then branch, else branch | For String Template (Maybe Template) -- key, body, separator + | Partial String -- filename deriving (Show, Eq, Typeable) @@ -49,12 +50,14 @@ instance Binary TemplateElement where put (Escaped) = putWord8 2 put (If key t f) = putWord8 3 >> put key >> put t >> put f put (For key b s) = putWord8 4 >> put key >> put b >> put s + put (Partial p) = putWord8 5 >> put p get = getWord8 >>= \tag -> case tag of 0 -> Chunk <$> get 1 -> Key <$> get 2 -> pure Escaped - 3 -> If <$> get <*> get <*> get + 3 -> If <$> get <*> get <*> get 4 -> For <$> get <*> get <*> get + 5 -> Partial <$> get _ -> error $ "Hakyll.Web.Template.Internal: Error reading cached template" diff --git a/src/Hakyll/Web/Template/Read.hs b/src/Hakyll/Web/Template/Read.hs index 2dd0fc5..bb5c8c2 100644 --- a/src/Hakyll/Web/Template/Read.hs +++ b/src/Hakyll/Web/Template/Read.hs @@ -27,7 +27,7 @@ readTemplate input = case parse template "" input of -------------------------------------------------------------------------------- template :: Parser Template template = Template <$> - (many1 $ chunk <|> escaped <|> conditional <|> for <|> key) + (many1 $ chunk <|> escaped <|> conditional <|> for <|> partial <|> key) -------------------------------------------------------------------------------- @@ -64,6 +64,15 @@ for = try $ do return $ For i body sep +-------------------------------------------------------------------------------- +partial :: Parser TemplateElement +partial = try $ do + void $ string "$partial(" + i <- stringLiteral + void $ string ")$" + return $ Partial i + + -------------------------------------------------------------------------------- key :: Parser TemplateElement key = try $ do @@ -71,3 +80,14 @@ key = try $ do k <- metadataKey void $ char '$' return $ Key k + + +-------------------------------------------------------------------------------- +stringLiteral :: Parser String +stringLiteral = do + void $ char '\"' + str <- many $ do + x <- noneOf "\"" + if x == '\\' then anyChar else return x + void $ char '\"' + return str -- cgit v1.2.3