Simplify callbacks in Renderer interface The callbacks used to return bools, but none of the actual implementations return false, always true. So in order to make further refactorings simpler, make the interface reflect the inner workings: no more return values, no more conditionals.
@@ -245,9 +245,8 @@ if end > i {
if id == "" && p.flags&AutoHeaderIDs != 0 { id = sanitized_anchor_name.Create(string(data[i:end])) } - work := func() bool { + work := func() { p.inline(out, data[i:end]) - return true } p.r.Header(out, work, level, id) }@@ -1052,7 +1051,7 @@ // parse ordered or unordered list block
func (p *parser) list(out *bytes.Buffer, data []byte, flags ListType) int { i := 0 flags |= ListItemBeginningOfList - work := func() bool { + work := func() { for i < len(data) { skip := p.listItem(out, data[i:], &flags) i += skip@@ -1062,7 +1061,6 @@ break
} flags &= ^ListItemBeginningOfList } - return true } p.r.List(out, work, flags)@@ -1273,9 +1271,8 @@ for end > beg && data[end-1] == ' ' {
end-- } - work := func() bool { + work := func() { p.inline(out, data[beg:end]) - return true } p.r.Paragraph(out, work) }@@ -1323,10 +1320,9 @@ }
// render the header // this ugly double closure avoids forcing variables onto the heap - work := func(o *bytes.Buffer, pp *parser, d []byte) func() bool { - return func() bool { + work := func(o *bytes.Buffer, pp *parser, d []byte) func() { + return func() { pp.inline(o, d) - return true } }(out, p, data[prev:eol])
@@ -202,8 +202,7 @@ out.Write(text)
out.WriteString("\n</h1>") } -func (options *Html) Header(out *bytes.Buffer, text func() bool, level int, id string) { - marker := out.Len() +func (options *Html) Header(out *bytes.Buffer, text func(), level int, id string) { doubleSpace(out) if id == "" && options.flags&Toc != 0 {@@ -227,10 +226,7 @@ out.WriteString(fmt.Sprintf("<h%d>", level))
} tocMarker := out.Len() - if !text() { - out.Truncate(marker) - return - } + text() // are we building a table of contents? if options.flags&Toc != 0 {@@ -345,7 +341,7 @@ out.Write(text)
out.WriteString("</td>") } -func (options *Html) Footnotes(out *bytes.Buffer, text func() bool) { +func (options *Html) Footnotes(out *bytes.Buffer, text func()) { out.WriteString("<div class=\"footnotes\">\n") options.HRule(out) options.List(out, text, ListTypeOrdered)@@ -375,8 +371,7 @@ }
out.WriteString("</li>\n") } -func (options *Html) List(out *bytes.Buffer, text func() bool, flags ListType) { - marker := out.Len() +func (options *Html) List(out *bytes.Buffer, text func(), flags ListType) { doubleSpace(out) if flags&ListTypeDefinition != 0 {@@ -386,10 +381,7 @@ out.WriteString("<ol>")
} else { out.WriteString("<ul>") } - if !text() { - out.Truncate(marker) - return - } + text() if flags&ListTypeDefinition != 0 { out.WriteString("</dl>\n") } else if flags&ListTypeOrdered != 0 {@@ -421,15 +413,11 @@ out.WriteString("</li>\n")
} } -func (options *Html) Paragraph(out *bytes.Buffer, text func() bool) { - marker := out.Len() +func (options *Html) Paragraph(out *bytes.Buffer, text func()) { doubleSpace(out) out.WriteString("<p>") - if !text() { - out.Truncate(marker) - return - } + text() out.WriteString("</p>\n") }
@@ -72,9 +72,7 @@ out.Write(text)
out.WriteString("\n\\end{verbatim}\n") } -func (options *Latex) Header(out *bytes.Buffer, text func() bool, level int, id string) { - marker := out.Len() - +func (options *Latex) Header(out *bytes.Buffer, text func(), level int, id string) { switch level { case 1: out.WriteString("\n\\section{")@@ -89,10 +87,7 @@ out.WriteString("\n\\subparagraph{")
case 6: out.WriteString("\n\\textbf{") } - if !text() { - out.Truncate(marker) - return - } + text() out.WriteString("}\n") }@@ -100,17 +95,13 @@ func (options *Latex) HRule(out *bytes.Buffer) {
out.WriteString("\n\\HRule\n") } -func (options *Latex) List(out *bytes.Buffer, text func() bool, flags ListType) { - marker := out.Len() +func (options *Latex) List(out *bytes.Buffer, text func(), flags ListType) { if flags&ListTypeOrdered != 0 { out.WriteString("\n\\begin{enumerate}\n") } else { out.WriteString("\n\\begin{itemize}\n") } - if !text() { - out.Truncate(marker) - return - } + text() if flags&ListTypeOrdered != 0 { out.WriteString("\n\\end{enumerate}\n") } else {@@ -123,13 +114,9 @@ out.WriteString("\n\\item ")
out.Write(text) } -func (options *Latex) Paragraph(out *bytes.Buffer, text func() bool) { - marker := out.Len() +func (options *Latex) Paragraph(out *bytes.Buffer, text func()) { out.WriteString("\n") - if !text() { - out.Truncate(marker) - return - } + text() out.WriteString("\n") }@@ -174,7 +161,7 @@ out.Write(text)
} // TODO: this -func (options *Latex) Footnotes(out *bytes.Buffer, text func() bool) { +func (options *Latex) Footnotes(out *bytes.Buffer, text func()) { }
@@ -163,16 +163,16 @@ // block-level callbacks
BlockCode(out *bytes.Buffer, text []byte, lang string) BlockQuote(out *bytes.Buffer, text []byte) BlockHtml(out *bytes.Buffer, text []byte) - Header(out *bytes.Buffer, text func() bool, level int, id string) + Header(out *bytes.Buffer, text func(), level int, id string) HRule(out *bytes.Buffer) - List(out *bytes.Buffer, text func() bool, flags ListType) + List(out *bytes.Buffer, text func(), flags ListType) ListItem(out *bytes.Buffer, text []byte, flags ListType) - Paragraph(out *bytes.Buffer, text func() bool) + Paragraph(out *bytes.Buffer, text func()) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) TableRow(out *bytes.Buffer, text []byte) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) TableCell(out *bytes.Buffer, text []byte, flags int) - Footnotes(out *bytes.Buffer, text func() bool) + Footnotes(out *bytes.Buffer, text func()) FootnoteItem(out *bytes.Buffer, name, text []byte, flags ListType) TitleBlock(out *bytes.Buffer, text []byte)@@ -453,7 +453,7 @@ p.r.DocumentHeader(&output)
p.block(&output, input) if p.flags&Footnotes != 0 && len(p.notes) > 0 { - p.r.Footnotes(&output, func() bool { + p.r.Footnotes(&output, func() { flags := ListItemBeginningOfList for i := 0; i < len(p.notes); i += 1 { ref := p.notes[i]@@ -467,8 +467,6 @@ }
p.r.FootnoteItem(&output, ref.link, buf.Bytes(), flags) flags &^= ListItemBeginningOfList | ListItemContainsBlock } - - return true }) }