add hardbreak and tests

This commit is contained in:
Erik Edrosa 2016-05-24 23:25:51 -04:00
parent 71d6d35722
commit b0e2b60d8c
3 changed files with 175 additions and 7 deletions

View File

@ -27,7 +27,8 @@ TESTS = \
tests/inlines/code-spans.scm \
tests/inlines/emphasis.scm \
tests/inlines/backslash-escape.scm \
tests/inlines/softbreak.scm
tests/inlines/softbreak.scm \
tests/inlines/hardbreak.scm
TEST_EXTENSIONS = .scm

View File

@ -215,13 +215,12 @@
(define ascii-punc-set (string->char-set ascii-punctuation-characters))
(char-set-contains? ascii-punc-set ch))
(define (blank-trailing-space? node)
(define* (blank-trailing-space? node #:optional (offset 1))
(let ((str (last-child node)))
(case (string-ref str (- (string-length str) 1))
(case (string-ref str (- (string-length str) offset))
((#\space) #t)
(else #f))))
(define (remove-trailing-space nodes)
(let ((str (last-child (car nodes))))
(cons (make-text-node (string-trim-right str #\space))
@ -231,7 +230,10 @@
(let ((new-text (text-advance-skip (text-advance text 1) #\space)))
(if (and (not (null? nodes)) (text-node? (car nodes)) (blank-trailing-space? (car nodes)))
(parse-char new-text
(cons (make-softbreak-node) (remove-trailing-space nodes))
(cons (if (blank-trailing-space? (car nodes) 2)
(make-hardbreak-node)
(make-softbreak-node))
(remove-trailing-space nodes))
delim-stack nodes-stack)
(parse-char new-text
(cons (make-softbreak-node) nodes)
@ -241,7 +243,7 @@
(let* ((next-ch-text (text-advance text 1))
(next-ch (and (not (text-end? next-ch-text)) (text-char next-ch-text))))
(cond ((eq? next-ch #\newline)
(parse-char (text-advance next-ch-text 1)
(parse-char (text-advance-skip (text-advance next-ch-text 1) #\space)
(cons (make-hardbreak-node) nodes)
delim-stack nodes-stack))
((and next-ch (ascii-punctuation-characters? next-ch))
@ -274,7 +276,7 @@
(define (pop-remaining-delim nodes delim-stack nodes-stack)
(if (null? delim-stack)
nodes
(if (text-node? (car nodes)) (remove-trailing-space nodes) nodes)
(pop-remaining-delim (append nodes (cons (delim->text (car delim-stack)) (car nodes-stack)))
(cdr delim-stack)
(cdr nodes-stack))))

165
tests/inlines/hardbreak.scm Normal file
View File

@ -0,0 +1,165 @@
;; Copyright (C) 2016 Erik Edrosa <erik.edrosa@gmail.com>
;;
;; This file is part of guile-commonmark
;;
;; guile-commonmark is free software: you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public License
;; as published by the Free Software Foundation, either version 3 of
;; the License, or (at your option) any later version.
;;
;; guile-commonmark is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public License
;; along with guile-commonmark. If not, see <http://www.gnu.org/licenses/>.
(define-module (test-inlines hardbreak)
#:use-module (srfi srfi-64)
#:use-module (ice-9 match)
#:use-module (commonmark inlines)
#:use-module (commonmark node))
(test-begin "inlines hardbreak")
(define (make-paragraph text)
(make-node 'document #f
(list (make-node 'paragraph #f
(list (make-node 'text #f (list text)))))))
(test-assert "parse-inlines, A line break that is preceded by two or more spaces
and does not occur at the end of a block is parsed as a hard line break"
(match (parse-inlines (make-paragraph "foo \nbaz"))
(('document doc-data
('paragraph para-data
('text text-data "baz")
('hardbreak break-data)
('text text-data "foo")))
#t)
(x (pk 'fail x #f))))
(test-assert "parse-inlines, a backslash before the line ending may be used
instead of two spaces"
(match (parse-inlines (make-paragraph "foo\\\nbaz"))
(('document doc-data
('paragraph para-data
('text text-data "baz")
('hardbreak break-data)
('text text-data "foo")))
#t)
(x (pk 'fail x #f))))
(test-assert "parse-inlines, hardbreak more than two spaces can be used"
(match (parse-inlines (make-paragraph "foo \nbaz"))
(('document doc-data
('paragraph para-data
('text text-data "baz")
('hardbreak break-data)
('text text-data "foo")))
#t)
(x (pk 'fail x #f))))
(test-assert "parse-inlines, hardbreak leading spaces at the beginning of the next
are ignored"
(match (parse-inlines (make-paragraph "foo \n bar"))
(('document doc-data
('paragraph para-data
('text text-data "bar")
('hardbreak break-data)
('text text-data "foo")))
#t)
(x (pk 'fail x #f))))
(test-assert "parse-inlines, hardbreak leading spaces at the beginning of the next
are ignored"
(match (parse-inlines (make-paragraph "foo\\\n bar"))
(('document doc-data
('paragraph para-data
('text text-data "bar")
('hardbreak break-data)
('text text-data "foo")))
#t)
(x (pk 'fail x #f))))
(test-assert "parse-inlines, line breaks can occur inside emphasis, links, and other
constructs that allow inline content"
(match (parse-inlines (make-paragraph "*foo \nbar*"))
(('document doc-data
('paragraph para-data
('emphasis emphasis-data
('text text-data "bar")
('hardbreak break-data)
('text text-data "foo"))))
#t)
(x (pk 'fail x #f))))
(test-assert "parse-inlines, line breaks can occur inside emphasis, links, and other
constructs that allow inline content"
(match (parse-inlines (make-paragraph "*foo\\\nbar*"))
(('document doc-data
('paragraph para-data
('emphasis emphasis-data
('text text-data "bar")
('hardbreak break-data)
('text text-data "foo"))))
#t)
(x (pk 'fail x #f))))
(test-assert "parse-inlines, line breaks do not occur inside code spans"
(match (parse-inlines (make-paragraph "`code \nspan`"))
(('document doc-data
('paragraph para-data
('code-span code-data "code span")))
#t)
(x (pk 'fail x #f))))
(test-assert "parse-inlines, line breaks do not occur inside code spans"
(match (parse-inlines (make-paragraph "`code\\\nspan`"))
(('document doc-data
('paragraph para-data
('code-span code-data "code\\ span")))
#t)
(x (pk 'fail x #f))))
(test-expect-fail 2)
(test-assert "parse-inlines, line breaks do not occur inside html tags"
(match (parse-inlines (make-paragraph "<a href=\"foo \nbar\">"))
(('document doc-data
('paragraph para-data
('code-span code-data "code span")))
#t)
(x (pk 'fail x #f))))
(test-assert "parse-inlines, line breaks do not occur inside html tags"
(match (parse-inlines (make-paragraph "<a href=\"foo\\\nbar\">"))
(('document doc-data
('paragraph para-data
('code-span code-data "code\\ span")))
#t)
(x (pk 'fail x #f))))
(test-assert "parse-inlines, hard line breaks do not work at the end of a paragraph or other
block element"
(match (parse-inlines (make-paragraph "foo\\"))
(('document doc-data
('paragraph para-data
('text text-data "\\")
('text text-data "foo")))
#t)
(x (pk 'fail x #f))))
(test-assert "parse-inlines, hard line breaks do not work at the end of a paragraph or other
block element"
(match (parse-inlines (make-paragraph "foo "))
(('document doc-data
('paragraph para-data
('text text-data "foo")))
#t)
(x (pk 'fail x #f))))
(test-end)
(exit (= (test-runner-fail-count (test-runner-current)) 0))