-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
44 lines (35 loc) · 979 Bytes
/
render.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package superscript
import (
"fmt"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/util"
)
type superscriptRenderer struct{}
func (r *superscriptRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(Kind, r.Render)
}
func (r *superscriptRenderer) Render(w util.BufWriter, _ []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n, ok := node.(*Node)
if !ok {
return ast.WalkStop, fmt.Errorf("unexpected node %T, expected *Node", node)
}
if entering {
if err := r.enter(w, n); err != nil {
return ast.WalkStop, err
}
} else {
r.exit(w, n)
}
return ast.WalkContinue, nil
}
func (r *superscriptRenderer) enter(w util.BufWriter, n *Node) error {
w.WriteString(`<sup`)
html.RenderAttributes(w, n, nil)
w.WriteString(`>`)
return nil
}
func (r *superscriptRenderer) exit(w util.BufWriter, n *Node) {
w.WriteString("</sup>")
}