| 1 |
-------------------------------------------------------------------------------- |
| 2 |
-- | Module for trimming whitespace from tempaltes. |
| 3 |
module Hakyll.Web.Template.Internal.Trim |
| 4 |
( trim |
| 5 |
) where |
| 6 |
|
| 7 |
|
| 8 |
-------------------------------------------------------------------------------- |
| 9 |
import Data.Char (isSpace) |
| 10 |
import Data.List (dropWhileEnd) |
| 11 |
|
| 12 |
|
| 13 |
-------------------------------------------------------------------------------- |
| 14 |
import Hakyll.Web.Template.Internal.Element |
| 15 |
|
| 16 |
|
| 17 |
-------------------------------------------------------------------------------- |
| 18 |
trim :: [TemplateElement] -> [TemplateElement] |
| 19 |
trim = cleanse . canonicalize |
| 20 |
|
| 21 |
|
| 22 |
-------------------------------------------------------------------------------- |
| 23 |
-- | Apply the Trim nodes to the Chunks. |
| 24 |
cleanse :: [TemplateElement] -> [TemplateElement] |
| 25 |
cleanse = recurse cleanse . process |
| 26 |
where process [] = [] |
| 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 |
| 37 |
|
| 38 |
process (t:ts) = t:process ts |
| 39 |
|
| 40 |
-------------------------------------------------------------------------------- |
| 41 |
-- | Enforce the invariant that: |
| 42 |
-- |
| 43 |
-- * Every 'TrimL' has a 'Chunk' to its left. |
| 44 |
-- * Every 'TrimR' has a 'Chunk' to its right. |
| 45 |
-- |
| 46 |
canonicalize :: [TemplateElement] -> [TemplateElement] |
| 47 |
canonicalize = go |
| 48 |
where go t = let t' = redundant . swap $ dedupe t |
| 49 |
in if t == t' then t else go t' |
| 50 |
|
| 51 |
|
| 52 |
-------------------------------------------------------------------------------- |
| 53 |
-- | Remove the 'TrimR' and 'TrimL's that are no-ops. |
| 54 |
redundant :: [TemplateElement] -> [TemplateElement] |
| 55 |
redundant = recurse redundant . process |
| 56 |
where -- Remove the leading 'TrimL's. |
| 57 |
process (TrimL:ts) = process ts |
| 58 |
-- Remove trailing 'TrimR's. |
| 59 |
process ts = foldr trailing [] ts |
| 60 |
where trailing TrimR [] = [] |
| 61 |
trailing x xs = x:xs |
| 62 |
|
| 63 |
|
| 64 |
-------------------------------------------------------------------------------- |
| 65 |
-- >>> swap $ [TrimR, TrimL] |
| 66 |
-- [TrimL, TrimR] |
| 67 |
swap :: [TemplateElement] -> [TemplateElement] |
| 68 |
swap = recurse swap . process |
| 69 |
where process [] = [] |
| 70 |
process (TrimR:TrimL:ts) = TrimL:process (TrimR:ts) |
| 71 |
process (t:ts) = t:process ts |
| 72 |
|
| 73 |
|
| 74 |
-------------------------------------------------------------------------------- |
| 75 |
-- | Remove 'TrimR' and 'TrimL' duplication. |
| 76 |
dedupe :: [TemplateElement] -> [TemplateElement] |
| 77 |
dedupe = recurse dedupe . process |
| 78 |
where process [] = [] |
| 79 |
process (TrimR:TrimR:ts) = process (TrimR:ts) |
| 80 |
process (TrimL:TrimL:ts) = process (TrimL:ts) |
| 81 |
process (t:ts) = t:process ts |
| 82 |
|
| 83 |
|
| 84 |
-------------------------------------------------------------------------------- |
| 85 |
-- | @'recurse' f t@ applies f to every '[TemplateElement]' in t. |
| 86 |
recurse :: ([TemplateElement] -> [TemplateElement]) |
| 87 |
-> [TemplateElement] |
| 88 |
-> [TemplateElement] |
| 89 |
recurse _ [] = [] |
| 90 |
recurse f (x:xs) = process x:recurse f xs |
| 91 |
where process y = case y of |
| 92 |
If e tb eb -> If e (f tb) (f <$> eb) |
| 93 |
For e t s -> For e (f t) (f <$> s) |
| 94 |
_ -> y |
| 95 |
|