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

Remove calls to Truncate() from linebreak parser

Replace output truncation with appropriate inline callbacks. lineBreak()
is now only responsible for handling HardLineBreak. BackslashLineBreak
is handled in escape() and trailing whitespace is considered in
maybeLineBreak().
This commit is contained in:
Vytautas Šaltenis 2015-10-27 21:33:43 +02:00
parent ef087889f4
commit 8e90e8b645
2 changed files with 25 additions and 23 deletions

View File

@ -155,31 +155,28 @@ func codeSpan(p *parser, out *bytes.Buffer, data []byte, offset int) int {
}
// newline preceded by two spaces becomes <br>
// newline without two spaces works when EXTENSION_HARD_LINE_BREAK is enabled
func lineBreak(p *parser, out *bytes.Buffer, data []byte, offset int) int {
// remove trailing spaces from out
outBytes := out.Bytes()
end := len(outBytes)
eol := end
for eol > 0 && outBytes[eol-1] == ' ' {
eol--
func maybeLineBreak(p *parser, out *bytes.Buffer, data []byte, offset int) int {
origOffset := offset
for offset < len(data) && data[offset] == ' ' {
offset++
}
if offset < len(data) && data[offset] == '\n' {
if offset-origOffset >= 2 {
p.r.LineBreak(out)
return offset - origOffset + 1
}
return offset - origOffset
}
out.Truncate(eol)
precededByTwoSpaces := offset >= 2 && data[offset-2] == ' ' && data[offset-1] == ' '
precededByBackslash := offset >= 1 && data[offset-1] == '\\' // see http://spec.commonmark.org/0.18/#example-527
precededByBackslash = precededByBackslash && p.flags&BackslashLineBreak != 0
// should there be a hard line break here?
if p.flags&HardLineBreak == 0 && !precededByTwoSpaces && !precededByBackslash {
return 0
}
}
if precededByBackslash && eol > 0 {
out.Truncate(eol - 1)
}
// newline without two spaces works when HardLineBreak is enabled
func lineBreak(p *parser, out *bytes.Buffer, data []byte, offset int) int {
if p.flags&HardLineBreak != 0 {
p.r.LineBreak(out)
return 1
}
return 0
}
type linkType int
@ -615,6 +612,10 @@ func escape(p *parser, out *bytes.Buffer, data []byte, offset int) int {
data = data[offset:]
if len(data) > 1 {
if p.flags&BackslashLineBreak != 0 && data[1] == '\n' {
p.r.LineBreak(out)
return 2
}
if bytes.IndexByte(escapeChars, data[1]) < 0 {
return 0
}

View File

@ -376,6 +376,7 @@ func MarkdownOptions(input []byte, renderer Renderer, opts Options) []byte {
p.inlineCallback['&'] = entity
p.inlineCallback['!'] = maybeImage
p.inlineCallback['^'] = maybeInlineFootnote
p.inlineCallback[' '] = maybeLineBreak
if extensions&Autolink != 0 {
p.inlineCallback[':'] = autoLink