mirror of
https://github.com/russross/blackfriday.git
synced 2024-03-22 13:40:34 +08:00
Remove callback from Header renderer event
Split Header into two events: BeginHeader and EndHeader, removing the need for callback.
This commit is contained in:
parent
b16c9b3787
commit
82be6cab6d
17
block.go
17
block.go
|
@ -245,10 +245,9 @@ func (p *parser) prefixHeader(out *bytes.Buffer, data []byte) int {
|
||||||
if id == "" && p.flags&AutoHeaderIDs != 0 {
|
if id == "" && p.flags&AutoHeaderIDs != 0 {
|
||||||
id = sanitized_anchor_name.Create(string(data[i:end]))
|
id = sanitized_anchor_name.Create(string(data[i:end]))
|
||||||
}
|
}
|
||||||
work := func() {
|
tocMarker := p.r.BeginHeader(out, level, id)
|
||||||
p.inline(out, data[i:end])
|
p.inline(out, data[i:end])
|
||||||
}
|
p.r.EndHeader(out, level, id, tocMarker)
|
||||||
p.r.Header(out, work, level, id)
|
|
||||||
}
|
}
|
||||||
return skip
|
return skip
|
||||||
}
|
}
|
||||||
|
@ -1318,20 +1317,14 @@ func (p *parser) paragraph(out *bytes.Buffer, data []byte) int {
|
||||||
eol--
|
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 := ""
|
id := ""
|
||||||
if p.flags&AutoHeaderIDs != 0 {
|
if p.flags&AutoHeaderIDs != 0 {
|
||||||
id = sanitized_anchor_name.Create(string(data[prev:eol]))
|
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
|
// find the end of the underline
|
||||||
for data[i] != '\n' {
|
for data[i] != '\n' {
|
||||||
|
|
11
html.go
11
html.go
|
@ -202,7 +202,7 @@ func (options *Html) TitleBlock(out *bytes.Buffer, text []byte) {
|
||||||
out.WriteString("\n</h1>")
|
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)
|
doubleSpace(out)
|
||||||
|
|
||||||
if id == "" && options.flags&Toc != 0 {
|
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))
|
out.WriteString(fmt.Sprintf("<h%d>", level))
|
||||||
}
|
}
|
||||||
|
|
||||||
tocMarker := out.Len()
|
return out.Len()
|
||||||
text()
|
}
|
||||||
|
|
||||||
|
func (r *Html) EndHeader(out *bytes.Buffer, level int, id string, tocMarker int) {
|
||||||
// are we building a table of contents?
|
// are we building a table of contents?
|
||||||
if options.flags&Toc != 0 {
|
if r.flags&Toc != 0 {
|
||||||
options.TocHeaderWithAnchor(out.Bytes()[tocMarker:], level, id)
|
r.TocHeaderWithAnchor(out.Bytes()[tocMarker:], level, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
out.WriteString(fmt.Sprintf("</h%d>\n", level))
|
out.WriteString(fmt.Sprintf("</h%d>\n", level))
|
||||||
|
|
7
latex.go
7
latex.go
|
@ -72,7 +72,7 @@ func (options *Latex) BlockHtml(out *bytes.Buffer, text []byte) {
|
||||||
out.WriteString("\n\\end{verbatim}\n")
|
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 {
|
switch level {
|
||||||
case 1:
|
case 1:
|
||||||
out.WriteString("\n\\section{")
|
out.WriteString("\n\\section{")
|
||||||
|
@ -87,7 +87,10 @@ func (options *Latex) Header(out *bytes.Buffer, text func(), level int, id strin
|
||||||
case 6:
|
case 6:
|
||||||
out.WriteString("\n\\textbf{")
|
out.WriteString("\n\\textbf{")
|
||||||
}
|
}
|
||||||
text()
|
return out.Len()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Latex) EndHeader(out *bytes.Buffer, level int, id string, tocMarker int) {
|
||||||
out.WriteString("}\n")
|
out.WriteString("}\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -163,7 +163,8 @@ type Renderer interface {
|
||||||
BlockCode(out *bytes.Buffer, text []byte, lang string)
|
BlockCode(out *bytes.Buffer, text []byte, lang string)
|
||||||
BlockQuote(out *bytes.Buffer, text []byte)
|
BlockQuote(out *bytes.Buffer, text []byte)
|
||||||
BlockHtml(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)
|
HRule(out *bytes.Buffer)
|
||||||
List(out *bytes.Buffer, text func(), flags ListType)
|
List(out *bytes.Buffer, text func(), flags ListType)
|
||||||
ListItem(out *bytes.Buffer, text []byte, flags ListType)
|
ListItem(out *bytes.Buffer, text []byte, flags ListType)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user