summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Hakyll/Web/Template.hs30
-rw-r--r--src/Hakyll/Web/Template/Trim.hs20
2 files changed, 44 insertions, 6 deletions
diff --git a/src/Hakyll/Web/Template.hs b/src/Hakyll/Web/Template.hs
index 204878c..13d5d35 100644
--- a/src/Hakyll/Web/Template.hs
+++ b/src/Hakyll/Web/Template.hs
@@ -115,6 +115,29 @@
-- That is, calling @$partial$@ is equivalent to just copying and pasting
-- template code.
--
+-- In the examples above you can see that outputs contain a lot of leftover
+-- whitespace that you may wish to remove. Using @'$-'@ or @'-$'@ instead of
+-- @'$'@ in a macro strips all whitespace to the left or right of that clause
+-- respectively. Given the context
+--
+-- > listField "counts" (field "count" (return . itemBody))
+-- > (sequence [makeItem "3", makeItem "2", makeItem "1"])
+--
+-- and a template
+--
+-- > <p>
+-- > $for(counts)-$
+-- > $count$
+-- > $-sep-$...
+-- > $-endfor$
+-- > </p>
+--
+-- the resulting page would look like
+--
+-- > <p>
+-- > 3...2...1
+-- > </p>
+--
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Hakyll.Web.Template
@@ -209,10 +232,17 @@ applyTemplate' tes context x = go tes
go = fmap concat . mapM applyElem
+ trimError = error $ "Hakyll.Web.Template.applyTemplate: template not " ++
+ "fully trimmed."
+
---------------------------------------------------------------------------
applyElem :: TemplateElement -> Compiler String
+ applyElem TrimL = trimError
+
+ applyElem TrimR = trimError
+
applyElem (Chunk c) = return c
applyElem (Expr e) = applyExpr e >>= getString e
diff --git a/src/Hakyll/Web/Template/Trim.hs b/src/Hakyll/Web/Template/Trim.hs
index 4ea3438..bc7e691 100644
--- a/src/Hakyll/Web/Template/Trim.hs
+++ b/src/Hakyll/Web/Template/Trim.hs
@@ -1,5 +1,5 @@
--------------------------------------------------------------------------------
--- | Module for trimming whitespace.
+-- | Module for trimming whitespace
module Hakyll.Web.Template.Trim
( trim
) where
@@ -20,15 +20,22 @@ trim = cleanse . canonicalize
--------------------------------------------------------------------------------
+-- | Apply the Trim nodes to the Chunks.
cleanse :: [TemplateElement] -> [TemplateElement]
cleanse = recurse cleanse . process
where process [] = []
- process (TrimR:Chunk str:ts) = Chunk (lstrip str):process ts
- process (Chunk str:TrimL:ts) = Chunk (rstrip str):process ts
- process (t:ts) = t:process ts
+ process (TrimR:Chunk str:ts) = let str' = dropWhile isSpace str
+ in if null str'
+ then process ts
+ -- Might need to TrimL.
+ else process $ Chunk str':ts
+
+ process (Chunk str:TrimL:ts) = let str' = dropWhileEnd isSpace str
+ in if null str'
+ then process ts
+ else Chunk str':process ts
- lstrip = dropWhile isSpace
- rstrip = dropWhileEnd isSpace
+ process (t:ts) = t:process ts
--------------------------------------------------------------------------------
-- | Enforce the invariant that:
@@ -75,6 +82,7 @@ dedupe = recurse dedupe . process
--------------------------------------------------------------------------------
+-- | @'recurse' f t@ applies f to every '[TemplateElement]' in t.
recurse :: ([TemplateElement] -> [TemplateElement])
-> [TemplateElement]
-> [TemplateElement]