forked from dim0x69/mdx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
250 lines (206 loc) · 7.87 KB
/
parser.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
248
249
250
package main
import (
"fmt"
"os"
"regexp"
"strings"
"github.com/sirupsen/logrus"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
"gopkg.in/yaml.v3"
)
type MdxHeading struct {
ast.BaseBlock
commandName string
deps []string
}
func (b *MdxHeading) Dump(source []byte, level int) {
ast.DumpHelper(b, source, level, nil, nil)
}
func (b *MdxHeading) Kind() ast.NodeKind {
return KindMdxHeading
}
var KindMdxHeading = ast.NewNodeKind("MdxHeading")
type MdxHeadingParser struct{}
func (p *MdxHeadingParser) Trigger() []byte {
return []byte{'#'}
}
func (p *MdxHeadingParser) Open(parent ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State) {
line, _ := reader.PeekLine()
commandName, deps := extractCommandAndDepsFromHeading(string(line))
if commandName == "" {
logrus.Debug(fmt.Sprintf("No command found in heading: %s", line))
return nil, parser.NoChildren
}
reader.Advance(len(line))
return &MdxHeading{commandName: commandName, deps: deps}, parser.NoChildren
}
func (p *MdxHeadingParser) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State {
return parser.Close
}
func (p *MdxHeadingParser) Close(node ast.Node, reader text.Reader, pc parser.Context) {
}
func (p *MdxHeadingParser) CanInterruptParagraph() bool {
return false
}
func (p *MdxHeadingParser) CanAcceptIndentedLine() bool {
return false
}
/*
extractCommandAndDepsFromHeading extracts the command name and dependencies from the given heading and source.
The command name is extracted from the link text and the dependencies are extracted from the link destination.
[commandName](dep1 dep2 dep3) => commandName, [dep1, dep2, dep3]
*/
func extractCommandAndDepsFromHeading(heading string) (string, []string) {
// NOTE: goldmark does not support parsing links inside of a heading.
// We have to use a regular expression to extract the command name and dependencies.
re := regexp.MustCompile(`\[([^\]]+)\]\((?:([^)]*))?\)`)
matches := re.FindStringSubmatch(heading)
if len(matches) < 2 {
return "", nil
}
// matches[1] is the text inside the square brackets
// matches[2] is the URL inside the parentheses (if present)
text := matches[1]
commandName := strings.TrimSpace(text)
var url string
if len(matches) > 2 {
url = matches[2]
depsString := strings.TrimSpace(url)
deps := strings.Fields(depsString)
return commandName, deps
}
return commandName, []string{}
}
func loadCommands(markdownFile string, commands map[string]CommandBlock) error {
/*
The search strategy is as follows. We start at the beginning of the document, parse the Markdown file into an AST and walk the tree:
1 We search for a heading. (findHeadingWalker)
2 If we find a heading, we walk all siblings of the heading and call parseCodeBlock for all FencedCodeBlock nodes and parseConfigBlock for all FencedCodeBlock nodes with the language 'mdx'.
praseCodeBlock extacts the code from the code block, updates the currentCommandBlock and appends the code block to the currentCommandBlock.CodeBlocks.
3 Goto 1.
*/
source, err := os.ReadFile(markdownFile)
if err != nil {
return err
}
md := goldmark.New(
goldmark.WithParserOptions(
parser.WithBlockParsers(
util.Prioritized(&MdxHeadingParser{}, 0),
),
),
)
reader := text.NewReader(source)
doc := md.Parser().Parse(reader)
var currentCommandBlock CommandBlock
var currentConfigBlock ConfigBlock
parseCodeBlock := func(n ast.Node) error {
if block, ok := n.(*ast.FencedCodeBlock); ok {
lang := string(block.Language(source))
code := string(block.Text(source))
if code == "" {
logrus.Warn(fmt.Sprintf("Empty code block found for command '%s' in '%s'.", currentCommandBlock.Name, markdownFile))
return nil
}
code_shebang := false
if len(code) >= 2 && code[:2] == "#!" {
code_shebang = true
}
if lang == "" && !code_shebang {
logrus.Warn(fmt.Sprintf("Found Code Block with no infostring and no shebang defined for command '%s' in '%s'. Ignoring", currentCommandBlock.Name, markdownFile))
return nil
}
if lang != "" && !code_shebang {
logrus.Debug(fmt.Sprintf("No language defined for command '%s' in '%s'. Using shebang!", currentCommandBlock.Name, markdownFile))
if _, ok := launchers[lang]; !ok {
return fmt.Errorf("%w: %s", ErrNoLauncherDefined, lang)
}
}
if lang != "" && code_shebang {
logrus.Warn(fmt.Sprintf("Both language and shebang defined for command '%s' in '%s'. The shebang will be used!", currentCommandBlock.Name, markdownFile))
}
codeBlock := CodeBlock{
Lang: lang,
Code: code,
Config: ConfigBlock{},
}
codeBlock.Config.SheBang = code_shebang
codeBlock.Config.OnError = currentConfigBlock.OnError
currentCommandBlock.CodeBlocks = append(currentCommandBlock.CodeBlocks, codeBlock)
logrus.Debug(fmt.Sprintf("Wrote new code block. Infostring: '%s', Command: '%s'", lang, currentCommandBlock.Name))
}
return nil
}
parseConfigBlock := func(n ast.Node) error {
// Called when a FencedCodeBlock with the language 'mdx' is found.
if block, ok := n.(*ast.FencedCodeBlock); ok {
mdxConfig := make(map[string]any)
code := string(block.Text(source))
if err := yaml.Unmarshal([]byte(code), &mdxConfig); err != nil {
return fmt.Errorf("failed to unmarshal YAML: %w", err)
}
logrus.Debug(fmt.Sprintf("Found config block: %v", mdxConfig))
if config, ok := mdxConfig["config"].(map[string]interface{}); ok {
if onError, ok := config["on-error"].(string); ok {
if onError == "ignore" || onError == "fail" {
currentConfigBlock.OnError = onError
} else {
logrus.Warn(fmt.Sprintf("Invalid value for onError: '%s'. Using default 'ignore'.", onError))
}
}
}
return nil
}
return fmt.Errorf("node is not a FencedCodeBlock")
}
findHeadingWalker := func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if heading, ok := n.(*MdxHeading); ok && entering {
currentCommandBlock = CommandBlock{}
currentCommandBlock.Filename = markdownFile
currentCommandBlock.Meta = make(map[string]any)
currentCommandBlock.CodeBlocks = []CodeBlock{}
currentCommandBlock.Name = heading.commandName
currentCommandBlock.Dependencies = heading.deps
// Reset the config block. Defaults are defined here.
// parseConfigBlock will update the currentConfigBlock if a config block is found.
// parseCodeBlock will use the currentConfigBlock to update the ConfigBlock of the CodeBlock.
currentConfigBlock = ConfigBlock{OnError: "ignore"}
if _, exists := commands[currentCommandBlock.Name]; exists {
return ast.WalkStop, fmt.Errorf("%w: '%s' was already defined in '%s'", ErrDuplicateCommand, currentCommandBlock.Name, commands[currentCommandBlock.Name].Filename)
}
logrus.Debug(fmt.Sprintf("Found heading with command: '%s' and dependencies: %v", currentCommandBlock.Name, currentCommandBlock.Dependencies))
// findCodeBlocksWalker will extract the code blocks below this heading
// and append them to the currentCommandBlock.CodeBlocks
for sibling := heading.NextSibling(); sibling != nil; sibling = sibling.NextSibling() {
if _, ok := sibling.(*ast.Heading); ok {
break
}
if _, ok := sibling.(*MdxHeading); ok {
break
}
if _, ok := sibling.(*ast.FencedCodeBlock); ok {
lang := string(sibling.(*ast.FencedCodeBlock).Language(source))
if lang == "mdx" {
err = parseConfigBlock(sibling)
} else {
err = parseCodeBlock(sibling)
}
}
if err != nil {
return ast.WalkStop, err
}
}
if len(currentCommandBlock.CodeBlocks) > 0 {
commands[currentCommandBlock.Name] = currentCommandBlock
} else {
logrus.Debug(fmt.Sprintf("No code blocks found for command '%s' in '%s'.", currentCommandBlock.Name, markdownFile))
}
}
return ast.WalkContinue, nil
}
return ast.Walk(doc, findHeadingWalker)
}