on master help sign in
Trim instructions. TrimRd chunk might need TrimL. Trim tests.
82d6402samgd
src/Hakyll/Web/Template.hs+30
@@ -115,6 +115,29 @@
115-- That is, calling @$partial$@ is equivalent to just copying and pasting
115-- That is, calling @$partial$@ is equivalent to just copying and pasting
116-- template code.
116-- template code.
117--
117--
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+--
118{-# LANGUAGE GeneralizedNewtypeDeriving #-}
141{-# LANGUAGE GeneralizedNewtypeDeriving #-}
119{-# LANGUAGE ScopedTypeVariables #-}
142{-# LANGUAGE ScopedTypeVariables #-}
120module Hakyll.Web.Template
143module Hakyll.Web.Template
@@ -209,10 +232,17 @@ applyTemplate' tes context x = go tes
209
232
210 go = fmap concat . mapM applyElem
233 go = fmap concat . mapM applyElem
211
234
235+ trimError = error $ "Hakyll.Web.Template.applyTemplate: template not " ++
236+ "fully trimmed."
237+
212 ---------------------------------------------------------------------------
238 ---------------------------------------------------------------------------
213
239
214 applyElem :: TemplateElement -> Compiler String
240 applyElem :: TemplateElement -> Compiler String
215
241
242+ applyElem TrimL = trimError
243+
244+ applyElem TrimR = trimError
245+
216 applyElem (Chunk c) = return c
246 applyElem (Chunk c) = return c
217
247
218 applyElem (Expr e) = applyExpr e >>= getString e
248 applyElem (Expr e) = applyExpr e >>= getString e
src/Hakyll/Web/Template/Trim.hs+14−6
@@ -1,5 +1,5 @@
1--------------------------------------------------------------------------------
1--------------------------------------------------------------------------------
2−-- | Module for trimming whitespace.
2+-- | Module for trimming whitespace
3module Hakyll.Web.Template.Trim
3module Hakyll.Web.Template.Trim
4 ( trim
4 ( trim
5 ) where
5 ) where
@@ -20,15 +20,22 @@ trim = cleanse . canonicalize
20
20
21
21
22--------------------------------------------------------------------------------
22--------------------------------------------------------------------------------
23+-- | Apply the Trim nodes to the Chunks.
23cleanse :: [TemplateElement] -> [TemplateElement]
24cleanse :: [TemplateElement] -> [TemplateElement]
24cleanse = recurse cleanse . process
25cleanse = recurse cleanse . process
25 where process [] = []
26 where process [] = []
26− process (TrimR:Chunk str:ts) = Chunk (lstrip str):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+
27− process (Chunk str:TrimL:ts) = Chunk (rstrip str):process ts
33+ process (Chunk str:TrimL:ts) = let str' = dropWhileEnd isSpace str
34+ in if null str'
28− process (t:ts) = t:process ts
35+ then process ts
36+ else Chunk str':process ts
29
37
30− lstrip = dropWhile isSpace
38+ process (t:ts) = t:process ts
31− rstrip = dropWhileEnd isSpace
32
39
33--------------------------------------------------------------------------------
40--------------------------------------------------------------------------------
34-- | Enforce the invariant that:
41-- | Enforce the invariant that:
@@ -75,6 +82,7 @@ dedupe = recurse dedupe . process
75
82
76
83
77--------------------------------------------------------------------------------
84--------------------------------------------------------------------------------
85+-- | @'recurse' f t@ applies f to every '[TemplateElement]' in t.
78recurse :: ([TemplateElement] -> [TemplateElement])
86recurse :: ([TemplateElement] -> [TemplateElement])
79 -> [TemplateElement]
87 -> [TemplateElement]
80 -> [TemplateElement]
88 -> [TemplateElement]
tests/Hakyll/Web/Template/Tests.hs+8−6
@@ -13,6 +13,7 @@ import Test.HUnit (Assertion, (@=?), (@?=))
13
13
14--------------------------------------------------------------------------------
14--------------------------------------------------------------------------------
15import Hakyll.Core.Compiler
15import Hakyll.Core.Compiler
16+import Hakyll.Core.Identifier
16import Hakyll.Core.Item
17import Hakyll.Core.Item
17import Hakyll.Core.Provider
18import Hakyll.Core.Provider
18import Hakyll.Web.Pandoc
19import Hakyll.Web.Pandoc
@@ -26,7 +27,8 @@ import TestSuite.Util
26--------------------------------------------------------------------------------
27--------------------------------------------------------------------------------
27tests :: Test
28tests :: Test
28tests = testGroup "Hakyll.Core.Template.Tests" $ concat
29tests = 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")
30 , testCase "applyJoinTemplateList" testApplyJoinTemplateList
32 , testCase "applyJoinTemplateList" testApplyJoinTemplateList
31 ]
33 ]
32
34
@@ -78,14 +80,14 @@ tests = testGroup "Hakyll.Core.Template.Tests" $ concat
78
80
79
81
80--------------------------------------------------------------------------------
82--------------------------------------------------------------------------------
81−case01 :: Assertion
83+test :: (Identifier, Identifier, Identifier) -> Assertion
82−case01 = do
84+test (outf, tplf, itemf) = do
83 store <- newTestStore
85 store <- newTestStore
84 provider <- newTestProvider store
86 provider <- newTestProvider store
85
87
86− out <- resourceString provider "template.html.out"
88+ out <- resourceString provider outf
87− tpl <- testCompilerDone store provider "template.html" templateBodyCompiler
89+ tpl <- testCompilerDone store provider tplf templateBodyCompiler
88− item <- testCompilerDone store provider "example.md" $
90+ item <- testCompilerDone store provider itemf $
89 pandocCompiler >>= applyTemplate (itemBody tpl) testContext
91 pandocCompiler >>= applyTemplate (itemBody tpl) testContext
90
92
91 out @=? itemBody item
93 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+