1
0
mirror of https://github.com/danog/blackfriday.git synced 2024-11-27 04:24:41 +01:00

Merge pull request #149 from tw4452852/fenced_code

Delete unnecessary copy of input when enable fenced code extension
This commit is contained in:
Vytautas Šaltenis 2015-02-11 10:22:51 +02:00
commit 77efab57b2
2 changed files with 21 additions and 13 deletions

View File

@ -556,9 +556,12 @@ func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (s
skip = 0 skip = 0
// skip up to three spaces // skip up to three spaces
for i < 3 && data[i] == ' ' { for i < len(data) && i < 3 && data[i] == ' ' {
i++ i++
} }
if i >= len(data) {
return
}
// check for the marker characters: ~ or ` // check for the marker characters: ~ or `
if data[i] != '~' && data[i] != '`' { if data[i] != '~' && data[i] != '`' {
@ -568,11 +571,15 @@ func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (s
c := data[i] c := data[i]
// the whole line must be the same char or whitespace // the whole line must be the same char or whitespace
for data[i] == c { for i < len(data) && data[i] == c {
size++ size++
i++ i++
} }
if i >= len(data) {
return
}
// the marker char must occur at least 3 times // the marker char must occur at least 3 times
if size < 3 { if size < 3 {
return return
@ -587,22 +594,26 @@ func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (s
if syntax != nil { if syntax != nil {
syn := 0 syn := 0
for data[i] == ' ' { for i < len(data) && data[i] == ' ' {
i++ i++
} }
if i >= len(data) {
return
}
syntaxStart := i syntaxStart := i
if data[i] == '{' { if data[i] == '{' {
i++ i++
syntaxStart++ syntaxStart++
for data[i] != '}' && data[i] != '\n' { for i < len(data) && data[i] != '}' && data[i] != '\n' {
syn++ syn++
i++ i++
} }
if data[i] != '}' { if i >= len(data) || data[i] != '}' {
return return
} }
@ -619,7 +630,7 @@ func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (s
i++ i++
} else { } else {
for !isspace(data[i]) { for i < len(data) && !isspace(data[i]) {
syn++ syn++
i++ i++
} }
@ -629,10 +640,10 @@ func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (s
*syntax = &language *syntax = &language
} }
for data[i] == ' ' { for i < len(data) && data[i] == ' ' {
i++ i++
} }
if data[i] != '\n' { if i >= len(data) || data[i] != '\n' {
return return
} }
@ -661,7 +672,7 @@ func (p *parser) fencedCode(out *bytes.Buffer, data []byte, doRender bool) int {
// copy the current line // copy the current line
end := beg end := beg
for data[end] != '\n' { for end < len(data) && data[end] != '\n' {
end++ end++
} }
end++ end++

View File

@ -327,10 +327,7 @@ func firstPass(p *parser, input []byte) []byte {
if p.flags&EXTENSION_FENCED_CODE != 0 { if p.flags&EXTENSION_FENCED_CODE != 0 {
// when last line was none blank and a fenced code block comes after // when last line was none blank and a fenced code block comes after
if beg >= lastFencedCodeBlockEnd { if beg >= lastFencedCodeBlockEnd {
// tmp var so we don't modify beyond bounds of `input` if i := p.fencedCode(&out, input[beg:], false); i > 0 {
var tmp = make([]byte, len(input[beg:]), len(input[beg:])+1)
copy(tmp, input[beg:])
if i := p.fencedCode(&out, append(tmp, '\n'), false); i > 0 {
if !lastLineWasBlank { if !lastLineWasBlank {
out.WriteByte('\n') // need to inject additional linebreak out.WriteByte('\n') // need to inject additional linebreak
} }