correct `merge-newlines` handling of empty input

pull/108/merge
Matthew Butterick 9 years ago
parent 27b783e6ba
commit a8231bc83f

@ -365,12 +365,10 @@
(define paragraph-pattern (pregexp (format "^~a+$" sep))) (define paragraph-pattern (pregexp (format "^~a+$" sep)))
(and (string? x) (regexp-match paragraph-pattern x))) (and (string? x) (regexp-match paragraph-pattern x)))
;; Find adjacent newline characters in a list and merge them into one item ;; Find adjacent newline characters in a list and merge them into one item
;; Scribble, by default, makes each newline a separate list item. ;; Scribble, by default, makes each newline a separate list item.
(define+provide/contract (merge-newlines x) (define+provide/contract (merge-newlines x)
(txexpr-elements? . -> . txexpr-elements?) (txexpr-elements? . -> . txexpr-elements?)
(define (newlines? x) (define (newlines? x)
(and (string? x) (and (string? x)
(let ([newline-pat (regexp (format "^~a+$" (world:current-newline)))]) (let ([newline-pat (regexp (format "^~a+$" (world:current-newline)))])
@ -382,12 +380,14 @@
xs)) xs))
(let loop ([x x]) (let loop ([x x])
(if (list? x) (if (pair? x)
(let ([xs (map loop x)]) (let ([xs (map loop x)])
(append-map merge-if-newlines (slicef xs newlines?))) (append-map merge-if-newlines (slicef xs newlines?)))
x))) x)))
(module-test-external (module-test-external
(require racket/list)
(check-equal? (merge-newlines empty) empty)
(check-equal? (merge-newlines '(p "\n" "\n" "foo" "\n" "\n\n" "bar" (em "\n" "\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")))) '(p "\n\n" "foo" "\n\n\n" "bar" (em "\n\n\n"))))

Loading…
Cancel
Save