This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
table.go
84 lines (72 loc) · 1.84 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
package blocks
import (
"fmt"
)
// Table is a table, which consists of several rows and has a caption.
type Table struct {
caption string // Make it Formatted maybe?
rows []TableRow
}
// ID returns table's id which is table- and its number.
func (t Table) ID(counter *IDCounter) string {
counter.tables++
return fmt.Sprintf("table-%d", counter.tables)
}
// NewTable returns a new Table with the given rows and caption.
func NewTable(caption string, rows []TableRow) Table {
return Table{
caption: caption,
rows: rows,
}
}
// Caption returns Table's caption. It may be empty.
func (t Table) Caption() string {
return t.caption
}
// Rows returns Table's rows.
func (t Table) Rows() []TableRow {
return t.rows
}
// WithNewRow returns a new table but with a new row.
func (t Table) WithNewRow(row TableRow) Table {
t.rows = append(t.rows, row)
return t
}
// TableRow is a row in a table. Thus, it can only be nested inside a table.
type TableRow struct {
cells []TableCell
}
// ID returns and empty string because table rows do not have ids.
func (tr TableRow) ID(_ *IDCounter) string {
return ""
}
// NewTableRow returns a new TableRow. It gets the hypha name from ctx.
func NewTableRow(cells []TableCell) TableRow {
return TableRow{
cells: cells,
}
}
// Cells returns the row's cells.
func (tr TableRow) Cells() []TableCell {
return tr.cells
}
// LooksLikeThead is true if the table row looks like it might as well be a thead row.
//
// Most likely, rows with more than two header cells are theads. I allow one extra datum cell for tables like this:
// | ! a ! b
// ! c | d | e
// ! f | g | h
func (tr TableRow) LooksLikeThead() bool {
var (
headerAmount = 0
datumAmount = 0
)
for _, tc := range tr.Cells() {
if tc.IsHeaderCell() {
headerAmount++
} else {
datumAmount++
}
}
return headerAmount >= 2 && datumAmount <= 1
}