feat(gemtext): write a table wider than 80 columns as records
All checks were successful
check / check (pull_request) Successful in 31s
All checks were successful
check / check (pull_request) Successful in 31s
A preformatted block is not wrapped by the readers, so a table with long cells (the data retention table of the services page) became unreadable. A wide table with a header row is now a heading for each row and a bullet for each other cell, named by its column; without a header, one bullet per row. Refs #10
This commit is contained in:
parent
2f169b8d9f
commit
889edbfd63
2 changed files with 76 additions and 2 deletions
|
|
@ -591,14 +591,72 @@ func (c *converter) table(n *node) (block, error) {
|
|||
if caption != "" {
|
||||
text = append(text, protect(caption))
|
||||
}
|
||||
if len(rows) > 0 {
|
||||
switch {
|
||||
case len(rows) == 0:
|
||||
case tableWidth(rows) <= maxTableWidth:
|
||||
text = append(text, "```")
|
||||
text = append(text, alignRows(rows, headerRow)...)
|
||||
text = append(text, "```")
|
||||
default:
|
||||
if caption != "" {
|
||||
text = append(text, "")
|
||||
}
|
||||
text = append(text, records(rows, headerRow)...)
|
||||
}
|
||||
return block{text: text, links: links}, nil
|
||||
}
|
||||
|
||||
// maxTableWidth is the widest a table can be and stay a preformatted block: lines
|
||||
// of a preformatted block are not wrapped by the readers.
|
||||
const maxTableWidth = 80
|
||||
|
||||
// tableWidth returns the width of the rows written as columns two spaces apart.
|
||||
func tableWidth(rows [][]string) int {
|
||||
widest := 0
|
||||
for _, line := range alignRows(rows, false) {
|
||||
if w := utf8.RuneCountInString(line); w > widest {
|
||||
widest = w
|
||||
}
|
||||
}
|
||||
return widest
|
||||
}
|
||||
|
||||
// records writes the rows of a wide table as a list. With a header row each
|
||||
// row is a heading, its first cell, and a bullet for each other cell that is not
|
||||
// empty, named by the header of its column. Without one each row is a bullet
|
||||
// with the cells between bars.
|
||||
func records(rows [][]string, header bool) []string {
|
||||
var lines []string
|
||||
if !header {
|
||||
for _, r := range rows {
|
||||
lines = append(lines, "* "+strings.Join(r, " | "))
|
||||
}
|
||||
return lines
|
||||
}
|
||||
names := rows[0]
|
||||
for i, r := range rows[1:] {
|
||||
if i > 0 {
|
||||
lines = append(lines, "")
|
||||
}
|
||||
title := ""
|
||||
if len(r) > 0 {
|
||||
title = r[0]
|
||||
}
|
||||
lines = append(lines, "### "+title)
|
||||
for j := 1; j < len(r); j++ {
|
||||
if r[j] == "" {
|
||||
continue
|
||||
}
|
||||
name := ""
|
||||
if j < len(names) {
|
||||
name = names[j] + ": "
|
||||
}
|
||||
lines = append(lines, "* "+name+r[j])
|
||||
}
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
// alignRows writes rows of cells as columns two spaces apart, each as wide as
|
||||
// its widest cell, with a line of dashes under the first row when it is a header.
|
||||
func alignRows(rows [][]string, header bool) []string {
|
||||
|
|
|
|||
|
|
@ -133,3 +133,19 @@ func TestTextAndLink(t *testing.T) {
|
|||
t.Errorf("Link() without a label = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConvertWideTable checks that a table that does not fit in 80 columns becomes a list of records, a
|
||||
// heading for the first cell of each row and a bullet for each other cell, named by its column.
|
||||
func TestConvertWideTable(t *testing.T) {
|
||||
long := strings.Repeat("word ", 20) + "end"
|
||||
html := `<table><caption>Data</caption><thead><tr><th>Service</th><th>Kept</th><th>Seen</th></tr></thead>` +
|
||||
`<tbody><tr><td><a href="/m">Mail</a></td><td>` + long + `</td><td>all</td></tr><tr><td>Chat</td><td></td><td>some</td></tr></tbody></table>`
|
||||
want := "Data\n\n### Mail\n* Kept: " + long + "\n* Seen: all\n\n### Chat\n* Seen: some\n=> /m Mail\n"
|
||||
if got := convert(t, html); got != want {
|
||||
t.Errorf("Convert() =\n%q\nwant\n%q", got, want)
|
||||
}
|
||||
noHeader := `<table><tr><td>a</td><td>` + long + `</td></tr><tr><td>b</td><td>c</td></tr></table>`
|
||||
if got, want := convert(t, noHeader), "* a | "+long+"\n* b | c\n"; got != want {
|
||||
t.Errorf("Convert() without a header =\n%q\nwant\n%q", got, want)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue