on master help sign in
Trim instructions. TrimRd chunk might need TrimL. Trim tests.
commit82d6402ba38b9e1ea789e83c5ea7d08bcbeff467 authorsamgd <sam@samgd.com> dateMon 25 Jul 2016, 12:47 (10 years ago) parent43c969f
5 files changed+104−12
src/Hakyll/Web/Template.hs+30 src/Hakyll/Web/Template/Trim.hs+14−6 tests/Hakyll/Web/Template/Tests.hs+8−6 tests/data/strip.html+34 tests/data/strip.html.out+18
src/Hakyll/Web/Template.hs+30
@@ -115,6 +115,29 @@
115115-- That is, calling @$partial$@ is equivalent to just copying and pasting
116116-- template code.
117117--
118+-- In the examples above you can see that outputs contain a lot of leftover
119+-- whitespace that you may wish to remove. Using @'$-'@ or @'-$'@ instead of
120+-- @'$'@ in a macro strips all whitespace to the left or right of that clause
121+-- respectively. Given the context
122+--
123+-- > listField "counts" (field "count" (return . itemBody))
124+-- > (sequence [makeItem "3", makeItem "2", makeItem "1"])
125+--
126+-- and a template
127+--
128+-- > <p>
129+-- > $for(counts)-$
130+-- > $count$
131+-- > $-sep-$...
132+-- > $-endfor$
133+-- > </p>
134+--
135+-- the resulting page would look like
136+--
137+-- > <p>
138+-- > 3...2...1
139+-- > </p>
140+--
118141{-# LANGUAGE GeneralizedNewtypeDeriving #-}
119142{-# LANGUAGE ScopedTypeVariables #-}
120143module Hakyll.Web.Template
@@ -209,10 +232,17 @@ applyTemplate' tes context x = go tes
209232
210233 go = fmap concat . mapM applyElem
211234
235+ trimError = error $ "Hakyll.Web.Template.applyTemplate: template not " ++
236+ "fully trimmed."
237+
212238 ---------------------------------------------------------------------------
213239
214240 applyElem :: TemplateElement -> Compiler String
215241
242+ applyElem TrimL = trimError
243+
244+ applyElem TrimR = trimError
245+
216246 applyElem (Chunk c) = return c
217247
218248 applyElem (Expr e) = applyExpr e >>= getString e
src/Hakyll/Web/Template/Trim.hs+14−6
@@ -1,5 +1,5 @@
11--------------------------------------------------------------------------------
2−-- | Module for trimming whitespace.
2+-- | Module for trimming whitespace
33module Hakyll.Web.Template.Trim
44 ( trim
55 ) where
@@ -20,15 +20,22 @@ trim = cleanse . canonicalize
2020
2121
2222--------------------------------------------------------------------------------
23+-- | Apply the Trim nodes to the Chunks.
2324cleanse :: [TemplateElement] -> [TemplateElement]
2425cleanse = recurse cleanse . process
2526 where process [] = []
26− process (TrimR:Chunk str:ts) = Chunk (lstrip str):process ts
27− process (Chunk str:TrimL:ts) = Chunk (rstrip str):process ts
28− process (t:ts) = t:process ts
27+ process (TrimR:Chunk str:ts) = let str' = dropWhile isSpace str
28+ in if null str'
29+ then process ts
30+ -- Might need to TrimL.
31+ else process $ Chunk str':ts
32+
33+ process (Chunk str:TrimL:ts) = let str' = dropWhileEnd isSpace str
34+ in if null str'
35+ then process ts
36+ else Chunk str':process ts
2937
30− lstrip = dropWhile isSpace
31− rstrip = dropWhileEnd isSpace
38+ process (t:ts) = t:process ts
3239
3340--------------------------------------------------------------------------------
3441-- | Enforce the invariant that:
@@ -75,6 +82,7 @@ dedupe = recurse dedupe . process
7582
7683
7784--------------------------------------------------------------------------------
85+-- | @'recurse' f t@ applies f to every '[TemplateElement]' in t.
7886recurse :: ([TemplateElement] -> [TemplateElement])
7987 -> [TemplateElement]
8088 -> [TemplateElement]
tests/Hakyll/Web/Template/Tests.hs+8−6
@@ -13,6 +13,7 @@ import Test.HUnit (Assertion, (@=?), (@?=))
1313
1414--------------------------------------------------------------------------------
1515import Hakyll.Core.Compiler
16+import Hakyll.Core.Identifier
1617import Hakyll.Core.Item
1718import Hakyll.Core.Provider
1819import Hakyll.Web.Pandoc
@@ -26,7 +27,8 @@ import TestSuite.Util
2627--------------------------------------------------------------------------------
2728tests :: Test
2829tests = testGroup "Hakyll.Core.Template.Tests" $ concat
29− [ [ testCase "case01" case01
30+ [ [ testCase "case01" $ test ("template.html.out", "template.html", "example.md")
31+ , testCase "case02" $ test ("strip.html.out", "strip.html", "example.md")
3032 , testCase "applyJoinTemplateList" testApplyJoinTemplateList
3133 ]
3234
@@ -78,14 +80,14 @@ tests = testGroup "Hakyll.Core.Template.Tests" $ concat
7880
7981
8082--------------------------------------------------------------------------------
81−case01 :: Assertion
82−case01 = do
83+test :: (Identifier, Identifier, Identifier) -> Assertion
84+test (outf, tplf, itemf) = do
8385 store <- newTestStore
8486 provider <- newTestProvider store
8587
86− out <- resourceString provider "template.html.out"
87− tpl <- testCompilerDone store provider "template.html" templateBodyCompiler
88− item <- testCompilerDone store provider "example.md" $
88+ out <- resourceString provider outf
89+ tpl <- testCompilerDone store provider tplf templateBodyCompiler
90+ item <- testCompilerDone store provider itemf $
8991 pandocCompiler >>= applyTemplate (itemBody tpl) testContext
9092
9193 out @=? itemBody item
tests/data/strip.htmlnew file+34
@@ -0,0 +1,34 @@
1+<div>
2+ I'm so rich I have $$3.
3+
4+ $rev("foo")$
5+ $-rev(rev("foo"))$
6+
7+ $if(body)-$
8+ I have body
9+ $else-$
10+ or no
11+ $-endif-$
12+
13+ $if(unbound)$
14+ should not be printed
15+ $endif$
16+
17+ $-if(body)-$
18+ should be printed
19+ $-endif$
20+
21+ <ul>
22+ $for(authors)-$
23+ <li>$name$</li>
24+ $endfor-$
25+ </ul>
26+
27+ $for(authors)-$
28+ $name-$
29+ $sep$,
30+ $-endfor$
31+
32+ $body$
33+</div>
34+
tests/data/strip.html.outnew file+18
@@ -0,0 +1,18 @@
1+<div>
2+ I'm so rich I have $3.
3+
4+ ooffoo
5+
6+ I have body
7+ should be printed
8+
9+ <ul>
10+ <li>Jan</li>
11+ <li>Piet</li>
12+ </ul>
13+
14+ Jan,Piet
15+
16+ <p>This is an example.</p>
17+</div>
18+