/
table.go
326 lines (312 loc) · 9.5 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
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
322
323
324
325
326
package sgml
import (
"fmt"
"strconv"
"strings"
"github.com/bytesparadise/libasciidoc/pkg/types"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func (r *sgmlRenderer) renderTable(ctx *context, t *types.Table) (string, error) {
number := 0
fit := "stretch"
frame := t.Attributes.GetAsStringWithDefault(types.AttrFrame, "all")
grid := t.Attributes.GetAsStringWithDefault(types.AttrGrid, "all")
float := t.Attributes.GetAsStringWithDefault(types.AttrFloat, "")
stripes := t.Attributes.GetAsStringWithDefault(types.AttrStripes, "")
width, _ := strconv.Atoi(
strings.TrimSuffix(t.Attributes.GetAsStringWithDefault(types.AttrWidth, ""), "%"))
// These are derived from asciidoctor, and our rules here:
// * Width can be a number or a percentage
// * If width is >= 100, then it becomes "stretch" role, and we clear it
// * If width is any other number (besides 0), we do not use the fitting role,
// and instead use an explicit style for the width.
// * If width is unset, and %autowidth is set, then we use a fit-content role.
// * If none of the above cases are true, we use stretch role (default)
if t.Attributes.HasOption("autowidth") {
fit = "fit-content"
}
if width >= 100 {
width = 0
fit = "stretch"
} else if width > 0 {
fit = ""
}
caption := &strings.Builder{}
if t.Attributes.Has(types.AttrTitle) {
c, found := t.Attributes.GetAsString(types.AttrCaption)
if !found {
c, found = ctx.attributes.GetAsString(types.AttrTableCaption)
if found && c != "" {
// We always append the figure number, unless the caption is disabled.
// This is for asciidoctor compatibility.
c += " {counter:table-number}. "
}
}
// TODO: This is a very primitive and incomplete replacement of the counter attribute only.
// This should be removed when attribute values are allowed to contain attributes.
// Also this expansion should be limited to just singly quoted strings in the Attribute list,
// or the default. Ultimately this should all be done long before it gets into the renderer.
if strings.Contains(c, "{counter:table-number}") {
number = ctx.GetAndIncrementTableCounter()
c = strings.ReplaceAll(c, "{counter:table-number}", strconv.Itoa(number))
}
caption.WriteString(c)
}
columns, err := t.Columns()
if err != nil {
return "", errors.Wrap(err, "failed to render table")
}
header, err := r.renderTableHeader(ctx, t.Header, columns)
if err != nil {
return "", errors.Wrap(err, "failed to render table")
}
footer, err := r.renderTableFooter(ctx, t.Footer, columns)
if err != nil {
return "", errors.Wrap(err, "failed to render table")
}
body, err := r.renderTableBody(ctx, t.Rows, columns)
if err != nil {
return "", errors.Wrap(err, "failed to render table")
}
roles, err := r.renderElementRoles(ctx, t.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render table roles")
}
title, err := r.renderElementTitle(ctx, t.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render table title")
}
return r.execute(r.table, struct {
Context *context
ID string
Title string
Columns []*types.TableColumn
TableNumber int
Caption string
Frame string
Grid string
Fit string
Float string
Stripes string
Width int
Roles string
Header string
Body string
Footer string
}{
Context: ctx,
ID: r.renderElementID(t.Attributes),
Title: title,
Columns: columns,
TableNumber: number,
Caption: caption.String(),
Roles: roles,
Frame: frame,
Grid: grid,
Fit: fit,
Float: float,
Stripes: stripes,
Width: width,
Header: header,
Body: body,
Footer: footer,
})
}
func (r *sgmlRenderer) renderTableHeader(ctx *context, h *types.TableRow, cols []*types.TableColumn) (string, error) {
if h == nil {
return "", nil
}
content := &strings.Builder{}
col := 0
for _, cell := range h.Cells {
c, err := r.renderTableHeaderCell(ctx, cell, cols[col%len(cols)])
col++
if err != nil {
return "", errors.Wrap(err, "unable to render header")
}
content.WriteString(c)
}
return r.execute(r.tableHeader, struct {
Content string
}{
Content: content.String(),
})
}
func (r *sgmlRenderer) renderTableHeaderCell(ctx *context, c *types.TableCell, col *types.TableColumn) (string, error) {
// assume that elements to render are within the first element of the cell, which should be a paragraph
if len(c.Elements) == 1 {
if p, ok := c.Elements[0].(*types.Paragraph); ok {
content, err := r.renderInlineElements(ctx, p.Elements)
if err != nil {
return "", errors.Wrap(err, "unable to render header cell")
}
return r.execute(r.tableHeaderCell, struct {
HAlign types.HAlign
VAlign types.VAlign
Content string
}{
HAlign: col.HAlign,
VAlign: col.VAlign,
Content: content,
})
}
}
return "", fmt.Errorf("invalid header content (expected a single paragraph)")
}
func (r *sgmlRenderer) renderTableFooter(ctx *context, f *types.TableRow, cols []*types.TableColumn) (string, error) {
if f == nil {
return "", nil
}
content := &strings.Builder{}
col := 0
for _, cell := range f.Cells {
c, err := r.renderTableFooterCell(ctx, cell, cols[col%len(cols)])
col++
if err != nil {
return "", errors.Wrap(err, "unable to render footer")
}
content.WriteString(c)
}
return r.execute(r.tableFooter, struct {
Context *context
Content string
Cells []*types.TableCell
}{
Context: ctx,
Content: content.String(),
Cells: f.Cells,
})
}
func (r *sgmlRenderer) renderTableFooterCell(ctx *context, c *types.TableCell, col *types.TableColumn) (string, error) {
// assume that elements to render are within the first element of the cell, which should be a paragraph
if len(c.Elements) == 1 {
if p, ok := c.Elements[0].(*types.Paragraph); ok {
content, err := r.renderInlineElements(ctx, p.Elements)
if err != nil {
return "", errors.Wrap(err, "unable to render footer cell")
}
return r.execute(r.tableFooterCell, struct {
HAlign types.HAlign
VAlign types.VAlign
Content string
}{
HAlign: col.HAlign,
VAlign: col.VAlign,
Content: content,
})
}
}
return "", fmt.Errorf("invalid footer content (expected a single paragraph)")
}
func (r *sgmlRenderer) renderTableBody(ctx *context, rows []*types.TableRow, columns []*types.TableColumn) (string, error) {
content := &strings.Builder{}
for _, row := range rows {
c, err := r.renderTableRow(ctx, row, columns)
if err != nil {
return "", errors.Wrap(err, "unable to render body")
}
content.WriteString(c)
}
return r.execute(r.tableBody, struct {
Context *context
Content string
Rows []*types.TableRow
Columns []*types.TableColumn
}{
Context: ctx,
Content: content.String(),
Rows: rows,
Columns: columns,
})
}
func (r *sgmlRenderer) renderTableRow(ctx *context, l *types.TableRow, cols []*types.TableColumn) (string, error) {
content := &strings.Builder{}
for i, cell := range l.Cells {
c, err := r.renderTableCell(ctx, cell, cols[i])
if err != nil {
return "", errors.Wrap(err, "unable to render row")
}
content.WriteString(c)
}
return r.execute(r.tableRow, struct {
Context *context
Content string
Cells []*types.TableCell
}{
Context: ctx,
Content: content.String(),
Cells: l.Cells,
})
}
func (r *sgmlRenderer) renderTableCell(ctx *context, cell *types.TableCell, col *types.TableColumn) (string, error) {
buff := &strings.Builder{}
for _, element := range cell.Elements {
renderedElement, err := r.renderTableCellBlock(ctx, element)
if err != nil {
return "", err
}
buff.WriteString(renderedElement)
}
tmpl := r.tableCell
if col.Style == types.HeaderStyle {
tmpl = r.tableHeaderCell
}
return r.execute(tmpl, struct {
Context *context
Content string
Cell *types.TableCell
HAlign types.HAlign
VAlign types.VAlign
}{
Context: ctx,
Content: buff.String(),
Cell: cell,
HAlign: col.HAlign,
VAlign: col.VAlign,
})
}
func (r *sgmlRenderer) renderTableCellBlock(ctx *context, element interface{}) (string, error) {
switch e := element.(type) {
case *types.Paragraph:
log.Debug("rendering paragraph within table cell")
content, err := r.renderElements(ctx, e.Elements)
if err != nil {
return "", errors.Wrap(err, "unable to render table cell paragraph content")
}
title, err := r.renderElementTitle(ctx, e.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render table cell paragraph content")
}
result, err := r.execute(r.embeddedParagraph, struct {
Context *context
ID string // TODO: not used in template?
Title string // TODO: not used in template?
CheckStyle string
Class string
Content string
}{
Context: ctx,
ID: r.renderElementID(e.Attributes),
Title: title,
Class: "tableblock",
CheckStyle: renderCheckStyle(e.Attributes[types.AttrCheckStyle]),
Content: string(content),
})
if err != nil {
return "", errors.Wrap(err, "unable to render table cell paragraph content")
}
return strings.TrimSuffix(result, "\n"), nil
default:
// Note: Asciidoctor wraps the `<div class=imageblock>` elements within a `<div class="content">`, which we also do here for the sake of compatibility
renderedElement, err := r.renderElement(ctx, e)
if err != nil {
return "", errors.Wrap(err, "unable to render table cell")
}
return r.execute(r.tableCellBlock, struct {
Content string
}{
Content: renderedElement,
})
}
}