markup/goldmark: Rename/reorder the hook methods

To make them easier to follow.

See #8755
This commit is contained in:
Bjørn Erik Pedersen 2021-07-15 09:05:13 +02:00
parent ee3d2bb1d3
commit 805664818d

View file

@ -179,33 +179,6 @@ func renderAttributes(w util.BufWriter, skipClass bool, attributes ...ast.Attrib
} }
} }
// Fall back to the default Goldmark render funcs. Method below borrowed from:
// https://github.com/yuin/goldmark/blob/b611cd333a492416b56aa8d94b04a67bf0096ab2/renderer/html/html.go#L404
func (r *hookedRenderer) renderDefaultImage(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
n := node.(*ast.Image)
_, _ = w.WriteString("<img src=\"")
if r.Unsafe || !html.IsDangerousURL(n.Destination) {
_, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true)))
}
_, _ = w.WriteString(`" alt="`)
_, _ = w.Write(n.Text(source))
_ = w.WriteByte('"')
if n.Title != nil {
_, _ = w.WriteString(` title="`)
r.Writer.Write(w, n.Title)
_ = w.WriteByte('"')
}
if r.XHTML {
_, _ = w.WriteString(" />")
} else {
_, _ = w.WriteString(">")
}
return ast.WalkSkipChildren, nil
}
func (r *hookedRenderer) renderImage(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { func (r *hookedRenderer) renderImage(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.Image) n := node.(*ast.Image)
var h hooks.Renderers var h hooks.Renderers
@ -217,7 +190,7 @@ func (r *hookedRenderer) renderImage(w util.BufWriter, source []byte, node ast.N
} }
if !ok { if !ok {
return r.renderDefaultImage(w, source, node, entering) return r.renderImageDefault(w, source, node, entering)
} }
if entering { if entering {
@ -247,24 +220,29 @@ func (r *hookedRenderer) renderImage(w util.BufWriter, source []byte, node ast.N
// Fall back to the default Goldmark render funcs. Method below borrowed from: // Fall back to the default Goldmark render funcs. Method below borrowed from:
// https://github.com/yuin/goldmark/blob/b611cd333a492416b56aa8d94b04a67bf0096ab2/renderer/html/html.go#L404 // https://github.com/yuin/goldmark/blob/b611cd333a492416b56aa8d94b04a67bf0096ab2/renderer/html/html.go#L404
func (r *hookedRenderer) renderDefaultLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { func (r *hookedRenderer) renderImageDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.Link) if !entering {
if entering { return ast.WalkContinue, nil
_, _ = w.WriteString("<a href=\"") }
n := node.(*ast.Image)
_, _ = w.WriteString("<img src=\"")
if r.Unsafe || !html.IsDangerousURL(n.Destination) { if r.Unsafe || !html.IsDangerousURL(n.Destination) {
_, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true))) _, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true)))
} }
_, _ = w.WriteString(`" alt="`)
_, _ = w.Write(n.Text(source))
_ = w.WriteByte('"') _ = w.WriteByte('"')
if n.Title != nil { if n.Title != nil {
_, _ = w.WriteString(` title="`) _, _ = w.WriteString(` title="`)
r.Writer.Write(w, n.Title) r.Writer.Write(w, n.Title)
_ = w.WriteByte('"') _ = w.WriteByte('"')
} }
_ = w.WriteByte('>') if r.XHTML {
_, _ = w.WriteString(" />")
} else { } else {
_, _ = w.WriteString("</a>") _, _ = w.WriteString(">")
} }
return ast.WalkContinue, nil return ast.WalkSkipChildren, nil
} }
func (r *hookedRenderer) renderLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { func (r *hookedRenderer) renderLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
@ -278,7 +256,7 @@ func (r *hookedRenderer) renderLink(w util.BufWriter, source []byte, node ast.No
} }
if !ok { if !ok {
return r.renderDefaultLink(w, source, node, entering) return r.renderLinkDefault(w, source, node, entering)
} }
if entering { if entering {
@ -309,6 +287,28 @@ func (r *hookedRenderer) renderLink(w util.BufWriter, source []byte, node ast.No
return ast.WalkContinue, err return ast.WalkContinue, err
} }
// Fall back to the default Goldmark render funcs. Method below borrowed from:
// https://github.com/yuin/goldmark/blob/b611cd333a492416b56aa8d94b04a67bf0096ab2/renderer/html/html.go#L404
func (r *hookedRenderer) renderLinkDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.Link)
if entering {
_, _ = w.WriteString("<a href=\"")
if r.Unsafe || !html.IsDangerousURL(n.Destination) {
_, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true)))
}
_ = w.WriteByte('"')
if n.Title != nil {
_, _ = w.WriteString(` title="`)
r.Writer.Write(w, n.Title)
_ = w.WriteByte('"')
}
_ = w.WriteByte('>')
} else {
_, _ = w.WriteString("</a>")
}
return ast.WalkContinue, nil
}
func (r *hookedRenderer) renderAutoLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { func (r *hookedRenderer) renderAutoLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering { if !entering {
return ast.WalkContinue, nil return ast.WalkContinue, nil
@ -324,7 +324,7 @@ func (r *hookedRenderer) renderAutoLink(w util.BufWriter, source []byte, node as
} }
if !ok { if !ok {
return r.renderDefaultAutoLink(w, source, node, entering) return r.renderAutoLinkDefault(w, source, node, entering)
} }
url := string(n.URL(source)) url := string(n.URL(source))
@ -353,7 +353,7 @@ func (r *hookedRenderer) renderAutoLink(w util.BufWriter, source []byte, node as
// Fall back to the default Goldmark render funcs. Method below borrowed from: // Fall back to the default Goldmark render funcs. Method below borrowed from:
// https://github.com/yuin/goldmark/blob/5588d92a56fe1642791cf4aa8e9eae8227cfeecd/renderer/html/html.go#L439 // https://github.com/yuin/goldmark/blob/5588d92a56fe1642791cf4aa8e9eae8227cfeecd/renderer/html/html.go#L439
func (r *hookedRenderer) renderDefaultAutoLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { func (r *hookedRenderer) renderAutoLinkDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.AutoLink) n := node.(*ast.AutoLink)
if !entering { if !entering {
return ast.WalkContinue, nil return ast.WalkContinue, nil
@ -377,23 +377,6 @@ func (r *hookedRenderer) renderDefaultAutoLink(w util.BufWriter, source []byte,
return ast.WalkContinue, nil return ast.WalkContinue, nil
} }
func (r *hookedRenderer) renderDefaultHeading(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.Heading)
if entering {
_, _ = w.WriteString("<h")
_ = w.WriteByte("0123456"[n.Level])
if n.Attributes() != nil {
r.renderAttributesForNode(w, node)
}
_ = w.WriteByte('>')
} else {
_, _ = w.WriteString("</h")
_ = w.WriteByte("0123456"[n.Level])
_, _ = w.WriteString(">\n")
}
return ast.WalkContinue, nil
}
func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.Heading) n := node.(*ast.Heading)
var h hooks.Renderers var h hooks.Renderers
@ -405,7 +388,7 @@ func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast
} }
if !ok { if !ok {
return r.renderDefaultHeading(w, source, node, entering) return r.renderHeadingDefault(w, source, node, entering)
} }
if entering { if entering {
@ -438,6 +421,23 @@ func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast
return ast.WalkContinue, err return ast.WalkContinue, err
} }
func (r *hookedRenderer) renderHeadingDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.Heading)
if entering {
_, _ = w.WriteString("<h")
_ = w.WriteByte("0123456"[n.Level])
if n.Attributes() != nil {
r.renderAttributesForNode(w, node)
}
_ = w.WriteByte('>')
} else {
_, _ = w.WriteString("</h")
_ = w.WriteByte("0123456"[n.Level])
_, _ = w.WriteString(">\n")
}
return ast.WalkContinue, nil
}
type links struct { type links struct {
} }