-
Notifications
You must be signed in to change notification settings - Fork 0
/
quote.go
88 lines (82 loc) · 2.07 KB
/
quote.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
package mdblock
import (
"bytes"
"gopkg.in/akavel/vfmd.v0/md"
)
func trimQuote(line []byte) []byte {
trimmed := bytes.TrimLeft(line, " ")
if bytes.HasPrefix(trimmed, []byte{'>'}) {
return bytes.TrimPrefix(trimmed[1:], []byte{' '})
} else {
return line
}
}
func DetectQuote(first, second Line, detectors Detectors) Handler {
ltrim := bytes.TrimLeft(first.Bytes, " ")
if len(ltrim) == 0 || ltrim[0] != '>' {
return nil
}
var buf *defaultContext
block := md.QuoteBlock{}
var carry *Line
var parser *Parser
return HandlerFunc(func(next Line, ctx Context) (bool, error) {
// TODO(akavel): verify it's coded ok, it was converted from a different approach
if next.EOF() {
ctx.Emit(block)
return quoteEnd(parser, buf, ctx)
}
prev := carry
carry = &next
// First line?
if prev == nil {
buf = &defaultContext{
mode: ctx.GetMode(),
detectors: changedParagraphDetector(ctx, true, false),
spanDetectors: ctx.GetSpanDetectors(),
}
block.Raw = append(block.Raw, md.Run(next))
if ctx.GetMode() != TopBlocks {
parser = &Parser{
Context: buf,
}
}
return pass(parser, next, trimQuote(next.Bytes))
}
if prev.isBlank() {
if next.isBlank() ||
next.hasFourSpacePrefix() ||
bytes.TrimLeft(next.Bytes, " ")[0] != '>' {
ctx.Emit(block)
return quoteEnd(parser, buf, ctx)
}
} else if !next.hasFourSpacePrefix() &&
reHorizontalRule.Match(bytes.TrimRight(next.Bytes, "\n")) {
ctx.Emit(block)
return quoteEnd(parser, buf, ctx)
}
block.Raw = append(block.Raw, md.Run(next))
return pass(parser, next, trimQuote(next.Bytes))
})
}
func quoteEnd(parser *Parser, buf *defaultContext, ctx Context) (bool, error) {
b, err := end(parser, buf)
for _, t := range buf.tags {
ctx.Emit(t)
}
return b, err
}
func changedParagraphDetector(ctx Context, inQuote, inList bool) Detectors {
ds := append(Detectors{}, ctx.GetDetectors()...)
for i := range ds {
_, ok := ds[i].(ParagraphDetector)
if !ok {
continue
}
ds[i] = ParagraphDetector{
InQuote: inQuote,
InList: inList,
}
}
return ds
}