-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathhiccup.clj
232 lines (191 loc) · 8.73 KB
/
hiccup.clj
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
(ns hiccup
(:require [meander.epsilon :as m]
[meander.strategy.epsilon :as m*]))
;; Interpreter (function) version
;; ---------------------------------------------------------------------
(defn str* [xs]
(apply str xs))
(defn html [hiccup]
(m/rewrite hiccup
;; Borrow :<> from Reagent.
[:<> . (m/cata !content) ...]
(m/app str* (!content ...))
;; Void tags.
(m/with [%attributes {(m/keyword !attr) !val & (m/or %attributes _)}]
[(m/keyword (m/re #"area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr" ?tag))
& (m/or [%attributes . _ ...]
[_ ...])])
(m/app str* ("<" ?tag . " " !attr "=\"" !val "\"" ... "/>"))
;; Normal tags.
(m/with [%content (m/cata !content)
%attributes {(m/keyword !attr) !val & (m/or %attributes _)}]
[(m/keyword ?tag)
& (m/or [%attributes . %content ...]
[%content ...])])
(m/app str* ("<" ?tag . " " !attr "=\"" !val "\"" ... ">" . !content ... "</" ?tag ">"))
;; Sequences.
((m/cata !content) ...)
(m/app str* (!content ...))
;; Everythign else.
?x
(m/app str ?x)))
(def tachyons-banner-basic-hiccup
"https://tachyons.io/components/banners/basic/index.html"
[:html
[:head
[:meta {:name "viewport"
:content "width=device-width, initial-scale=1"}]
[:link {:rel "stylesheet"
:href "https://unpkg.com/tachyons/css/tachyons.min.css"}]]
[:body {:class "w-100 sans-serif bg-white pt5"}
[:article {:class "mw7 center ph3 ph5-ns tc br2 pv5 bg-washed-green dark-green mb5"}
[:h1 {:class "fw6 f3 f2-ns lh-title mt0 mb3"}
"This is a tagline. For x."]
[:h2 {:class "fw2 f4 lh-copy mt0 mb3"}
"This will change things. And we want you to be involved. This text needs to"
"be longer for testing sake."]
[:p {:class "fw1 f5 mt0 mb3"}
"Sign up for beta access or learn more about x."]
[:div
[:a {:class "f6 br-pill bg-dark-green no-underline washed-green ba b--dark-green grow pv2 ph3 dib mr3"
:href "#"}
"Sign Up"]
[:a {:class "f6 br-pill dark-green no-underline ba grow pv2 ph3 dib"
:href "#"}
"Learn More"]]]]])
(comment
(def tachyons-banner-basic-html
(html tachyons-banner-basic-hiccup))
tachyons-banner-basic-html
;; =>
"<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/><link rel=\"stylesheet\" href=\"https://unpkg.com/tachyons/css/tachyons.min.css\"/></head><body class=\"w-100 sans-serif bg-white pt5\"><article class=\"mw7 center ph3 ph5-ns tc br2 pv5 bg-washed-green dark-green mb5\"><h1 class=\"fw6 f3 f2-ns lh-title mt0 mb3\">This is a tagline. For x.</h1><h2 class=\"fw2 f4 lh-copy mt0 mb3\">This will change things. And we want you to be involved. This text needs tobe longer for testing sake.</h2><p class=\"fw1 f5 mt0 mb3\">Sign up for beta access or learn more about x.</p><div><a href=\"#\" class=\"f6 br-pill bg-dark-green no-underline washed-green ba b--dark-green grow pv2 ph3 dib mr3\">Sign Up</a><a href=\"#\" class=\"f6 br-pill dark-green no-underline ba grow pv2 ph3 dib\">Learn More</a></div></article></body></html>"
(spit "tachyons-banner-basic.html" tachyons-banner-basic-html))
;; Compiler (macro) version
;; ---------------------------------------------------------------------
(defn stringifiable? [x]
(or (string? x)
(number? x)
(keyword? x)))
(defn flatten-str-form [str-form]
(m/rewrite str-form
;; (clojure.core/str) == "".
(`str)
""
;; Flatten nested calls to `clojure.core/str` recursively.
(`str . (m/or (`str . (m/cata (m/or "" !args)) ...)
(m/cata (m/or "" !args))) ...)
(`str . !args ...)
;; Stop.
('quote & _ :as ?form)
?form
;; Apply to lists.
((m/cata !xs) ...)
(!xs ...)
;; Apply to vectors.
[(m/cata !xs) ...]
[!xs ...]
;; Apply to vectors.
(m/and {} (m/gather [(m/cata !k) (m/cata !v)]))
{& [[!k !v] ...]}
;; Apply to sets.
(m/and #{} (m/gather (m/cata !x)))
#{^& [!x ...]}
;; Stop.
?x
?x))
(defn interpret-str-form* [str-form]
(m/rewrite str-form
;; (clojure.core/str 1) == "1"
;; (clojure.core/str "foo") == "foo"
(`str (m/pred stringifiable? ?string))
(m/app str ?string)
;; (clojure.core/str "foo" "bar") == "foo bar"
(`str . !init ... (m/pred stringifiable? ?string-1) (m/pred stringifiable? ?string-2) & ?rest)
(`str . !init ... (m/app str ?string-1 ?string-2) & ?rest)
;; (clojure.core/str x [,,,] ,,,) == (clojure.core/str x "[" ,,, "]" ,,,)
(`str . (m/and (m/not [& _]) (m/cata !ys)) ... [(m/cata !xs) ...] & ?tail)
(`str . !ys ... "[" . !xs ... "]" & ?tail)
;; (clojure.core/str x {,,,} ,,,) == (clojure.core/str x "{" ,,, "}" ,,,)
(`str . (m/and (m/not {}) (m/cata !ys)) ... (m/and {} (m/gather [(m/cata !k) (m/cata !v)])) & ?tail)
(`str . !ys ... "{" . !k !v ... "}" & ?tail)
;; (clojure.core/str x #{,,,} ,,,) == (clojure.core/str x "#{" ,,, "}" ,,,)
(`str . (m/and (m/not #{}) (m/cata !ys)) ... (m/and #{} (m/gather (m/cata !x))) & ?tail)
(`str . !ys ... "#{" . !x ... "}" & ?tail)
;; (clojure.core/str x "foo" "bar" y ,,,) == (clojure.core/str x "foobar" y ,,,)
(`str . !init ... (m/pred stringifiable? !string) ..1 & ?tail)
(`str . !init ... (m/app str* [!string ...]) & ?tail)
;; Stop.
('quote & _ :as ?form)
?form
;; Apply to lists.
((m/cata !xs) ...)
(!xs ...)
;; Apply to vectors.
[(m/cata !xs) ...]
[!xs ...]
;; Apply to vectors.
(m/and {} (m/gather [(m/cata !k) (m/cata !v)]))
{& [[!k !v] ...]}
;; Apply to sets.
(m/and #{} (m/gather (m/cata !k)))
#{^& [!k ...]}
;; Stop.
?x
?x))
(def ^{:arglists '([str-form])}
interpret-str-form
(m*/pipe #'flatten-str-form (m*/until = #'interpret-str-form*)))
(defmacro html* [hiccup]
(interpret-str-form
(m/rewrite hiccup
;; Borrow :<> from Reagent.
[:<> . (m/cata !content) ...]
(`list !content ...)
;; Void tags.
(m/with [%attributes {(m/keyword !attr) !val & (m/or %attributes _)}]
[(m/keyword (m/re #"area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr" ?tag))
& (m/or [%attributes . _ ...]
[_ ...])])
(`str "<" ?tag . " " !attr "=\"" !val "\"" ... "/>")
;; Normal tags.
(m/with [%content (m/cata !content)
%attributes {(m/keyword !attr) !val & (m/or %attributes _)}]
[(m/keyword ?tag)
& (m/or [%attributes . %content ...]
[%content ...])])
(`str . "<" ?tag . " " !attr "=\"" !val "\"" ... ">" . !content ... "</" ?tag ">")
(m/pred string? ?x)
?x
;; Everything else.
?x
(`html ?x))))
(comment
(interpret-str-form `(str (get #{(str 1 2 3) (str "a" ["b" "c"] "e")} (str "1" (str "23")))))
;; =>
(clojure.core/str (clojure.core/get #{"123" "a[bc]e"} "123"))
(macroexpand
(html*
[:html
[:head
[:meta {:name "viewport"
:content "width=device-width, initial-scale=1"}]
[:link {:rel "stylesheet"
:href "https://unpkg.com/tachyons/css/tachyons.min.css"}]]
[:body {:class "w-100 sans-serif bg-white pt5"}
[:article {:class "mw7 center ph3 ph5-ns tc br2 pv5 bg-washed-green dark-green mb5"}
[:h1 {:class "fw6 f3 f2-ns lh-title mt0 mb3"}
"This is a tagline. For x."]
[:h2 {:class "fw2 f4 lh-copy mt0 mb3"}
"This will change things. And we want you to be involved. This text needs to"
"be longer for testing sake."]
[:p {:class "fw1 f5 mt0 mb3"}
"Sign up for beta access or learn more about x."]
[:div
[:a {:class "f6 br-pill bg-dark-green no-underline washed-green ba b--dark-green grow pv2 ph3 dib mr3"
:href "#"}
"Sign Up"]
[:a {:class "f6 br-pill dark-green no-underline ba grow pv2 ph3 dib"
:href "#"}
"Learn More"]]]]]))
;; =>
"<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/><link rel=\"stylesheet\" href=\"https://unpkg.com/tachyons/css/tachyons.min.css\"/></head><body class=\"w-100 sans-serif bg-white pt5\"><article class=\"mw7 center ph3 ph5-ns tc br2 pv5 bg-washed-green dark-green mb5\"><h1 class=\"fw6 f3 f2-ns lh-title mt0 mb3\">This is a tagline. For x.</h1><h2 class=\"fw2 f4 lh-copy mt0 mb3\">This will change things. And we want you to be involved. This text needs tobe longer for testing sake.</h2><p class=\"fw1 f5 mt0 mb3\">Sign up for beta access or learn more about x.</p><div><a href=\"#\" class=\"f6 br-pill bg-dark-green no-underline washed-green ba b--dark-green grow pv2 ph3 dib mr3\">Sign Up</a><a href=\"#\" class=\"f6 br-pill dark-green no-underline ba grow pv2 ph3 dib\">Learn More</a></div></article></body></html>")