This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
/
table.go
217 lines (200 loc) · 4.76 KB
/
table.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package parser
import (
"bytes"
"github.com/bouncepaw/mycomarkup/v4/blocks"
"github.com/bouncepaw/mycomarkup/v4/mycocontext"
"regexp"
"strings"
"unicode"
)
func nextTable(ctx mycocontext.Context) (t blocks.Table, eof bool) {
line, eof := mycocontext.NextLine(ctx)
t, tableDone := tableFromFirstLine(line)
if tableDone || eof {
return t, eof
}
for {
row, rowFound, tableDone := nextTableRow(ctx)
if rowFound {
t = t.WithNewRow(row)
}
if tableDone {
break
}
}
_, eof = mycocontext.NextLine(ctx) // Ignore text after }
return t, eof
}
func nextTableRow(ctx mycocontext.Context) (row blocks.TableRow, foundRow, tableDone bool) {
var (
cleaningLeadingWhitespace = true
countingColspan = false
currColspan uint = 0
currCellMarker rune
cellText *bytes.Buffer
r rune
eof bool
cells []blocks.TableCell
)
runeWalker:
for {
r, eof = mycocontext.NextRune(ctx)
if eof {
tableDone = true
break
}
automaton:
switch {
case r == '\n':
break runeWalker
case cleaningLeadingWhitespace && unicode.IsSpace(r):
continue
case cleaningLeadingWhitespace: // When non-space rune, try again
cleaningLeadingWhitespace = false
goto automaton // The next and the nextnext case-clauses might trigger
case (!cleaningLeadingWhitespace && !countingColspan) && r == '}':
tableDone = true
break runeWalker
case (!cleaningLeadingWhitespace && !countingColspan) && (r == '|' || r == '!'):
// Proper cell marker, great! Let's start counting colspan then
currCellMarker = r
currColspan = 1
countingColspan = true
case countingColspan && r == currCellMarker:
currColspan++
case !cleaningLeadingWhitespace && !countingColspan, countingColspan:
mycocontext.UnreadRune(ctx)
var contents []blocks.Block
cellText, tableDone = nextTableCellContents(ctx)
parseSubdocumentForEachBlock(ctx, cellText, func(block blocks.Block) {
contents = append(contents, block)
})
cell := blocks.NewTableCell(currCellMarker == '!', currColspan, contents)
cells = append(cells, cell)
if tableDone {
break runeWalker
}
// Reset state
countingColspan = false
currColspan = 0
currCellMarker = 0
case r == '}':
tableDone = true
break runeWalker
}
}
if len(cells) == 0 {
return blocks.TableRow{}, false, tableDone
}
return blocks.NewTableRow(cells), true, tableDone
}
func nextTableCellContents(
ctx mycocontext.Context,
) (
contents *bytes.Buffer,
tableDone bool,
) {
var (
contentsBuilder bytes.Buffer
escaping = false
inLink = false
)
runeWalker:
for {
r, eof := mycocontext.NextRune(ctx)
if eof {
tableDone = true
break
}
switch {
case r == '\n':
mycocontext.UnreadRune(ctx)
break runeWalker
case escaping:
contentsBuilder.WriteRune(r)
escaping = false
case r == '\\':
contentsBuilder.WriteRune('\\')
escaping = true
case r == '[':
contentsBuilder.WriteRune('[')
r, eof = mycocontext.NextRune(ctx)
if r == '[' {
inLink = true
}
contentsBuilder.WriteRune(r)
case inLink && r == ']':
contentsBuilder.WriteRune(']')
r, eof = mycocontext.NextRune(ctx)
if r == ']' {
inLink = false
}
contentsBuilder.WriteRune(r)
case !inLink && r == '|', r == '!': // looks like a new cell
mycocontext.UnreadRune(ctx)
break runeWalker
case !inLink && r == '{':
contentsBuilder.WriteString(nextTableMultiline(ctx))
case r == '}':
tableDone = true
default:
contentsBuilder.WriteRune(r)
}
}
return &contentsBuilder, tableDone
}
// return text until the next unmatched unescaped } (exclusively).
func nextTableMultiline(ctx mycocontext.Context) string {
var (
curlyCount = 1 // 1 is the initial state: multiline open. When it is 0, done.
escaping = false
onNewLine = true
r rune
eof bool
ret strings.Builder
)
for {
r, eof = mycocontext.NextRune(ctx)
if eof {
break
}
automaton:
switch {
case r == '\n':
onNewLine = true
case escaping:
escaping = false
case onNewLine && unicode.IsSpace(r):
continue
case onNewLine:
onNewLine = false
goto automaton
case r == '\\':
escaping = true
case r == '{':
curlyCount++
case r == '}':
curlyCount--
}
if curlyCount == 0 {
break
}
ret.WriteRune(r)
}
return ret.String()
}
var tableRe = regexp.MustCompile(`^table\s*{`)
func matchesTable(ctx mycocontext.Context) bool {
return tableRe.Match(ctx.Input().Bytes())
}
func tableFromFirstLine(line string) (t blocks.Table, done bool) { // V3
var (
caption = line[strings.IndexRune(line, '{')+1:]
closeBracePos = strings.IndexRune(caption, '}')
isClosed = closeBracePos != -1
)
if isClosed {
caption = caption[:closeBracePos]
}
return blocks.NewTable(caption, make([]blocks.TableRow, 0)), isClosed
}