forked from 88250/lute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_block.go
142 lines (128 loc) · 4.17 KB
/
math_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
// Lute - 一款结构化的 Markdown 引擎,支持 Go 和 JavaScript
// Copyright (c) 2019-present, b3log.org
//
// Lute is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
package parse
import (
"bytes"
"github.com/88250/lute/ast"
"github.com/88250/lute/editor"
"github.com/88250/lute/lex"
"github.com/88250/lute/util"
)
// MathBlockStart 判断数学公式块($$)是否开始。
func MathBlockStart(t *Tree, container *ast.Node) int {
if t.Context.indented {
return 0
}
if ok, mathBlockDollarOffset := t.parseMathBlock(); ok {
t.Context.closeUnmatchedBlocks()
block := t.Context.addChild(ast.NodeMathBlock)
block.MathBlockDollarOffset = mathBlockDollarOffset
t.Context.advanceNextNonspace()
t.Context.advanceOffset(mathBlockDollarOffset, false)
return 2
}
return 0
}
func MathBlockContinue(mathBlock *ast.Node, context *Context) int {
ln := context.currentLine
indent := context.indent
if 3 >= indent && context.isMathBlockClose(ln[context.nextNonspace:]) {
context.finalize(mathBlock)
return 2
} else {
// 跳过 $ 之前可能存在的空格
i := mathBlock.MathBlockDollarOffset
var token byte
for i > 0 {
token = lex.Peek(ln, context.offset)
if lex.ItemSpace != token && lex.ItemTab != token {
break
}
context.advanceOffset(1, true)
i--
}
}
return 0
}
var MathBlockMarker = util.StrToBytes("$$")
var MathBlockMarkerNewline = util.StrToBytes("$$\n")
var MathBlockMarkerCaret = util.StrToBytes("$$" + editor.Caret)
var MathBlockMarkerCaretNewline = util.StrToBytes("$$" + editor.Caret + "\n")
func (context *Context) mathBlockFinalize(mathBlock *ast.Node) {
if 2 > len(mathBlock.Tokens) {
/*
- foo
$$
bar
$$
*/
mathBlock.AppendChild(&ast.Node{Type: ast.NodeMathBlockOpenMarker})
mathBlock.AppendChild(&ast.Node{Type: ast.NodeMathBlockContent})
mathBlock.AppendChild(&ast.Node{Type: ast.NodeMathBlockCloseMarker})
return
}
tokens := mathBlock.Tokens[2:] // 剔除开头的 $$
tokens = lex.TrimWhitespace(tokens)
if context.ParseOption.VditorWYSIWYG || context.ParseOption.VditorIR || context.ParseOption.VditorSV || context.ParseOption.ProtyleWYSIWYG {
if bytes.HasSuffix(tokens, MathBlockMarkerCaret) {
// 剔除结尾的 $$‸
tokens = bytes.TrimSuffix(tokens, MathBlockMarkerCaret)
// 把 Vditor 插入符移动到内容末尾
tokens = append(tokens, editor.CaretTokens...)
}
}
if bytes.HasSuffix(tokens, MathBlockMarker) {
tokens = tokens[:len(tokens)-2] // 剔除结尾的 $$
}
mathBlock.Tokens = nil
mathBlock.AppendChild(&ast.Node{Type: ast.NodeMathBlockOpenMarker})
mathBlock.AppendChild(&ast.Node{Type: ast.NodeMathBlockContent, Tokens: tokens})
mathBlock.AppendChild(&ast.Node{Type: ast.NodeMathBlockCloseMarker})
}
func (t *Tree) parseMathBlock() (ok bool, mathBlockDollarOffset int) {
marker := t.Context.currentLine[t.Context.nextNonspace]
if lex.ItemDollar != marker {
return
}
fenceChar := marker
fenceLength := 0
for i := t.Context.nextNonspace; i < t.Context.currentLineLen && fenceChar == t.Context.currentLine[i]; i++ {
fenceLength++
}
if 2 > fenceLength {
return
}
return true, t.Context.indent
}
func (context *Context) isMathBlockClose(tokens []byte) bool {
if context.ParseOption.KramdownBlockIAL && simpleCheckIsBlockIAL(tokens) {
// 判断 IAL 打断
if ial := context.parseKramdownBlockIAL(tokens); 0 < len(ial) {
context.Tip.ID = IAL2Map(ial)["id"]
context.Tip.KramdownIAL = ial
context.Tip.InsertAfter(&ast.Node{Type: ast.NodeKramdownBlockIAL, Tokens: tokens})
return true
}
}
closeMarker := tokens[0]
if closeMarker != lex.ItemDollar {
return false
}
if 2 > lex.Accept(tokens, closeMarker) {
return false
}
tokens = lex.TrimWhitespace(tokens)
for _, token := range tokens {
if token != lex.ItemDollar {
return false
}
}
return true
}