Skip to content
This repository has been archived by the owner on Nov 10, 2020. It is now read-only.

Commit

Permalink
Patch blackfriday's fix for issue 122 in
Browse files Browse the repository at this point in the history
To be more inline with black friday I removed from CommonMark-isms
(fenced code blocks at the end of the file). Updated the tests
and manual patched stuff in.
  • Loading branch information
miekg committed Oct 31, 2015
1 parent d91e12f commit 4187b26
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 52 deletions.
50 changes: 11 additions & 39 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package mmark
import (
"bytes"
"strconv"
"strings"
"unicode"
)

Expand Down Expand Up @@ -1042,44 +1041,32 @@ func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (s

c := data[i]

// the whole line must be the same char per CommonMark whitespace is not allowed
// the whole line must be the same char or whitespace
for i < len(data) && data[i] == c {
size++
i++
}
if i >= len(data) {
return
}
// if we find spaces and them some more markers, this is not a fenced code block
j := i
for j < len(data) && data[j] == ' ' {
j++
}
if j >= len(data) {
return
}
if data[j] == c {
return
}

// the marker char must occur at least 3 times
if size < 3 {
return
}

marker = string(data[i-size : i])

// if this is the end marker, it must be at least as long as the
// original marker we have
if oldmarker != "" && !strings.HasPrefix(marker, oldmarker) {
// if this is the end marker, it must match the beginning marker
if oldmarker != "" && marker != oldmarker {
return
}

if syntax != nil {
syn := 0

for i < len(data) && data[i] == ' ' {
i++
}
i = skipChar(data, i, ' ')

if i >= len(data) {
return
}
Expand Down Expand Up @@ -1122,9 +1109,9 @@ func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (s
*syntax = &language
}

// CommonMark: skip garbage until end of line
for i < len(data) && data[i] != '\n' {
i++
i = skipChar(data, i, ' ')
if i >= len(data) || data[i] != '\n' {
return
}

skip = i + 1
Expand Down Expand Up @@ -1156,7 +1143,7 @@ func (p *parser) isTOMLBlockBlock(data []byte) int {
func (p *parser) fencedCode(out *bytes.Buffer, data []byte, doRender bool) int {
var lang *string
beg, marker := p.isFencedCode(data, &lang, "")
if beg == 0 {
if beg == 0 || beg >= len(data) {
return 0
}

Expand All @@ -1166,18 +1153,6 @@ func (p *parser) fencedCode(out *bytes.Buffer, data []byte, doRender bool) int {
co = p.ial.Value("callout")
}

if beg >= len(data) {
// only the marker and end of doc. CommonMark dictates this is valid

p.r.SetInlineAttr(p.ial)
p.ial = nil

// Data here?
p.r.BlockCode(out, nil, "", nil, p.insideFigure, false)

return len(data)
}

// CommonMark: if indented strip this many leading spaces from code block
indent := 0
for indent < beg && data[indent] == ' ' {
Expand All @@ -1204,11 +1179,8 @@ func (p *parser) fencedCode(out *bytes.Buffer, data []byte, doRender bool) int {
end++

// did we reach the end of the buffer without a closing marker?
// CommonMark: end of buffer closes fencec code block
if end >= len(data) {
work.Write(data[beg:])
beg = end
break
return 0
}

// CommmonMark, strip beginning spaces
Expand Down
24 changes: 12 additions & 12 deletions block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,25 +836,25 @@ func TestFencedCodeBlock(t *testing.T) {
"<pre><code class=\"language-python\">extra\n</code></pre>\n",

"~~~ perl\nthree to start, four to end\n~~~~\n",
"<pre><code class=\"language-perl\">three to start, four to end\n</code></pre>\n",
"<p>~~~ perl\nthree to start, four to end\n~~~~</p>\n",

"~~~~ perl\nfour to start, three to end\n~~~\n",
"<pre><code class=\"language-perl\">four to start, three to end\n~~~\n</code></pre>\n",
"<p>~~~~ perl\nfour to start, three to end\n~~~</p>\n",

"~~~ bash\ntildes\n~~~\n",
"<pre><code class=\"language-bash\">tildes\n</code></pre>\n",

"``` lisp\nno ending\n",
"<pre><code class=\"language-lisp\">no ending\n</code></pre>\n",
"<p>``` lisp\nno ending</p>\n",

"~~~ lisp\nend with language\n~~~ lisp\n",
"<pre><code class=\"language-lisp\">end with language\n</code></pre>\n",
"<p>~~~ lisp\nend with language\n~~~ lisp</p>\n",

"```\nmismatched begin and end\n~~~\n",
"<pre><code>mismatched begin and end\n~~~\n</code></pre>\n",
"<p>```\nmismatched begin and end\n~~~</p>\n",

"~~~\nmismatched begin and end\n```\n",
"<pre><code>mismatched begin and end\n```\n</code></pre>\n",
"<p>~~~\nmismatched begin and end\n```</p>\n",

" ``` oz\nleading spaces\n```\n",
"<pre><code class=\"language-oz\">leading spaces\n</code></pre>\n",
Expand Down Expand Up @@ -1221,25 +1221,25 @@ func TestFencedCodeBlock_EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK(t *testing.T) {
"<pre><code class=\"language-python\">extra\n</code></pre>\n",

"~~~ perl\nthree to start, four to end\n~~~~\n",
"<pre><code class=\"language-perl\">three to start, four to end\n</code></pre>\n",
"<p>~~~ perl\nthree to start, four to end\n~~~~</p>\n",

"~~~~ perl\nfour to start, three to end\n~~~\n",
"<pre><code class=\"language-perl\">four to start, three to end\n~~~\n</code></pre>\n",
"<p>~~~~ perl\nfour to start, three to end\n~~~</p>\n",

"~~~ bash\ntildes\n~~~\n",
"<pre><code class=\"language-bash\">tildes\n</code></pre>\n",

"``` lisp\nno ending\n",
"<pre><code class=\"language-lisp\">no ending\n</code></pre>\n",
"<p>``` lisp\nno ending</p>\n",

"~~~ lisp\nend with language\n~~~ lisp\n",
"<pre><code class=\"language-lisp\">end with language\n</code></pre>\n",
"<p>~~~ lisp\nend with language\n~~~ lisp</p>\n",

"```\nmismatched begin and end\n~~~\n",
"<pre><code>mismatched begin and end\n~~~\n</code></pre>\n",
"<p>```\nmismatched begin and end\n~~~</p>\n",

"~~~\nmismatched begin and end\n```\n",
"<pre><code>mismatched begin and end\n```\n</code></pre>\n",
"<p>~~~\nmismatched begin and end\n```</p>\n",

" ``` oz\nleading spaces\n```\n",
"<pre><code class=\"language-oz\">leading spaces\n</code></pre>\n",
Expand Down
4 changes: 3 additions & 1 deletion cm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ func TestFencedCodeBlockCommonMark_81(t *testing.T) {
"```\n bbb\nbbb\n```\n",
"<pre><code> bbb\nbbb\n</code></pre>\n",

// disabled this behavior, using blackfriday's standard now.
"```\nbbb\nbbb\n",
"<pre><code>bbb\nbbb\n</code></pre>\n",
//"<pre><code>bbb\nbbb\n</code></pre>\n",
"<p>```\nbbb\nbbb</p>\n",

"~~~~ ruby\ndef foo(x)\n return 3\nend\n~~~~\n",
"<pre><code class=\"language-ruby\">def foo(x)\n return 3\nend\n</code></pre>\n",
Expand Down
8 changes: 8 additions & 0 deletions html.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,14 @@ func skipSpace(tag []byte, i int) int {
return i
}

func skipChar(data []byte, start int, char byte) int {
i := start
for i < len(data) && data[i] == char {
i++
}
return i
}

func doubleSpace(out *bytes.Buffer) {
if out.Len() > 0 {
out.WriteByte('\n')
Expand Down

0 comments on commit 4187b26

Please sign in to comment.