-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_parser.go
177 lines (159 loc) · 3.4 KB
/
table_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
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
177
// SPDX-FileCopyrightText: 2020 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package asciidoctor
import (
"bytes"
"strings"
libstrings "git.sr.ht/~shulhan/pakakeh.go/lib/strings"
)
type tableParser struct {
p *libstrings.Parser
cells []*tableCell
nrow int
x int
}
func newTableParser(content []byte) (pt *tableParser) {
pt = &tableParser{
p: libstrings.NewParser(string(content), "|\n"),
}
pt.toCells()
return pt
}
// toCells parse the raw table content into cells.
func (pt *tableParser) toCells() {
var (
token, c = pt.p.ReadEscaped('\\')
tokenTrim = strings.TrimSpace(token)
l = len(tokenTrim)
cell = &tableCell{}
cf *cellFormat
)
// Parse the first cell with three possibilities,
// Case 1: cell without '|', for example "...\n|..."
// Case 2: empty line, for example "\n\n|..."
// Case 3: cell with formatting "3*|..."
for c == '\n' {
if l > 0 {
// Case 1.
cell.writeString(token)
cell.writeByte('\n')
} else {
// Case 2.
pt.cells = append(pt.cells, nil)
}
token, c = pt.p.ReadEscaped('\\')
tokenTrim = strings.TrimSpace(token)
l = len(tokenTrim)
}
if c == 0 {
if l > 0 {
cell.writeString(token)
pt.addCell(cell)
}
return
}
if c == '|' {
cf = parseCellFormat(token)
if cf == nil {
if l > 0 {
// Case 1.
cell.writeString(token)
pt.cells = append(pt.cells, cell)
cell = &tableCell{}
}
} else {
// Case 3.
cell.format = *cf
}
}
token, c = pt.p.ReadEscaped('\\')
tokenTrim = strings.TrimSpace(token)
l = len(tokenTrim)
for {
// We use if-condition with "continue" to break and continue
// the for-loop, so it is not possible to use switch-case
// here.
//
//nolint:gocritic
if c == '\n' {
if l > 0 {
cell.writeString(token)
}
cell.writeByte('\n')
} else if c == '|' {
cf = parseCellFormat(token)
if cf == nil {
cell.writeString(token)
pt.addCell(cell)
cell = &tableCell{}
} else {
pt.addCell(cell)
cell = &tableCell{
format: *cf,
}
}
} else {
cell.writeString(token)
pt.addCell(cell)
break
}
token, c = pt.p.ReadEscaped('\\')
tokenTrim = strings.TrimSpace(token)
l = len(tokenTrim)
}
}
func (pt *tableParser) addCell(cell *tableCell) {
var (
emptyLine = []byte("\n\n")
endWithEmptyLine = bytes.HasSuffix(cell.content, emptyLine)
)
if endWithEmptyLine {
cell.content = bytes.TrimSuffix(cell.content, emptyLine)
}
pt.cells = append(pt.cells, cell)
if endWithEmptyLine {
pt.cells = append(pt.cells, nil)
}
}
// firstRow get the first row of the table to get the number of columns.
func (pt *tableParser) firstRow() (row *tableRow) {
var (
cell *tableCell
)
row = &tableRow{}
// Skip empty lines..
for ; pt.x < len(pt.cells) && pt.cells[pt.x] == nil; pt.x++ {
pt.nrow++
}
for ; pt.x < len(pt.cells); pt.x++ {
cell = pt.cells[pt.x]
if cell == nil {
// Row with empty line.
break
}
row.add(cell)
if cell.endWithLF() && row.ncell > 1 {
// Row with multiple columns in single line.
pt.x++
break
}
}
pt.nrow++
return row
}
// row get n number of cells as row, skip any nil cell if exist on list.
func (pt *tableParser) row(ncols int) (row *tableRow) {
row = &tableRow{}
for ; pt.x < len(pt.cells); pt.x++ {
if pt.cells[pt.x] == nil {
continue
}
row.add(pt.cells[pt.x])
if row.ncell == ncols {
pt.x++
break
}
}
pt.nrow++
return row
}