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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
// Package renderer renders the given AST to certain formats.
package renderer
import (
"bufio"
"io"
"sync"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/util"
)
// A Config struct is a data structure that holds configuration of the Renderer.
type Config struct {
Options map[OptionName]interface{}
NodeRenderers util.PrioritizedSlice
}
// NewConfig returns a new Config
func NewConfig() *Config {
return &Config{
Options: map[OptionName]interface{}{},
NodeRenderers: util.PrioritizedSlice{},
}
}
// An OptionName is a name of the option.
type OptionName string
// An Option interface is a functional option type for the Renderer.
type Option interface {
SetConfig(*Config)
}
type withNodeRenderers struct {
value []util.PrioritizedValue
}
func (o *withNodeRenderers) SetConfig(c *Config) {
c.NodeRenderers = append(c.NodeRenderers, o.value...)
}
// WithNodeRenderers is a functional option that allow you to add
// NodeRenderers to the renderer.
func WithNodeRenderers(ps ...util.PrioritizedValue) Option {
return &withNodeRenderers{ps}
}
type withOption struct {
name OptionName
value interface{}
}
func (o *withOption) SetConfig(c *Config) {
c.Options[o.name] = o.value
}
// WithOption is a functional option that allow you to set
// an arbitrary option to the parser.
func WithOption(name OptionName, value interface{}) Option {
return &withOption{name, value}
}
// A SetOptioner interface sets given option to the object.
type SetOptioner interface {
// SetOption sets given option to the object.
// Unacceptable options may be passed.
// Thus implementations must ignore unacceptable options.
SetOption(name OptionName, value interface{})
}
// NodeRendererFunc is a function that renders a given node.
type NodeRendererFunc func(writer util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error)
// A NodeRenderer interface offers NodeRendererFuncs.
type NodeRenderer interface {
// RendererFuncs registers NodeRendererFuncs to given NodeRendererFuncRegisterer.
RegisterFuncs(NodeRendererFuncRegisterer)
}
// A NodeRendererFuncRegisterer registers
type NodeRendererFuncRegisterer interface {
// Register registers given NodeRendererFunc to this object.
Register(ast.NodeKind, NodeRendererFunc)
}
// A Renderer interface renders given AST node to given
// writer with given Renderer.
type Renderer interface {
Render(w io.Writer, source []byte, n ast.Node) error
// AddOptions adds given option to this renderer.
AddOptions(...Option)
}
type renderer struct {
config *Config
options map[OptionName]interface{}
nodeRendererFuncsTmp map[ast.NodeKind]NodeRendererFunc
maxKind int
nodeRendererFuncs []NodeRendererFunc
initSync sync.Once
}
// NewRenderer returns a new Renderer with given options.
func NewRenderer(options ...Option) Renderer {
config := NewConfig()
for _, opt := range options {
opt.SetConfig(config)
}
r := &renderer{
options: map[OptionName]interface{}{},
config: config,
nodeRendererFuncsTmp: map[ast.NodeKind]NodeRendererFunc{},
}
return r
}
func (r *renderer) AddOptions(opts ...Option) {
for _, opt := range opts {
opt.SetConfig(r.config)
}
}
func (r *renderer) Register(kind ast.NodeKind, v NodeRendererFunc) {
r.nodeRendererFuncsTmp[kind] = v
if int(kind) > r.maxKind {
r.maxKind = int(kind)
}
}
// Render renders the given AST node to the given writer with the given Renderer.
func (r *renderer) Render(w io.Writer, source []byte, n ast.Node) error {
r.initSync.Do(func() {
r.options = r.config.Options
r.config.NodeRenderers.Sort()
l := len(r.config.NodeRenderers)
for i := l - 1; i >= 0; i-- {
v := r.config.NodeRenderers[i]
nr, _ := v.Value.(NodeRenderer)
if se, ok := v.Value.(SetOptioner); ok {
for oname, ovalue := range r.options {
se.SetOption(oname, ovalue)
}
}
nr.RegisterFuncs(r)
}
r.nodeRendererFuncs = make([]NodeRendererFunc, r.maxKind+1)
for kind, nr := range r.nodeRendererFuncsTmp {
r.nodeRendererFuncs[kind] = nr
}
r.config = nil
r.nodeRendererFuncsTmp = nil
})
writer, ok := w.(util.BufWriter)
if !ok {
writer = bufio.NewWriter(w)
}
err := ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
s := ast.WalkStatus(ast.WalkContinue)
var err error
f := r.nodeRendererFuncs[n.Kind()]
if f != nil {
s, err = f(writer, source, n, entering)
}
return s, err
})
if err != nil {
return err
}
return writer.Flush()
}
|