From 02dd1cf2360cb2a650cb61c5e6ae2ca573eb5125 Mon Sep 17 00:00:00 2001 From: samgd Date: Fri, 22 Jul 2016 16:39:22 +0200 Subject: [PATCH] Add Trim_ data constructors. Add If test for Trim --- src/Hakyll/Web/Template/Internal.hs | 16 ++++++++++------ tests/Hakyll/Web/Template/Tests.hs | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Hakyll/Web/Template/Internal.hs b/src/Hakyll/Web/Template/Internal.hs index 45db2e4..2f702f9 100644 --- a/src/Hakyll/Web/Template/Internal.hs +++ b/src/Hakyll/Web/Template/Internal.hs @@ -64,6 +64,8 @@ data TemplateElement | If TemplateExpr Template (Maybe Template) -- expr, then, else | For TemplateExpr Template (Maybe Template) -- expr, body, separator | Partial TemplateExpr -- filename + | TrimL + | TrimR deriving (Show, Eq, Typeable) @@ -71,10 +73,12 @@ data TemplateElement instance Binary TemplateElement where put (Chunk string) = putWord8 0 >> put string put (Expr e) = putWord8 1 >> put e - put (Escaped) = putWord8 2 - put (If e t f ) = putWord8 3 >> put e >> put t >> put f + put Escaped = putWord8 2 + put (If e t f) = putWord8 3 >> put e >> put t >> put f put (For e b s) = putWord8 4 >> put e >> put b >> put s put (Partial e) = putWord8 5 >> put e + put TrimL = putWord8 6 + put TrimR = putWord8 7 get = getWord8 >>= \tag -> case tag of 0 -> Chunk <$> get @@ -83,8 +87,9 @@ instance Binary TemplateElement where 3 -> If <$> get <*> get <*> get 4 -> For <$> get <*> get <*> get 5 -> Partial <$> get - _ -> error $ - "Hakyll.Web.Template.Internal: Error reading cached template" + 6 -> pure TrimL + 7 -> pure TrimR + _ -> error "Hakyll.Web.Template.Internal: Error reading cached template" -------------------------------------------------------------------------------- @@ -114,8 +119,7 @@ instance Binary TemplateExpr where 0 -> Ident <$> get 1 -> Call <$> get <*> get 2 -> StringLiteral <$> get - _ -> error $ - "Hakyll.Web.Tamplte.Internal: Error reading cached template" + _ -> error "Hakyll.Web.Template.Internal: Error reading cached template" -------------------------------------------------------------------------------- diff --git a/tests/Hakyll/Web/Template/Tests.hs b/tests/Hakyll/Web/Template/Tests.hs index 7d5d6ec..453cd49 100644 --- a/tests/Hakyll/Web/Template/Tests.hs +++ b/tests/Hakyll/Web/Template/Tests.hs @@ -39,6 +39,21 @@ tests = testGroup "Hakyll.Core.Template.Tests" $ concat (Template [Chunk "foo"]) Nothing] @=? readTemplate "$if(a(\"bar\"))$foo$endif$" + -- 'If' 'Trim_' test. + , Template + [ TrimL + , If (Ident (TemplateKey "body")) + (Template [ TrimR + , Expr (Ident (TemplateKey "body")) + ]) + (Just (Template [ TrimL + , TrimR + , Expr (Ident (TemplateKey "body")) + ])) + , TrimL + , TrimR + ] + @=? readTemplate "$-if(body)-$\n$body$\n$-else-$\n$body$\n$-endif-$" ] ]