From d4c6f6b0ec1e3548655136270bc09c01059dbb1f Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 5 May 2015 17:37:23 -0700 Subject: [PATCH] correct `detect-paragraphs` behavior with series of blocks --- decode.rkt | 13 +++++-------- tests/tests-decode.rkt | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/decode.rkt b/decode.rkt index 2000d45..852dcf8 100644 --- a/decode.rkt +++ b/decode.rkt @@ -342,18 +342,15 @@ (define (prep-paragraph-flow xc) (linebreak-proc (merge-newlines (trimf xc whitespace?)))) - (define my-paragraph-break? (λ(x) (and (paragraph-break? x #:separator sep) #t))) (define (wrap-paragraph xc) (match xc - [(list (? block-txexpr? bx)) bx] ; leave a single block xexpr alone - [else (make-txexpr tag empty xc)])) ; otherwise wrap in p tag - + [(list (? block-txexpr? bxs) ...) bxs] ; leave a series of block xexprs alone + [else (list (make-txexpr tag empty xc))])) ; otherwise wrap in p tag (let ([elements (prep-paragraph-flow elements)]) (if (ormap my-paragraph-break? elements) ; need this condition to prevent infinite recursion - (map wrap-paragraph (filter-split elements my-paragraph-break?)) ; split into ¶¶ - elements))) - - + ;; use append-map rather than map to permit return of multiple elements + (append-map wrap-paragraph (filter-split elements my-paragraph-break?)) ; split into ¶¶ + elements))) diff --git a/tests/tests-decode.rkt b/tests/tests-decode.rkt index 7fed125..f3f6aa8 100644 --- a/tests/tests-decode.rkt +++ b/tests/tests-decode.rkt @@ -36,8 +36,23 @@ (check-equal? (detect-linebreaks '("foo" "moo" "bar") #:insert "moo") '("foo" "moo" "bar")) (check-equal? (detect-linebreaks '("foo" "\n\n" "bar")) '("foo" "\n\n" "bar")) - - +(check-equal? (detect-paragraphs '("First para" "\n\n" "Second para")) + '((p "First para") (p "Second para"))) +(check-equal? (detect-paragraphs '("First para" "\n\n" "Second para" "\n" "Second line")) + '((p "First para") (p "Second para" (br) "Second line"))) +(check-equal? (detect-paragraphs '("First para" "\n\n" (div "Second block"))) + '((p "First para") (div "Second block"))) +(check-equal? (detect-paragraphs '((div "First block") "\n\n" (div "Second block"))) + '((div "First block") (div "Second block"))) +(check-equal? (detect-paragraphs '("First para" "\n\n" "Second para") #:tag 'ns:p) + '((ns:p "First para") (ns:p "Second para"))) +(check-equal? (detect-paragraphs '("First para" "\n\n" "Second para" "\n" "Second line") + #:linebreak-proc (λ(x) (detect-linebreaks x #:insert '(newline)))) + '((p "First para") (p "Second para" (newline) "Second line"))) +(check-equal? (detect-paragraphs '("foo" "\n\n" (div "bar") (div "zam"))) + '((p "foo") (div "bar") (div "zam"))) +(check-equal? (detect-paragraphs '("foo" "\n\n" (div "bar") "\n\n" (div "zam"))) + '((p "foo") (div "bar") (div "zam"))) (check-equal? (merge-newlines '(p "\n" "foo" "\n" "\n" "bar" (em "\n" "\n" "\n"))) '(p "\n" "foo" "\n\n" "bar" (em "\n\n\n")))