motorino/internal/build/gemini_news.go
tgiachi 2f169b8d9f feat(gemini): write the site as a Gemini capsule
`motorino gemini [-site DIR] [-out DIR] [-lang CODE] [-drafts]` turns the pages and the
news into gemtext with the new gemtext package.

- the language served by default sits at the root (-lang, then gemini_language in
  site.json, then the default language) and the others under /<code>/
- the news exist once, under /news/: index, posts, categories, archives and a
  gemlog style feed (news/feed.gmi, a date before each title)
- every page ends with the navigation of its language, the language links and, on
  translations, the machine translation note
- .meta for Agate (--central-conf): two lines per language other than the root one
  and two for the news when they are in another language, never linked
- addresses are mapped to the capsule, a fragment is dropped, a missing target stops
  the build, assets are copied only when a page points to them, gemini_web_paths
  keeps paths served by other means as links to the website
- blocks of generated lists become gemtext with the strings of the language file
- the block replacement of a page is shared with the HTML build

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

248 lines
8.1 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, empty), 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.
func (g *gemini) archiveLines(lang, empty string) string {
years := groupArchive(g.posts)
if len(years) == 0 {
return empty
}
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)
}