motorino/internal/build/gemini_news.go
tgiachi 3764470e9b
All checks were successful
check / check (pull_request) Successful in 30s
fix(gemini): list items, spaces in addresses, wide tables, per post language, empty lists, layout images
- a list item with no text (only an image or a link without a label) has no empty bullet,
  its links are still written
- paragraphs and table cells of a list item do not stick together, and a preformatted
  block in an item stays a block after its bullet
- spaces in the address of a link line are written as %20
- a wide table with an empty first cell has no empty heading, a wide table with only a
  header or only one column is written as bullets, and the header is not lost
- a post in another language than the news has its own line in .meta, before the lines
  of the folders: the server uses the first line that matches a document (checked with
  Agate 3.3.25)
- an empty {{news_archive}} or {{categories_list}} leaves nothing, with its heading, so
  a site without news says so once
- images of the layout (class logo, mark or decorative, role presentation or none) leave
  no link line and the file is not copied
- the command test names files instead of counting them, and -clean and the warning
  about old files work for the gemini command too

Refs #10
2026-11-16 12:53:37 +01:00

249 lines
8.2 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"fmt"
"strings"
"git.kmalloc.org/squid/motorino/internal/content"
"git.kmalloc.org/squid/motorino/internal/gemtext"
)
// newsLines returns the lines of a list of posts: a link line for each, with
// its date in front of the title, the format feed readers know, and the
// description under it when asked for.
func newsLines(posts []content.Post, withDescription bool) string {
var lines []string
for _, p := range posts {
lines = append(lines, gemtext.Link(gemPostAddress(p), p.Date.Format(dateOnly)+" "+p.Title))
if withDescription && p.Description != "" {
lines = append(lines, gemtext.Text(p.Description))
}
}
return strings.Join(lines, "\n")
}
// block returns the gemtext of a generated list. The blog list is the whole list
// of the posts: a capsule has no pages of ten.
func (g *gemini) block(name, lang string) (string, error) {
l := g.langs[lang]
empty := gemtext.Text(l.Strings["news_empty"])
switch name {
case "news_list", "news_blog":
if len(g.posts) == 0 {
return empty, nil
}
return newsLines(g.posts, true), nil
case "latest_news":
return g.latestNews(lang), nil
case "categories_list":
lines := make([]string, len(g.categories))
for i, c := range g.categories {
lines[i] = gemtext.Link(gemCategoryAddress(c.Slug), fmt.Sprintf("%s (%d)", c.Name, len(c.posts)))
}
return strings.Join(lines, "\n"), nil
default:
return g.archiveLines(lang), nil
}
}
// latestNews returns the block of the latest news of the home page: a heading
// with the name of the news in the menu, the sentence news_p, the first posts
// and the link to all of them. It is empty without posts.
func (g *gemini) latestNews(lang string) string {
if len(g.posts) == 0 {
return ""
}
l := g.langs[lang]
var lines []string
if name := l.Nav["news"]; name != "" {
lines = append(lines, "#"+heading(name)+"\n")
}
if text := gemtext.Text(l.Strings["news_p"]); text != "" {
lines = append(lines, text)
}
n := newsOnHome
if n > len(g.posts) {
n = len(g.posts)
}
lines = append(lines, newsLines(g.posts[:n], false))
label := l.Strings["news_all"]
if label == "" {
label = l.Nav["news"]
}
return strings.Join(append(lines, gemtext.Link(gemNews, label)), "\n")
}
// archiveLines returns the years that have posts, each followed by its months, or
// nothing when there are none: the list of the news says there is no news.
func (g *gemini) archiveLines(lang string) string {
years := groupArchive(g.posts)
if len(years) == 0 {
return ""
}
var lines []string
for _, y := range years {
lines = append(lines, gemtext.Link(gemYearAddress(y.year), fmt.Sprintf("%d (%d)", y.year, len(y.posts))))
lines = append(lines, g.monthLines(y, lang)...)
}
return strings.Join(lines, "\n")
}
// monthLines returns the link lines of the months of a year.
func (g *gemini) monthLines(y archiveYear, lang string) []string {
lines := make([]string, len(y.months))
for i, m := range y.months {
lines[i] = gemtext.Link(gemMonthAddress(m.year, m.month), fmt.Sprintf("%s (%d)", monthYear(m.year, m.month, g.langs[lang]), len(m.posts)))
}
return lines
}
// newsDocument describes a page of the news for the navigation.
func (g *gemini) newsDocument(lang, path, title, source string) document {
return document{lang: lang, nav: "news", path: path, title: title, source: source}
}
// renderPosts converts every post.
func (g *gemini) renderPosts() error {
for i, p := range g.posts {
if err := g.renderPost(i, p); err != nil {
return err
}
}
return nil
}
// renderPost converts a post: the title, a line with the date, the author, the
// categories and the reading time, the summary when it was written, the text,
// the link to reply on Mastodon, and the links to the older and the newer post
// and to all the news.
func (g *gemini) renderPost(i int, p content.Post) error {
l := g.langs[p.Language]
d := g.newsDocument(p.Language, p.Path, p.Title, p.Source)
ctx := g.documentContext(d)
html, err := g.markdown(p.Source, p.Body, ctx)
if err != nil {
return err
}
body, err := g.convert(p.Source, html, p.Path, p.Language)
if err != nil {
return err
}
parts := []string{longDate(p.Date, l)}
if p.Author != "" {
parts = append(parts, p.Author)
}
if len(p.Categories) > 0 {
names := make([]string, len(p.Categories))
for i, c := range p.Categories {
names[i] = c.Name
}
parts = append(parts, strings.Join(names, ", "))
}
if reading := readingText(p.ReadingMinutes(), l); reading != "" {
parts = append(parts, reading)
}
meta := []string{gemtext.Text(strings.Join(parts, " · "))}
for _, c := range p.Categories {
meta = append(meta, gemtext.Link(gemCategoryAddress(c.Slug), c.Name))
}
summary := ""
if p.HasDescription {
summary = gemtext.Text(p.Description)
}
mastodon := ""
if p.Mastodon != "" {
label := l.Strings["reply_mastodon"]
if label == "" {
return fmt.Errorf("%s: the post has a Mastodon address, but lang/%s.json has no reply_mastodon to label the link", p.Source, p.Language)
}
mastodon = gemtext.Link(p.Mastodon, label)
}
out := compose(heading(p.Title), strings.Join(meta, "\n"), summary, withoutTitle(p.Title, body), mastodon, g.postNavigation(i, l.Code), g.navigation(d))
return g.publish(gemFile(gemPostAddress(p)), p.Source, out)
}
// postNavigation returns the links to the newer and the older post and to all
// the news. A link whose label the language lacks is left out.
func (g *gemini) postNavigation(i int, lang string) string {
l := g.langs[lang]
var lines []string
if label := l.Strings["post_newer"]; label != "" && i > 0 {
lines = append(lines, gemtext.Link(gemPostAddress(g.posts[i-1]), label+": "+g.posts[i-1].Title))
}
if label := l.Strings["post_older"]; label != "" && i+1 < len(g.posts) {
lines = append(lines, gemtext.Link(gemPostAddress(g.posts[i+1]), label+": "+g.posts[i+1].Title))
}
if label := l.Strings["news_all"]; label != "" {
lines = append(lines, gemtext.Link(gemNews, label))
}
return strings.Join(lines, "\n")
}
// renderCategories writes the page of every category.
func (g *gemini) renderCategories() error {
l := g.langs[g.newsLang]
for _, c := range g.categories {
address := gemCategoryAddress(c.Slug)
d := g.newsDocument(g.newsLang, categoryPath(c.Slug), c.Name, fmt.Sprintf("the page of the category %q", c.Name))
title := c.Name
if h := l.Strings["category_h"]; h != "" {
title = h + ": " + c.Name
}
text := compose(heading(title), newsLines(c.posts, true), g.navigation(d))
if err := g.publish(gemFile(address), d.source, text); err != nil {
return err
}
}
return nil
}
// renderArchives writes the page of every year and every month that has posts.
func (g *gemini) renderArchives() error {
lang := g.newsLang
l := g.langs[lang]
page := func(address, htmlPath, name string, posts []content.Post, links []string, source string) error {
title := name
if h := l.Strings["archive_h"]; h != "" {
title = h + ": " + name
}
d := g.newsDocument(lang, htmlPath, name, source)
text := compose(heading(title), strings.Join(links, "\n"), newsLines(posts, true), g.navigation(d))
return g.publish(gemFile(address), source, text)
}
for _, y := range groupArchive(g.posts) {
year := fmt.Sprint(y.year)
if err := page(gemYearAddress(y.year), g.yearPath(y.year), year, y.posts, g.monthLines(y, lang), "the archive of "+year); err != nil {
return err
}
var up []string
if text := l.Strings["archive_up"]; text != "" {
up = []string{gemtext.Link(gemYearAddress(y.year), text+" "+year)}
}
for _, m := range y.months {
name := monthYear(m.year, m.month, l)
if err := page(gemMonthAddress(m.year, m.month), g.monthPath(m.year, m.month), name, m.posts, up, "the archive of "+name); err != nil {
return err
}
}
}
return nil
}
// renderFeed writes the list of the latest posts, newest first, a date before
// each title: the format that Gemini feed readers subscribe to.
func (g *gemini) renderFeed() error {
title := g.byKey[pageKey{"news", g.newsLang}].Title
if !strings.HasPrefix(title, sitePrefix) {
title = sitePrefix + title
}
posts := g.posts
if len(posts) > feedLimit {
posts = posts[:feedLimit]
}
text := compose(heading(title), newsLines(posts, false))
return g.publish(gemFile(gemNews+"feed.gmi"), "the feed", text)
}