forked from princjef/gomarkdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.go
198 lines (170 loc) · 5.88 KB
/
renderer.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package gomarkdoc
import (
"fmt"
"reflect"
"strings"
"text/template"
"github.com/princjef/gomarkdoc/format"
"github.com/princjef/gomarkdoc/lang"
)
type (
// Renderer provides capabilities for rendering various types of
// documentation with the configured format and templates.
Renderer struct {
templateOverrides map[string]string
tmpl *template.Template
format format.Format
}
// RendererOption configures the renderer's behavior.
RendererOption func(renderer *Renderer) error
)
//go:generate ./gentmpl.sh templates templates
// NewRenderer initializes a Renderer configured using the provided options. If
// nothing special is provided, the created renderer will use the default set of
// templates and the GitHubFlavoredMarkdown.
func NewRenderer(opts ...RendererOption) (*Renderer, error) {
renderer := &Renderer{
templateOverrides: make(map[string]string),
format: &format.GitHubFlavoredMarkdown{},
}
for _, opt := range opts {
if err := opt(renderer); err != nil {
return nil, err
}
}
for name, tmplStr := range templates {
// Use the override if present
if val, ok := renderer.templateOverrides[name]; ok {
tmplStr = val
}
if renderer.tmpl == nil {
tmpl := template.New(name)
tmpl.Funcs(map[string]interface{}{
"add": func(n1, n2 int) int {
return n1 + n2
},
"spacer": func() string {
return "\n\n"
},
"inlineSpacer": func() string {
return "\n"
},
"hangingIndent": func(s string, n int) string {
return strings.ReplaceAll(s, "\n", fmt.Sprintf("\n%s", strings.Repeat(" ", n)))
},
"include": func(name string, data any) (string, error) {
var b strings.Builder
err := tmpl.ExecuteTemplate(&b, name, data)
if err != nil {
return "", err
}
return b.String(), nil
},
"iter": func(l any) (any, error) {
type iter struct {
First bool
Last bool
Entry any
}
switch reflect.TypeOf(l).Kind() {
case reflect.Slice:
s := reflect.ValueOf(l)
out := make([]iter, s.Len())
for i := 0; i < s.Len(); i++ {
out[i] = iter{
First: i == 0,
Last: i == s.Len()-1,
Entry: s.Index(i).Interface(),
}
}
return out, nil
default:
return nil, fmt.Errorf("renderer: iter only accepts slices")
}
},
"bold": renderer.format.Bold,
"anchor": renderer.format.Anchor,
"anchorHeader": renderer.format.AnchorHeader,
"header": renderer.format.Header,
"rawAnchorHeader": renderer.format.RawAnchorHeader,
"rawHeader": renderer.format.RawHeader,
"codeBlock": renderer.format.CodeBlock,
"link": renderer.format.Link,
"listEntry": renderer.format.ListEntry,
"accordion": renderer.format.Accordion,
"accordionHeader": renderer.format.AccordionHeader,
"accordionTerminator": renderer.format.AccordionTerminator,
"localHref": renderer.format.LocalHref,
"rawLocalHref": renderer.format.RawLocalHref,
"codeHref": renderer.format.CodeHref,
"escape": renderer.format.Escape,
})
if _, err := tmpl.Parse(tmplStr); err != nil {
return nil, err
}
renderer.tmpl = tmpl
} else if _, err := renderer.tmpl.New(name).Parse(tmplStr); err != nil {
return nil, err
}
}
return renderer, nil
}
// WithTemplateOverride adds a template that overrides the template with the
// provided name using the value provided in the tmpl parameter.
func WithTemplateOverride(name, tmpl string) RendererOption {
return func(renderer *Renderer) error {
if _, ok := templates[name]; !ok {
return fmt.Errorf(`gomarkdoc: invalid template name "%s"`, name)
}
renderer.templateOverrides[name] = tmpl
return nil
}
}
// WithFormat changes the renderer to use the format provided instead of the
// default format.
func WithFormat(format format.Format) RendererOption {
return func(renderer *Renderer) error {
renderer.format = format
return nil
}
}
// File renders a file containing one or more packages to document to a string.
// You can change the rendering of the file by overriding the "file" template
// or one of the templates it references.
func (out *Renderer) File(file *lang.File) (string, error) {
return out.writeTemplate("file", file)
}
// Package renders a package's documentation to a string. You can change the
// rendering of the package by overriding the "package" template or one of the
// templates it references.
func (out *Renderer) Package(pkg *lang.Package) (string, error) {
return out.writeTemplate("package", pkg)
}
// Func renders a function's documentation to a string. You can change the
// rendering of the package by overriding the "func" template or one of the
// templates it references.
func (out *Renderer) Func(fn *lang.Func) (string, error) {
return out.writeTemplate("func", fn)
}
// Type renders a type's documentation to a string. You can change the
// rendering of the type by overriding the "type" template or one of the
// templates it references.
func (out *Renderer) Type(typ *lang.Type) (string, error) {
return out.writeTemplate("type", typ)
}
// Example renders an example's documentation to a string. You can change the
// rendering of the example by overriding the "example" template or one of the
// templates it references.
func (out *Renderer) Example(ex *lang.Example) (string, error) {
return out.writeTemplate("example", ex)
}
// writeTemplate renders the template of the provided name using the provided
// data object to a string. It uses the set of templates provided to the
// renderer as a template library.
func (out *Renderer) writeTemplate(name string, data interface{}) (string, error) {
var result strings.Builder
if err := out.tmpl.ExecuteTemplate(&result, name, data); err != nil {
return "", err
}
return result.String(), nil
}