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.
This commit is contained in:
Vytautas Šaltenis 2015-10-26 19:41:15 +02:00
parent ee98bc0bf4
commit b16c9b3787
4 changed files with 24 additions and 55 deletions

View File

@ -245,9 +245,8 @@ func (p *parser) prefixHeader(out *bytes.Buffer, data []byte) int {
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 @@ func (p *parser) dliPrefix(data []byte) int {
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 @@ func (p *parser) list(out *bytes.Buffer, data []byte, flags ListType) int {
}
flags &= ^ListItemBeginningOfList
}
return true
}
p.r.List(out, work, flags)
@ -1273,9 +1271,8 @@ func (p *parser) renderParagraph(out *bytes.Buffer, data []byte) {
end--
}
work := func() bool {
work := func() {
p.inline(out, data[beg:end])
return true
}
p.r.Paragraph(out, work)
}
@ -1323,10 +1320,9 @@ func (p *parser) paragraph(out *bytes.Buffer, data []byte) int {
// 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])

26
html.go
View File

@ -202,8 +202,7 @@ func (options *Html) TitleBlock(out *bytes.Buffer, text []byte) {
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 @@ func (options *Html) Header(out *bytes.Buffer, text func() bool, level int, id s
}
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 @@ func (options *Html) TableCell(out *bytes.Buffer, text []byte, align int) {
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 @@ func (options *Html) FootnoteItem(out *bytes.Buffer, name, text []byte, flags Li
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 @@ func (options *Html) List(out *bytes.Buffer, text func() bool, flags ListType) {
} 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 @@ func (options *Html) ListItem(out *bytes.Buffer, text []byte, flags ListType) {
}
}
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")
}

View File

@ -72,9 +72,7 @@ func (options *Latex) BlockHtml(out *bytes.Buffer, text []byte) {
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 @@ func (options *Latex) Header(out *bytes.Buffer, text func() bool, level int, id
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 @@ func (options *Latex) ListItem(out *bytes.Buffer, text []byte, flags ListType) {
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 @@ func (options *Latex) TableCell(out *bytes.Buffer, text []byte, align int) {
}
// TODO: this
func (options *Latex) Footnotes(out *bytes.Buffer, text func() bool) {
func (options *Latex) Footnotes(out *bytes.Buffer, text func()) {
}

View File

@ -163,16 +163,16 @@ type Renderer interface {
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 @@ func secondPass(p *parser, input []byte) []byte {
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 @@ func secondPass(p *parser, input []byte) []byte {
p.r.FootnoteItem(&output, ref.link, buf.Bytes(), flags)
flags &^= ListItemBeginningOfList | ListItemContainsBlock
}
return true
})
}