From ea8dfc488069987834e0db3ef4443398b0bb4934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytautas=20=C5=A0altenis?= Date: Mon, 29 Aug 2016 23:57:59 +0300 Subject: [PATCH] Move reference extraction to paragraph parser Move reference and footnote extraction code from firstPass to a paragraph block parser. This makes firstPass a little bit slimmer, ergo closer to elimination. --- block.go | 13 ++++++++++++- markdown.go | 10 +++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/block.go b/block.go index 4a7df43..42bed26 100644 --- a/block.go +++ b/block.go @@ -1380,7 +1380,10 @@ func (p *parser) paragraph(data []byte) int { // line: index of 1st char of current line // i: index of cursor/end of current line var prev, line, i int - + tabSize := TabSizeDefault + if p.flags&TabSizeEight != 0 { + tabSize = TabSizeDouble + } // keep going until we find something to mark the end of the paragraph for i < len(data) { // mark the beginning of the current line @@ -1388,6 +1391,14 @@ func (p *parser) paragraph(data []byte) int { current := data[i:] line = i + // did we find a reference or a footnote? If so, end a paragraph + // preceding it and report that we have consumed up to the end of that + // reference: + if refEnd := isReference(p, current, tabSize); refEnd > 0 { + p.renderParagraph(data[:i]) + return i + refEnd + } + // did we find a blank line marking the end of the paragraph? if n := p.isEmpty(current); n > 0 { // did this blank line followed by a definition list item? diff --git a/markdown.go b/markdown.go index 0bf1c12..13a18d3 100644 --- a/markdown.go +++ b/markdown.go @@ -453,7 +453,6 @@ func (p *parser) parseRefsToAST() { // first pass: // - normalize newlines -// - extract references (outside of fenced code blocks) // - expand tabs (outside of fenced code blocks) // - copy everything else func firstPass(p *parser, input []byte) []byte { @@ -485,9 +484,6 @@ func firstPass(p *parser, input []byte) []byte { if end > beg { if end < lastFencedCodeBlockEnd { // Do not expand tabs while inside fenced code blocks. out.Write(input[beg:end]) - } else if refEnd := isReference(p, input[beg:], tabSize); refEnd > 0 { - beg += refEnd - continue } else { expandTabs(&out, input[beg:end], tabSize) } @@ -593,7 +589,11 @@ func isReference(p *parser, data []byte, tabSize int) int { return 0 } idEnd := i - + // footnotes can have empty ID, like this: [^], but a reference can not be + // empty like this: []. Break early if it's not a footnote and there's no ID + if noteID == 0 && idOffset == idEnd { + return 0 + } // spacer: colon (space | tab)* newline? (space | tab)* i++ if i >= len(data) || data[i] != ':' {