on master help sign in
Add Trim_ data constructors. Add If test for Trim
02dd1cfsamgd
src/Hakyll/Web/Template/Internal.hs+10−6
@@ -64,6 +64,8 @@ data TemplateElement
64 | If TemplateExpr Template (Maybe Template) -- expr, then, else
64 | If TemplateExpr Template (Maybe Template) -- expr, then, else
65 | For TemplateExpr Template (Maybe Template) -- expr, body, separator
65 | For TemplateExpr Template (Maybe Template) -- expr, body, separator
66 | Partial TemplateExpr -- filename
66 | Partial TemplateExpr -- filename
67+ | TrimL
68+ | TrimR
67 deriving (Show, Eq, Typeable)
69 deriving (Show, Eq, Typeable)
68
70
69
71
@@ -71,10 +73,12 @@ data TemplateElement
71instance Binary TemplateElement where
73instance Binary TemplateElement where
72 put (Chunk string) = putWord8 0 >> put string
74 put (Chunk string) = putWord8 0 >> put string
73 put (Expr e) = putWord8 1 >> put e
75 put (Expr e) = putWord8 1 >> put e
74− put (Escaped) = putWord8 2
76+ put Escaped = putWord8 2
75− put (If e t f ) = putWord8 3 >> put e >> put t >> put f
77+ put (If e t f) = putWord8 3 >> put e >> put t >> put f
76 put (For e b s) = putWord8 4 >> put e >> put b >> put s
78 put (For e b s) = putWord8 4 >> put e >> put b >> put s
77 put (Partial e) = putWord8 5 >> put e
79 put (Partial e) = putWord8 5 >> put e
80+ put TrimL = putWord8 6
81+ put TrimR = putWord8 7
78
82
79 get = getWord8 >>= \tag -> case tag of
83 get = getWord8 >>= \tag -> case tag of
80 0 -> Chunk <$> get
84 0 -> Chunk <$> get
@@ -83,8 +87,9 @@ instance Binary TemplateElement where
83 3 -> If <$> get <*> get <*> get
87 3 -> If <$> get <*> get <*> get
84 4 -> For <$> get <*> get <*> get
88 4 -> For <$> get <*> get <*> get
85 5 -> Partial <$> get
89 5 -> Partial <$> get
86− _ -> error $
90+ 6 -> pure TrimL
91+ 7 -> pure TrimR
87− "Hakyll.Web.Template.Internal: Error reading cached template"
92+ _ -> error "Hakyll.Web.Template.Internal: Error reading cached template"
88
93
89
94
90--------------------------------------------------------------------------------
95--------------------------------------------------------------------------------
@@ -114,8 +119,7 @@ instance Binary TemplateExpr where
114 0 -> Ident <$> get
119 0 -> Ident <$> get
115 1 -> Call <$> get <*> get
120 1 -> Call <$> get <*> get
116 2 -> StringLiteral <$> get
121 2 -> StringLiteral <$> get
117− _ -> error $
118− "Hakyll.Web.Tamplte.Internal: Error reading cached template"
122+ _ -> error "Hakyll.Web.Template.Internal: Error reading cached template"
119
123
120
124
121--------------------------------------------------------------------------------
125--------------------------------------------------------------------------------
tests/Hakyll/Web/Template/Tests.hs+15
@@ -39,6 +39,21 @@ tests = testGroup "Hakyll.Core.Template.Tests" $ concat
39 (Template [Chunk "foo"])
39 (Template [Chunk "foo"])
40 Nothing]
40 Nothing]
41 @=? readTemplate "$if(a(\"bar\"))$foo$endif$"
41 @=? readTemplate "$if(a(\"bar\"))$foo$endif$"
42+ -- 'If' 'Trim_' test.
43+ , Template
44+ [ TrimL
45+ , If (Ident (TemplateKey "body"))
46+ (Template [ TrimR
47+ , Expr (Ident (TemplateKey "body"))
48+ ])
49+ (Just (Template [ TrimL
50+ , TrimR
51+ , Expr (Ident (TemplateKey "body"))
52+ ]))
53+ , TrimL
54+ , TrimR
55+ ]
56+ @=? readTemplate "$-if(body)-$\n$body$\n$-else-$\n$body$\n$-endif-$"
42 ]
57 ]
43 ]
58 ]
44
59