forked from tonyg/racket-xe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.rkt
189 lines (171 loc) · 3.06 KB
/
html.rkt
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
#lang racket
(provide emit-tag
tag-maker
define-tag-maker
define/provide-tag-maker
*void-elements*
void-element?
*html5-elements*
html5-element?
html-xexpr->string
trim-content
)
(require (only-in xml empty-tag-shorthand xexpr->string))
(require (only-in "xexpr-utilities.rkt" xe* add-class classes->attr))
;;---------------------------------------------------------------------------
(define (emit-tag tag #:classes [classes '()] #:attrs [attrs '()] kids)
(let* ((attrs (if (null? classes)
attrs
(cons (list 'class (classes->attr classes)) attrs))))
(xe* tag attrs kids)))
(define ((tag-maker tag) #:classes [classes '()] #:attrs [attrs '()] . kids)
(emit-tag tag #:classes classes #:attrs attrs kids))
(define-syntax-rule (define-tag-maker tag ...)
(begin
(define tag (tag-maker 'tag)) ...))
(define-syntax-rule (define/provide-tag-maker tag ...)
(begin
(define-tag-maker tag ...)
(provide tag ...)))
(define *void-elements*
'(
;; From https://developer.mozilla.org/en-US/docs/Glossary/Void_element :
area
base
br
col
embed
hr
img
input
link
meta
param
source
track
wbr
;; Additional, from Racket's own `html-empty-tags`:
basefont
frame
isindex
))
(define (void-element? sym)
(and (memq sym *void-elements*) #t))
(define *html5-elements*
'(
;; List of HTML5 tags taken from https://gist.github.com/mrmrs/7650266 !
a
abbr
address
area
article
aside
audio
b
bdi
bdo
blockquote
body
br
button
canvas
caption
cite
code
col
colgroup
command
datalist
dd
del
details
dfn
div
dl
dt
em
embed
fieldset
figcaption
figure
footer
form
h1
h2
h3
h4
h5
h6
header
hr
html
i
iframe
img
input
ins
kbd
keygen
label
legend
li
main
map
mark
menu
meter
nav
object
ol
optgroup
option
output
p
param
pre
progress
q
rp
rt
ruby
s
samp
section
select
small
source
span
strong
sub
summary
sup
table
tbody
td
textarea
tfoot
th
thead
time
tr
track
u
ul
var
video
wbr
))
(define (html5-element? sym)
(and (memq sym *html5-elements*) #t))
(define (html-xexpr->string xexpr)
(parameterize ((empty-tag-shorthand *void-elements*))
;; ^ Void elements aren't *quite* empty-tags, but close enough.
(xexpr->string xexpr)))
(define (trim-content #:left? [left? #t] #:right? [right? #t] items)
(match items
[(list) (list)]
[(list X) (list (if (string? X) (string-trim #:left? left? #:right? right? X) X))]
[(list L Ms ... R)
`(,(if (string? L) (string-trim #:left? left? #:right? #f L) L)
,@Ms
,(if (string? R) (string-trim #:left? #f #:right? right? R) R))]))