on master help sign in
For trimming
commit6e14d33a101e4ea9559d13d7a562da7ebc72acf2 authorsamgd <sam@samgd.com> dateSat 23 Jul 2016, 12:41 (10 years ago) parent6c0be2e
2 files changed+31−9
src/Hakyll/Web/Template/Internal.hs+18−6 tests/Hakyll/Web/Template/Tests.hs+13−3
src/Hakyll/Web/Template/Internal.hs+18−6
@@ -141,7 +141,7 @@ template :: P.Parser Template
141141template = mconcat <$> P.many (P.choice [ lift chunk
142142 , lift escaped
143143 , conditional
144− , lift for
144+ , for
145145 , lift partial
146146 , lift expr
147147 ])
@@ -222,15 +222,27 @@ conditional = P.try $ do
222222
223223
224224--------------------------------------------------------------------------------
225−for :: P.Parser TemplateElement
225+for :: P.Parser Template
226226for = P.try $ do
227− void $ P.string "$for("
227+ trimLFor <- trimOpen
228+ void $ P.string "for("
228229 e <- expr'
229− void $ P.string ")$"
230+ void $ P.char ')'
231+ trimRFor <- trimClose
232+
230233 body <- template
231234 sep <- P.optionMaybe $ P.try (P.string "$sep$") >> template
232− void $ P.string "$endfor$"
233− return $ For e body sep
235+
236+ trimLEnd <- trimOpen
237+ void $ P.string "endfor"
238+ trimREnd <- trimClose
239+
240+ pure $ Template $ mconcat [ [TrimL | trimLFor]
241+ , [TrimR | trimRFor]
242+ , [For e body sep]
243+ , [TrimL | trimLEnd]
244+ , [TrimR | trimREnd]
245+ ]
234246
235247
236248--------------------------------------------------------------------------------
tests/Hakyll/Web/Template/Tests.hs+13−3
@@ -6,7 +6,6 @@ module Hakyll.Web.Template.Tests
66
77
88--------------------------------------------------------------------------------
9−import Data.Monoid (mconcat)
109import Test.Framework (Test, testGroup)
1110import Test.Framework.Providers.HUnit (testCase)
1211import Test.HUnit (Assertion, (@=?), (@?=))
@@ -33,12 +32,12 @@ tests = testGroup "Hakyll.Core.Template.Tests" $ concat
3332
3433 , fromAssertions "readTemplate"
3534 [ Template [Chunk "Hello ", Expr (Call "guest" [])]
36− @=? readTemplate "Hello $guest()$"
35+ @=? readTemplate "Hello $guest()$"
3736 , Template
3837 [If (Call "a" [StringLiteral "bar"])
3938 (Template [Chunk "foo"])
4039 Nothing]
41− @=? readTemplate "$if(a(\"bar\"))$foo$endif$"
40+ @=? readTemplate "$if(a(\"bar\"))$foo$endif$"
4241 -- 'If' trim check.
4342 , Template
4443 [ TrimL
@@ -58,6 +57,17 @@ tests = testGroup "Hakyll.Core.Template.Tests" $ concat
5857 , TrimR
5958 ]
6059 @=? readTemplate "$-if(body)-$\n$body$\n$-else-$\n$body$\n$-endif-$"
60+ -- 'For' trim check.
61+ , Template
62+ [ TrimL
63+ , TrimR
64+ , For (Ident (TemplateKey "authors"))
65+ (Template [Chunk "\n body \n"])
66+ Nothing
67+ , TrimL
68+ , TrimR
69+ ]
70+ @=? readTemplate "$-for(authors)-$\n body \n$-endfor-$"
6171 ]
6272 ]
6373