forked from tonyg/racket-xe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xexpr-utilities.rkt
357 lines (301 loc) · 13.3 KB
/
xexpr-utilities.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#lang racket/base
;; xexpr utilities, plus traversal and filtering
(provide xe
xe*
xa
xa*
attr-ref
attr-ref/remove
attr->classes
classes->attr
add-class
add-id
clean-xexpr-whitespace
(struct-out path-selection)
xexpr@*
xexpr@
xexpr1
;; Axes: move around, applying filters after moving
@= ;; Doesn't move anywhere
@/ ;; Moves into immediate children
@// ;; Flattens children recursively
@@ ;; Moves into an immediate attribute
@text ;; Moves into contained text
@xml ;; Yields xexpr->string of selected nodes
;; Filters: narrow down a selection without moving
=path ;; Applies a nested path, keeps nodes having submatches
=tag ;; Matches a tag
=@ ;; Matches an attribute with a particular value
=@? ;; Matches a present attribute
=text ;; Matches text
)
(require racket/match)
(require (only-in racket/list flatten))
(require (only-in racket/string string-split string-join string-trim string-append* non-empty-string?))
(require (only-in racket/format ~a))
(require (only-in xml xexpr->string xexpr-drop-empty-attributes))
(module+ test (require rackunit))
;;---------------------------------------------------------------------------
(define-match-expander xe*
(syntax-rules ()
[(_ tag attrs kids)
(or (list* (? symbol? tag) (and (or '() (cons (list _ _) _)) attrs) kids)
(and (list* (? symbol? tag) kids)
(app (lambda (v) '()) attrs)))])
(syntax-rules ()
[(_ tag attrs kids)
(let ((a attrs))
(if (and (null? a) (xexpr-drop-empty-attributes))
(list* tag kids)
(list* tag a kids)))]))
(define-match-expander xe
(syntax-rules () [(_ tag attrs kids ...) (xe* tag attrs (list kids ...))])
(syntax-rules () [(_ tag attrs kids ...) (xe* tag attrs (list kids ...))]))
(define-match-expander xa*
(syntax-rules ()
[(_ (id pat) ... rest)
(list-no-order (list 'id pat) ... rest (... ...))]))
(define-match-expander xa
(syntax-rules ()
[(_ (id pat) ...) (xa* (id pat) ... _)]))
(define (attr-ref a attrs [default #f])
(cond [(assq a attrs) => cadr]
[(procedure? default) (default)]
[else default]))
(define (remove-attr a attrs)
(filter (lambda (e) (not (eq? (car e) a))) attrs))
(define (attr-ref/remove a attrs [default #f])
(values (attr-ref a attrs default)
(remove-attr a attrs)))
(define (attr->classes a)
(map string->symbol (string-split a)))
(define (classes->attr cs)
(string-join (map ~a cs) " "))
(define (add-class #:wrapper [wrapper 'span] class-or-classes item)
(match item
[(xe* tag attrs0 kids)
(define-values (cs attrs) (attr-ref/remove 'class attrs0 ""))
(xe* tag
`((class ,(classes->attr (flatten (cons class-or-classes
(attr->classes cs)))))
,@attrs)
kids)]
[(? string? s)
(xe wrapper `((class ,(classes->attr (flatten class-or-classes)))) s)]))
(define (add-id #:wrapper [wrapper 'span] id item)
(match item
[(xe* tag attrs kids)
(xe* tag `((id ,id) ,@(remove-attr 'id attrs)) kids)]
[(? string? s)
(xe wrapper `((id ,id)) s)]))
(module+ test
(check-equal? (match '((a "hi")) [(xe* _ _ _) 'is-element] [_ 'not-element]) 'not-element)
(check-equal? (match '((a 1) (b 2) (c 3)) [(xa (b 2)) #t] [_ #f]) #t)
(check-equal? (match '((a 1) (b 2) (c 3)) [(xa (b 1)) #t] [_ #f]) #f)
(check-equal? (match '((a 1) (b 2) (c 3)) [(xa (b 2) (a x)) x] [_ #f]) 1)
(check-equal? (xe 'p '()) `(p ()))
(check-equal? (xe 'p '() "hello") `(p () "hello"))
(check-equal? (xe 'p '() "hello" "there") `(p () "hello" "there"))
(check-equal? (xe 'p '((id "6")) "hello" "there") `(p ((id "6")) "hello" "there"))
(parameterize ((xexpr-drop-empty-attributes #t))
(check-equal? (xe 'p '()) `(p))
(check-equal? (xe 'p '() "hello") `(p "hello"))
(check-equal? (xe 'p '() "hello" "there") `(p "hello" "there"))
(check-equal? (xe 'p '((id "6")) "hello" "there") `(p ((id "6")) "hello" "there")))
(check-equal? (xe* 'p '() '()) `(p ()))
(check-equal? (xe* 'p '() '("hello")) `(p () "hello"))
(check-equal? (xe* 'p '() '("hello" "there")) `(p () "hello" "there"))
(check-equal? (xe* 'p '((id "6")) '("hello" "there")) `(p ((id "6")) "hello" "there"))
(parameterize ((xexpr-drop-empty-attributes #t))
(check-equal? (xe* 'p '() '()) `(p))
(check-equal? (xe* 'p '() '("hello")) `(p "hello"))
(check-equal? (xe* 'p '() '("hello" "there")) `(p "hello" "there"))
(check-equal? (xe* 'p '((id "6")) '("hello" "there")) `(p ((id "6")) "hello" "there")))
(check-equal? (match `(tag "text") [(xe 'tag '() "text") #t] [_ #f]) #t)
(check-equal? (match `(tag () "text") [(xe 'tag '() "text") #t] [_ #f]) #t)
(check-equal? (match `(tag "text2") [(xe 'tag '() "text") #t] [_ #f]) #f)
(check-equal? (match `(tag "text") [(xe t as ks ...) (list t as ks)])
(list 'tag '() '("text")))
(check-equal? (match `(tag () "text") [(xe t as ks ...) (list t as ks)])
(list 'tag '() '("text")))
(check-equal? (match `(tag ((k "v")) "text") [(xe t as ks ...) (list t as ks)])
(list 'tag '((k "v")) '("text")))
(check-equal? (match `(tag "text1" "text2") [(xe t as ks ...) (list t as ks)])
(list 'tag '() '("text1" "text2")))
(check-equal? (match `(tag "text1" "text2") [(xe* t as ks) (list t as ks)])
(list 'tag '() '("text1" "text2")))
(check-equal? (attr-ref 'class '((foo "bar") (class "c") (id "123"))) "c")
(check-equal? (attr-ref 'klass '((foo "bar") (class "c") (id "123"))) #f)
(check-equal? (attr-ref 'klass '((foo "bar") (class "c") (id "123")) #t) #t)
(check-equal? (attr-ref 'klass
'((foo "bar") (class "c") (id "123"))
(lambda () (+ 1 2)))
3)
(check-equal? (call-with-values (lambda ()
(attr-ref/remove
'class
'((foo "bar") (class "c") (id "123"))))
list)
(list "c" '((foo "bar") (id "123"))))
(check-equal? (call-with-values (lambda ()
(attr-ref/remove
'klass
'((foo "bar") (class "c") (id "123"))))
list)
(list #f '((foo "bar") (class "c") (id "123")))))
;;---------------------------------------------------------------------------
(define (clean-xexpr-whitespace xexpr)
(define (finish string-acc acc)
(define s (string-trim (string-append* (reverse string-acc))))
(if (non-empty-string? s)
(cons s acc)
acc))
(define (clean-kids string-acc acc kids)
(match kids
['()
(reverse (finish string-acc acc))]
[(cons (? string? s) more)
(clean-kids (cons s string-acc) acc more)]
[(cons other more)
(clean-kids '() (cons (clean-xexpr-whitespace other) (finish string-acc acc)) more)]))
(match xexpr
[(? string? s) (string-trim s)]
[(xe* tag attrs kids)
(xe* tag attrs (clean-kids '() '() kids))]))
;;---------------------------------------------------------------------------
(struct path-selection (context element) #:transparent)
(define (elaborate-step step)
(match step
[(== /) @/] ;; shortcut
[(== *) @=] ;; shortcut
[(? procedure? p) p]
['/ @/]
['// @//]
['* @=]
[(? symbol? tag)
(match (symbol->string tag)
[(regexp #px"^@(.*)$" (list _ a)) (@@ (string->symbol a))]
[_ (=tag tag)])]
[other (error 'xexpr@ "Invalid step: ~v" other)]))
(define (xexpr@** c e steps)
(match e [(xe* _ _ _) 'ok] [_ (error 'xexpr@** "Invalid node: ~v" e)])
(for/fold [(acc (list (path-selection c e)))] [(step (in-list steps))]
(flatten (map (elaborate-step step) acc))))
(define (xexpr@* #:context [c '()] e . steps)
(xexpr@** c e steps))
(define (xexpr@ #:context [c '()] e . steps)
(map path-selection-element (xexpr@** c e steps)))
(define (xexpr1 #:default [default (lambda () (error 'xexpr1 "No remaining selection"))]
#:context [c '()]
e . steps)
(match (xexpr@** c e steps)
[(list (path-selection _ e)) e]
['() (if (procedure? default) (default) default)]
[selections (error 'xexpr1 "Ambiguous selection (~a entries)" (length selections))]))
(define @=
(lambda (s) s))
(define @/
(match-lambda [(path-selection c (and e (xe* _ _ ks)))
(define c1 (cons e c))
(for/list [(k ks)] (path-selection c1 k))]
[(path-selection _ _)
'()]))
(define @//
(match-lambda [(and s (path-selection c (and e (xe* _ _ ks))))
(define c1 (cons e c))
(cons s (for/list [(k ks)] (@// (path-selection c1 k))))]
[(path-selection _ _)
'()]))
(define (@@ attr)
(match-lambda [(path-selection c (and e (xe* _ attrs _)))
(match (assq attr attrs)
[(list _ v) (path-selection (cons e c) v)]
[#f '()])]
[(path-selection _ _)
'()]))
(define (nodes->text ks)
(define (walk n)
(match n
[(xe* _ _ ks) (map walk ks)]
[(? string? s) s]
[(? symbol? s) (format "&~a;" s)]
[(? number? n) (format "&#~a;" n)]))
(string-append* (flatten (map walk ks))))
(define @text
(match-lambda [(path-selection c (and e (xe* _ _ ks)))
(path-selection (cons e c) (nodes->text ks))]
[(path-selection _ _)
'()]))
(define @xml
(match-lambda [(path-selection c e)
(path-selection (cons e c) (xexpr->string e))]))
(define (=path . steps)
(match-lambda [(and s (path-selection c e))
(if (null? (xexpr@** c e steps)) '() s)]))
(define (=tag t)
(match-lambda [(and s (path-selection _ (xe* (== t) _ _))) s]
[(path-selection _ _) '()]))
(define (text-matches? p v)
(cond
[(string? p) (equal? p v)]
[(regexp? p) (regexp-match? p v)]
[else (error 'text-matches? "Unsupported xexpr string-match pattern: ~v" p)]))
(define (=@ attr pattern)
(match-lambda [(and s (path-selection _ (xe* _ attrs _)))
(match (assq attr attrs)
[(list _ v) (if (text-matches? pattern v) s '())]
[#f '()])]
[(path-selection _ _) '()]))
(define (=@? attr)
(match-lambda [(and s (path-selection _ (xe* _ attrs _))) #:when (assq attr attrs) s]
[(path-selection _ _) '()]))
(define (=text p)
(match-lambda
[(and s (path-selection _ (xe* _ _ ks))) #:when (text-matches? p (nodes->text ks)) s]
[(and s (path-selection _ (? string? v))) #:when (text-matches? p v) s]
[(path-selection _ _) '()]))
(module+ test
(define d1 `(AAA (BBB (CCC)
(www " www content " (xxx))
(zzz))
(XXX (DDD " content in ccc\n"))))
(define d2 `(greetings (greeting "Hello World!")
(greeting "Ahoj Světe!")))
(define d3 `(dataset (value ((type "int")) "42")
(value ((type "str")) "foo")
(value ((type "int")) "13")))
(define d4 `(table
(tr (td "a") (td "b"))
(tr (td "c") (td "d"))))
(define d5 `(div (p (table
(tr (td "a") (td "b"))
(tr (td "c") (td "d"))))
(table
(tr (td "e")))))
(define d6 `(div (p (div "3")
(div (div "4")))))
(define d7 `(div (p (i "3")
(froogy (i "4")))))
(define d8 `(object (prop ((id "kind")) "show")
(prop ((id "name")) "Lucky" 9734 "Star")))
(define d9 `(p "Hello" nbsp (testing) "world!"))
(check-equal? (xexpr@ d2 / 'greeting) '((greeting "Hello World!") (greeting "Ahoj Světe!")))
(check-equal? (xexpr@ d3 / 'value (=@ 'type "int"))
'((value ((type "int")) "42") (value ((type "int")) "13")))
(check-equal? (xexpr@ d4 'table / 'tr / 'td) `((td "a") (td "b") (td "c") (td "d")))
(check-equal? (xexpr@ d5 @// 'td) '((td "a") (td "b") (td "c") (td "d") (td "e")))
(check-equal? (xexpr@ d6 @// 'div)
'((div (p (div "3") (div (div "4")))) (div "3") (div (div "4")) (div "4")))
(check-equal? (xexpr@ d7 @// 'p / 'i) '((i "3")))
(check-equal? (xexpr@ d8 / 'prop (=@ 'id "name") /) '("Lucky" 9734 "Star"))
(check-equal? (xexpr@ d8 / 'prop (=@ 'id "name") @text) '("Lucky☆Star"))
(check-equal? (xexpr@ d9 @text) '("Hello world!"))
(check-equal? (xexpr@ d9 @xml) '("<p>Hello <testing></testing>world!</p>"))
(check-equal? (string-append* (xexpr@ d9 / @xml)) "Hello <testing></testing>world!")
(check-equal? (xexpr@ `(bltx:name ()
(bltx:namepart ((type "family"))
(bltx:namepart ((initial "D")) "D")
(bltx:namepart () "E"))
(bltx:namepart ((initial "A") (type "given")) "A"))
@// @text)
'("DEA" "DE" "D" "E" "A")))