-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement_table.go
321 lines (286 loc) · 6.48 KB
/
element_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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// SPDX-FileCopyrightText: 2020 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package asciidoctor
import (
"bytes"
"fmt"
"strings"
"git.sr.ht/~shulhan/pakakeh.go/lib/math/big"
libstrings "git.sr.ht/~shulhan/pakakeh.go/lib/strings"
)
type elementTable struct {
styles map[string]string
rows []*tableRow
formats []*columnFormat
classes attributeClass
ncols int
hasHeader bool
hasFooter bool
}
func newTable(ea *elementAttribute, content []byte) (table *elementTable) {
var (
row *tableRow
pt *tableParser
attrValue string
)
table = &elementTable{
classes: attributeClass{
classNameTableblock,
classNameFrameAll,
classNameGridAll,
},
styles: make(map[string]string),
}
attrValue = ea.Attrs[attrNameCols]
if len(attrValue) > 0 {
table.ncols, table.formats = parseAttrCols(attrValue)
}
table.parseOptions(ea.options)
pt = newTableParser(content)
if table.ncols == 0 {
row = pt.firstRow()
if row.ncell == 0 {
return table
}
table.ncols = row.ncell
} else {
row = pt.row(table.ncols)
}
if pt.nrow == 1 && !row.cells[0].endWithLF() {
if !libstrings.IsContain(ea.options, attrValueNoHeader) {
if pt.x < len(pt.cells) && pt.cells[pt.x] == nil {
table.hasHeader = true
}
}
}
for row.ncell == table.ncols {
table.rows = append(table.rows, row)
row = pt.row(table.ncols)
}
if len(table.rows) == 1 {
if !libstrings.IsContain(ea.options, attrValueHeader) {
table.hasHeader = false
}
}
if len(table.formats) == 0 {
var x int
for x = 0; x < table.ncols; x++ {
table.formats = append(table.formats, newColumnFormat())
}
}
table.recalculateWidth()
table.initializeFormats()
table.initializeClassAndStyles(ea)
return table
}
func (table *elementTable) initializeFormats() {
var (
format *columnFormat
)
for _, format = range table.formats {
var classes = []string{classNameTableBlock}
switch format.alignHor {
case colAlignTop:
classes = append(classes, classNameHalignLeft)
case colAlignMiddle:
classes = append(classes, classNameHalignCenter)
case colAlignBottom:
classes = append(classes, classNameHalignRight)
}
switch format.alignVer {
case colAlignTop:
classes = append(classes, classNameValignTop)
case colAlignMiddle:
classes = append(classes, classNameValignMiddle)
case colAlignBottom:
classes = append(classes, classNameValignBottom)
}
format.classes = classes
}
}
func (table *elementTable) initializeClassAndStyles(ea *elementAttribute) {
var (
k string
v string
withWidth bool
)
for k, v = range ea.Attrs {
switch k {
case attrNameWidth:
if len(v) == 0 {
continue
}
if v[len(v)-1] != '%' {
v += `%`
}
table.styles[k] = v
withWidth = true
case attrNameFrame:
switch v {
case attrValueTopbot:
table.classes.replace(classNameFrameAll,
classNameFrameEnds)
case attrValueSides:
table.classes.replace(classNameFrameAll,
classNameFrameSides)
case attrValueNone:
table.classes.replace(classNameFrameAll,
classNameFrameNone)
}
case attrNameGrid:
switch v {
case attrValueCols:
table.classes.replace(classNameGridAll,
classNameGridCols)
case attrValueNone:
table.classes.replace(classNameGridAll,
classNameGridNone)
case attrValueRows:
table.classes.replace(classNameGridAll,
classNameGridRows)
}
case attrNameStripes:
switch v {
case attrValueAll:
table.classes.add(classNameStripesAll)
case attrValueEven:
table.classes.add(classNameStripesEven)
case attrValueHover:
table.classes.add(classNameStripesHover)
case attrValueOdd:
table.classes.add(classNameStripesOdd)
}
}
}
for _, k = range ea.options {
if k == optNameAutowidth {
withWidth = true
table.classes.add(classNameFitContent)
var f *columnFormat
for _, f = range table.formats {
f.width = nil
}
}
}
for _, k = range ea.roles {
table.classes.add(k)
}
if !withWidth {
table.classes.add(classNameStretch)
}
}
func (table *elementTable) parseOptions(opts []string) {
if opts == nil {
return
}
var key string
for _, key = range opts {
switch key {
case attrValueHeader:
table.hasHeader = true
case attrValueFooter:
table.hasFooter = true
}
}
}
func (table *elementTable) recalculateWidth() {
var (
totalWidth = big.NewRat(0)
lastWidth = big.NewRat(100)
format *columnFormat
x int
hasAutowidth bool
)
for _, format = range table.formats {
if format.isAutowidth {
hasAutowidth = true
format.width = nil
} else {
totalWidth.Add(format.width)
}
}
for x, format = range table.formats {
if hasAutowidth {
continue
}
if x == len(table.formats)-1 {
format.width = lastWidth
} else {
format.width = big.QuoRat(format.width, totalWidth).Mul(100)
lastWidth.Sub(format.width)
}
}
}
func (table *elementTable) htmlStyle() string {
var (
buf bytes.Buffer
k string
v string
)
for k, v = range table.styles {
fmt.Fprintf(&buf, `%s: %s;`, k, v)
}
return buf.String()
}
// parseAttrCols parse the value of attribute "cols=".
//
// ATTR_COLS = (NCOLS ("*")) / COL_ATTR ("," COL_ATTR)
//
// NCOLS = 1*DIGITS
//
// COL_ATTR = (".") ( "<" / "^" / ">" ) ( COL_WIDTH ) (COL_STYLE)
//
// COL_WIDTH = DIGITS / (2DIGITs)
//
// COL_STYLE = "a" / "e" / "h" / "l" / "m" / "d" / "s" / "v"
func parseAttrCols(val string) (ncols int, formats []*columnFormat) {
val = strings.TrimSpace(val)
if len(val) == 0 {
return 0, nil
}
var (
rawFormat = strings.Split(val, `,`)
format *columnFormat
n int
x int
idxFromLast int
hasDefault bool
)
n, format = parseColumnFormat(rawFormat[0])
if format.isDefault {
hasDefault = true
ncols = n
format.isDefault = false
for x = 0; x < n; x++ {
var f = newColumnFormat()
f.merge(format)
formats = append(formats, f)
}
} else {
ncols = len(rawFormat)
formats = append(formats, format)
}
idxFromLast = ncols - (len(rawFormat) - 1)
for x = 1; x < len(rawFormat); x++ {
_, format = parseColumnFormat(rawFormat[x])
if hasDefault {
formats[idxFromLast].merge(format)
idxFromLast++
} else {
formats = append(formats, format)
}
}
return ncols, formats
}
// parseToRawRows convert raw table content into multiple raw rows.
func parseToRawRows(raw []byte) (rows [][]byte) {
var (
lines = bytes.Split(raw, []byte{'\n'})
line []byte
)
for _, line = range lines {
line = bytes.TrimSpace(line)
rows = append(rows, line)
}
return rows
}