all repos — grayfriday @ 8e90e8b6455f1608d6a0948d0eb07409bcf874d4

blackfriday fork with a few changes

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().
Vytautas Ĺ altenis vytas@rtfb.lt
Tue, 27 Oct 2015 21:33:43 +0200
commit

8e90e8b6455f1608d6a0948d0eb07409bcf874d4

parent

ef087889f49d63f4ada853a7b255072f85ec970f

2 files changed, 23 insertions(+), 21 deletions(-)

jump to
M inline.goinline.go

@@ -155,31 +155,28 @@

} // 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++ } - 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 offset < len(data) && data[offset] == '\n' { + if offset-origOffset >= 2 { + p.r.LineBreak(out) + return offset - origOffset + 1 + } + return offset - origOffset } + 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 } - 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 }
M markdown.gomarkdown.go

@@ -376,6 +376,7 @@ p.inlineCallback['\\'] = escape

p.inlineCallback['&'] = entity p.inlineCallback['!'] = maybeImage p.inlineCallback['^'] = maybeInlineFootnote + p.inlineCallback[' '] = maybeLineBreak if extensions&Autolink != 0 { p.inlineCallback[':'] = autoLink