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.
This commit is contained in:
Vytautas Šaltenis 2016-08-29 23:57:59 +03:00
parent efa77da18b
commit ea8dfc4880
2 changed files with 17 additions and 6 deletions

View File

@ -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?

View File

@ -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] != ':' {