|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
#lang debug racket/base
|
|
|
|
|
(require racket/class racket/match racket/list txexpr racket/dict racket/function
|
|
|
|
|
(require racket/string racket/class racket/match racket/list txexpr racket/dict racket/function
|
|
|
|
|
"quad.rkt" "param.rkt")
|
|
|
|
|
(provide (all-defined-out))
|
|
|
|
|
(module+ test (require rackunit))
|
|
|
|
@ -16,15 +16,15 @@
|
|
|
|
|
((hasheq 'foo "bar" 'zim "zam") . update-with . (hasheq 'zim "BANG") (hasheq 'toe "jam") (hasheq 'foo "zay"))
|
|
|
|
|
(make-hasheq '((zim . "BANG") (foo . "zay") (toe . "jam")))))
|
|
|
|
|
|
|
|
|
|
(define (merge-whitespace aqs [white-aq? (λ (aq) (char-whitespace? (car (get-field elems aq))))])
|
|
|
|
|
;; collapse each sequence of whitespace aqs to the first one, and make it a space
|
|
|
|
|
(define (merge-whitespace qs [white-q? (λ (aq) (char-whitespace? (car (get-field elems aq))))])
|
|
|
|
|
;; collapse each sequence of whitespace qs to the first one, and make it a space
|
|
|
|
|
;; also drop leading & trailing whitespaces
|
|
|
|
|
;; (same behavior as web browsers)
|
|
|
|
|
(let loop ([acc null][aqs aqs])
|
|
|
|
|
(if (null? aqs)
|
|
|
|
|
(let loop ([acc null][qs qs])
|
|
|
|
|
(if (null? qs)
|
|
|
|
|
(flatten acc)
|
|
|
|
|
(let*-values ([(bs rest) (splitf-at aqs (negate white-aq?))]
|
|
|
|
|
[(ws rest) (splitf-at rest white-aq?)])
|
|
|
|
|
(let*-values ([(bs rest) (splitf-at qs (negate white-q?))]
|
|
|
|
|
[(ws rest) (splitf-at rest white-q?)])
|
|
|
|
|
(loop (list acc bs (if (and (pair? rest) ;; we precede bs (only #t if rest starts with bs, because we took the ws)
|
|
|
|
|
(pair? bs) ;; we follow bs
|
|
|
|
|
(pair? ws)) ;; we have ws
|
|
|
|
@ -32,8 +32,8 @@
|
|
|
|
|
null)) rest)))))
|
|
|
|
|
|
|
|
|
|
#;(module+ test
|
|
|
|
|
(check-equal? (merge-whitespace (list (q #\space) (q #\newline) (q #\H) (q #\space) (q #\newline) (q #\space) (q #\i) (q #\newline)))
|
|
|
|
|
(list (q #\H) (q #\space) (q #\i))))
|
|
|
|
|
(check-equal? (merge-whitespace (list (q #\space) (q #\newline) (q #\H) (q #\space) (q #\newline) (q #\space) (q #\i) (q #\newline)))
|
|
|
|
|
(list (q #\H) (q #\space) (q #\i))))
|
|
|
|
|
|
|
|
|
|
(define (atomize qx)
|
|
|
|
|
;; normalize a quad by reducing it to one-character quads.
|
|
|
|
@ -43,58 +43,56 @@
|
|
|
|
|
(match x
|
|
|
|
|
[(? char? c) (list (q attrs c))]
|
|
|
|
|
[(? string?) (append* (for/list ([c (in-string x)]) ;; strings are exploded
|
|
|
|
|
(loop c attrs)))]
|
|
|
|
|
(loop c attrs)))]
|
|
|
|
|
[(? quad?) ;; qexprs with attributes are recursed
|
|
|
|
|
(define this-attrs (get-field attrs x))
|
|
|
|
|
(define elems (get-field elems x))
|
|
|
|
|
(define merged-attrs (attrs . update-with . this-attrs))
|
|
|
|
|
(append* (for/list ([elem (in-list elems)])
|
|
|
|
|
(loop elem merged-attrs)))]
|
|
|
|
|
(loop elem merged-attrs)))]
|
|
|
|
|
[else (raise-argument-error 'atomize "valid item" x)])))
|
|
|
|
|
(merge-whitespace atomic-quads))
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
|
(require rackunit)
|
|
|
|
|
(check-equal? (atomize (q "Hi")) (list (q #\H) (q #\i)))
|
|
|
|
|
(check-equal? (atomize (q "Hi " (q "You"))) (list (q #\H) (q #\i) (q #\space) (q #\Y) (q #\o) (q #\u)))
|
|
|
|
|
(check-exn exn:fail:contract? (λ () (atomize #t)))
|
|
|
|
|
(check-equal? (atomize (q "H i")) (list (q #\H) (q #\space) (q #\i)))
|
|
|
|
|
(check-equal? (atomize (q "H \n\n i")) (list (q #\H) (q #\space) (q #\i))) ;; collapse whitespace to single
|
|
|
|
|
#;(module+ test
|
|
|
|
|
(require rackunit)
|
|
|
|
|
(check-equal? (atomize (q "Hi")) (list (q #\H) (q #\i)))
|
|
|
|
|
(check-equal? (atomize (q "Hi " (q "You"))) (list (q #\H) (q #\i) (q #\space) (q #\Y) (q #\o) (q #\u)))
|
|
|
|
|
(check-exn exn:fail:contract? (λ () (atomize #t)))
|
|
|
|
|
(check-equal? (atomize (q "H i")) (list (q #\H) (q #\space) (q #\i)))
|
|
|
|
|
(check-equal? (atomize (q "H \n\n i")) (list (q #\H) (q #\space) (q #\i))) ;; collapse whitespace to single
|
|
|
|
|
|
|
|
|
|
;; with attributes
|
|
|
|
|
(check-equal? (atomize (q (hasheq 'k "v") "Hi")) (list (q (hasheq 'k "v") #\H) (q (hasheq 'k "v") #\i)))
|
|
|
|
|
(check-equal? (atomize (q (hasheq 'k "v") "Hi " (q "You")))
|
|
|
|
|
(list
|
|
|
|
|
(quad (hasheq 'k "v") #\H)
|
|
|
|
|
(quad (hasheq 'k "v") #\i)
|
|
|
|
|
(quad (hasheq 'k "v") #\space)
|
|
|
|
|
(quad (hasheq 'k "v") #\Y)
|
|
|
|
|
(quad (hasheq 'k "v") #\o)
|
|
|
|
|
(quad (hasheq 'k "v") #\u)))
|
|
|
|
|
(check-equal? (atomize (q (hasheq 'k1 "v1" 'k2 42) "Hi \n\n" (q (hasheq 'k1 "v2" 'k3 "foo") "\n \nYou")))
|
|
|
|
|
(list
|
|
|
|
|
(quad (hasheq 'k1 "v1" 'k2 42) #\H)
|
|
|
|
|
(quad (hasheq 'k1 "v1" 'k2 42) #\i)
|
|
|
|
|
(quad (hasheq 'k1 "v1" 'k2 42) #\space)
|
|
|
|
|
(quad (hasheq 'k1 "v2" 'k2 42 'k3 "foo") #\Y)
|
|
|
|
|
(quad (hasheq 'k1 "v2" 'k2 42 'k3 "foo") #\o)
|
|
|
|
|
(quad (hasheq 'k1 "v2" 'k2 42 'k3 "foo") #\u))))
|
|
|
|
|
;; with attributes
|
|
|
|
|
(check-equal? (atomize (q (hasheq 'k "v") "Hi")) (list (q (hasheq 'k "v") #\H) (q (hasheq 'k "v") #\i)))
|
|
|
|
|
(check-equal? (atomize (q (hasheq 'k "v") "Hi " (q "You")))
|
|
|
|
|
(list
|
|
|
|
|
(quad (hasheq 'k "v") #\H)
|
|
|
|
|
(quad (hasheq 'k "v") #\i)
|
|
|
|
|
(quad (hasheq 'k "v") #\space)
|
|
|
|
|
(quad (hasheq 'k "v") #\Y)
|
|
|
|
|
(quad (hasheq 'k "v") #\o)
|
|
|
|
|
(quad (hasheq 'k "v") #\u)))
|
|
|
|
|
(check-equal? (atomize (q (hasheq 'k1 "v1" 'k2 42) "Hi \n\n" (q (hasheq 'k1 "v2" 'k3 "foo") "\n \nYou")))
|
|
|
|
|
(list
|
|
|
|
|
(quad (hasheq 'k1 "v1" 'k2 42) #\H)
|
|
|
|
|
(quad (hasheq 'k1 "v1" 'k2 42) #\i)
|
|
|
|
|
(quad (hasheq 'k1 "v1" 'k2 42) #\space)
|
|
|
|
|
(quad (hasheq 'k1 "v2" 'k2 42 'k3 "foo") #\Y)
|
|
|
|
|
(quad (hasheq 'k1 "v2" 'k2 42 'k3 "foo") #\o)
|
|
|
|
|
(quad (hasheq 'k1 "v2" 'k2 42 'k3 "foo") #\u))))
|
|
|
|
|
|
|
|
|
|
(define whitespace-pat #px"\\s+")
|
|
|
|
|
(define (merge-white str) (regexp-replace* whitespace-pat str " "))
|
|
|
|
|
|
|
|
|
|
(define (isolate-white str)
|
|
|
|
|
(for/list ([m (in-list (regexp-match* " " str #:gap-select? #t))]
|
|
|
|
|
#:when (positive? (string-length m)))
|
|
|
|
|
m))
|
|
|
|
|
(define (merge-and-isolate-white str)
|
|
|
|
|
(for/list ([(m idx) (in-indexed (regexp-match* whitespace-pat str #:gap-select? #t))]
|
|
|
|
|
#:when (non-empty-string? m))
|
|
|
|
|
(if (even? idx) m " ")))
|
|
|
|
|
|
|
|
|
|
(define (merge-adjacent-strings xs [isolate-white? #false])
|
|
|
|
|
(let loop ([xs xs][acc null])
|
|
|
|
|
(match xs
|
|
|
|
|
[(== empty) (reverse acc)]
|
|
|
|
|
[(list) (reverse acc)]
|
|
|
|
|
[(list (? string? strs) ..1 others ...)
|
|
|
|
|
(loop others (append (reverse ((if isolate-white?
|
|
|
|
|
(compose1 isolate-white merge-white)
|
|
|
|
|
merge-and-isolate-white
|
|
|
|
|
list) (apply string-append strs))) acc))]
|
|
|
|
|
[(cons x others) (loop others (cons x acc))])))
|
|
|
|
|
|
|
|
|
@ -127,7 +125,7 @@
|
|
|
|
|
(λ (q) (string=? " " (car (get-field elems q))))))
|
|
|
|
|
|
|
|
|
|
#;(module+ test
|
|
|
|
|
(check-equal?
|
|
|
|
|
(check-equal?
|
|
|
|
|
(runify (quad (hasheq 'foo 42) (quad "Hi" " idiot" (quad (hasheq 'bar 84) "There") "Eve" "ry" "one")))
|
|
|
|
|
(list (quad (hasheq 'foo 42) "Hi") (quad (hasheq 'foo 42) " ") (quad (hasheq 'foo 42) "idiot") (quad (hasheq 'foo 42 'bar 84) "There") (quad (hasheq 'foo 42) "Everyone"))))
|
|
|
|
|
|
|
|
|
|