-
Notifications
You must be signed in to change notification settings - Fork 4
/
parser.go
101 lines (85 loc) · 1.58 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
package katex
import (
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
)
type Parser struct {
}
func (s *Parser) Trigger() []byte {
return []byte{'$'}
}
func (s *Parser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node {
buf := block.Source()
ln, pos := block.Position()
lstart := pos.Start
lend := pos.Stop
line := buf[lstart:lend]
var start, end, advance int
trigger := line[0]
display := len(line) > 1 && line[1] == trigger
if display { // Display
start = lstart + 2
offset := 2
L:
for x := 0; x < 20; x++ {
for j := offset; j < len(line); j++ {
if len(line) > j+1 && line[j] == trigger && line[j+1] == trigger {
end = lstart + j
advance = 2
break L
}
}
if lend == len(buf) {
break
}
if end == 0 {
rest := buf[lend:]
j := 1
for j < len(rest) && rest[j] != '\n' {
j++
}
lstart = lend
lend += j
line = buf[lstart:lend]
ln++
offset = 0
}
}
} else { // Inline
start = lstart + 1
for i := 1; i < len(line); i++ {
c := line[i]
if c == '\\' {
i++
continue
}
if c == trigger {
end = lstart + i
advance = 1
break
}
}
if end >= len(buf) || buf[end] != trigger {
return nil
}
}
if start >= end {
return nil
}
newpos := end + advance
if newpos < lend {
block.SetPosition(ln, text.NewSegment(newpos, lend))
} else {
block.Advance(newpos)
}
if display {
return &Block{
Equation: buf[start:end],
}
} else {
return &Inline{
Equation: buf[start:end],
}
}
}