forked from b3log/lute
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.go
More file actions
176 lines (155 loc) · 3.84 KB
/
table.go
File metadata and controls
176 lines (155 loc) · 3.84 KB
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
// Lute - A structured markdown engine.
// Copyright (c) 2019-present, b3log.org
//
// Lute is licensed under the Mulan PSL v1.
// You can use this software according to the terms and conditions of the Mulan PSL v1.
// You may obtain a copy of Mulan PSL v1 at:
// http://license.coscl.org.cn/MulanPSL
// 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 v1 for more details.
package lute
func (context *Context) parseTable(paragraph *Node) (ret *Node) {
lines := split(paragraph.tokens, itemNewline)
length := len(lines)
if 2 > length {
return
}
aligns := context.parseTableDelimRow(trimWhitespace(lines[1]))
if nil == aligns {
return
}
headRow := context.parseTableRow(trimWhitespace(lines[0]), aligns, true)
if nil == headRow {
return
}
ret = &Node{typ: NodeTable, tableAligns: aligns}
ret.tableAligns = aligns
ret.AppendChild(context.newTableHead(headRow))
for i := 2; i < length; i++ {
tableRow := context.parseTableRow(trimWhitespace(lines[i]), aligns, false)
if nil == tableRow {
return
}
ret.AppendChild(tableRow)
}
return
}
func (context *Context) newTableHead(headRow *Node) *Node {
ret := &Node{typ: NodeTableHead}
tr := &Node{typ: NodeTableRow}
ret.AppendChild(tr)
for c := headRow.firstChild; nil != c; {
next := c.next
tr.AppendChild(c)
c = next
}
return ret
}
func (context *Context) parseTableRow(line []byte, aligns []int, isHead bool) (ret *Node) {
ret = &Node{typ: NodeTableRow, tableAligns: aligns}
cols := splitWithoutBackslashEscape(line, itemPipe)
if 1 > len(cols) {
return nil
}
if isBlank(cols[0]) {
cols = cols[1:]
}
if len(cols) > 0 && isBlank(cols[len(cols)-1]) {
cols = cols[:len(cols)-1]
}
colsLen := len(cols)
alignsLen := len(aligns)
if isHead && colsLen > alignsLen { // 分隔符行定义了表的列数,如果表头列数还大于这个列数,则说明不满足表格式
return nil
}
var i int
var col []byte
for ; i < colsLen && i < alignsLen; i++ {
col = trimWhitespace(cols[i])
cell := &Node{typ: NodeTableCell, tableCellAlign: aligns[i]}
if !context.option.VditorWYSIWYG {
length := len(col)
var token byte
for i := 0; i < length; i++ {
token = col[i]
if token == itemBackslash && i < length-1 && col[i+1] == itemPipe {
col = append(col[:i], col[i+1:]...)
length--
}
}
}
cell.tokens = col
ret.AppendChild(cell)
}
// 可能需要补全剩余的列
for ; i < alignsLen; i++ {
cell := &Node{typ: NodeTableCell, tableCellAlign: aligns[i]}
ret.AppendChild(cell)
}
return
}
func (context *Context) parseTableDelimRow(line []byte) (aligns []int) {
length := len(line)
if 1 > length {
return nil
}
var token byte
var i int
for ; i < length; i++ {
token = line[i]
if itemPipe != token && itemHyphen != token && itemColon != token && itemSpace != token {
return nil
}
}
cols := splitWithoutBackslashEscape(line, itemPipe)
if isBlank(cols[0]) {
cols = cols[1:]
}
if len(cols) > 0 && isBlank(cols[len(cols)-1]) {
cols = cols[:len(cols)-1]
}
var alignments []int
for _, col := range cols {
col = trimWhitespace(col)
if 1 > length || nil == col {
return nil
}
align := context.tableDelimAlign(col)
if -1 == align {
return nil
}
alignments = append(alignments, align)
}
return alignments
}
func (context *Context) tableDelimAlign(col []byte) int {
length := len(col)
if 1 > length {
return -1
}
var left, right bool
first := col[0]
left = itemColon == first
last := col[length-1]
right = itemColon == last
i := 1
var token byte
for ; i < length-1; i++ {
token = col[i]
if itemHyphen != token {
return -1
}
}
if left && right {
return 2
}
if left {
return 1
}
if right {
return 3
}
return 0
}