From 3ffe8c7f6b8f7e2026a2aee3477fb63fc33b33ff Mon Sep 17 00:00:00 2001 From: choueric Date: Wed, 22 Feb 2017 09:15:46 +0800 Subject: [PATCH 1/3] add extension to join lines --- inline.go | 4 ++++ markdown.go | 1 + 2 files changed, 5 insertions(+) diff --git a/inline.go b/inline.go index cb00ed6..c1f7475 100644 --- a/inline.go +++ b/inline.go @@ -170,6 +170,10 @@ func lineBreak(p *parser, out *bytes.Buffer, data []byte, offset int) int { precededByBackslash := offset >= 1 && data[offset-1] == '\\' // see http://spec.commonmark.org/0.18/#example-527 precededByBackslash = precededByBackslash && p.flags&EXTENSION_BACKSLASH_LINE_BREAK != 0 + if p.flags&EXTENSION_JOIN_LINES != 0 { + return 1 + } + // should there be a hard line break here? if p.flags&EXTENSION_HARD_LINE_BREAK == 0 && !precededByTwoSpaces && !precededByBackslash { return 0 diff --git a/markdown.go b/markdown.go index 58ba68d..09fc2d7 100644 --- a/markdown.go +++ b/markdown.go @@ -46,6 +46,7 @@ const ( EXTENSION_AUTO_HEADER_IDS // Create the header ID from the text EXTENSION_BACKSLASH_LINE_BREAK // translate trailing backslashes into line breaks EXTENSION_DEFINITION_LISTS // render definition lists + EXTENSION_JOIN_LINES // delete newline and join lines commonHtmlFlags = 0 | HTML_USE_XHTML | From 8098dab4eb9c32e0145407e1222b0e7c698f2d76 Mon Sep 17 00:00:00 2001 From: choueric Date: Wed, 22 Feb 2017 09:18:21 +0800 Subject: [PATCH 2/3] add testcase for joinLines extension --- markdown_test.go | 30 ++++++++++++++++++++++++++++++ testdata/zhJoinLines.text | 8 ++++++++ 2 files changed, 38 insertions(+) create mode 100644 testdata/zhJoinLines.text diff --git a/markdown_test.go b/markdown_test.go index d897644..34d5efe 100644 --- a/markdown_test.go +++ b/markdown_test.go @@ -14,6 +14,8 @@ package blackfriday import ( + "io/ioutil" + "os" "testing" ) @@ -73,3 +75,31 @@ func TestDocument(t *testing.T) { } doTests(t, tests) } + +func TestJoinLines(t *testing.T) { + result := `

标题

+ +

第一行文字。

+ +

第二行文字。

+` + + file, err := os.Open("testdata/zhJoinLines.text") + if err != nil { + t.Fatal(err) + } + defer file.Close() + + input, err := ioutil.ReadAll(file) + if err != nil { + t.Fatal(err) + } + + opt := Options{Extensions: commonExtensions | EXTENSION_JOIN_LINES} + renderer := HtmlRenderer(commonHtmlFlags, "", "") + output := MarkdownOptions(input, renderer, opt) + + if string(output) != result { + t.Error("output dose not match.") + } +} diff --git a/testdata/zhJoinLines.text b/testdata/zhJoinLines.text new file mode 100644 index 0000000..704ff27 --- /dev/null +++ b/testdata/zhJoinLines.text @@ -0,0 +1,8 @@ +# 标题 + +第一 +行文字。 + +第 +二 +行文字。 From f0bb45f44c5407a0c458d18e0f285126006e1ae7 Mon Sep 17 00:00:00 2001 From: choueric Date: Fri, 17 Mar 2017 17:21:30 +0800 Subject: [PATCH 3/3] modify testcase of joinLine. 1. delete testdata 'zhJoinLines.txt'. 2. move the testcase from markdown_test.go into block_test.go. --- block_test.go | 25 +++++++++++++++++++++++++ markdown_test.go | 30 ------------------------------ testdata/zhJoinLines.text | 8 -------- 3 files changed, 25 insertions(+), 38 deletions(-) delete mode 100644 testdata/zhJoinLines.text diff --git a/block_test.go b/block_test.go index 62d9a47..83ed2e1 100644 --- a/block_test.go +++ b/block_test.go @@ -1713,3 +1713,28 @@ func TestIsFenceLine(t *testing.T) { } } } + +func TestJoinLines(t *testing.T) { + input := `# 标题 + +第一 +行文字。 + +第 +二 +行文字。 +` + result := `

标题

+ +

第一行文字。

+ +

第二行文字。

+` + opt := Options{Extensions: commonExtensions | EXTENSION_JOIN_LINES} + renderer := HtmlRenderer(commonHtmlFlags, "", "") + output := MarkdownOptions([]byte(input), renderer, opt) + + if string(output) != result { + t.Error("output dose not match.") + } +} diff --git a/markdown_test.go b/markdown_test.go index 34d5efe..d897644 100644 --- a/markdown_test.go +++ b/markdown_test.go @@ -14,8 +14,6 @@ package blackfriday import ( - "io/ioutil" - "os" "testing" ) @@ -75,31 +73,3 @@ func TestDocument(t *testing.T) { } doTests(t, tests) } - -func TestJoinLines(t *testing.T) { - result := `

标题

- -

第一行文字。

- -

第二行文字。

-` - - file, err := os.Open("testdata/zhJoinLines.text") - if err != nil { - t.Fatal(err) - } - defer file.Close() - - input, err := ioutil.ReadAll(file) - if err != nil { - t.Fatal(err) - } - - opt := Options{Extensions: commonExtensions | EXTENSION_JOIN_LINES} - renderer := HtmlRenderer(commonHtmlFlags, "", "") - output := MarkdownOptions(input, renderer, opt) - - if string(output) != result { - t.Error("output dose not match.") - } -} diff --git a/testdata/zhJoinLines.text b/testdata/zhJoinLines.text deleted file mode 100644 index 704ff27..0000000 --- a/testdata/zhJoinLines.text +++ /dev/null @@ -1,8 +0,0 @@ -# 标题 - -第一 -行文字。 - -第 -二 -行文字。