Store table separator indices

We want to support fat rows in tables - for that we need to know where the
separators are.
This commit is contained in:
Niklas Fasching 2020-06-26 20:05:59 +02:00
parent a383eef7a6
commit 5917d8fb1c

View file

@ -8,8 +8,9 @@ import (
) )
type Table struct { type Table struct {
Rows []Row Rows []Row
ColumnInfos []ColumnInfo ColumnInfos []ColumnInfo
SeparatorIndices []int
} }
type Row struct { type Row struct {
@ -43,7 +44,7 @@ func lexTable(line string) (token, bool) {
} }
func (d *Document) parseTable(i int, parentStop stopFn) (int, Node) { func (d *Document) parseTable(i int, parentStop stopFn) (int, Node) {
rawRows, start := [][]string{}, i rawRows, separatorIndices, start := [][]string{}, []int{}, i
for ; !parentStop(d, i); i++ { for ; !parentStop(d, i); i++ {
if t := d.tokens[i]; t.kind == "tableRow" { if t := d.tokens[i]; t.kind == "tableRow" {
rawRow := strings.FieldsFunc(d.tokens[i].content, func(r rune) bool { return r == '|' }) rawRow := strings.FieldsFunc(d.tokens[i].content, func(r rune) bool { return r == '|' })
@ -52,13 +53,14 @@ func (d *Document) parseTable(i int, parentStop stopFn) (int, Node) {
} }
rawRows = append(rawRows, rawRow) rawRows = append(rawRows, rawRow)
} else if t.kind == "tableSeparator" { } else if t.kind == "tableSeparator" {
separatorIndices = append(separatorIndices, i-start)
rawRows = append(rawRows, nil) rawRows = append(rawRows, nil)
} else { } else {
break break
} }
} }
table := Table{nil, getColumnInfos(rawRows)} table := Table{nil, getColumnInfos(rawRows), separatorIndices}
for _, rawColumns := range rawRows { for _, rawColumns := range rawRows {
row := Row{nil, isSpecialRow(rawColumns)} row := Row{nil, isSpecialRow(rawColumns)}
if len(rawColumns) != 0 { if len(rawColumns) != 0 {