forked from litao91/goldmark-mathjax
-
Notifications
You must be signed in to change notification settings - Fork 1
/
block.go
81 lines (68 loc) · 1.97 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
package mathjax
import (
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
type mathJaxBlockParser struct {
}
var defaultMathJaxBlockParser = &mathJaxBlockParser{}
type mathBlockData struct {
indent int
}
var mathBlockInfoKey = parser.NewContextKey()
func NewMathJaxBlockParser() parser.BlockParser {
return defaultMathJaxBlockParser
}
func (b *mathJaxBlockParser) Open(parent ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State) {
line, _ := reader.PeekLine()
pos := pc.BlockOffset()
if pos == -1 {
return nil, parser.NoChildren
}
if line[pos] != '$' {
return nil, parser.NoChildren
}
i := pos
for ; i < len(line) && line[i] == '$'; i++ {
}
if i-pos < 2 {
return nil, parser.NoChildren
}
pc.Set(mathBlockInfoKey, &mathBlockData{indent: pos})
node := NewMathBlock()
return node, parser.NoChildren
}
func (b *mathJaxBlockParser) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State {
line, segment := reader.PeekLine()
data := pc.Get(mathBlockInfoKey).(*mathBlockData)
w, pos := util.IndentWidth(line, 0)
if w < 4 {
i := pos
for ; i < len(line) && line[i] == '$'; i++ {
}
length := i - pos
if length >= 2 && util.IsBlank(line[i:]) {
reader.Advance(segment.Stop - segment.Start - segment.Padding)
return parser.Close
}
}
pos, padding := util.DedentPosition(line, 0, data.indent)
seg := text.NewSegmentPadding(segment.Start+pos, segment.Stop, padding)
node.Lines().Append(seg)
reader.AdvanceAndSetPadding(segment.Stop-segment.Start-pos-1, padding)
return parser.Continue | parser.NoChildren
}
func (b *mathJaxBlockParser) Close(node ast.Node, reader text.Reader, pc parser.Context) {
pc.Set(mathBlockInfoKey, nil)
}
func (b *mathJaxBlockParser) CanInterruptParagraph() bool {
return true
}
func (b *mathJaxBlockParser) CanAcceptIndentedLine() bool {
return false
}
func (b *mathJaxBlockParser) Trigger() []byte {
return nil
}