Remove callback from Header renderer event

Split Header into two events: BeginHeader and EndHeader, removing the
need for callback.
This commit is contained in:
Vytautas Šaltenis 2015-10-26 20:25:23 +02:00
parent b16c9b3787
commit 82be6cab6d
4 changed files with 19 additions and 21 deletions

View File

@ -245,10 +245,9 @@ 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() {
p.inline(out, data[i:end])
}
p.r.Header(out, work, level, id)
tocMarker := p.r.BeginHeader(out, level, id)
p.inline(out, data[i:end])
p.r.EndHeader(out, level, id, tocMarker)
}
return skip
}
@ -1318,20 +1317,14 @@ func (p *parser) paragraph(out *bytes.Buffer, data []byte) int {
eol--
}
// render the header
// this ugly double closure avoids forcing variables onto the heap
work := func(o *bytes.Buffer, pp *parser, d []byte) func() {
return func() {
pp.inline(o, d)
}
}(out, p, data[prev:eol])
id := ""
if p.flags&AutoHeaderIDs != 0 {
id = sanitized_anchor_name.Create(string(data[prev:eol]))
}
p.r.Header(out, work, level, id)
tocMarker := p.r.BeginHeader(out, level, id)
p.inline(out, data[prev:eol])
p.r.EndHeader(out, level, id, tocMarker)
// find the end of the underline
for data[i] != '\n' {

11
html.go
View File

@ -202,7 +202,7 @@ func (options *Html) TitleBlock(out *bytes.Buffer, text []byte) {
out.WriteString("\n</h1>")
}
func (options *Html) Header(out *bytes.Buffer, text func(), level int, id string) {
func (options *Html) BeginHeader(out *bytes.Buffer, level int, id string) int {
doubleSpace(out)
if id == "" && options.flags&Toc != 0 {
@ -225,12 +225,13 @@ func (options *Html) Header(out *bytes.Buffer, text func(), level int, id string
out.WriteString(fmt.Sprintf("<h%d>", level))
}
tocMarker := out.Len()
text()
return out.Len()
}
func (r *Html) EndHeader(out *bytes.Buffer, level int, id string, tocMarker int) {
// are we building a table of contents?
if options.flags&Toc != 0 {
options.TocHeaderWithAnchor(out.Bytes()[tocMarker:], level, id)
if r.flags&Toc != 0 {
r.TocHeaderWithAnchor(out.Bytes()[tocMarker:], level, id)
}
out.WriteString(fmt.Sprintf("</h%d>\n", level))

View File

@ -72,7 +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(), level int, id string) {
func (r *Latex) BeginHeader(out *bytes.Buffer, level int, id string) int {
switch level {
case 1:
out.WriteString("\n\\section{")
@ -87,7 +87,10 @@ func (options *Latex) Header(out *bytes.Buffer, text func(), level int, id strin
case 6:
out.WriteString("\n\\textbf{")
}
text()
return out.Len()
}
func (r *Latex) EndHeader(out *bytes.Buffer, level int, id string, tocMarker int) {
out.WriteString("}\n")
}

View File

@ -163,7 +163,8 @@ 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(), level int, id string)
BeginHeader(out *bytes.Buffer, level int, id string) int
EndHeader(out *bytes.Buffer, level int, id string, tocMarker int)
HRule(out *bytes.Buffer)
List(out *bytes.Buffer, text func(), flags ListType)
ListItem(out *bytes.Buffer, text []byte, flags ListType)