make `split-meta-elements` less uptight (closes #63)

pull/84/head
Matthew Butterick 10 years ago
parent 3d2c0c91e6
commit 8fc3256499

@ -69,11 +69,22 @@
(loop (make-txexpr (world:current-meta-tag-name) null (cdr txexpr-elements)) (cons (apply make-atomic-meta (car txexpr-elements)) acc))])] (loop (make-txexpr (world:current-meta-tag-name) null (cdr txexpr-elements)) (cons (apply make-atomic-meta (car txexpr-elements)) acc))])]
[else (reverse acc)]))) [else (reverse acc)])))
(define (splitter x pred)
(define acc null)
(define leftover (let loop ([x x])
(cond
[(list? x) (define-values (pred-elements rest) (partition pred x))
(set! acc (append pred-elements acc))
(map loop rest)]
[else x])))
(values leftover (reverse acc)))
(define (split-meta-elements x) ; pull metas out of doc and put them into meta-elements accumulator (define (split-meta-elements x) ; pull metas out of doc and put them into meta-elements accumulator
(when (not (txexpr? x)) ;; watch out: x may not be a txexpr, because we are extracting metas before the pollen parser has finished its work
(error 'split-meta-elements "Not a txexpr: ~v" x)) ;; for instance, floating-point numbers will eventually become strings, but here they're still numbers
(define-values (thing-without-meta-elements meta-elements) (splitf-txexpr x meta-element?)) ;; thus instead of split-txexpr, use a custom splitter
(define-values (thing-without-meta-elements meta-elements) (splitter x meta-element?))
;; trivial metas are discarded ;; trivial metas are discarded
(define exploded-meta-elements (append-map explode-meta-element (filter nontrivial-meta-element? meta-elements))) (define exploded-meta-elements (append-map explode-meta-element (filter nontrivial-meta-element? meta-elements)))
(values thing-without-meta-elements exploded-meta-elements)) (values thing-without-meta-elements exploded-meta-elements))
@ -99,12 +110,13 @@
(module+ test (module+ test
(require rackunit) (require rackunit)
(let ([x '(root (meta ((foo "bar"))) "hello" (p (meta ((foo "zam"))) (meta) "there"))]) ;; 1.5 instead of "1.5" is deliberate: input may not yet be a true txexpr
(let ([x '(root (meta ((foo "bar"))) "hello" (p (meta ((foo "zam"))) (meta) "there" 1.5))])
(define-values (doc-without-metas metahash) (split-metas-to-hash x)) (define-values (doc-without-metas metahash) (split-metas-to-hash x))
(check-equal? doc-without-metas '(root "hello" (p "there"))) (check-equal? doc-without-metas '(root "hello" (p "there" 1.5)))
(check-equal? (hash-ref metahash 'foo) "zam")) (check-equal? (hash-ref metahash 'foo) "zam"))
(let ([x '(root (meta (foo "bar")) "hello" (p (meta (foo (zim "zam"))) (meta) "there"))]) (let ([x '(root (meta (foo "bar")) "hello" (p (meta (foo (zim "zam"))) (meta) "there" 1.5))])
(define-values (doc-without-metas metahash) (split-metas-to-hash x)) (define-values (doc-without-metas metahash) (split-metas-to-hash x))
(check-equal? doc-without-metas '(root "hello" (p "there"))) (check-equal? doc-without-metas '(root "hello" (p "there" 1.5)))
(check-equal? (hash-ref metahash 'foo) '(zim "zam")))) (check-equal? (hash-ref metahash 'foo) '(zim "zam"))))
Loading…
Cancel
Save