-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
276 lines (251 loc) · 6.53 KB
/
block.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
271
272
273
274
275
276
package mdblock // import "gopkg.in/akavel/vfmd.v0/mdblock"
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
"gopkg.in/akavel/vfmd.v0/md"
"gopkg.in/akavel/vfmd.v0/mdspan"
)
// func unstack() {
// pc, _, _, _ := runtime.Caller(2)
// for i, j := 2, 0; i < 100; i++ {
// ipc, _, _, _ := runtime.Caller(i)
// if ipc == pc {
// j++
// }
// if j == 10 {
// panic("stack too high")
// }
// }
// }
func unstack() {}
type Mode int
const (
// TODO(akavel): make sure they're ordered & named as I wanted
BlocksAndSpans Mode = iota
BlocksOnly
TopBlocks
)
type Context interface {
GetMode() Mode
GetDetectors() Detectors
GetSpanDetectors() []mdspan.Detector
Emit(md.Tag)
}
type defaultContext struct {
mode Mode
tags []md.Tag
detectors Detectors
spanDetectors []mdspan.Detector
}
func (c *defaultContext) GetMode() Mode { return c.mode }
func (c *defaultContext) GetDetectors() Detectors { return c.detectors }
func (c *defaultContext) GetSpanDetectors() []mdspan.Detector { return c.spanDetectors }
func (c *defaultContext) Emit(tag md.Tag) { c.tags = append(c.tags, tag) }
type Parser struct {
Context
start *Line
handler Handler
}
// func (p *Parser) Emit(tag Tag) { unstack(); p.Context.Emit(tag) }
func (p *Parser) Close() error { return p.WriteLine(Line{}) }
func (p *Parser) WriteLine(line Line) error {
unstack()
// Continue previous block if appropriate.
if p.handler != nil {
// NOTE(akavel): assert(p.start==nil)
// fmt.Printf("...handle? %d %q\n", line.Line, string(line.Bytes))
consumed, err := p.handler.Handle(line, p)
// fmt.Printf("...handled %v\n", consumed)
if err != nil {
return err
}
if line.EOF() {
return nil
}
if consumed {
return nil
}
}
p.handler = nil
// New block needs 2 lines for detection.
if p.start == nil {
if line.EOF() {
return nil
}
p.start = &line
return nil
}
p.handler = p.GetDetectors().Find(*p.start, line)
if p.handler == nil {
// TODO(akavel): return error object with line number and contents
return fmt.Errorf("vfmd: no block detector matched line %d: %q", p.start.Line, string(p.start.Bytes))
}
// fmt.Printf(".:.handle? %d %q\n", p.start.Line, string(p.start.Bytes))
consumed, err := p.handler.Handle(*p.start, p)
// fmt.Printf(".:.handled %v\n", consumed)
if err != nil {
return err
}
if !consumed {
return fmt.Errorf("vfmd: detector %T failed to handle first line %d: %q", p.handler, p.start.Line, string(p.start.Bytes))
}
p.start = nil
return p.WriteLine(line)
}
// Important: r must be pre-processed with vfmd.QuickPrep or vfmd.Preprocessor
func QuickParse(r io.Reader, mode Mode, detectors Detectors, spanDetectors []mdspan.Detector) ([]md.Tag, error) {
scan := bufio.NewScanner(r)
scan.Split(splitKeepingEOLs)
if detectors == nil {
detectors = DefaultDetectors
}
if spanDetectors == nil {
spanDetectors = mdspan.DefaultDetectors
}
context := &defaultContext{
mode: mode,
detectors: detectors,
spanDetectors: spanDetectors,
}
parser := Parser{
Context: context,
}
for i := 0; scan.Scan(); i++ {
// fmt.Print(scan.Text())
err := parser.WriteLine(Line{
Line: i,
// Copy the line contents so that scan.Scan() doesn't invalidate it
Bytes: append([]byte(nil), scan.Bytes()...),
})
if err != nil {
return nil, err
}
}
if scan.Err() != nil {
return nil, scan.Err()
}
err := parser.Close()
if err != nil {
return nil, err
}
return context.tags, nil
}
// Line is a Run that may have at most one '\n', as last byte
type Line md.Run
func (line Line) EOF() bool { return line.Bytes == nil }
func (line Line) isBlank() bool {
return len(bytes.Trim(line.Bytes, " \t\n")) == 0
}
func (line Line) hasNonSpaceInPrefix(n int) bool {
bs := line.Bytes
for i := 0; i < n && i < len(bs) && bs[i] != '\n'; i++ {
if bs[i] != ' ' {
return true
}
}
return false
}
func (line Line) hasFourSpacePrefix() bool {
return bytes.HasPrefix(line.Bytes, []byte(" "))
}
type Detector interface {
Detect(first, second Line, detectors Detectors) Handler
}
type Handler interface {
Handle(Line, Context) (consumed bool, err error)
}
type DetectorFunc func(first, second Line, detectors Detectors) Handler
type HandlerFunc func(Line, Context) (consumed bool, err error)
func (f DetectorFunc) Detect(first, second Line, detectors Detectors) Handler {
return f(first, second, detectors)
}
func (f HandlerFunc) Handle(line Line, context Context) (bool, error) {
return f(line, context)
}
func splitKeepingEOLs(data []byte, atEOF bool) (advance int, token []byte, err error) {
for i, c := range data {
if c == '\n' {
return i + 1, data[:i+1], nil
}
}
switch {
case !atEOF:
return 0, nil, nil
case len(data) > 0:
return len(data), data, nil
default:
return 0, nil, io.EOF
}
}
type Detectors []Detector
// DefaultDetectors contains the list of default detectors in order in which
// they should be normally applied.
var DefaultDetectors = Detectors{
DetectorFunc(DetectNull),
DetectorFunc(DetectReferenceResolution),
DetectorFunc(DetectSetextHeader),
DetectorFunc(DetectCode),
DetectorFunc(DetectAtxHeader),
DetectorFunc(DetectQuote),
DetectorFunc(DetectHorizontalRule),
DetectorFunc(DetectUnorderedList),
DetectorFunc(DetectOrderedList),
ParagraphDetector{},
}
func (ds Detectors) Find(first, second Line) Handler {
for _, d := range ds {
handler := d.Detect(first, second, ds)
if handler != nil {
return handler
}
}
return nil
}
func end(parser *Parser, ctx Context) (bool, error) {
var err error
if parser != nil {
err = parser.Close()
}
ctx.Emit(md.End{})
return false, err
}
func end2(parser *Parser, ctx Context) (bool, error) {
var err error
if parser != nil {
err = parser.Close()
ctx.Emit(md.End{})
}
ctx.Emit(md.End{})
return false, err
}
func pass(parser *Parser, next Line, bytes []byte) (bool, error) {
if parser != nil {
return true, parser.WriteLine(Line{next.Line, bytes})
} else {
return true, nil
}
}
func trimLeftN(s []byte, cutset string, nmax int) []byte {
for nmax > 0 && len(s) > 0 && strings.IndexByte(cutset, s[0]) != -1 {
nmax--
s = s[1:]
}
return s
}
func parseSpans(region md.Raw, ctx Context) {
if ctx.GetMode() != BlocksAndSpans {
return
}
// FIXME(akavel): parse the spans correctly w.r.t. region Run boundaries and collect proper Run.Line values
buf := []byte{}
for _, run := range region {
buf = append(buf, run.Bytes...) // FIXME(akavel): quick & dirty & foul prototyping hack
}
spans := mdspan.Parse(buf, ctx.GetSpanDetectors())
for _, span := range spans {
ctx.Emit(span)
}
}