summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/Template/Trim.hs
blob: 6b7c6c83525fad078aeb934b0c21b2ef2f922d0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
--------------------------------------------------------------------------------
-- | TODO
module Hakyll.Web.Template.Internal.Trim
    ( trim
    ) where


--------------------------------------------------------------------------------
import           Data.Char                    (isSpace)
import           Data.List                    (dropWhileEnd)


--------------------------------------------------------------------------------
import           Hakyll.Web.Template.Internal


--------------------------------------------------------------------------------
trim :: Template -> Template
trim = cleanse . canonicalize


--------------------------------------------------------------------------------
cleanse :: Template -> Template
cleanse = tmap (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

          lstrip = dropWhile isSpace
          rstrip = dropWhileEnd isSpace

--------------------------------------------------------------------------------
--
-- Invariant: Every TrimL should have a Chunk to its Left
--            Every TrimR should have a Chunk to its Right
--
--
-- Some initial implementation notes. Note: Not valid syntax etc.
--
--
--
--
--------------------------------------------------------------------------------
canonicalize :: Template -> Template
canonicalize = go
    where go t = let t' = redundant . swap . dedupe $ sink t
                 in if t == t' then t else go t'


--------------------------------------------------------------------------------
-- | 'redundant' removes the redundant 'TrimR's and 'TrimL's from the
-- 'Template's list of 'TemplateExpr's. It does _not_ recurse down the AST.
--
-- Note: Should _only_ be used on the top level 'Template'.
--
redundant :: Template -> Template
redundant = tmap (recurse redundant . process)
    where -- Remove the leading 'TrimL's.
          process (TrimL:ts) = process ts
          -- Remove trailing 'TrimR's.
          process ts = foldr trailing [] ts
              where trailing TrimR [] = []
                    trailing t ts = t:ts


--------------------------------------------------------------------------------
-- Commute:
--
--   List:
--
--      [t1, TrimR, TrimL, t2] = [t1, TrimL, TrimR, t2]
--
--   Rest should come for free provided Trim's are Sunk/Shifted etc I think.
--
swap :: Template -> Template
swap = tmap (recurse swap . process)
    where process [] = []
          process (TrimR:TrimL:ts) = TrimL:process (TrimR:ts)
          process (t:ts) = t:process ts


--------------------------------------------------------------------------------
--
-- Dedupe:
--
--   List:
--
--      [t1, TrimR, TrimR, t2] = [t1, TrimR, t2]
--
--      [t1, TrimL, TrimL, t2] = [t1, TrimL, t2]
--
--   If: Should come for free after Trim_'s have been sunk.
--
--      [t1, TrimR, If ex [TrimR, t] e, t2] = [t1, If ex [TrimR, t] e, t2]
--
--      [t1, If ex t [e, TrimL], TrimL, t2] = [t1, If ex t [e, TrimL], t2]
--
--      [t1, If ex [t, TrimL] Nothing, TrimL, t2] = [t1, If ex [t, TrimL] Nothing, t2]
--
--   For:
--
--      [t1, TrimR, For e [TrimR, b] sep, t2] = [t1, For e [TrimR, b] sep, t2]
--
--      [t1, For e b [sep, TrimL], TrimL, t2] = [t1, For e b [sep, TrimL], t2]
--
--      [t1, For e [b, TrimL] Nothing, TrimL, t2] = [t1, For e [b, TrimL] Nothing, t2]
--
dedupe :: Template -> Template
dedupe = tmap (recurse dedupe . process)
    where process [] = []
          process (TrimR:TrimR:ts) = process (TrimR:ts)
          process (TrimL:TrimL:ts) = process (TrimL:ts)
          process (t:ts) = t:process ts


--------------------------------------------------------------------------------
--
-- Sink:
--
--   If:
--
--      [t1, TrimR, If ex t e, t2] = [t1, If ex [TrimR, t] e, t2]
--
--      [t1, If ex t e, TrimL, t2] = if isJust e
--                                     then [t1, If ex t [e, TrimL], t2]
--                                     else [t1, If ex [t, TrimL] e, t2]
--
--   For:
--
--      [t1, TrimR, For e b sep, t2] = [t1, For e [TrimR, b] sep, t2]
--
--      [t1, For e b sep, TrimL, t2] = if isJust sep
--                                       then [t1, For e b [sep, TrimL], t2]
--                                       else [t1, For e [b, TrimL] sep, t2]
--
--
sink :: Template -> Template
sink = tmap (recurse sink . process)
    where process [] = []
          -- Right sink TrimR into If thenbody.
          process (TrimR:If e (Template tb) eb:ts)
              = If e (Template (TrimR:tb)) eb:process ts
          -- Left sink TrimL into If thenbody.
          process (If e (Template tb) Nothing:TrimL:ts)
              = If e (Template (tb ++ [TrimL])) Nothing:process ts
          -- Left sink TrimL into If elsebody.
          process (If e tb (Just (Template eb)):TrimL:ts)
              = If e tb (Just (Template (eb ++ [TrimL]))):process ts
          -- Right sink TrimR into For body.
          process (TrimR:For e (Template b) sep:ts)
              = For e (Template (TrimR:b)) sep:process ts
          -- Left sink TrimL into For body.
          process (For e (Template b) Nothing:TrimL:ts)
              = For e (Template (b ++ [TrimL])) Nothing:process ts
          -- Left sink TrimL into For sep.
          process (For e b (Just (Template sep)):TrimL:ts)
              = For e b (Just (Template (sep ++ [TrimL]))):process ts
          -- Otherwise move on.
          process (t:ts) = t:process ts


--------------------------------------------------------------------------------
tmap :: ([TemplateElement] -> [TemplateElement]) -> Template -> Template
tmap f (Template t) = Template (f t)


--------------------------------------------------------------------------------
recurse :: (Template -> Template) -> [TemplateElement] -> [TemplateElement]
recurse f []     = []
recurse f (x:xs) = process x:recurse f xs
    where process x = case x of
                          If e tb eb -> If e (f tb) (f <$> eb)
                          For e t s  -> For e (f t) (f <$> s)
                          _          -> x


--------------------------------------------------------------------------------