feat(gemtext): write a table wider than 80 columns as records
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:
tgiachi 2026-11-16 12:28:54 +01:00
commit 889edbfd63
3 changed files with 76 additions and 2 deletions

View file

@ -86,7 +86,7 @@ Pages: `Title`, `ID` (the same in every language, it links the translations), `D
- **Language at the root.** The language served by default sits at the root and the others under `/<code>/`, the default language of the site included. It is set with `-lang`, else `gemini_language` in `site.json`, else the default language of the site. The server must be started with the same default (Agate: `--lang`). It must be a language that has a home page.
- **`.meta`.** The server reads the language of each document from `<out>/.meta`, which it never serves and no page links to. For every language other than the root one there are two lines, `en/*:;lang=en` and `en/**/*:;lang=en`, because a `*` does not cross folders, and the same two lines for `news/` when the news are not in the root language. It is for Agate run with `--central-conf`. A post that names another `Language` than the news is not given its own line.
- **News.** They exist once, under `/news/`, as in the website: the index, the posts, the categories, the archives and `news/feed.gmi`, a list of `=> /news/2026/11/post.gmi 2026-11-14 Title` lines, newest first, which feed readers subscribe to. Every page ends with the navigation of its language, which has the news. `{{news_blog}}` lists all the posts, without pages of ten.
- **Conversion.** A heading is a heading (three levels), a paragraph is a line, the links of a paragraph, list, quote or table follow it on lines of their own, a list is bullets (a list of only links is link lines), a quote or a note is a quote, code is a preformatted block, a table is a preformatted block of aligned columns, an image with an alt text is a link line, and emphasis is dropped. A page has one first level heading: its title; a first heading that says the title is dropped and the others go one level down. The blocks (`{{news_list}}`, `{{latest_news}}` and the others) become generated lines, with the texts of the language file.
- **Conversion.** A heading is a heading (three levels), a paragraph is a line, the links of a paragraph, list, quote or table follow it on lines of their own, a list is bullets (a list of only links is link lines), a quote or a note is a quote, code is a preformatted block, a table is a preformatted block of aligned columns, or, when it is wider than 80 columns, a heading for each row with a bullet for each cell, an image with an alt text is a link line, and emphasis is dropped. A page has one first level heading: its title; a first heading that says the title is dropped and the others go one level down. The blocks (`{{news_list}}`, `{{latest_news}}` and the others) become generated lines, with the texts of the language file.
- **Addresses.** An address of the site becomes the address of its page in the capsule (`/people.html` is `/en/people.gmi`), and a fragment is dropped. A file listed in `assets` is copied when a page points to it, and only then. `http`, `https`, `mailto`, `gemini`, `xmpp`, `irc`, `ircs`, `matrix`, `bitcoin`, `magnet`, `tel` and `sms` links stay as they are. A path the website serves by other means (`/~squid/`) becomes a link to the website when it starts with one of the comma separated prefixes of `gemini_web_paths` in `site.json`. Any other address that leads nowhere stops the build and names the page.
## Development

View file

@ -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 {

View file

@ -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)
}
}