-
Notifications
You must be signed in to change notification settings - Fork 10
/
runnable_finder.go
270 lines (237 loc) · 7.54 KB
/
runnable_finder.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package gfmrun
import (
"fmt"
"regexp"
"strings"
"github.com/sirupsen/logrus"
)
var (
rawTagsRe = regexp.MustCompile("<!-- *({.+}) *-->")
codeGateCharsRe = regexp.MustCompile("[`~]+")
)
type mdState int
func (s mdState) String() string {
switch s {
case mdStateText:
return "text"
case mdStateCodeBlock:
return "code-block"
case mdStateRunnable:
return "runnable"
case mdStateComment:
return "comment"
default:
return "unknown"
}
}
const (
mdStateText mdState = iota
mdStateCodeBlock
mdStateRunnable
mdStateComment
)
var (
mdStateTransTextCodeBlock = calcStateTransition(mdStateText, mdStateCodeBlock)
mdStateTransTextRunnable = calcStateTransition(mdStateText, mdStateRunnable)
mdStateTransTextComment = calcStateTransition(mdStateText, mdStateComment)
mdStateTransCodeBlockText = calcStateTransition(mdStateCodeBlock, mdStateText)
mdStateTransCodeBlockRunnable = calcStateTransition(mdStateCodeBlock, mdStateRunnable)
mdStateTransCodeBlockComment = calcStateTransition(mdStateCodeBlock, mdStateComment)
mdStateTransRunnableText = calcStateTransition(mdStateRunnable, mdStateText)
mdStateTransRunnableCodeBlock = calcStateTransition(mdStateRunnable, mdStateCodeBlock)
mdStateTransRunnableComment = calcStateTransition(mdStateRunnable, mdStateComment)
mdStateTransCommentText = calcStateTransition(mdStateComment, mdStateText)
mdStateTransCommentCodeBlock = calcStateTransition(mdStateComment, mdStateCodeBlock)
mdStateTransCommentRunnable = calcStateTransition(mdStateComment, mdStateRunnable)
)
func calcStateTransition(a, b mdState) int {
return int(a | (b << 2))
}
type runnableFinder struct {
sourceName string
source string
log *logrus.Logger
state mdState
cur *Runnable
line string
trimmedLine string
lineno int
textSize int
lastLine string
lastComment string
codeBlockStart string
}
// custom markdown scanner thingy egad
// (because blackfriday doesn't have all the things and/or I'm horrible)
func newRunnableFinder(sourceName, source string, log *logrus.Logger) *runnableFinder {
rf := &runnableFinder{sourceName: sourceName, source: source, log: log}
rf.reset()
return rf
}
func (rf *runnableFinder) reset() {
rf.cur = NewRunnable(rf.sourceName, rf.log)
rf.state = mdStateText
rf.line = ""
rf.trimmedLine = ""
rf.codeBlockStart = ""
rf.lineno = 0
rf.lastLine = ""
rf.lastComment = ""
}
func (rf *runnableFinder) Find() []*Runnable {
rf.reset()
runnables := []*Runnable{}
for j, line := range strings.Split(rf.source, "\n") {
rf.line = line
rf.lineno = j
rf.lastLine = line
rf.trimmedLine = strings.TrimSpace(rf.line)
runnable := rf.handleLine()
if runnable != nil {
runnables = append(runnables, runnable)
rf.log.WithField("runnable_count", len(runnables)).Debug("leaving runnable code block")
}
rf.log.WithFields(logrus.Fields{
"source_name": rf.sourceName,
"lineno": rf.lineno,
"line": fmt.Sprintf("%q", rf.trimmedLine),
"state": rf.state,
}).Debug("scanning")
}
if rf.state == mdStateRunnable {
// whatever, let's give it a shot
rf.cur.Lines = append(rf.cur.Lines, rf.lastLine)
runnables = append(runnables, rf.cur)
}
return runnables
}
func (rf *runnableFinder) handleLine() *Runnable {
if strings.HasPrefix(rf.trimmedLine, "```") || strings.HasPrefix(rf.trimmedLine, "~~~") {
if rf.state == mdStateCodeBlock {
if rf.trimmedLine == rf.codeBlockStart {
return rf.setState(mdStateText)
} else {
rf.log.Debug("assuming nested code block")
return nil
}
}
if rf.state == mdStateRunnable {
if rf.trimmedLine == rf.cur.BlockStart {
// assuming this is the matching closing gate of the runnable code block
return rf.setState(mdStateText)
} else {
rf.log.WithFields(logrus.Fields{
"block_start": rf.cur.BlockStart,
"lang": rf.cur.Lang,
"line": rf.trimmedLine,
}).Debug("mismatched closing gate")
}
}
if len(codeGateCharsRe.ReplaceAllString(rf.trimmedLine, "")) > 0 {
return rf.setState(mdStateRunnable)
}
return rf.setState(mdStateCodeBlock)
} else if strings.HasPrefix(rf.trimmedLine, "<!--") && strings.HasSuffix(rf.trimmedLine, "-->") {
if rf.state == mdStateText {
rf.setState(mdStateComment)
rf.line = ""
rf.trimmedLine = ""
rf.setState(mdStateText)
} else {
rf.log.WithFields(logrus.Fields{
"state": rf.state,
"line": rf.trimmedLine,
}).Debug("not setting lastComment")
}
} else if strings.HasPrefix(rf.trimmedLine, "<!--") {
return rf.setState(mdStateComment)
} else if strings.HasPrefix(rf.trimmedLine, "-->") && rf.state == mdStateComment {
rf.trimmedLine = strings.TrimSpace(strings.Replace(rf.trimmedLine, "-->", "", 1))
return rf.setState(mdStateText)
}
rf.handleLineInState()
return nil
}
func (rf *runnableFinder) setState(newState mdState) *Runnable {
oldState := rf.state
rf.state = newState
transition := calcStateTransition(oldState, newState)
rf.log.WithFields(logrus.Fields{
"transition": transition,
"old_state": oldState,
"new_state": newState,
}).Debug("setting state")
return rf.handleTransition(transition)
}
func (rf *runnableFinder) handleTransition(transition int) *Runnable {
switch transition {
case mdStateTransCodeBlockComment:
rf.log.WithFields(logrus.Fields{
"state": mdStateCodeBlock,
"invalid_state": mdStateComment,
}).Debug("ignoring transition")
rf.state = mdStateCodeBlock
case mdStateTransRunnableCodeBlock:
rf.log.WithFields(logrus.Fields{
"state": mdStateRunnable,
"invalid_state": mdStateCodeBlock,
}).Debug("ignoring transition")
rf.state = mdStateRunnable
case mdStateTransCodeBlockRunnable:
rf.log.WithFields(logrus.Fields{
"state": mdStateCodeBlock,
"invalid_state": mdStateRunnable,
}).Debug("ignoring transition")
rf.state = mdStateCodeBlock
case mdStateTransTextCodeBlock:
rf.codeBlockStart = rf.trimmedLine
rf.textSize = 0
case mdStateTransTextComment:
rf.textSize = 0
rf.lastComment = rf.line
case mdStateTransCommentText:
rf.textSize = len(rf.trimmedLine)
case mdStateTransCodeBlockText:
rf.codeBlockStart = ""
rf.log.Debug("leaving non-runnable code block")
case mdStateTransRunnableText:
runnable := rf.cur
rf.cur = NewRunnable(rf.sourceName, rf.log)
rf.lastComment = ""
return runnable
case mdStateTransTextCodeBlock:
rf.log.WithField("lineno", rf.lineno).Debug("starting new non-runnable code block")
rf.lastComment = ""
case mdStateTransTextRunnable, mdStateTransCommentRunnable:
rf.log.WithFields(logrus.Fields{
"lineno": rf.lineno,
"text_size": rf.textSize,
"last_comment": rf.lastComment,
}).Debug("starting new runnable")
// textSize of 0 means that the last comment is adjacent to the runnable
if rf.textSize == 0 {
trimmedComment := rawTagsRe.FindStringSubmatch(strings.TrimSpace(rf.lastComment))
if len(trimmedComment) > 1 {
rf.log.WithField("raw_tags", trimmedComment[1]).Debug("setting raw tags")
rf.cur.RawTags = trimmedComment[1]
}
}
rf.lastComment = ""
rf.cur.Begin(rf.lineno, rf.trimmedLine)
case mdStateTransRunnableComment, mdStateTransCommentCodeBlock:
rf.log.WithField("transition", transition).Debug("you found the marble in the oatmeal")
default:
rf.log.WithField("transition", transition).Debug("unhandled transition")
}
return nil
}
func (rf *runnableFinder) handleLineInState() {
switch rf.state {
case mdStateComment:
rf.lastComment += rf.line
case mdStateRunnable:
rf.cur.Lines = append(rf.cur.Lines, rf.line)
case mdStateText:
rf.textSize += len(rf.trimmedLine)
}
}