-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown.go
247 lines (232 loc) · 7.49 KB
/
markdown.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package config
import (
"regexp"
"strings"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension"
extensionAst "github.com/yuin/goldmark/extension/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
"gitlab.com/tozd/go/errors"
)
var tierRegexp = regexp.MustCompile(` *\((FREE|PREMIUM|ULTIMATE)?( (SELF|SAAS|ALL))?( (EXPERIMENT|BTEA))?\)$`)
// walker is the expected number of columns to find in a table.
const tableColumns = 4
// walker interface provides a Walker to be passed to goldmark.ast.Walk function.
type walker interface {
Walker(n ast.Node, entering bool) (ast.WalkStatus, error)
}
// chainVisitor is a visitor which runs a series of Moves visitors one after
// the other, starting with the next one when the previous one stops,
// starting on the same node the previous one stopped.
type chainVisitor struct {
Moves []walker
}
// Walker implements walker interface for chainVisitor.
func (v *chainVisitor) Walker(n ast.Node, entering bool) (ast.WalkStatus, error) {
if len(v.Moves) == 0 {
return ast.WalkStop, nil
}
w := v.Moves[0]
status, err := w.Walker(n, entering)
if err != nil {
return status, err //nolint:wrapcheck
} else if status == ast.WalkStop {
v.Moves = v.Moves[1:]
return v.Walker(n, entering)
}
return status, nil
}
// findHeadingVisitor is a visitor which stops when it finds the first heading
// with text contents equal to Heading.
type findHeadingVisitor struct {
Source []byte
Heading string
}
// Walker implements walker interface for findHeadingVisitor.
func (v *findHeadingVisitor) Walker(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
if n.Kind() == ast.KindHeading && string(n.Text(v.Source)) == v.Heading {
return ast.WalkStop, nil
}
return ast.WalkContinue, nil
}
// findFirstVisitor is a visitor which stops when it finds the first node with
// kind matching Kind.
type findFirstVisitor struct {
Kind ast.NodeKind
}
// Walker implements walker interface for findFirstVisitor.
func (v *findFirstVisitor) Walker(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
if n.Kind() == v.Kind {
return ast.WalkStop, nil
}
return ast.WalkContinue, nil
}
// extractTableVisitor is a visitor which extracts data from a table given
// CheckHeader, Key, and Value functions. Extracted data is available in
// Result field after walking is done. It expects that the node it starts
// walking on is of kind KindTable.
//
// CheckHeader gets a slice of cell values for the header row and it should
// return an error if header is invalid.
//
// Key gets a slice of cell values for the row and it should return a string
// used as the extracted key for the row. If it returns an empty string, the
// row is skipped.
//
// Value gets a slice of cell values for the row and it should return a string
// used as the extracted value for the row.
type extractTableVisitor struct {
Source []byte
CheckHeader func([]string) errors.E
Key func([]string) (string, errors.E)
Value func([]string) (string, errors.E)
Result map[string]string
currentRow []string
}
// Walker implements walker interface for extractTableVisitor.
func (v *extractTableVisitor) Walker(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering || n.Kind() != extensionAst.KindTable {
return ast.WalkStop, errors.New("not starting at a table")
}
err := ast.Walk(n, v.tableWalker)
return ast.WalkStop, err //nolint:wrapcheck
}
// tableWalker is a sub-walker which walks an individual table node only (and its children nodes).
// It does the extraction.
func (v *extractTableVisitor) tableWalker(n ast.Node, entering bool) (ast.WalkStatus, error) {
if entering && (n.Kind() == extensionAst.KindTableHeader || n.Kind() == extensionAst.KindTableRow) {
v.currentRow = []string{}
}
if entering && n.Kind() == extensionAst.KindTableCell {
v.currentRow = append(v.currentRow, string(n.Text(v.Source)))
return ast.WalkSkipChildren, nil
}
if !entering && n.Kind() == extensionAst.KindTableHeader {
err := v.CheckHeader(v.currentRow)
if err != nil {
return ast.WalkStop, err
}
}
if !entering && n.Kind() == extensionAst.KindTableRow {
key, err := v.Key(v.currentRow)
if err != nil {
return ast.WalkStop, err
}
value, err := v.Value(v.currentRow)
if err != nil {
return ast.WalkStop, err
}
if key != "" {
_, ok := v.Result[key]
if ok {
errE := errors.New("duplicate key")
errors.Details(errE)["key"] = key
return ast.WalkStop, errE
}
v.Result[key] = value
}
}
return ast.WalkContinue, nil
}
// parseTable is a halper function which parses Markdown input and find the first table after
// the heading, which then converts into a map between fields (attributes) and their descriptions.
//
// keyMapper is used to optionally (when not nil) further transform found fields.
func parseTable(input []byte, heading string, keyMapper func(string) string) (map[string]string, errors.E) {
p := parser.NewParser(
parser.WithBlockParsers(parser.DefaultBlockParsers()...),
parser.WithInlineParsers(parser.DefaultInlineParsers()...),
parser.WithParagraphTransformers(parser.DefaultParagraphTransformers()...),
parser.WithParagraphTransformers(
util.Prioritized(extension.NewTableParagraphTransformer(), 200), //nolint:gomnd
),
parser.WithASTTransformers(
util.Prioritized(extension.NewTableASTTransformer(), 0),
),
)
parsed := p.Parse(text.NewReader(input))
extractTable := extractTableVisitor{
Source: input,
CheckHeader: func(row []string) errors.E {
expectedHeader := []string{"Attribute", "Type", "Required", "Description"}
if len(row) != len(expectedHeader) {
errE := errors.New("invalid header")
errors.Details(errE)["row"] = row
return errE
}
for i, h := range expectedHeader {
if row[i] != h {
errE := errors.New("invalid header")
errors.Details(errE)["row"] = row
return errE
}
}
return nil
},
Key: func(row []string) (string, errors.E) {
if len(row) != tableColumns {
errE := errors.New("invalid row")
errors.Details(errE)["row"] = row
return "", errE
}
// We skip deprecated fields.
if strings.Contains(row[3], "(Deprecated") || strings.Contains(row[3], "Deprecated in") {
return "", nil
}
key := row[0]
// We do not care for which tier the field is.
// See: https://gitlab.com/gitlab-org/gitlab/-/blob/master/doc/development/documentation/styleguide/index.md#available-product-tier-badges
key = tierRegexp.ReplaceAllString(key, "")
if key == "id" {
// This is a documented parameter for project ID.
key = ""
}
if keyMapper != nil {
key = keyMapper(key)
}
return key, nil
},
Value: func(row []string) (string, errors.E) {
if len(row) != tableColumns {
errE := errors.New("invalid row")
errors.Details(errE)["row"] = row
return "", errE
}
description := row[3]
if len(description) > 0 {
if !strings.HasSuffix(description, ".") && !strings.HasSuffix(description, ")") {
description += "."
}
description += " "
}
return description + "Type: " + row[1], nil
},
Result: map[string]string{},
currentRow: nil,
}
visitor := &chainVisitor{
Moves: []walker{
&findHeadingVisitor{
Source: input,
Heading: heading,
},
&findFirstVisitor{
Kind: extensionAst.KindTable,
},
&extractTable,
},
}
err := ast.Walk(parsed, visitor.Walker)
if err != nil {
return nil, errors.WithStack(err)
}
return extractTable.Result, nil
}