From 5d3d5c198ebf779798f95e73e990ecc271db3735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytautas=20=C5=A0altenis?= Date: Sun, 11 Oct 2015 11:01:48 +0300 Subject: [PATCH 1/2] Handle comments within a block Added test cases both for inline and block workflows. Closes #136. --- block.go | 19 +------------------ block_test.go | 14 ++++++++++++-- inline.go | 23 ++++++++++++++++++++++- inline_test.go | 22 ++++++++++++++++++++++ 4 files changed, 57 insertions(+), 21 deletions(-) diff --git a/block.go b/block.go index 1f300b3..768c40b 100644 --- a/block.go +++ b/block.go @@ -399,23 +399,7 @@ func (p *parser) html(out *bytes.Buffer, data []byte, doRender bool) int { // HTML comment, lax form func (p *parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool) int { - if data[0] != '<' || data[1] != '!' || data[2] != '-' || data[3] != '-' { - return 0 - } - - i := 5 - - // scan for an end-of-comment marker, across lines if necessary - for i < len(data) && !(data[i-2] == '-' && data[i-1] == '-' && data[i] == '>') { - i++ - } - i++ - - // no end-of-comment marker - if i >= len(data) { - return 0 - } - + i := p.inlineHtmlComment(out, data) // needs to end with a blank line if j := p.isEmpty(data[i:]); j > 0 { size := i + j @@ -429,7 +413,6 @@ func (p *parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool) int } return size } - return 0 } diff --git a/block_test.go b/block_test.go index f52506f..86089e7 100644 --- a/block_test.go +++ b/block_test.go @@ -1401,7 +1401,17 @@ func TestTitleBlock_EXTENSION_TITLEBLOCK(t *testing.T) { "Yep, more here too\n" + "", } - doTestsBlock(t, tests, EXTENSION_TITLEBLOCK) - +} + +func TestBlockComments(t *testing.T) { + var tests = []string{ + "Some text\n\n\n", + "

Some text

\n\n\n", + "Some text\n\n\n", + "

Some text

\n\n\n", + "Some text\n\n\n", + "

Some text

\n\n\n", + } + doTestsBlock(t, tests, 0) } diff --git a/inline.go b/inline.go index 3f39b52..853bbf8 100644 --- a/inline.go +++ b/inline.go @@ -547,12 +547,33 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int { return i } +func (p *parser) inlineHtmlComment(out *bytes.Buffer, data []byte) int { + if len(data) < 5 { + return 0 + } + if data[0] != '<' || data[1] != '!' || data[2] != '-' || data[3] != '-' { + return 0 + } + i := 5 + // scan for an end-of-comment marker, across lines if necessary + for i < len(data) && !(data[i-2] == '-' && data[i-1] == '-' && data[i] == '>') { + i++ + } + // no end-of-comment marker + if i >= len(data) { + return 0 + } + return i + 1 +} + // '<' when tags or autolinks are allowed func leftAngle(p *parser, out *bytes.Buffer, data []byte, offset int) int { data = data[offset:] altype := LINK_TYPE_NOT_AUTOLINK end := tagLength(data, &altype) - + if size := p.inlineHtmlComment(out, data); size > 0 { + end = size + } if end > 2 { if altype != LINK_TYPE_NOT_AUTOLINK { var uLink bytes.Buffer diff --git a/inline_test.go b/inline_test.go index 3821d49..72fbf8d 100644 --- a/inline_test.go +++ b/inline_test.go @@ -973,6 +973,28 @@ func TestFootnotesWithParameters(t *testing.T) { doTestsInlineParam(t, tests, Options{Extensions: EXTENSION_FOOTNOTES}, HTML_FOOTNOTE_RETURN_LINKS, params) } +func TestInlineComments(t *testing.T) { + var tests = []string{ + "Hello \n", + "

Hello

\n", + "Hello ", + "

Hello

\n", + "Hello \n", + "

Hello

\n", + "Hello \na", + "

Hello \na

\n", + "* list \n", + "\n", + " comment\n", + "

comment

\n", + "blahblah\n\nrhubarb\n", + "

blahblah\n\nrhubarb

\n", + } + doTestsInlineParam(t, tests, Options{}, HTML_USE_SMARTYPANTS, HtmlRendererParameters{}) +} + func TestSmartDoubleQuotes(t *testing.T) { var tests = []string{ "this should be normal \"quoted\" text.\n", From c9f5708bd5bc263527a4893542d3a0668a19a065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytautas=20=C5=A0altenis?= Date: Tue, 20 Oct 2015 20:31:08 +0300 Subject: [PATCH 2/2] Spread out test cases for readability --- block_test.go | 2 ++ inline_test.go | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/block_test.go b/block_test.go index 86089e7..a78a7d5 100644 --- a/block_test.go +++ b/block_test.go @@ -1408,8 +1408,10 @@ func TestBlockComments(t *testing.T) { var tests = []string{ "Some text\n\n\n", "

Some text

\n\n\n", + "Some text\n\n\n", "

Some text

\n\n\n", + "Some text\n\n\n", "

Some text

\n\n\n", } diff --git a/inline_test.go b/inline_test.go index 72fbf8d..ff0e489 100644 --- a/inline_test.go +++ b/inline_test.go @@ -977,18 +977,25 @@ func TestInlineComments(t *testing.T) { var tests = []string{ "Hello \n", "

Hello

\n", + "Hello ", "

Hello

\n", + "Hello \n", "

Hello

\n", + "Hello \na", "

Hello \na

\n", + "* list \n", "\n", + " comment\n", "

comment

\n", + "blahblah\n\nrhubarb\n", "

blahblah\n\nrhubarb

\n", }