fix some defective logic with handling empty strings

pull/111/head
Matthew Butterick 9 years ago
parent 0879347cde
commit d6332dfe78

@ -1,13 +1,9 @@
#lang racket/base
(require xml txexpr racket/list sugar/list sugar/define sugar/test)
(require "setup.rkt"
"private/whitespace.rkt")
(require "setup.rkt" "private/splice.rkt")
(require "unstable/typography.rkt")
(provide (all-from-out "unstable/typography.rkt")) ; bw compat
(require "private/whitespace.rkt")
(provide (all-from-out "private/whitespace.rkt")) ; bw compat
(provide (all-from-out "unstable/typography.rkt")) ; bw compat, includes `whitespace?`
(define (->list/tx x)
;; same as ->list but catches special case of single txexpr,
@ -170,7 +166,7 @@
xs))
(define not-empty-string? (λ(x) (not (and (string? x) (= (string-length x) 0)))))
(let loop ([x x])
(if (pair? x)
(if (and (pair? x) (not (attrs? x)))
(let ([xs (map loop (filter not-empty-string? x))])
(append-map merge-if-newlines (slicef xs newlines?)))
x)))
@ -178,7 +174,8 @@
(module-test-external
(require racket/list)
(check-equal? (merge-newlines empty) empty)
(check-equal? (merge-newlines '(p "\n" "" "\n")) '(p "\n\n"))
(check-equal? (merge-newlines '((p ((id "")) "\n" "" "\n"))) '((p ((id "")) "\n\n")))
(check-equal? (merge-newlines '((p "\n" "" "\n"))) '((p "\n\n")))
(check-equal? (merge-newlines '(p "\n" "\n" "foo" "\n" "\n\n" "bar" (em "\n" "\n" "\n")))
'(p "\n\n" "foo" "\n\n\n" "bar" (em "\n\n\n"))))

@ -4,12 +4,21 @@
;; (string->symbol (format "~a" #\u200B))
(define splice-signal-tag '@)
(define (attrs? x)
(and (list? x)
(andmap (λ(xi)
(and (list? xi)
(= (length xi) 2)
(symbol? (car xi))
(string? (cadr xi)))) x)))
(define (splice x [splicing-tag splice-signal-tag])
; (listof txexpr-elements?) . -> . (listof txexpr-elements?))
(define spliceable? (λ(x) (and (pair? x) (eq? (car x) splicing-tag))))
(define not-null-string? (λ(x) (not (and (string? x) (= (string-length x) 0)))))
(let loop ([x x])
(if (list? x)
(if (and (list? x) (not (attrs? x))) ; don't splice null strings inside attrs
(apply append (map (λ(x) ((if (spliceable? x)
cdr
list) (loop x))) (filter not-null-string? x)))
@ -22,7 +31,8 @@
(check-equal? (splice `((,splice-signal-tag 1 (,splice-signal-tag 2 "" (,splice-signal-tag 3 (div 4 (,splice-signal-tag 5))) 6) "" 7)))
'(1 2 3 (div 4 5) 6 7))
(check-equal? (splice `((,splice-signal-tag "foo" "" "bar"))) '("foo" "bar"))
(check-equal? (splice null) null))
(check-equal? (splice null) null)
(check-equal? (splice '(a ((href "")(foo "bar")) "zam")) '(a ((href "")(foo "bar")) "zam")))
(define (strip-empty-attrs x)

@ -1 +1 @@
1455676575
1455733334

Loading…
Cancel
Save