From 82d6402ba38b9e1ea789e83c5ea7d08bcbeff467 Mon Sep 17 00:00:00 2001 From: samgd Date: Mon, 25 Jul 2016 12:47:30 +0200 Subject: Trim instructions. TrimRd chunk might need TrimL. Trim tests. --- src/Hakyll/Web/Template.hs | 30 ++++++++++++++++++++++++++++++ src/Hakyll/Web/Template/Trim.hs | 20 ++++++++++++++------ 2 files changed, 44 insertions(+), 6 deletions(-) (limited to 'src/Hakyll') 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 +-- +-- >

+-- > $for(counts)-$ +-- > $count$ +-- > $-sep-$... +-- > $-endfor$ +-- >

+-- +-- the resulting page would look like +-- +-- >

+-- > 3...2...1 +-- >

+-- {-# 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] -- cgit v1.2.3