-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.zp
224 lines (207 loc) · 9.57 KB
/
html.zp
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
(module "html"
(export
(list "doctype" doctype)
(list "build" build)
(list "with-skeleton" with-skeleton)
(list "parse" parse))
(implicit ["meta" "img" "input" "br" "hr" "frame" "area" "base" "basefont"
"col" "isindex" "link" "param" "html" "head" "body" "p" "dt"
"dd" "li" "option" "thead" "th" "tbody" "tr" "td" "tfoot" "colgroup"])
(empty-acc #{:tags () :text ""})
(doctype (lambda x
"generates a doctype HTML tag.
params:
- x: an optional argument to change the doctype type (defaults to html)
complexity: O(1)
returns: a string representing the doctype tag"
(if (null? x)
"<!DOCTYPE html>"
(++ "<!DOCTYPE " (car x) ">"))))
(parse (lambda (str)
"parses a HTML formatted string into a nested list of tags.
Example:
<zepto>
(html:parse \"<html><body><p class='section'>Hi</p></body></html>\")
; => #{:tags (#{:name \"html\"
; :tags (#{:name \"body\"
; :tags (#{:name \"p\"
; :attributes #{\"class\" \"section\"}
; :text \"Hi\"})})})}
</zepto>
params:
str: the string to parse
complexity: O(n)
returns: the list"
(define (add-to-tags hash tag)
(hash:update hash :tags (lambda (tags) (++ tags tag))))
(define (add-to-text hash text)
(hash:update hash :text (lambda (t) (++ t text))))
(define (make-tag tag content subtags attributes)
(make-hash :name tag :text content :tags subtags :attributes attributes))
(define (split-attrs tag-contents parsed)
(if (string:empty? tag-contents)
parsed
(let ((quoted (string:find tag-contents #\"))
(whitespace (string:find tag-contents #\space)))
(if (< quoted whitespace)
(let ((sep (add1
(+
(string:find
(substring tag-contents (add1 quoted) (length tag-contents)) #\") quoted))))
(if (eq? sep 0)
:err-quote-mismatch
(split-attrs (substring tag-contents (add1 sep) (length tag-contents))
(++ parsed (substring tag-contents 0 (add1 sep))))))
(if (eq? whitespace -1)
(++ parsed tag-contents)
(if (eq? whitespace 0)
(split-attrs (substring tag-contents (add1 whitespace) (length tag-contents))
parsed)
(split-attrs (substring tag-contents (add1 whitespace) (length tag-contents))
(++ parsed (substring tag-contents 0 whitespace)))))))))
(define (parse-self-closing tag)
(let* ((attrs (split-attrs tag []))
(tname (car attrs))
(pairs
(make-hash
(map
(lambda (x)
(let* ((split (string:split x "=\""))
(val (cadr split)))
(list (car split) (substring val 0 (sub1 (length val))))))
(cdr attrs)))))
(make-tag tname "" [] pairs)))
(define (internal str pos acc)
(if (string:empty? str)
acc
(cond
((string:starts-with str "<!--")
(let ((index (string:find str "-->")))
(if (eq? index -1)
acc
(internal (substring str index (length str)) (+ pos index) acc))))
((string:starts-with str "<!")
(if (eq? acc empty-acc)
(let ((index (string:find str ">")))
(if (eq? index -1)
(begin
(error "html:parse" "Unclosed meta tag at pos" pos)
:err-unclosed-meta)
(internal (substring str index (length str)) (+ pos index)
(add-to-tags acc
(let ((split (string:split (substring str 2 index) #\space)))
(if (= (length split) 1)
(make-tag (car split) "" [] #{})
(make-tag (car split) (cadr split) [] #{})))))))))
((string:starts-with str "<")
(let ((index (string:find str ">")))
(if (eq? index -1)
(begin
(error "html:parse" "Unclosed tag at pos" pos)
:err-unclosed-tag)
(if (eq? (string:ref str (sub1 index)) #\/)
(internal (substring str (add1 index) (length str)) (+ pos index)
(add-to-tags (add-to-text acc (++ "{" (->string (length (acc :tags))) "}"))
(parse-self-closing (substring str 1 (sub1 index)))))
(let* ((tag (parse-self-closing (substring str 1 index)))
(closing (++ "</" (tag :name) ">"))
(rest (substring str (add1 index) (length str)))
(cindex (string:find rest closing)))
(if (eq? cindex -1)
(if (in? implicit (string:lower-case (tag :name)))
'()
(begin
(error "html:parse" "Tag at pos" pos "has no closing tag")
:err-unclosed-tag))
(internal (substring rest (+ cindex (length closing)) (length rest))
(+ pos index cindex (length closing))
(add-to-tags (add-to-text acc
(++ "{" (->string (length (acc :tags))) "}"))
(internal (substring str (add1 index)
(+ (add1 index) cindex))
(+ pos index 1)
tag)))))))))
(else
(let ((index (string:find str "<")))
(if (eq? index -1)
(add-to-text acc str)
(internal (substring str index (length str)) (+ pos index)
(add-to-text acc (substring str 0 index)))))))))
(internal (string:remove-newlines str) 0 empty-acc)))
(build (lambda (m)
"build will build HTML (strings) from hashmaps.
As such, it is inverse to parse.
Example:
<zepto>
(html:build #{:tags (#{:name \"html\"
:attribues #{\"class\" \"not-full-screen\"}
:tags (#{:name \"body\" :text \"empty\"})})})
; => <html class=\"not-full-screen\"><body>empty</body></html>
</zepto>
params:
- m: the map from which to build a HTML string
complexity: O(n)
returns: a HTML string"
(define (_build h)
(define (destructure hash)
(if (nil? hash)
""
(hash:keys-reduce (lambda (acc x) (++ acc " " x "=\"" (hash x) "\"")) "" hash)))
(define (fill template tags)
(cond
((string:empty? template)
(reduce ++ "" tags))
((null? tags)
template)
(else
(let loop ((template template)
(el (car tags))
(tags (cdr tags))
(index 0))
(let* ((i (string:find template (++ "{" (->string index) "}")))
(template (++ (substring template 0 i)
el
(substring template (+ i 3) (length template)))))
(if (null? tags)
template
(loop template (car tags) (cdr tags) (add1 index))))))))
(let* ((tag (get-from h :name ""))
(attrs (destructure (h :attributes)))
(tags (get-from h :tags [])))
(++ "<" tag attrs ">"
(fill (get-from h :text "") (map _build tags))
"</" tag ">")))
(cond
((string? m) m)
((number? m) (number->string m))
((hash-map? m) (reduce (lambda (acc x) (++ (_build x))) "" (m :tags)))
(else :no))))
(with-skeleton (lambda contents
"with-skeleton abstracts a bit over the admittedly tedious build
procedure by providing a HTML skeleton.
Example:
<zepto>
(html:build (html:with-skeleton
#{:head #{:tags (#{:name \"script\"
:attribute #{\"type\" \"text/javascript\"}
:text \"alert(\\\"yay\\\");\"})}
:body #{:tags (#{:name \"p\"
:text \"notifications are annoying\"})}}))
; => <html><head><script type=\"text/javascript\">alert(\"yay\");</script></head><body><p>notifications are annoying</p></body></html>
</zepto>
params:
- contents: optional argument that describes the skeleton body (if none is provided, an empty html page will be generated)
complexity: O(n)
returns: a hashmap you can pipe into <fun>html:build</fun>"
(if (null? contents)
#{:tags (#{:name "html"
:tags (#{:name "head"}
#{:name "body"})})}
(let* ((hash (car contents))
(tags (++ []
(if (truthy? (hash :head)) (hash:set (hash :head) :name "head") [])
(if (truthy? (hash :body)) (hash:set (hash :body) :name "body") []))))
(make-hash :tags
(list
(make-hash :name "html"
:tags tags))))))))