Compare commits

...
Author SHA1 Message Date
semantic-release-bot
bc713769b9 chore(release): 1.3.0 [skip ci]
## [1.3.0](https://git.kmalloc.org/squid/motorino/compare/v1.2.0...v1.3.0) (2026-11-16)

### Features

* **build:** report the old files of the output folder, add -clean ([52edb15](52edb15b64)), closes [#9](#9)
* **feed:** carry the full text in content:encoded ([3f62c79](3f62c79603)), closes [#9](#9)
* **gemini:** write the site as a Gemini capsule ([2f169b8](2f169b8d9f)), closes [#10](#10)
* **gemtext:** convert the HTML of a page to gemtext ([28745d4](28745d452f)), closes [#10](#10)
* **gemtext:** write a table wider than 80 columns as records ([889edbf](889edbfd63)), closes [#10](#10)
* motorino new writes a draft, motorino build -drafts previews it ([633da1c](633da1c46b)), closes [#9](#9)
* **news:** a post page with date, author, categories, summary and links ([bf50b2e](bf50b2e1c2)), closes [#9](#9)
* **news:** blog list with pages, and an archive by year and month ([4a501d7](4a501d77b3)), closes [#9](#9)
* **news:** hide drafts and future posts, give the news one language ([26edf78](26edf78449)), closes [#9](#9)
* **news:** show the posts of the lists as note panels ([71cf140](71cf14006d)), closes [#9](#9)

### Bug Fixes

* **gemini:** list items, spaces in addresses, wide tables, per post language, empty lists, layout images ([3764470](3764470e9b)), closes [#10](#10)
* **news:** group the archive by the month as written, drop the empty lists ([6987541](6987541f88)), closes [#9](#9)
2026-11-16 12:56:23 +01:00
35b123dec8 Merge pull request 'feat: write the site as a Gemini capsule' (#12) from feat/gemini-output into main
All checks were successful
check / check (push) Successful in 31s
2026-11-16 11:55:39 +00:00
80c31d0da0 Merge pull request 'News as blog posts: richer post page, blog list with pages, archives, drafts and motorino new' (#11) from feat/blog-news into main
Some checks failed
check / check (push) Has been cancelled
2026-11-16 11:55:34 +00:00
tgiachi
3764470e9b fix(gemini): list items, spaces in addresses, wide tables, per post language, empty lists, layout images
All checks were successful
check / check (pull_request) Successful in 30s
- 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
tgiachi
faff6476ab Merge branch 'feat/blog-news' into feat/gemini-output
All checks were successful
check / check (pull_request) Successful in 30s
2026-11-16 12:50:17 +01:00
tgiachi
52edb15b64 feat(build): report the old files of the output folder, add -clean
All checks were successful
check / check (pull_request) Successful in 29s
A build never emptied the output folder, so the pages of a list that got shorter stayed
behind. The build now returns the files it did not write and the command prints a
warning that names them. -clean empties the folder first, only when it is new, empty or
holds the .motorino-out file that an earlier -clean left, and never the folder of the site
or one that holds it. The default is unchanged: no extra file in a folder built without it,
so the deploy (a new folder published with rsync --delete) is not affected.

Refs #9
2026-11-16 12:49:17 +01:00
tgiachi
6987541f88 fix(news): group the archive by the month as written, drop the empty lists
- groupArchive compared each post with the last group only, so two posts near midnight
  at the end of a month, written with different offsets, listed the same month twice and
  the build failed with "both published as". Posts are grouped by their own year and
  month, newest first.
- {{news_archive}} is empty when there are no posts, so the index says there is no news
  once, and the heading of an empty {{news_archive}} or {{categories_list}} goes with it.
- tests for exactly ten posts, eleven posts, a single post, a title with quotes, an
  ampersand, a tag and non Latin letters, and a neighbour written in another language

Refs #9
2026-11-16 12:48:17 +01:00
tgiachi
cc90df8c6a Merge branch 'feat/blog-news' into feat/gemini-output
All checks were successful
check / check (pull_request) Successful in 29s
2026-11-16 12:32:39 +01:00
tgiachi
71cf14006d feat(news): show the posts of the lists as note panels
All checks were successful
check / check (pull_request) Successful in 30s
The blog list, the pages of a category and the pages of an archive show every post as a
note panel: a small line with the long date and the categories, the title in bold with
a full stop (unless it ends with a full stop, a question mark or an exclamation mark),
the description and a read more link whose aria-label names the post. The page of a
post, {{news_list}} and the latest news of the home page are unchanged.

A language without read_more gets the title as the link on the category and archive
pages, as those pages worked before; the blog list still needs the string.

Refs #9
2026-11-16 12:32:09 +01:00
tgiachi
889edbfd63 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
2026-11-16 12:28:54 +01:00
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
tgiachi
28745d452f feat(gemtext): convert the HTML of a page to gemtext
A new package, with no new dependency, that reads the HTML a page renders to (the
lenient mode of encoding/xml with the HTML void elements and entities) and writes
gemtext: three heading levels, one line paragraphs, the links of a paragraph, a list,
a quote or a table on lines of their own after it, bullets (a list of only links is
link lines), quotes (a note is a quote), preformatted blocks, tables as aligned
preformatted blocks, images as link lines, no emphasis, and lines that look like
gemtext are protected. Options.Link maps or drops an address and its error stops
the conversion; Options.Block expands the placeholders of generated blocks.

The language files also keep the order of the menu now (Lang.NavOrder), which the
Gemini output needs.

Refs #10
2026-11-16 12:19:38 +01:00
tgiachi
633da1c46b feat: motorino new writes a draft, motorino build -drafts previews it
All checks were successful
check / check (pull_request) Successful in 30s
motorino new -site DIR "A title" writes news/a-title.md with the header ready:
the date of now, the author from site.json, the category News and Draft: true.
motorino build -drafts publishes the drafts and the posts dated in the future,
for a preview. The README describes the header keys, the news language, the blog
placeholders and blocks, and the language strings they use.

Refs #9
2026-11-16 12:10:30 +01:00
tgiachi
3f62c79603 feat(feed): carry the full text in content:encoded
The description of an item is now the plain summary of the post, the full text with
absolute links goes in content:encoded (the end marker of a CDATA section inside
the text is split), and an item has its categories and its author (dc:creator).

Refs #9
2026-11-16 12:09:03 +01:00
tgiachi
4a501d77b3 feat(news): blog list with pages, and an archive by year and month
- {{news_blog}} lists the posts as entries (title, long date, categories,
  description, a read more link) 10 to a page; the other pages are written under
  the index, at page/2/, page/3/ and so on, with a pager.
- Archive pages for every year and month that has posts, at /news/2026/ and
  /news/2026/11/, when templates/archive.html exists, and a {{news_archive}} block.

Refs #9
2026-11-16 12:08:07 +01:00
tgiachi
bf50b2e1c2 feat(news): a post page with date, author, categories, summary and links
New placeholders for templates/post.html, written in the language of the post:
date_long, post_summary, post_meta, post_reading, post_mastodon and post_nav
(older, newer and all news), plus og_type and article_meta for Open Graph, which
every other page sets to website. A post with a Mastodon address needs the
reply_mastodon string; the other strings are optional and a feature without its
string is left out.

Refs #9
2026-11-16 12:06:55 +01:00
tgiachi
26edf78449 feat(news): hide drafts and future posts, give the news one language
- Draft: true and a date in the future keep a post out of the pages, the lists,
  the categories and the feed. motorino build -drafts (next commit) shows them.
- news_language in site.json, and an optional Language key in a post, choose the
  language of the news pages and of the interface strings they use. The news
  still exist once, not once per language.
- New header keys Mastodon (an https address), Draft and Language; a post knows
  its reading time and whether its description was written.

Refs #9
2026-11-16 12:04:50 +01:00
tgiachi
b2f43c34bb chore(release): 1.2.0 [skip ci] 2026-11-15 18:08:42 +01:00
tgiachi
eb2a8dc9b4 feat: take the menu from the site, not from the code
The menu entries were three lists in Go (the menu, the linked pages and
the required pages), so a new menu entry meant a new motorino release.
Now every page ID gets url_<id> and cur_<id> for the templates, and
header.html alone decides what is in the menu and in which order. Only
the home page and the news section stay required, since motorino builds
them itself.

Output of the real kmalloc.org site is byte for byte the same as 1.1.0.
2026-11-15 18:08:42 +01:00
tgiachi
d7f076659a chore(release): 1.1.0 [skip ci]
## [1.1.0](https://git.kmalloc.org/squid/motorino/compare/v1.0.1...v1.1.0) (2026-11-15)

### Features

* add an optional projects page to the menu (1505e5d)
2026-11-15 17:44:50 +01:00
tgiachi
1505e5d782 feat(nav): add an optional projects page to the menu
kmalloc.org wants a "Projects" entry between People and Services,
linking to a page listing the software it currently builds. Adds
"projects" to the menu, the linked pages and the required pages, and
keeps the test fixtures (and the main_test page count) in sync.
2026-11-15 17:44:40 +01:00
semantic-release-bot
f92d5e8cb4 chore(release): 1.0.1 [skip ci]
## [1.0.1](https://git.kmalloc.org/squid/motorino/compare/v1.0.0...v1.0.1) (2026-11-15)

### Bug Fixes

* close the minor findings of the first review ([917683a](917683afa4))
* find code blocks with the Markdown parser and make the escape work everywhere ([4ab8f8d](4ab8f8d122))
2026-11-15 10:40:28 +01:00
3411f2c522 Merge pull request 'Close the minor findings of the first review' (#7) from fix/review-minors into main
All checks were successful
check / check (push) Successful in 29s
2026-11-15 09:40:06 +00:00
tgiachi
4ab8f8d122 fix: find code blocks with the Markdown parser and make the escape work everywhere
All checks were successful
check / check (pull_request) Successful in 30s
The hand written fence scanner took a line that starts with a code span for a
fence and missed fences in quotes and lists. The code blocks are now the ones
goldmark finds. A backslash before a placeholder is turned into a marker that
survives the Markdown and the list expansion, so it works in a code span and
next to a block name, and two backslashes are a literal one. The em dash rule
reports the line of the body, refuses padded entities and checks the header keys
by name, the feed leaves protocol-relative links alone, base_url needs a host
and a language file cannot set it.
2026-11-15 10:38:56 +01:00
tgiachi
917683afa4 fix: close the minor findings of the first review
Require refuses an empty value. Heading ids: an explicit {#id} must be sound
and unique, generated ids avoid the explicit ones and each other, and a heading
with no ASCII letters gets a fallback. Fenced code blocks keep their
placeholders and a backslash keeps one anywhere. The em dash rule covers the
header of the manifesto pages and the HTML entities. The feed makes src links
absolute, base_url is required, and the news page must be a folder index. A
category written twice in a post counts once, a block name in a news title is no
longer taken for a placeholder, and build refuses extra arguments.
2026-11-15 10:32:19 +01:00
tgiachi
8f294d92e2 docs: mention the check workflow in the README
All checks were successful
check / check (push) Successful in 29s
2026-11-15 10:27:25 +01:00
94c23442be Merge pull request 'Run make check on every push and pull request' (#5) from ci/check into main
All checks were successful
check / check (push) Successful in 1m15s
2026-11-15 09:21:44 +00:00
tgiachi
beb7698a77 ci: run make check on every push and pull request
All checks were successful
check / check (pull_request) Successful in 1m28s
2026-11-15 10:18:21 +01:00
44 changed files with 5568 additions and 148 deletions

View file

@ -0,0 +1,23 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Runs `make check` (gofmt, go vet, staticcheck and the tests) on every push to main and on every
# pull request, on the shared runner of git.kmalloc.org.
name: check
on:
push:
branches: [main]
pull_request:
jobs:
check:
runs-on: docker
steps:
- uses: https://code.forgejo.org/actions/checkout@v4
- uses: https://code.forgejo.org/actions/setup-go@v5
with:
go-version-file: go.mod
- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest
- name: Run the checks
run: PATH="$PATH:$(go env GOPATH)/bin" make check

View file

@ -1,3 +1,42 @@
## [1.3.0](https://git.kmalloc.org/squid/motorino/compare/v1.2.0...v1.3.0) (2026-11-16)
### Features
* **build:** report the old files of the output folder, add -clean ([52edb15](https://git.kmalloc.org/squid/motorino/commit/52edb15b64d95959e6acdec9bc194cd2cc6aba7e)), closes [#9](https://git.kmalloc.org/squid/motorino/issues/9)
* **feed:** carry the full text in content:encoded ([3f62c79](https://git.kmalloc.org/squid/motorino/commit/3f62c7960373c044dc014c8355496038ede94e22)), closes [#9](https://git.kmalloc.org/squid/motorino/issues/9)
* **gemini:** write the site as a Gemini capsule ([2f169b8](https://git.kmalloc.org/squid/motorino/commit/2f169b8d9f79a49dcefd3ddc48e5db1404dd7e17)), closes [#10](https://git.kmalloc.org/squid/motorino/issues/10)
* **gemtext:** convert the HTML of a page to gemtext ([28745d4](https://git.kmalloc.org/squid/motorino/commit/28745d452f923081f68983679a92772fe948d53e)), closes [#10](https://git.kmalloc.org/squid/motorino/issues/10)
* **gemtext:** write a table wider than 80 columns as records ([889edbf](https://git.kmalloc.org/squid/motorino/commit/889edbfd63e9693d12957c63eda5fff1aa453229)), closes [#10](https://git.kmalloc.org/squid/motorino/issues/10)
* motorino new writes a draft, motorino build -drafts previews it ([633da1c](https://git.kmalloc.org/squid/motorino/commit/633da1c46bf9bccd5b1ee9279a4c711ff441879d)), closes [#9](https://git.kmalloc.org/squid/motorino/issues/9)
* **news:** a post page with date, author, categories, summary and links ([bf50b2e](https://git.kmalloc.org/squid/motorino/commit/bf50b2e1c2d835340415c776886e1c533f047765)), closes [#9](https://git.kmalloc.org/squid/motorino/issues/9)
* **news:** blog list with pages, and an archive by year and month ([4a501d7](https://git.kmalloc.org/squid/motorino/commit/4a501d77b3c2ecd737db39620e226efcba78d873)), closes [#9](https://git.kmalloc.org/squid/motorino/issues/9)
* **news:** hide drafts and future posts, give the news one language ([26edf78](https://git.kmalloc.org/squid/motorino/commit/26edf78449dfd66da49b0e05b5bd79869c2701ca)), closes [#9](https://git.kmalloc.org/squid/motorino/issues/9)
* **news:** show the posts of the lists as note panels ([71cf140](https://git.kmalloc.org/squid/motorino/commit/71cf14006d48084281779b83ee3d0b07fbe91024)), closes [#9](https://git.kmalloc.org/squid/motorino/issues/9)
### Bug Fixes
* **gemini:** list items, spaces in addresses, wide tables, per post language, empty lists, layout images ([3764470](https://git.kmalloc.org/squid/motorino/commit/3764470e9b3ef294de63f069efce54bc2ef773fe)), closes [#10](https://git.kmalloc.org/squid/motorino/issues/10)
* **news:** group the archive by the month as written, drop the empty lists ([6987541](https://git.kmalloc.org/squid/motorino/commit/6987541f88246f092b4325654f4c6789773afa53)), closes [#9](https://git.kmalloc.org/squid/motorino/issues/9)
## [1.2.0](https://git.kmalloc.org/squid/motorino/compare/v1.1.0...v1.2.0) (2026-11-15)
### Features
* take the menu from the site, not from the code ([eb2a8dc](https://git.kmalloc.org/squid/motorino/commit/eb2a8dc))
## [1.1.0](https://git.kmalloc.org/squid/motorino/compare/v1.0.1...v1.1.0) (2026-11-15)
### Features
* add an optional projects page to the menu ([1505e5d](https://git.kmalloc.org/squid/motorino/commit/1505e5d))
## [1.0.1](https://git.kmalloc.org/squid/motorino/compare/v1.0.0...v1.0.1) (2026-11-15)
### Bug Fixes
* close the minor findings of the first review ([917683a](https://git.kmalloc.org/squid/motorino/commit/917683afa491c7f0b4331757d6066d215b5f19d9))
* find code blocks with the Markdown parser and make the escape work everywhere ([4ab8f8d](https://git.kmalloc.org/squid/motorino/commit/4ab8f8d12223fca0db20ec5afee92217533d058b))
## 1.0.0 (2026-11-15) ## 1.0.0 (2026-11-15)
### Features ### Features

View file

@ -1,6 +1,6 @@
# motorino # motorino
The static site generator of [kmalloc.org](https://kmalloc.org/). It turns Markdown pages, news, templates and language files into plain HTML with no JavaScript. It is made for that one site and is not a general-purpose generator. The static site generator of [kmalloc.org](https://kmalloc.org/). It turns Markdown pages, news, templates and language files into plain HTML with no JavaScript, and into a Gemini capsule. It is made for that one site and is not a general-purpose generator.
Licence: GPL-3.0-or-later, see `LICENSE`. Licence: GPL-3.0-or-later, see `LICENSE`.
@ -8,11 +8,18 @@ Licence: GPL-3.0-or-later, see `LICENSE`.
``` ```
motorino build -site DIR -out DIR build the site in DIR into the output folder (defaults: . and public) motorino build -site DIR -out DIR build the site in DIR into the output folder (defaults: . and public)
motorino build -drafts ... the same, with the drafts and the posts dated in the future, for a preview
motorino gemini -site DIR -out DIR write the same site as gemtext, for a Gemini server (defaults: . and capsule)
motorino gemini -lang CODE ... the language at the root of the capsule; -drafts and -clean work as for build
motorino build -clean ... empty the output folder first, if it is new, empty or holds .motorino-out
motorino new -site DIR "A title" write news/a-title.md, a draft with the header ready, and print its path
motorino version print the version motorino version print the version
``` ```
The binary is a single static Go program. The version comes from semantic-release. The binary is a single static Go program. The version comes from semantic-release.
A build writes the files it makes and leaves the others in the output folder, so the pages of a list that got shorter (`news/page/3/`) stay behind. When that happens the command prints a warning that names the old files. The deploy never has them: it builds into a new folder and publishes it with `rsync --delete`. For a folder you reuse, `-clean` empties it first. It does that only for a folder that is new, empty or holds the `.motorino-out` file that an earlier `-clean` left, never for any other folder with files in it and never for the folder of the site or one that holds it. A build without `-clean` writes no extra file.
## What a site looks like ## What a site looks like
``` ```
@ -38,17 +45,52 @@ Author: Matteo Bini
Date: 2026-09-18T17:00:45+0200 Date: 2026-09-18T17:00:45+0200
Categories: Linux Day, FLUG Categories: Linux Day, FLUG
Description: An optional one-line summary. Description: An optional one-line summary.
Mastodon: https://social.kmalloc.org/@squid/115000000000000000
Language: it
Draft: true
- -
The text of the news. The text of the news.
``` ```
A news file `news/name.md` is published as `/news/YYYY/MM/name.html`, with the year and the month of `Date` as written. Every category gets a page, `/news/category/<name>.html`. A news file `news/name.md` is published as `/news/YYYY/MM/name.html`, with the year and the month of `Date` as written. Every category gets a page, `/news/category/<name>.html`. The optional keys:
- `Description`: the summary of the post. When it is written the post page shows it under the title; when it is not, the first sentence of the text is used for the meta description and the lists, and the page does not repeat it.
- `Mastodon`: an `https` address, the post on Mastodon. The post page then has a link to reply there (the language needs `reply_mastodon`).
- `Language`: the language the post is written in; the default is the language of the news (below).
- `Draft`: `true` or `yes` keeps the post out of the site. So does a `Date` in the future, until that moment. `motorino build -drafts` publishes both, for a preview.
#### The news and their language
The news exist once, not once per language. Their language is `news_language` in `site.json`, else `en` when the site has it, else the default language. The news index page (`content/<lang>/news/index.md`, ID `news`) must be in that language; the posts, the lists, the archive, the categories and the feed are all in it, with the interface strings of its language file. A post can say another language with `Language`; its page then uses that language file. The other languages of the site link to the news from their home.
#### The blog
Everything below is opt in, so a site that does not use it builds as it did.
- **The post page** (`templates/post.html`) has, besides `post_title`, `date`, `post_body` and `post_byline`: `date_long` (such as "14 November 2026"), `post_summary` (the written description as a lead paragraph), `post_meta` (a line with the date, the author, the category links and the reading time), `post_reading`, `post_mastodon` (the reply link) and `post_nav` (the newer post, the older post and all the news). `og_type` is `article` there and `website` on every other page, and `article_meta` holds the published time and a tag for each category, for the head of `base.html`.
- **The blog list**: a paragraph with only `{{news_blog}}` on the news index lists the posts as note panels (`<p class="note" role="note">`: a small line with the long date and the categories, the title in bold with a full stop unless it ends with `.`, `?` or `!`, the description, and a read more link named after the post), 10 to a page. The other pages are written under the index, at `page/2/`, `page/3/`, with a pager. The block needs an index page.
- **The archive**: when `templates/archive.html` exists, every year and month with posts gets a page, `/news/2026/` and `/news/2026/11/`, with `archive_name`, `archive_nav` and `archive_posts`. The posts of an archive page, of a category page and of the blog list are the same panels; where the language has no `read_more` the title is the link instead, except on the blog list, where it is an error. `{{news_list}}` and the latest news of the home page stay plain lists. `{{news_archive}}` lists the years and the months with their counts, and needs the template.
- **The feed** (`/news/feed.xml`) has the summary in `description` and the full text, with absolute links, in `content:encoded`, plus the categories and the author.
The strings come from the language files. These are required when the feature is used: `read_more`, `pager_label`, `pager_newer`, `pager_older` and `pager_page` (`Page {n} of {total}`) for the blog list, and `reply_mastodon` for a post with a Mastodon address. These are optional, and without them the feature is left out: `date_long` (`{d} {month} {y}`) and `month_01` to `month_12` (without them the date is `2026-11-14`), `date_month_year` (`{month} {y}`), `reading_time` (`{n} min read`), and `post_older`, `post_newer` and `post_nav` for the links between posts. The archive uses `archive_h` in its template and, optionally, `archive_up`.
A new post is a draft: `motorino new "A title"` writes `news/a-title.md` with the date of now and `Draft: true`, and the author from the `author` key of `site.json` when there is one.
Pages: `Title`, `ID` (the same in every language, it links the translations), `Description`, and optionally `Layout` (`page`, `bare` or `manifesto`), `Note`, `Switch` (`lang=/path, lang=/path`, overrides the language links) and `ManifestoVersion` and `ManifestoDate` (a translation of an older manifesto says which version it translates). Pages: `Title`, `ID` (the same in every language, it links the translations), `Description`, and optionally `Layout` (`page`, `bare` or `manifesto`), `Note`, `Switch` (`lang=/path, lang=/path`, overrides the language links) and `ManifestoVersion` and `ManifestoDate` (a translation of an older manifesto says which version it translates).
### Placeholders ### Placeholders
`{{name}}`, with lowercase letters, digits and underscores, is replaced by a string from `site.json` or the language file. An unknown placeholder stops the build and names the file. `{{news_list}}`, `{{latest_news}}` and `{{categories_list}}` stand alone in a paragraph and expand to generated lists. `{{name}}`, with lowercase letters, digits and underscores, is replaced by a string from `site.json` or the language file. An unknown placeholder stops the build and names the file. Fenced code blocks (found the way CommonMark finds them, in lists and quotes too) are left as written, so an article can show a placeholder there. Anywhere else a backslash before the placeholder (`\{{name}}`) keeps it as written, in a code span too; two backslashes are one literal backslash followed by the value. Code spans are otherwise substituted, which is how a page shows a value in monospace. The id of a heading is not checked against ids that a template writes itself, such as the `news` of the latest news block. `{{news_list}}`, `{{latest_news}}` and `{{categories_list}}` stand alone in a paragraph and expand to generated lists.
## Gemini
`motorino gemini` reads the same site and writes gemtext: the pages of each language, the news once and a `.meta` file for the server. It does not read the templates, the style or the layouts: a page is its title, its text and a few navigation links.
- **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 has a line of its own, with the path of its file (`news/2026/11/slug.gmi:;lang=it`). The server uses the first line that matches a document, so these come first, then the folders.
- **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, or, when it is wider than 80 columns, a heading for each row (its first cell) with a bullet for each other cell named by its column, a bullet for each row when there is no header or only one column, an image with an alt text is a link line, unless it belongs to the layout (it has the class `logo`, `mark` or `decorative`, or the role `presentation` or `none`: then nothing is written and the file is not copied), and emphasis is dropped. A list item with no text, only an image or a link, has no bullet; a preformatted block in an item follows its bullet. 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 ## Development
@ -58,7 +100,7 @@ make build a local binary in bin/motorino
make dist the release binary and its SHA-256 in dist/ make dist the release binary and its SHA-256 in dist/
``` ```
Go code has a doc comment on every package and declaration, and is formatted with `gofmt`. Commits follow conventional commits. Go code has a doc comment on every package and declaration, and is formatted with `gofmt`. Commits follow conventional commits. A Forgejo Actions workflow (`.forgejo/workflows/check.yml`) runs `make check` on every push to `main` and on every pull request, on the shared runner of git.kmalloc.org.
## Release ## Release

View file

@ -5,7 +5,9 @@
// //
// Usage: // Usage:
// //
// motorino build [-site DIR] [-out DIR] // motorino build [-site DIR] [-out DIR] [-drafts] [-clean]
// motorino gemini [-site DIR] [-out DIR] [-lang CODE] [-drafts] [-clean]
// motorino new [-site DIR] "title"
// motorino version // motorino version
package main package main
@ -15,13 +17,17 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"strings"
"time"
"git.kmalloc.org/squid/motorino/internal/build" "git.kmalloc.org/squid/motorino/internal/build"
"git.kmalloc.org/squid/motorino/internal/content"
"git.kmalloc.org/squid/motorino/internal/site"
"git.kmalloc.org/squid/motorino/internal/version" "git.kmalloc.org/squid/motorino/internal/version"
) )
// usage is the text printed for a missing or unknown command. // usage is the text printed for a missing or unknown command.
const usage = "usage: motorino build [-site DIR] [-out DIR] | motorino version" const usage = "usage: motorino build [-site DIR] [-out DIR] [-drafts] [-clean] | motorino gemini [-site DIR] [-out DIR] [-lang CODE] [-drafts] [-clean] | motorino new [-site DIR] \"title\" | motorino version"
// main runs the command line and exits with its status code. // main runs the command line and exits with its status code.
func main() { func main() {
@ -41,6 +47,10 @@ func run(args []string, stdout, stderr io.Writer) int {
return 0 return 0
case "build": case "build":
return runBuild(args[1:], stdout, stderr) return runBuild(args[1:], stdout, stderr)
case "gemini":
return runGemini(args[1:], stdout, stderr)
case "new":
return runNew(args[1:], stdout, stderr)
default: default:
fmt.Fprintf(stderr, "motorino: unknown command %q\n%s\n", args[0], usage) fmt.Fprintf(stderr, "motorino: unknown command %q\n%s\n", args[0], usage)
return 2 return 2
@ -53,17 +63,96 @@ func runBuild(args []string, stdout, stderr io.Writer) int {
flags.SetOutput(stderr) flags.SetOutput(stderr)
site := flags.String("site", ".", "the folder of the site") site := flags.String("site", ".", "the folder of the site")
out := flags.String("out", "public", "the folder the HTML is written to") out := flags.String("out", "public", "the folder the HTML is written to")
drafts := flags.Bool("drafts", false, "publish the drafts and the posts dated in the future, for a preview")
clean := flags.Bool("clean", false, "empty the output folder first, if it is new, empty or was emptied this way before")
if err := flags.Parse(args); err != nil { if err := flags.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) { if errors.Is(err, flag.ErrHelp) {
return 0 return 0
} }
return 2 return 2
} }
result, err := build.Build(build.Options{SiteDir: *site, OutDir: *out, Version: version.String()}) if flags.NArg() > 0 {
fmt.Fprintf(stderr, "motorino: unexpected arguments: %s\n%s\n", strings.Join(flags.Args(), " "), usage)
return 2
}
result, err := build.Build(build.Options{SiteDir: *site, OutDir: *out, Version: version.String(), Drafts: *drafts, Clean: *clean})
if err != nil { if err != nil {
fmt.Fprintf(stderr, "motorino: %v\n", err) fmt.Fprintf(stderr, "motorino: %v\n", err)
return 1 return 1
} }
fmt.Fprintf(stdout, "built %d pages into %s\n", result.Pages, *out) fmt.Fprintf(stdout, "built %d pages into %s\n", result.Pages, *out)
warnStale(stderr, *out, result.Stale)
return 0
}
// warnStale tells the files of the output folder that the build did not write,
// which stay there unless -clean is used.
func warnStale(stderr io.Writer, out string, stale []string) {
if len(stale) == 0 {
return
}
noun := "files"
if len(stale) == 1 {
noun = "file"
}
fmt.Fprintf(stderr, "motorino: warning: %d %s in %s were not written by this build, such as %s; build into a new folder or use -clean\n", len(stale), noun, out, stale[0])
}
// runGemini parses the flags of the gemini command and writes the site as a
// Gemini capsule.
func runGemini(args []string, stdout, stderr io.Writer) int {
flags := flag.NewFlagSet("gemini", flag.ContinueOnError)
flags.SetOutput(stderr)
site := flags.String("site", ".", "the folder of the site")
out := flags.String("out", "capsule", "the folder the gemtext is written to")
lang := flags.String("lang", "", "the language at the root of the capsule (default: gemini_language of site.json, then the default language)")
drafts := flags.Bool("drafts", false, "publish the drafts and the posts dated in the future, for a preview")
clean := flags.Bool("clean", false, "empty the output folder first, if it is new, empty or was emptied this way before")
if err := flags.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
return 0
}
return 2
}
if flags.NArg() > 0 {
fmt.Fprintf(stderr, "motorino: unexpected arguments: %s\n%s\n", strings.Join(flags.Args(), " "), usage)
return 2
}
result, err := build.BuildGemini(build.GeminiOptions{SiteDir: *site, OutDir: *out, Version: version.String(), Drafts: *drafts, Language: *lang, Clean: *clean})
if err != nil {
fmt.Fprintf(stderr, "motorino: %v\n", err)
return 1
}
fmt.Fprintf(stdout, "wrote %d gemtext files into %s\n", result.Pages, *out)
warnStale(stderr, *out, result.Stale)
return 0
}
// runNew parses the flags of the new command and writes the file of a new post.
// The author is the author key of site.json, when it has one.
func runNew(args []string, stdout, stderr io.Writer) int {
flags := flag.NewFlagSet("new", flag.ContinueOnError)
flags.SetOutput(stderr)
dir := flags.String("site", ".", "the folder of the site")
if err := flags.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
return 0
}
return 2
}
if flags.NArg() != 1 {
fmt.Fprintf(stderr, "motorino: new takes one title\n%s\n", usage)
return 2
}
author := ""
if cfg, _, err := site.Load(*dir); err == nil {
author = cfg.Strings["author"]
}
rel, err := content.NewPost(*dir, flags.Arg(0), author, time.Now())
if err != nil {
fmt.Fprintf(stderr, "motorino: %v\n", err)
return 1
}
fmt.Fprintln(stdout, rel)
return 0 return 0
} }

View file

@ -28,10 +28,13 @@ func miniSite(t *testing.T) string {
"templates/pages/page.html": "<h1>{{page_title}}</h1>{{body}}\n", "templates/pages/page.html": "<h1>{{page_title}}</h1>{{body}}\n",
"templates/post.html": "{{post_body}}\n", "templates/category.html": "{{category_posts}}\n", "templates/post.html": "{{post_body}}\n", "templates/category.html": "{{category_posts}}\n",
} }
for _, id := range []string{"home", "manifesto", "people", "services", "news", "history"} { for _, id := range []string{"home", "manifesto", "people", "projects", "services", "news", "history"} {
name := id name := id
if id == "home" { switch id {
case "home":
name = "index" name = "index"
case "news":
name = "news/index"
} }
files["content/en/"+name+".md"] = "Title: " + id + "\nID: " + id + "\nDescription: d\n-\nBody of " + id + ".\n" files["content/en/"+name+".md"] = "Title: " + id + "\nID: " + id + "\nDescription: d\n-\nBody of " + id + ".\n"
} }
@ -68,7 +71,7 @@ func TestRunBuildSuccess(t *testing.T) {
if code != 0 { if code != 0 {
t.Fatalf("run(build) = %d, stderr %q", code, errOut.String()) t.Fatalf("run(build) = %d, stderr %q", code, errOut.String())
} }
if want := "built 6 pages into " + outDir; !strings.Contains(out.String(), want) { if want := "built 7 pages into " + outDir; !strings.Contains(out.String(), want) {
t.Errorf("output %q does not contain %q", out.String(), want) t.Errorf("output %q does not contain %q", out.String(), want)
} }
if _, err := os.Stat(filepath.Join(outDir, "index.html")); err != nil { if _, err := os.Stat(filepath.Join(outDir, "index.html")); err != nil {
@ -101,6 +104,17 @@ func TestRunUnknownCommand(t *testing.T) {
} }
} }
// TestRunBuildRejectsExtraArguments checks that arguments that are not flags are not ignored.
func TestRunBuildRejectsExtraArguments(t *testing.T) {
var out, errOut bytes.Buffer
if code := run([]string{"build", "somesite", "someout"}, &out, &errOut); code != 2 {
t.Errorf("run(build somesite someout) = %d, want 2", code)
}
if !strings.Contains(errOut.String(), "unexpected") {
t.Errorf("stderr = %q, want it to say what was unexpected", errOut.String())
}
}
// TestRunBuildBadFlag checks that an unknown flag is a usage error. // TestRunBuildBadFlag checks that an unknown flag is a usage error.
func TestRunBuildBadFlag(t *testing.T) { func TestRunBuildBadFlag(t *testing.T) {
var out, errOut bytes.Buffer var out, errOut bytes.Buffer
@ -108,3 +122,192 @@ func TestRunBuildBadFlag(t *testing.T) {
t.Errorf("run(build -nope) = %d, want 2", code) t.Errorf("run(build -nope) = %d, want 2", code)
} }
} }
// TestRunNewCreatesADraft checks the new command: it prints the file, writes a draft and
// takes the author from site.json.
func TestRunNewCreatesADraft(t *testing.T) {
dir := miniSite(t)
site := filepath.Join(dir, "site.json")
if err := os.WriteFile(site, []byte(`{"base_url": "https://x.org", "languages": ["en"], "mastodon_url": "m", "motorino_url": "u", "author": "squid"}`), 0o644); err != nil {
t.Fatal(err)
}
var out, errOut bytes.Buffer
if code := run([]string{"new", "-site", dir, "My first post"}, &out, &errOut); code != 0 {
t.Fatalf("run(new) = %d, stderr %q", code, errOut.String())
}
if got := strings.TrimSpace(out.String()); got != "news/my-first-post.md" {
t.Errorf("stdout = %q, want news/my-first-post.md", got)
}
data, err := os.ReadFile(filepath.Join(dir, "news", "my-first-post.md"))
if err != nil {
t.Fatal(err)
}
for _, want := range []string{"Title: My first post\n", "Author: squid\n", "Draft: true\n"} {
if !strings.Contains(string(data), want) {
t.Errorf("the file does not contain %q\n%s", want, data)
}
}
if code := run([]string{"new", "-site", dir, "My first post"}, &out, &errOut); code != 1 || !strings.Contains(errOut.String(), "already exists") {
t.Errorf("the same title again: code %d, stderr %q, want 1 and an error", code, errOut.String())
}
}
// TestRunNewNeedsATitle checks the usage mistakes of the new command.
func TestRunNewNeedsATitle(t *testing.T) {
for _, args := range [][]string{{"new"}, {"new", "-site", t.TempDir()}, {"new", "one", "two"}} {
var out, errOut bytes.Buffer
if code := run(args, &out, &errOut); code != 2 || !strings.Contains(errOut.String(), "usage") {
t.Errorf("run(%v) = %d, stderr %q, want 2 and the usage", args, code, errOut.String())
}
}
}
// TestRunBuildDraftsFlag checks that a draft is only published with -drafts.
func TestRunBuildDraftsFlag(t *testing.T) {
dir := miniSite(t)
if err := os.MkdirAll(filepath.Join(dir, "news"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "news", "wip.md"), []byte("Title: Work in progress\nDate: 2026-01-01T12:00:00+0100\nDraft: true\n-\nSoon.\n"), 0o644); err != nil {
t.Fatal(err)
}
var out, errOut bytes.Buffer
plain := filepath.Join(t.TempDir(), "plain")
if code := run([]string{"build", "-site", dir, "-out", plain}, &out, &errOut); code != 0 {
t.Fatalf("build = %d, stderr %q", code, errOut.String())
}
if _, err := os.Stat(filepath.Join(plain, "news", "2026", "01", "wip.html")); err == nil {
t.Error("a draft is published without -drafts")
}
preview := filepath.Join(t.TempDir(), "preview")
if code := run([]string{"build", "-drafts", "-site", dir, "-out", preview}, &out, &errOut); code != 0 {
t.Fatalf("build -drafts = %d, stderr %q", code, errOut.String())
}
if _, err := os.Stat(filepath.Join(preview, "news", "2026", "01", "wip.html")); err != nil {
t.Errorf("a draft is missing from the build with -drafts: %v", err)
}
}
// TestRunGemini checks the gemini command: the capsule, the language flag and the drafts flag.
func TestRunGemini(t *testing.T) {
dir := miniSite(t)
if err := os.MkdirAll(filepath.Join(dir, "news"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "news", "wip.md"), []byte("Title: Work in progress\nDate: 2026-01-01T12:00:00+0100\nDraft: true\n-\nSoon.\n"), 0o644); err != nil {
t.Fatal(err)
}
var out, errOut bytes.Buffer
capsule := filepath.Join(t.TempDir(), "capsule")
if code := run([]string{"gemini", "-site", dir, "-out", capsule, "-lang", "en"}, &out, &errOut); code != 0 {
t.Fatalf("run(gemini) = %d, stderr %q", code, errOut.String())
}
if want := "gemtext files into " + capsule; !strings.Contains(out.String(), want) {
t.Errorf("output %q does not contain %q", out.String(), want)
}
for _, rel := range []string{"index.gmi", "manifesto.gmi", "people.gmi", "services.gmi", "news/index.gmi", "news/feed.gmi", ".meta"} {
if _, err := os.Stat(filepath.Join(capsule, filepath.FromSlash(rel))); err != nil {
t.Errorf("no %s in the capsule: %v", rel, err)
}
}
if _, err := os.Stat(filepath.Join(capsule, "news", "2026", "01", "wip.gmi")); err == nil {
t.Error("a draft is published without -drafts")
}
preview := filepath.Join(t.TempDir(), "preview")
if code := run([]string{"gemini", "-drafts", "-site", dir, "-out", preview}, &out, &errOut); code != 0 {
t.Fatalf("run(gemini -drafts) = %d, stderr %q", code, errOut.String())
}
if _, err := os.Stat(filepath.Join(preview, "news", "2026", "01", "wip.gmi")); err != nil {
t.Errorf("a draft is missing from the capsule with -drafts: %v", err)
}
}
// TestRunGeminiErrors checks the exit codes: 1 for a failed build, 2 for a mistake in the command line.
func TestRunGeminiErrors(t *testing.T) {
var out, errOut bytes.Buffer
if code := run([]string{"gemini", "-site", miniSite(t), "-out", filepath.Join(t.TempDir(), "c"), "-lang", "xx"}, &out, &errOut); code != 1 || !strings.Contains(errOut.String(), `"xx"`) {
t.Errorf("an unknown language: code %d, stderr %q, want 1 and a message that names it", code, errOut.String())
}
errOut.Reset()
if code := run([]string{"gemini", "extra"}, &out, &errOut); code != 2 || !strings.Contains(errOut.String(), "unexpected arguments") {
t.Errorf("an extra argument: code %d, stderr %q, want 2", code, errOut.String())
}
}
// TestRunBuildWarnsAboutOldFiles checks that old files in the output folder are named in a warning and left alone.
func TestRunBuildWarnsAboutOldFiles(t *testing.T) {
site := miniSite(t)
outDir := t.TempDir()
if err := os.WriteFile(filepath.Join(outDir, "old.html"), []byte("old"), 0o644); err != nil {
t.Fatal(err)
}
var out, errOut bytes.Buffer
if code := run([]string{"build", "-site", site, "-out", outDir}, &out, &errOut); code != 0 {
t.Fatalf("run(build) = %d, stderr %q", code, errOut.String())
}
for _, want := range []string{"warning", "1 file", "old.html", "-clean"} {
if !strings.Contains(errOut.String(), want) {
t.Errorf("stderr %q does not contain %q", errOut.String(), want)
}
}
if _, err := os.Stat(filepath.Join(outDir, "old.html")); err != nil {
t.Error("a build without -clean removed the old file")
}
errOut.Reset()
if code := run([]string{"build", "-site", site, "-out", filepath.Join(t.TempDir(), "new")}, &out, &errOut); code != 0 || errOut.Len() != 0 {
t.Errorf("a build into a new folder: code %d, stderr %q, want 0 and nothing", code, errOut.String())
}
}
// TestRunBuildClean checks -clean on a folder that is marked and on one that is not.
func TestRunBuildClean(t *testing.T) {
site := miniSite(t)
var out, errOut bytes.Buffer
outDir := filepath.Join(t.TempDir(), "public")
if code := run([]string{"build", "-clean", "-site", site, "-out", outDir}, &out, &errOut); code != 0 {
t.Fatalf("run(build -clean) into a new folder = %d, stderr %q", code, errOut.String())
}
if err := os.WriteFile(filepath.Join(outDir, "old.html"), []byte("old"), 0o644); err != nil {
t.Fatal(err)
}
if code := run([]string{"build", "-clean", "-site", site, "-out", outDir}, &out, &errOut); code != 0 || errOut.Len() != 0 {
t.Fatalf("run(build -clean) again = %d, stderr %q", code, errOut.String())
}
if _, err := os.Stat(filepath.Join(outDir, "old.html")); err == nil {
t.Error("-clean left the old file")
}
other := t.TempDir()
if err := os.WriteFile(filepath.Join(other, "mine.txt"), []byte("mine"), 0o644); err != nil {
t.Fatal(err)
}
if code := run([]string{"build", "-clean", "-site", site, "-out", other}, &out, &errOut); code != 1 || !strings.Contains(errOut.String(), ".motorino-out") {
t.Errorf("run(build -clean) into a folder that is not motorino's = %d, stderr %q, want 1 and the marker named", code, errOut.String())
}
if _, err := os.Stat(filepath.Join(other, "mine.txt")); err != nil {
t.Error("-clean removed a file of a folder that is not motorino's")
}
}
// TestRunGeminiOldFilesAndClean checks the warning about old files and -clean for the gemini command.
func TestRunGeminiOldFilesAndClean(t *testing.T) {
site := miniSite(t)
var out, errOut bytes.Buffer
capsule := t.TempDir()
if err := os.WriteFile(filepath.Join(capsule, "old.gmi"), []byte("old"), 0o644); err != nil {
t.Fatal(err)
}
if code := run([]string{"gemini", "-site", site, "-out", capsule}, &out, &errOut); code != 0 || !strings.Contains(errOut.String(), "old.gmi") {
t.Fatalf("run(gemini) = %d, stderr %q, want 0 and a warning that names old.gmi", code, errOut.String())
}
errOut.Reset()
if code := run([]string{"gemini", "-clean", "-site", site, "-out", capsule}, &out, &errOut); code != 1 || !strings.Contains(errOut.String(), ".motorino-out") {
t.Fatalf("run(gemini -clean) into a folder that is not motorino's = %d, stderr %q", code, errOut.String())
}
fresh := filepath.Join(t.TempDir(), "capsule")
for i := 0; i < 2; i++ {
errOut.Reset()
if code := run([]string{"gemini", "-clean", "-site", site, "-out", fresh}, &out, &errOut); code != 0 || errOut.Len() != 0 {
t.Fatalf("run(gemini -clean) %d = %d, stderr %q", i+1, code, errOut.String())
}
}
}

239
internal/build/blog.go Normal file
View file

@ -0,0 +1,239 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"fmt"
"sort"
"strconv"
"strings"
"git.kmalloc.org/squid/motorino/internal/content"
"git.kmalloc.org/squid/motorino/internal/render"
)
// newsPerPage is how many posts one page of the blog list has.
const newsPerPage = 10
// pageCount returns how many pages n posts make: at least one.
func pageCount(n int) int {
if n <= newsPerPage {
return 1
}
return (n + newsPerPage - 1) / newsPerPage
}
// pagePath returns the address of page n of an index: the index itself for the
// first page, then base + "page/2/", "page/3/" and so on. base ends with "/".
func pagePath(base string, n int) string {
if n <= 1 {
return base
}
return base + "page/" + strconv.Itoa(n) + "/"
}
// pageLabel is "Page 2 of 3" in the language lang, from the string pager_page.
func (b *builder) pageLabel(lang string, n, total int) (string, error) {
text := b.langs[lang].Strings["pager_page"]
if text == "" {
return "", fmt.Errorf("lang/%s.json: the language has no pager_page string, which the pages of the blog list need", lang)
}
return strings.NewReplacer("{n}", strconv.Itoa(n), "{total}", strconv.Itoa(total)).Replace(text), nil
}
// newsBlog returns the page n of the blog list of the language lang, with the
// pager when the list has more than one page. A site without posts gets the
// news_empty text.
func (b *builder) newsBlog(lang string, n int, ctx map[string]string) (string, error) {
if len(b.posts) == 0 {
return "<p>\n" + ctx["news_empty"] + "\n</p>", nil
}
total := pageCount(len(b.posts))
if n < 1 || n > total {
n = 1
}
end := n * newsPerPage
if end > len(b.posts) {
end = len(b.posts)
}
entries := make([]string, 0, newsPerPage)
for _, p := range b.posts[(n-1)*newsPerPage : end] {
entry, err := b.newsPanel(p, lang, true)
if err != nil {
return "", err
}
entries = append(entries, entry)
}
html := strings.Join(entries, "\n")
if total == 1 {
return html, nil
}
pager, err := b.pager(lang, n, total)
if err != nil {
return "", err
}
return html + "\n" + pager, nil
}
// pager returns the navigation between the pages of the blog list.
func (b *builder) pager(lang string, n, total int) (string, error) {
l := b.langs[lang]
label, newer, older := l.Strings["pager_label"], l.Strings["pager_newer"], l.Strings["pager_older"]
if label == "" || newer == "" || older == "" {
return "", fmt.Errorf("lang/%s.json: the language lacks pager_label, pager_newer or pager_older, which the pages of the blog list need", lang)
}
page, err := b.pageLabel(lang, n, total)
if err != nil {
return "", err
}
var parts []string
if n > 1 {
parts = append(parts, fmt.Sprintf(`<a href="%s" rel="prev">%s</a>`, pagePath(b.newsPath, n-1), render.Escape(newer)))
}
parts = append(parts, render.Escape(page))
if n < total {
parts = append(parts, fmt.Sprintf(`<a href="%s" rel="next">%s</a>`, pagePath(b.newsPath, n+1), render.Escape(older)))
}
return fmt.Sprintf("<nav aria-label=\"%s\">\n<p>\n%s\n</p>\n</nav>", render.EscapeAttr(label), strings.Join(parts, "\n&middot;\n")), nil
}
// archiveMonth is the posts of one month; archiveYear the posts of one year.
type (
archiveMonth struct {
year, month int
posts []content.Post
}
archiveYear struct {
year int
posts []content.Post
months []archiveMonth
}
)
// groupArchive groups posts by year and by month, as written in their dates.
// The years and the months are newest first, and the posts of a group keep the
// order they were given. A post is put in its group by its own year and month,
// not by the group before it: two posts near midnight at the end of a month,
// written with different offsets, can be in the order of their instants and in
// the opposite order of their months.
func groupArchive(posts []content.Post) []archiveYear {
type key struct{ year, month int }
years := map[int]*archiveYear{}
months := map[key]*archiveMonth{}
for _, p := range posts {
y, m := p.Date.Year(), int(p.Date.Month())
year := years[y]
if year == nil {
year = &archiveYear{year: y}
years[y] = year
}
year.posts = append(year.posts, p)
month := months[key{y, m}]
if month == nil {
month = &archiveMonth{year: y, month: m}
months[key{y, m}] = month
}
month.posts = append(month.posts, p)
}
var out []archiveYear
for _, year := range years {
for k, month := range months {
if k.year == year.year {
year.months = append(year.months, *month)
}
}
sort.Slice(year.months, func(i, j int) bool { return year.months[i].month > year.months[j].month })
out = append(out, *year)
}
sort.Slice(out, func(i, j int) bool { return out[i].year > out[j].year })
return out
}
// yearPath and monthPath are the addresses of an archive year and month.
func (b *builder) yearPath(year int) string { return fmt.Sprintf("%s%04d/", b.newsPath, year) }
func (b *builder) monthPath(year, month int) string {
return fmt.Sprintf("%s%04d/%02d/", b.newsPath, year, month)
}
// hasArchive reports whether the site has the template of an archive page.
func (b *builder) hasArchive() bool {
_, ok := b.templates["archive"]
return ok
}
// monthList returns the list of the months of a year, each with its posts.
func (b *builder) monthList(y archiveYear, lang string) string {
items := make([]string, len(y.months))
for i, m := range y.months {
items[i] = fmt.Sprintf(`<li><a href="%s">%s</a> (%d)</li>`, b.monthPath(m.year, m.month), render.Escape(monthYear(m.year, m.month, b.langs[lang])), len(m.posts))
}
return "<ul>\n" + strings.Join(items, "\n") + "\n</ul>"
}
// newsArchive returns the nested list of the years and the months that have
// posts, or nothing when there are none: the blog list says there is no news. It
// needs the archive pages.
func (b *builder) newsArchive(lang string, ctx map[string]string) (string, error) {
if _, err := b.template("archive"); err != nil {
return "", fmt.Errorf("the {{news_archive}} block needs the archive pages: %w", err)
}
years := groupArchive(b.posts)
if len(years) == 0 {
return "", nil
}
items := make([]string, len(years))
for i, y := range years {
items[i] = fmt.Sprintf("<li><a href=\"%s\">%d</a> (%d)\n%s</li>", b.yearPath(y.year), y.year, len(y.posts), b.monthList(y, lang))
}
return "<ul>\n" + strings.Join(items, "\n") + "\n</ul>", nil
}
// renderArchives writes the page of every year and every month that has posts,
// when the site has templates/archive.html.
func (b *builder) renderArchives() error {
if !b.hasArchive() {
return nil
}
tmpl, err := b.template("archive")
if err != nil {
return err
}
lang := b.newsLang
l := b.langs[lang]
page := func(path, name, nav string, posts []content.Post, source string) error {
d := document{
lang: lang, nav: "news", lead: "news", path: path, title: name, prefixed: true,
description: l.Strings["archive_h"] + ": " + name, source: source,
}
ctx := b.documentContext(d)
ctx["archive_name"] = render.Escape(name)
ctx["archive_nav"] = nav
panels, err := b.newsPanels(posts, lang)
if err != nil {
return err
}
ctx["archive_posts"] = panels
body, err := render.Substitute("templates/archive.html", tmpl, ctx)
if err != nil {
return err
}
return b.frame(d, ctx, body)
}
for _, y := range groupArchive(b.posts) {
year := strconv.Itoa(y.year)
if err := page(b.yearPath(y.year), year, b.monthList(y, lang), y.posts, "the archive of "+year); err != nil {
return err
}
up := ""
if text := l.Strings["archive_up"]; text != "" {
up = fmt.Sprintf("<p><a href=\"%s\">%s %s</a></p>", b.yearPath(y.year), render.Escape(text), year)
}
for _, m := range y.months {
name := monthYear(m.year, m.month, l)
if err := page(b.monthPath(m.year, m.month), name, up, m.posts, "the archive of "+name); err != nil {
return err
}
}
}
return nil
}

706
internal/build/blog_test.go Normal file
View file

@ -0,0 +1,706 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"encoding/xml"
"path/filepath"
"strings"
"testing"
"time"
)
// fixedNow is the moment the blog tests build at, so that drafts and future
// dates do not depend on the clock.
var fixedNow = time.Date(2026, 11, 15, 12, 0, 0, 0, time.FixedZone("CET", 3600))
// withFiles returns the base site with files added or replaced; an empty value removes the file.
func withFiles(extra map[string]string) map[string]string {
files := baseFiles()
for name, content := range extra {
if content == "" {
delete(files, name)
continue
}
files[name] = content
}
return files
}
// buildWith builds the site made of files with the options changed by tune.
func buildWith(t *testing.T, files map[string]string, tune func(*Options)) (string, error) {
t.Helper()
out := filepath.Join(t.TempDir(), "public")
opts := Options{SiteDir: writeFiles(t, files), OutDir: out, Version: "1.2.3", Now: fixedNow}
if tune != nil {
tune(&opts)
}
_, err := Build(opts)
return out, err
}
// exists reports whether the output has the file rel.
func exists(t *testing.T, out, rel string) bool {
t.Helper()
_, err := readOutMaybe(out, rel)
return err == nil
}
// postNews returns the news files used by the draft and future tests.
func postNews() map[string]string {
return map[string]string{
"news/a-draft.md": "Title: A draft\nDate: 2026-11-10T12:00:00+0100\nCategories: Secret\nDraft: true\n-\nNot yet.\n",
"news/in-future.md": "Title: In the future\nDate: 2026-12-24T12:00:00+0100\nCategories: Later\n-\nSoon.\n",
}
}
// TestDraftsAndFutureStayHidden checks that a draft and a post dated in the
// future are not published anywhere, and that no category exists only for them.
func TestDraftsAndFutureStayHidden(t *testing.T) {
out, err := buildWith(t, withFiles(postNews()), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
for _, rel := range []string{"news/2026/11/a-draft.html", "news/2026/12/in-future.html", "news/category/secret.html", "news/category/later.html"} {
if exists(t, out, rel) {
t.Errorf("%s is published, but the post is hidden", rel)
}
}
for _, rel := range []string{"news/index.html", "news/feed.xml", "index.html"} {
got := readOut(t, out, rel)
for _, hidden := range []string{"A draft", "In the future", "Secret", "Later"} {
if strings.Contains(got, hidden) {
t.Errorf("%s mentions %q, but the post is hidden", rel, hidden)
}
}
}
mustContain(t, "news index", readOut(t, out, "news/index.html"), "Status page")
}
// TestDraftsFlagShowsHiddenPosts checks the preview build.
func TestDraftsFlagShowsHiddenPosts(t *testing.T) {
out, err := buildWith(t, withFiles(postNews()), func(o *Options) { o.Drafts = true })
if err != nil {
t.Fatalf("Build() error = %v", err)
}
for _, rel := range []string{"news/2026/11/a-draft.html", "news/2026/12/in-future.html", "news/category/secret.html"} {
if !exists(t, out, rel) {
t.Errorf("%s is missing from a build with drafts", rel)
}
}
}
// TestFuturePostAppearsWhenItsDateComes checks the moment of publication.
func TestFuturePostAppearsWhenItsDateComes(t *testing.T) {
later := time.Date(2026, 12, 24, 12, 0, 0, 0, time.FixedZone("CET", 3600))
out, err := buildWith(t, withFiles(postNews()), func(o *Options) { o.Now = later })
if err != nil {
t.Fatalf("Build() error = %v", err)
}
if !exists(t, out, "news/2026/12/in-future.html") {
t.Error("a post is published from the moment of its date")
}
if exists(t, out, "news/2026/11/a-draft.html") {
t.Error("a draft stays hidden whatever the date")
}
}
// TestNewsLanguage checks the language of the news and its errors.
func TestNewsLanguage(t *testing.T) {
tests := []struct {
name string
extra map[string]string
want string
}{
{"unknown language in site.json", map[string]string{
"site.json": strings.Replace(baseFiles()["site.json"], `"languages"`, `"news_language": "xx", "languages"`, 1)}, `news_language "xx"`},
{"no news page in that language", map[string]string{
"site.json": strings.Replace(baseFiles()["site.json"], `"languages"`, `"news_language": "it", "languages"`, 1)}, `content/it`},
{"post in a language the site does not have", map[string]string{
"news/tedesco.md": "Title: Deutsch\nDate: 2026-11-01T12:00:00+0100\nLanguage: de\n-\nText.\n"}, `Language "de"`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := buildWith(t, withFiles(tt.extra), nil)
if err == nil || !strings.Contains(err.Error(), tt.want) {
t.Errorf("error = %v, want one that contains %q", err, tt.want)
}
})
}
}
// monthsJSON writes the month_01..month_12 strings of a test language.
func monthsJSON(names ...string) string {
var b strings.Builder
for i, n := range names {
b.WriteString(`"month_` + string(rune('0'+(i+1)/10)) + string(rune('0'+(i+1)%10)) + `": "` + n + `", `)
}
return b.String()
}
// blogLangEn and blogLangIt are the English and Italian language files with the strings the blog features need.
var (
blogLangEn = `{"lang_name": "English", "sections": "Sections", "home_link": "/", "nojs": "No JavaScript.",
"powered_by": "Powered by", "back": "kmalloc.org", "version": "Version", "toc": "Contents", "history_a": "Manifesto history",
"news_p": "What is new.", "news_all": "All news", "news_empty": "No news yet.", "category_h": "Category",
"date_long": "{d} {month} {y}", "date_month_year": "{month} {y}", "reading_time": "{n} min read",
` + monthsJSON("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") + `
"post_older": "Older", "post_newer": "Newer", "post_nav": "Other news", "reply_mastodon": "Reply on Mastodon",
"read_more": "Read more", "pager_label": "News pages", "pager_newer": "Newer news", "pager_older": "Older news", "pager_page": "Page {n} of {total}",
"archive_h": "Archive", "archive_year_h": "Year", "archive_up": "Archive of the year",
"nav": {"manifesto": "Manifesto", "people": "People", "services": "Services", "news": "News"}}`
blogLangIt = `{"lang_name": "Italiano", "sections": "Sezioni", "home_link": "/it/", "nojs": "Niente JavaScript.",
"powered_by": "Powered by", "back": "kmalloc.org", "version": "Versione", "toc": "Indice", "history_a": "Cronologia",
"news_p": "Le novita.", "news_all": "Tutte le news", "news_empty": "Nessuna news.", "category_h": "Categoria",
"date_long": "{d} {month} {y}", "date_month_year": "{month} {y}", "reading_time": "{n} min di lettura",
` + monthsJSON("gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre") + `
"post_older": "Piu vecchia", "post_newer": "Piu recente", "post_nav": "Altre news", "reply_mastodon": "Rispondi su Mastodon",
"read_more": "Leggi tutto", "pager_label": "Pagine delle news", "pager_newer": "News piu recenti", "pager_older": "News piu vecchie", "pager_page": "Pagina {n} di {total}",
"archive_h": "Archivio", "archive_year_h": "Anno", "archive_up": "Archivio dell'anno",
"nav": {"manifesto": "Manifesto", "people": "Persone", "services": "Servizi", "news": "News"}}`
)
// blogPostTemplate is a post page that uses every placeholder of the blog.
const blogPostTemplate = `<h1>{{post_title}}</h1>
{{post_summary}}
{{post_meta}}
{{post_body}}
{{post_mastodon}}
{{post_nav}}
`
// blogBaseTemplate is the base template with the Open Graph placeholders.
const blogBaseTemplate = `<!doctype html>
<html lang="{{lang}}">
<head>
<meta charset="utf-8">
<title>{{title_html}}</title>
<meta property="og:type" content="{{og_type}}">
<meta property="og:title" content="{{title_html}}">
{{article_meta}}
<meta name="description" content="{{description_attr}}">
{{alt_links}}
<style>
{{css}}
</style>
</head>
<body>
{{header}}
{{content}}
{{footer}}
</body>
</html>
`
// blogSite is the base site with the blog language strings and templates.
func blogSite(extra map[string]string) map[string]string {
files := map[string]string{
"lang/en.json": blogLangEn, "lang/it.json": blogLangIt,
"templates/post.html": blogPostTemplate, "templates/base.html": blogBaseTemplate,
"news/shop.md": "Title: A shop\nAuthor: squid\nDate: 2026-11-14T12:00:00+0100\nCategories: FLUG\nDescription: About the shop.\nMastodon: https://social.kmalloc.org/@squid/115\n-\nThere is a shop.\n",
}
for k, v := range extra {
files[k] = v
}
return withFiles(files)
}
// TestPostPageHasTheBlogFields checks the long date, the byline, the summary,
// the reading time, the Mastodon link and the links to the other news.
func TestPostPageHasTheBlogFields(t *testing.T) {
out, err := buildWith(t, blogSite(nil), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
shop := readOut(t, out, "news/2026/11/shop.html")
mustContain(t, "shop post", shop,
`<p class="lede">About the shop.</p>`,
`<p class="meta"><time datetime="2026-11-14">14 November 2026</time> &middot; squid &middot; <a href="/news/category/flug.html">FLUG</a> &middot; 1 min read</p>`,
`<p><a href="https://social.kmalloc.org/@squid/115" rel="noopener">Reply on Mastodon</a></p>`,
`<nav aria-label="Other news">`,
`<li><a href="/news/2026/11/status.html" rel="prev">Older: Status page</a></li>`,
`<li><a href="/news/">All news</a></li>`,
)
if strings.Contains(shop, "Newer:") {
t.Error("the newest post links to a newer one")
}
status := readOut(t, out, "news/2026/11/status.html")
mustContain(t, "status post", status,
`<p class="meta"><time datetime="2026-11-12">12 November 2026</time> &middot; 1 min read</p>`,
`<li><a href="/news/2026/11/shop.html" rel="next">Newer: A shop</a></li>`,
`<li><a href="/news/2026/09/gnu-linux-day-2026.html" rel="prev">Older: GNU/Linux Day 2026</a></li>`,
)
for _, absent := range []string{"lede", "Reply on Mastodon"} {
if strings.Contains(status, absent) {
t.Errorf("a post without a written description or a Mastodon address shows %q", absent)
}
}
}
// TestPostPageSkipsFeaturesWithoutStrings checks that a language file without
// the optional strings gives plain pages and not an error.
func TestPostPageSkipsFeaturesWithoutStrings(t *testing.T) {
files := blogSite(map[string]string{"lang/en.json": baseFiles()["lang/en.json"], "news/shop.md": baseFiles()["news/shop.md"]})
out, err := buildWith(t, files, nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
status := readOut(t, out, "news/2026/11/status.html")
mustContain(t, "status post", status, `<time datetime="2026-11-12">2026-11-12</time>`)
for _, absent := range []string{"min read", "<nav", "Older"} {
if strings.Contains(status, absent) {
t.Errorf("without the strings the page shows %q", absent)
}
}
}
// TestMastodonNeedsItsString checks that an address with no label is an error, not a silent omission.
func TestMastodonNeedsItsString(t *testing.T) {
files := blogSite(map[string]string{"lang/en.json": baseFiles()["lang/en.json"]})
_, err := buildWith(t, files, nil)
if err == nil || !strings.Contains(err.Error(), "reply_mastodon") {
t.Errorf("error = %v, want one that names reply_mastodon", err)
}
}
// TestPostInAnotherLanguage checks that a post with Language uses the strings and the page language of that language.
func TestPostInAnotherLanguage(t *testing.T) {
out, err := buildWith(t, blogSite(map[string]string{
"news/ciao.md": "Title: Ciao\nDate: 2026-11-05T09:00:00+0100\nLanguage: it\n-\nUn testo.\n"}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
ciao := readOut(t, out, "news/2026/11/ciao.html")
mustContain(t, "italian post", ciao, `<html lang="it">`, `5 novembre 2026`, `1 min di lettura`, `<nav aria-label="Altre news">`)
list := readOut(t, out, "news/index.html")
mustContain(t, "news index", list, `hreflang="it" lang="it">Ciao</a>`)
if strings.Contains(list, `hreflang="en"`) {
t.Error("an English post on the English index is marked as another language")
}
}
// TestPostOpenGraphArticle checks og:type and the article meta on posts only.
func TestPostOpenGraphArticle(t *testing.T) {
out, err := buildWith(t, blogSite(nil), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
mustContain(t, "post", readOut(t, out, "news/2026/09/gnu-linux-day-2026.html"),
`<meta property="og:type" content="article">`,
`<meta property="article:published_time" content="2026-09-18T17:00:45+02:00">`,
`<meta property="article:tag" content="Linux Day">`,
`<meta property="article:tag" content="FLUG">`)
for _, rel := range []string{"index.html", "news/index.html", "news/category/flug.html", "it/index.html"} {
got := readOut(t, out, rel)
mustContain(t, rel, got, `<meta property="og:type" content="website">`)
if strings.Contains(got, "article:") {
t.Errorf("%s has article meta", rel)
}
}
}
// blogIndex is a news index that uses the blog list and the archive.
const blogIndex = "Title: News\nID: news\nDescription: About News.\n-\n# News\n\n{{news_blog}}\n\n## Archive\n\n{{news_archive}}\n"
// archiveTemplate is the page of a year or a month of the archive.
const archiveTemplate = "<h1>{{archive_h}}: {{archive_name}}</h1>\n\n{{archive_nav}}\n\n{{archive_posts}}\n"
// manyPosts adds n posts, dated one per day from 2026-10-01, to the files.
func manyPosts(files map[string]string, n int) map[string]string {
for i := 1; i <= n; i++ {
day := "2026-10-" + string(rune('0'+i/10)) + string(rune('0'+i%10))
name := "p" + string(rune('0'+i/10)) + string(rune('0'+i%10))
files["news/"+name+".md"] = "Title: Post " + name[1:] + "\nDate: " + day + "T12:00:00+0100\nCategories: Many\n-\nText of " + name + ".\n"
}
return files
}
// TestNewsBlogEntries checks one entry of the blog list and the absence of a pager for a short list.
func TestNewsBlogEntries(t *testing.T) {
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
index := readOut(t, out, "news/index.html")
mustContain(t, "news index", index,
"<p class=\"note\" role=\"note\">\n"+
`<span class="meta"><time datetime="2026-11-14">14 November 2026</time> &middot; <a href="/news/category/flug.html">FLUG</a></span><br>`+"\n"+
"<strong>A shop.</strong>\n"+
"About the shop.\n"+
`<a href="/news/2026/11/shop.html" aria-label="Read more: A shop">Read more</a>.`+"\n</p>",
// a post with two categories, and one with a description made of its first sentence
`<a href="/news/category/linux-day.html">Linux Day</a>, <a href="/news/category/flug.html">FLUG</a></span><br>`,
`<strong>Status page.</strong>`,
)
if got := strings.Count(index, `role="note"`); got != 3 {
t.Errorf("the index has %d panels, want 3", got)
}
if strings.Contains(index, "<article") {
t.Error("the index has an article element, the panels replaced them")
}
if strings.Contains(index, "News pages") {
t.Error("a list that fits one page has a pager")
}
}
// TestNewsBlogPagination checks the pages of a long list.
func TestNewsBlogPagination(t *testing.T) {
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate})
out, err := buildWith(t, manyPosts(files, 22), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
first := readOut(t, out, "news/index.html")
mustContain(t, "page 1", first, `<nav aria-label="News pages">`, `Page 1 of 3`, `<a href="/news/page/2/" rel="next">Older news</a>`)
if strings.Contains(first, `rel="prev"`) || strings.Count(first, `role="note"`) != 10 || !strings.Contains(first, "Post 15") || strings.Contains(first, "Post 14") {
t.Errorf("page 1 is wrong:\n%s", first)
}
second := readOut(t, out, "news/page/2/index.html")
mustContain(t, "page 2", second, `Page 2 of 3`, `<a href="/news/" rel="prev">Newer news</a>`, `<a href="/news/page/3/" rel="next">Older news</a>`,
"<title>News, Page 2 of 3</title>", "Post 14", "Post 05")
if strings.Count(second, `role="note"`) != 10 || strings.Contains(second, "Post 15") || strings.Contains(second, "Post 04") {
t.Errorf("page 2 is wrong:\n%s", second)
}
third := readOut(t, out, "news/page/3/index.html")
mustContain(t, "page 3", third, `Page 3 of 3`, `<a href="/news/page/2/" rel="prev">Newer news</a>`, "GNU/Linux Day 2026")
if strings.Contains(third, `rel="next"`) || strings.Count(third, `role="note"`) != 5 {
t.Errorf("page 3 is wrong:\n%s", third)
}
if exists(t, out, "news/page/4/index.html") || exists(t, out, "news/page/1/index.html") {
t.Error("there is a page 4 or a page 1 under /news/page/")
}
robots := readOut(t, out, "robots.txt")
mustContain(t, "robots.txt", robots, "/news/page/2/")
}
// TestNewsBlogNeedsItsStrings checks that a blog list without read_more is an error.
func TestNewsBlogNeedsItsStrings(t *testing.T) {
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "lang/en.json": strings.Replace(blogLangEn, `"read_more": "Read more", `, "", 1), "templates/archive.html": archiveTemplate})
_, err := buildWith(t, files, nil)
if err == nil || !strings.Contains(err.Error(), "read_more") {
t.Errorf("error = %v, want one that names read_more", err)
}
}
// TestNewsBlogOnlyOnAnIndex checks that the blog list needs an index page.
func TestNewsBlogOnlyOnAnIndex(t *testing.T) {
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "content/en/extra.md": page("extra", "Extra", "", "{{news_blog}}\n"), "templates/archive.html": archiveTemplate})
_, err := buildWith(t, files, nil)
if err == nil || !strings.Contains(err.Error(), "index") {
t.Errorf("error = %v, want one about an index page", err)
}
}
// TestArchivePages checks the pages of a year and of a month, and the block.
func TestArchivePages(t *testing.T) {
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
mustContain(t, "news index", readOut(t, out, "news/index.html"),
`<li><a href="/news/2026/">2026</a> (3)`,
`<li><a href="/news/2026/11/">November 2026</a> (2)</li>`,
`<li><a href="/news/2026/09/">September 2026</a> (1)</li>`)
year := readOut(t, out, "news/2026/index.html")
mustContain(t, "year", year, "<h1>Archive: 2026</h1>", `<a href="/news/2026/11/">November 2026</a> (2)`, "A shop", "Status page", "GNU/Linux Day 2026")
month := readOut(t, out, "news/2026/11/index.html")
mustContain(t, "month", month, "<h1>Archive: November 2026</h1>", `<a href="/news/2026/">Archive of the year 2026</a>`, "A shop", "Status page")
if strings.Contains(month, "GNU/Linux Day 2026") {
t.Error("the month of November lists a post of September")
}
if exists(t, out, "news/2026/12/index.html") || exists(t, out, "news/2025/index.html") {
t.Error("an archive page exists for a month or a year without posts")
}
}
// TestNoArchiveWithoutTemplate checks that the archive is opt in, and that its block then is an error.
func TestNoArchiveWithoutTemplate(t *testing.T) {
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": strings.Replace(blogIndex, "{{news_archive}}", "", 1)}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
if exists(t, out, "news/2026/index.html") {
t.Error("an archive page is built without templates/archive.html")
}
_, err = buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex}), nil)
if err == nil || !strings.Contains(err.Error(), "archive") {
t.Errorf("error = %v, want one that names the archive template", err)
}
}
// TestNewsPanelTitlePunctuation checks the full stop after the title of a panel: added, unless the title ends with one, a question mark or an exclamation mark.
func TestNewsPanelTitlePunctuation(t *testing.T) {
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
"news/q.md": "Title: Why a shop?\nDate: 2026-11-01T12:00:00+0100\n-\nText.\n",
"news/e.md": "Title: It is open!\nDate: 2026-11-02T12:00:00+0100\n-\nText.\n",
"news/d.md": "Title: Done.\nDate: 2026-11-03T12:00:00+0100\n-\nText.\n",
"news/a.md": "Title: Tom & Jerry\nDate: 2026-11-04T12:00:00+0100\n-\nText.\n"})
out, err := buildWith(t, files, nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
index := readOut(t, out, "news/index.html")
mustContain(t, "news index", index, "<strong>Why a shop?</strong>", "<strong>It is open!</strong>", "<strong>Done.</strong>", "<strong>Tom &amp; Jerry.</strong>",
`aria-label="Read more: Tom &amp; Jerry"`)
for _, wrong := range []string{"shop?.", "open!.", "Done..", "Done.</strong>."} {
if strings.Contains(index, wrong) {
t.Errorf("the index has %q", wrong)
}
}
}
// TestNewsPanelWithoutCategoriesOrDescription checks the two optional lines of a panel.
func TestNewsPanelWithoutCategoriesOrDescription(t *testing.T) {
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
"news/bare.md": "Title: Bare\nDate: 2026-11-13T12:00:00+0100\nDescription:\n-\n"}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
mustContain(t, "news index", readOut(t, out, "news/index.html"),
"<p class=\"note\" role=\"note\">\n"+
`<span class="meta"><time datetime="2026-11-13">13 November 2026</time></span><br>`+"\n"+
"<strong>Bare.</strong>\n"+
`<a href="/news/2026/11/bare.html" aria-label="Read more: Bare">Read more</a>.`+"\n</p>")
}
// TestPanelsOnCategoryAndArchivePages checks that the lists of the categories and of the archives are panels too,
// and that the page of a post and the latest news of the home page are not.
func TestPanelsOnCategoryAndArchivePages(t *testing.T) {
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
shop := "<strong>A shop.</strong>\nAbout the shop.\n" + `<a href="/news/2026/11/shop.html" aria-label="Read more: A shop">Read more</a>.`
for rel, want := range map[string]int{"news/category/flug.html": 2, "news/2026/index.html": 3, "news/2026/11/index.html": 2} {
got := readOut(t, out, rel)
if n := strings.Count(got, `role="note"`); n != want {
t.Errorf("%s has %d panels, want %d", rel, n, want)
}
if !strings.Contains(got, shop) {
t.Errorf("%s lacks the panel of the shop post", rel)
}
if strings.Contains(got, "<li><time") {
t.Errorf("%s still has the list of dates", rel)
}
}
if post := readOut(t, out, "news/2026/11/shop.html"); strings.Contains(post, `role="note"`) {
t.Error("the page of a post has a panel")
}
if home := readOut(t, out, "index.html"); strings.Contains(home, `role="note"`) || !strings.Contains(home, "<li><time") {
t.Error("the latest news of the home page changed")
}
}
// TestPanelsWithoutReadMore checks that a language without read_more gets panels whose title is the link on the
// category and archive pages, as before the panels, and an error on the blog list, which has no other link.
func TestPanelsWithoutReadMore(t *testing.T) {
withoutMore := strings.Replace(blogLangEn, `"read_more": "Read more", `, "", 1)
out, err := buildWith(t, blogSite(map[string]string{"lang/en.json": withoutMore, "templates/archive.html": archiveTemplate}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
for _, rel := range []string{"news/category/flug.html", "news/2026/11/index.html"} {
got := readOut(t, out, rel)
mustContain(t, rel, got, `<strong><a href="/news/2026/11/shop.html">A shop.</a></strong>`+"\nAbout the shop.\n</p>")
if strings.Contains(got, "aria-label") || strings.Contains(got, "Read more") {
t.Errorf("%s has a read more link without the string", rel)
}
}
_, err = buildWith(t, blogSite(map[string]string{"lang/en.json": withoutMore, "content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate}), nil)
if err == nil || !strings.Contains(err.Error(), "read_more") {
t.Errorf("error = %v, want one that names read_more", err)
}
}
// TestPanelOfAPostInAnotherLanguage checks that the link of a panel says the language of a post that is not in the language of the page.
func TestPanelOfAPostInAnotherLanguage(t *testing.T) {
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
"news/ciao.md": "Title: Ciao\nDate: 2026-11-05T09:00:00+0100\nLanguage: it\n-\nUn testo.\n"}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
index := readOut(t, out, "news/index.html")
mustContain(t, "news index", index, `<a href="/news/2026/11/ciao.html" aria-label="Read more: Ciao" hreflang="it" lang="it">Read more</a>.`)
if strings.Count(index, `hreflang="it" lang="it">Read more`) != 1 {
t.Error("an English post is marked as written in Italian")
}
}
// TestArchiveGroupsByTheDateAsWritten checks that two posts either side of midnight, written with different
// offsets, land in their own months and that no month is listed twice, whatever the order of the instants.
func TestArchiveGroupsByTheDateAsWritten(t *testing.T) {
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
"news/late.md": "Title: Late\nDate: 2026-11-30T23:45:00+0000\n-\nText.\n",
"news/early.md": "Title: Early\nDate: 2026-12-01T00:30:00+0100\n-\nText.\n",
"news/mid.md": "Title: Mid\nDate: 2026-11-29T12:00:00+0100\n-\nText.\n"})
out, err := buildWith(t, files, func(o *Options) { o.Now = time.Date(2026, 12, 20, 12, 0, 0, 0, time.UTC) })
if err != nil {
t.Fatalf("Build() error = %v", err)
}
index := readOut(t, out, "news/index.html")
mustContain(t, "news index", index, `<a href="/news/2026/">2026</a> (6)`)
dec, nov, sep := strings.Index(index, ">December 2026</a> (1)"), strings.Index(index, ">November 2026</a> (4)"), strings.Index(index, ">September 2026</a> (1)")
if dec < 0 || nov < dec || sep < nov {
t.Errorf("the months are not December, November, September, one each:\n%s", index)
}
if strings.Count(index, "November 2026</a>") != 1 {
t.Error("November is listed more than once")
}
month := readOut(t, out, "news/2026/11/index.html")
late, mid, shop := strings.Index(month, "<strong>Late.</strong>"), strings.Index(month, "<strong>Mid.</strong>"), strings.Index(month, "<strong>A shop.</strong>")
if late < 0 || mid < late || shop < mid || strings.Contains(month, "<strong>Early.</strong>") {
t.Errorf("the month of November is wrong:\n%s", month)
}
mustContain(t, "month of December", readOut(t, out, "news/2026/12/index.html"), "<strong>Early.</strong>")
}
// TestNoNewsAtAll checks that a site without posts says so once, and that the empty lists take their headings with them.
func TestNoNewsAtAll(t *testing.T) {
index := "Title: News\nID: news\nDescription: About News.\n-\n# News\n\n{{news_blog}}\n\n## Categories\n\n{{categories_list}}\n\n## Archive\n\n{{news_archive}}\n"
files := blogSite(map[string]string{"content/en/news/index.md": index, "templates/archive.html": archiveTemplate,
"news/shop.md": "", "news/gnu-linux-day-2026.md": "", "news/status.md": ""})
out, err := buildWith(t, files, nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
got := readOut(t, out, "news/index.html")
if n := strings.Count(got, "No news yet."); n != 1 {
t.Errorf("the index says there is no news %d times, want once:\n%s", n, got)
}
for _, heading := range []string{">Categories</h2>", ">Archive</h2>"} {
if strings.Contains(got, heading) {
t.Errorf("the index keeps the heading %q of an empty list", heading)
}
}
mustContain(t, "news index", got, "<h1>News</h1>")
}
// TestHomeKeepsItsHeadingWithoutNews checks that the heading before the latest news is the page's own and stays.
func TestHomeKeepsItsHeadingWithoutNews(t *testing.T) {
files := blogSite(map[string]string{"news/shop.md": "", "news/gnu-linux-day-2026.md": "", "news/status.md": ""})
out, err := buildWith(t, files, nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
mustContain(t, "home", readOut(t, out, "index.html"), `<h1 class="hero">Technology.</h1>`)
}
// TestHeadingStaysWithAListThatHasItems checks that the heading of a generated list is dropped only when the list is empty.
func TestHeadingStaysWithAListThatHasItems(t *testing.T) {
index := "Title: News\nID: news\nDescription: About News.\n-\n# News\n\n## Categories\n\n{{categories_list}}\n\n## Archive\n\n{{news_archive}}\n"
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": index, "templates/archive.html": archiveTemplate}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
mustContain(t, "news index", readOut(t, out, "news/index.html"), ">Categories</h2>", ">Archive</h2>")
}
// listFiles returns the site of the pagination tests: the three base posts and extra ones, so that n posts in all.
func listFiles(t *testing.T, n int) map[string]string {
t.Helper()
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate})
return manyPosts(files, n-3)
}
// TestExactlyOnePageOfPosts checks that ten posts make one page with no pager and no second page.
func TestExactlyOnePageOfPosts(t *testing.T) {
out, err := buildWith(t, listFiles(t, 10), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
index := readOut(t, out, "news/index.html")
if n := strings.Count(index, `role="note"`); n != 10 {
t.Errorf("the index has %d panels, want 10", n)
}
if strings.Contains(index, "News pages") || strings.Contains(index, "Page 1 of") || exists(t, out, "news/page/2/index.html") {
t.Error("ten posts have a pager or a second page")
}
}
// TestEleventhPostIsAloneOnPageTwo checks the page that holds the one post that does not fit.
func TestEleventhPostIsAloneOnPageTwo(t *testing.T) {
out, err := buildWith(t, listFiles(t, 11), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
first := readOut(t, out, "news/index.html")
mustContain(t, "page 1", first, "Page 1 of 2", `<a href="/news/page/2/" rel="next">Older news</a>`)
if n := strings.Count(first, `role="note"`); n != 10 || strings.Contains(first, "GNU/Linux Day 2026.") {
t.Errorf("page 1 has %d panels or the oldest post", n)
}
second := readOut(t, out, "news/page/2/index.html")
mustContain(t, "page 2", second, "Page 2 of 2", `<a href="/news/" rel="prev">Newer news</a>`, "<strong>GNU/Linux Day 2026.</strong>")
if n := strings.Count(second, `role="note"`); n != 1 || strings.Contains(second, `rel="next"`) {
t.Errorf("page 2 has %d panels or a link to a third page", n)
}
if exists(t, out, "news/page/3/index.html") {
t.Error("eleven posts have a third page")
}
}
// TestOnlyOnePost checks that the one post has no link to a newer or an older post anywhere, and no pager.
func TestOnlyOnePost(t *testing.T) {
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
"news/gnu-linux-day-2026.md": "", "news/status.md": ""})
out, err := buildWith(t, files, nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
for _, rel := range []string{"news/2026/11/shop.html", "news/index.html", "news/2026/11/index.html"} {
got := readOut(t, out, rel)
for _, absent := range []string{"Older", "Newer", `rel="prev"`, `rel="next"`, "News pages"} {
if strings.Contains(got, absent) {
t.Errorf("%s has %q, but there is one post", rel, absent)
}
}
}
mustContain(t, "the post", readOut(t, out, "news/2026/11/shop.html"), `<li><a href="/news/">All news</a></li>`)
}
// TestUnusualTitle checks that a title with quotes, an ampersand, a tag and non Latin letters is escaped everywhere.
func TestUnusualTitle(t *testing.T) {
title := `Say "hi" & <b>go</b> ünï 日本語`
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
"news/odd.md": "Title: " + title + "\nDate: 2026-11-15T09:00:00+0100\nCategories: Odd\n-\nText.\n"})
out, err := buildWith(t, files, nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
escaped := `Say "hi" &amp; &lt;b&gt;go&lt;/b&gt; ünï 日本語`
attr := `Say &quot;hi&quot; &amp; &lt;b&gt;go&lt;/b&gt; ünï 日本語`
for _, rel := range []string{"news/index.html", "news/category/odd.html", "news/2026/11/index.html"} {
mustContain(t, rel, readOut(t, out, rel), "<strong>"+escaped+".</strong>", `aria-label="Read more: `+attr+`"`)
}
mustContain(t, "the post", readOut(t, out, "news/2026/11/odd.html"), "<h1>"+escaped+"</h1>")
mustContain(t, "the post below", readOut(t, out, "news/2026/11/shop.html"), `Newer: `+escaped)
for _, rel := range []string{"news/index.html", "news/category/odd.html", "news/2026/11/odd.html", "news/feed.xml"} {
if strings.Contains(readOut(t, out, rel), "<b>go</b>") {
t.Errorf("%s has the tag of the title as markup", rel)
}
}
var feed struct {
Channel struct {
Items []struct {
Title string `xml:"title"`
} `xml:"item"`
} `xml:"channel"`
}
if err := xml.Unmarshal([]byte(readOut(t, out, "news/feed.xml")), &feed); err != nil {
t.Fatalf("the feed is not well formed: %v", err)
}
if len(feed.Channel.Items) == 0 || feed.Channel.Items[0].Title != title {
t.Errorf("the first item of the feed is %+v, want the title %q", feed.Channel.Items, title)
}
}
// TestNeighbourInAnotherLanguageIsMarked checks that the link to an older post in another language names the language.
func TestNeighbourInAnotherLanguageIsMarked(t *testing.T) {
out, err := buildWith(t, blogSite(map[string]string{"news/ciao.md": "Title: Ciao\nDate: 2026-11-05T09:00:00+0100\nLanguage: it\n-\nUn testo.\n"}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
mustContain(t, "status post", readOut(t, out, "news/2026/11/status.html"), `rel="prev">Older: <span lang="it">Ciao</span></a>`)
mustContain(t, "italian post", readOut(t, out, "news/2026/11/ciao.html"), `rel="next">Piu recente: <span lang="en">Status page</span></a>`)
}

View file

@ -9,6 +9,7 @@ import (
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
"time"
"git.kmalloc.org/squid/motorino/internal/content" "git.kmalloc.org/squid/motorino/internal/content"
"git.kmalloc.org/squid/motorino/internal/site" "git.kmalloc.org/squid/motorino/internal/site"
@ -24,11 +25,24 @@ type Options struct {
OutDir string OutDir string
// Version is the version of motorino, shown in the generator meta. // Version is the version of motorino, shown in the generator meta.
Version string Version string
// Now is the moment the site is built at: a post dated later is not
// published yet. The zero value means the current time.
Now time.Time
// Drafts publishes the drafts and the posts dated in the future too, for a
// preview of the site.
Drafts bool
// Clean empties OutDir before the site is written, when it is new, empty or
// was emptied this way before (it holds the file .motorino-out); any other
// folder with files in it is an error. Without it old files stay.
Clean bool
} }
// requiredPages are the IDs every site must have in its default language, // requiredPages are the IDs every site must have, because motorino builds the
// because the menu and the footer link to them. // home page and the news section itself: the home in the default language and
var requiredPages = []string{"home", "manifesto", "people", "services", "news", "history"} // the news page in the language of the news. The other pages, and the menu
// that links to them, are the site's own: a template can link to any page with
// url_<id> and mark it with cur_<id>.
var requiredPages = []string{"home", "news"}
// newsOnHome is how many news the home page lists; feedLimit is how many the feed has. // newsOnHome is how many news the home page lists; feedLimit is how many the feed has.
const ( const (
@ -45,12 +59,22 @@ type builder struct {
cfg site.Config cfg site.Config
langs map[string]site.Lang langs map[string]site.Lang
defaultLang string defaultLang string
pages []content.Page // newsLang is the language the news are written in, and so the language of
posts []content.Post // the news pages: the posts, the index, the archives, the categories and
categories []categoryEntry // the feed exist once, in this language. A post can say another one.
templates map[string]string newsLang string
css string // newsPath is the address of the news index, such as "/news/".
byKey map[pageKey]content.Page newsPath string
// now is the moment the site is built at.
now time.Time
pages []content.Page
posts []content.Post
categories []categoryEntry
templates map[string]string
css string
byKey map[pageKey]content.Page
// pageIDs lists the ID of every page, once, whatever the language.
pageIDs []string
// files holds the rendered output: relative path to content. // files holds the rendered output: relative path to content.
files map[string]string files map[string]string
// owners names, for each output file, what produces it. // owners names, for each output file, what produces it.
@ -73,10 +97,18 @@ type categoryEntry struct {
type Result struct { type Result struct {
// Pages is the number of HTML pages written: pages, news and categories. // Pages is the number of HTML pages written: pages, news and categories.
Pages int Pages int
// Stale lists the files that were already in the output folder and that this
// build did not write, such as the pages of a list that got shorter. They are
// left alone unless Clean is set, which removes them first.
Stale []string
} }
// Build reads the site in opts.SiteDir and writes it to opts.OutDir. It renders // Build reads the site in opts.SiteDir and writes it to opts.OutDir. It renders
// everything in memory first, so a failure leaves the output folder untouched. // everything in memory first, so a failure in the content leaves the output
// folder untouched. Writing the files is not atomic, and files of an earlier
// build that are no longer produced are not removed, but listed in Result.Stale:
// build into a fresh folder when that matters, as the deploy does (it publishes
// the new folder with rsync --delete), or set Options.Clean.
func Build(opts Options) (Result, error) { func Build(opts Options) (Result, error) {
b, err := load(opts) b, err := load(opts)
if err != nil { if err != nil {
@ -91,6 +123,9 @@ func Build(opts Options) (Result, error) {
if err := b.renderCategories(); err != nil { if err := b.renderCategories(); err != nil {
return Result{}, err return Result{}, err
} }
if err := b.renderArchives(); err != nil {
return Result{}, err
}
if err := b.renderFeed(); err != nil { if err := b.renderFeed(); err != nil {
return Result{}, err return Result{}, err
} }
@ -101,10 +136,18 @@ func Build(opts Options) (Result, error) {
if err != nil { if err != nil {
return Result{}, err return Result{}, err
} }
if err := b.write(assets); err != nil { files := make(map[string][]byte, len(b.files)+len(assets))
for rel, text := range b.files {
files[rel] = []byte(text)
}
for rel, data := range assets {
files[rel] = data
}
stale, err := writeOutput(opts.OutDir, opts.SiteDir, opts.Clean, files)
if err != nil {
return Result{}, err return Result{}, err
} }
return Result{Pages: len(b.indexable)}, nil return Result{Pages: len(b.indexable), Stale: stale}, nil
} }
// load reads the configuration, the content, the templates and the style. // load reads the configuration, the content, the templates and the style.
@ -114,18 +157,33 @@ func load(opts Options) (*builder, error) {
return nil, err return nil, err
} }
b := &builder{ b := &builder{
opts: opts, cfg: cfg, langs: langs, defaultLang: cfg.Languages[0], opts: opts, cfg: cfg, langs: langs, defaultLang: cfg.Languages[0], now: opts.Now,
byKey: map[pageKey]content.Page{}, files: map[string]string{}, owners: map[string]string{}, byKey: map[pageKey]content.Page{}, files: map[string]string{}, owners: map[string]string{},
langCtx: map[string]map[string]string{}, langCtx: map[string]map[string]string{},
} }
if b.now.IsZero() {
b.now = time.Now()
}
if b.newsLang, err = newsLanguage(cfg); err != nil {
return nil, err
}
if b.pages, err = content.LoadPages(opts.SiteDir, cfg.Languages); err != nil { if b.pages, err = content.LoadPages(opts.SiteDir, cfg.Languages); err != nil {
return nil, err return nil, err
} }
if b.posts, err = content.LoadPosts(opts.SiteDir); err != nil { all, err := content.LoadPosts(opts.SiteDir)
if err != nil {
return nil, err return nil, err
} }
if b.posts, err = b.publishedPosts(all); err != nil {
return nil, err
}
seenID := map[string]bool{}
for _, p := range b.pages { for _, p := range b.pages {
b.byKey[pageKey{p.ID, p.Lang}] = p b.byKey[pageKey{p.ID, p.Lang}] = p
if !seenID[p.ID] {
seenID[p.ID] = true
b.pageIDs = append(b.pageIDs, p.ID)
}
for lang := range p.Switch { for lang := range p.Switch {
if _, known := langs[lang]; !known { if _, known := langs[lang]; !known {
return nil, fmt.Errorf("%s: Switch names the language %q, which is not in site.json", p.Source, lang) return nil, fmt.Errorf("%s: Switch names the language %q, which is not in site.json", p.Source, lang)
@ -133,10 +191,19 @@ func load(opts Options) (*builder, error) {
} }
} }
for _, id := range requiredPages { for _, id := range requiredPages {
if _, ok := b.byKey[pageKey{id, b.defaultLang}]; !ok { lang := b.defaultLang
return nil, fmt.Errorf("content/%s: there is no page with ID %q", b.defaultLang, id) if id == "news" {
lang = b.newsLang
}
if _, ok := b.byKey[pageKey{id, lang}]; !ok {
return nil, fmt.Errorf("content/%s: there is no page with ID %q", lang, id)
} }
} }
news := b.byKey[pageKey{"news", b.newsLang}]
if !strings.HasSuffix(news.Path, "/") {
return nil, fmt.Errorf("%s: the news page must be an index (news/index.md), so that the feed has an address next to it", news.Source)
}
b.newsPath = news.Path
if b.templates, err = readTemplates(opts.SiteDir); err != nil { if b.templates, err = readTemplates(opts.SiteDir); err != nil {
return nil, err return nil, err
} }
@ -149,6 +216,49 @@ func load(opts Options) (*builder, error) {
return b, nil return b, nil
} }
// newsLanguage returns the language of the news: news_language in site.json,
// else English when the site has it, else the default language.
func newsLanguage(cfg site.Config) (string, error) {
listed := func(code string) bool {
for _, l := range cfg.Languages {
if l == code {
return true
}
}
return false
}
switch code := cfg.Strings["news_language"]; {
case code == "" && listed("en"):
return "en", nil
case code == "":
return cfg.Languages[0], nil
case !listed(code):
return "", fmt.Errorf("site.json: news_language %q is not one of the languages of the site", code)
default:
return code, nil
}
}
// publishedPosts gives every post the language of the news when it names none,
// checks that the language is one of the site, and keeps the posts that are
// published at the moment of the build: not a draft and not dated in the
// future, unless the build is a preview with drafts.
func (b *builder) publishedPosts(all []content.Post) ([]content.Post, error) {
var out []content.Post
for _, p := range all {
if p.Language == "" {
p.Language = b.newsLang
}
if _, ok := b.langs[p.Language]; !ok {
return nil, fmt.Errorf("%s: Language %q is not one of the languages of site.json", p.Source, p.Language)
}
if b.opts.Drafts || p.Visible(b.now) {
out = append(out, p)
}
}
return out, nil
}
// groupCategories lists the categories of the posts, sorted by name, each with // groupCategories lists the categories of the posts, sorted by name, each with
// its posts. // its posts.
func groupCategories(posts []content.Post) []categoryEntry { func groupCategories(posts []content.Post) []categoryEntry {
@ -238,21 +348,6 @@ func (b *builder) readAssets() (map[string][]byte, error) {
return out, nil return out, nil
} }
// write creates the output folder and writes the pages and the assets.
func (b *builder) write(assets map[string][]byte) error {
for rel, text := range b.files {
if err := writeFile(b.opts.OutDir, rel, []byte(text)); err != nil {
return err
}
}
for rel, data := range assets {
if err := writeFile(b.opts.OutDir, rel, data); err != nil {
return err
}
}
return nil
}
// writeFile writes data to rel under dir, creating the folders. // writeFile writes data to rel under dir, creating the folders.
func writeFile(dir, rel string, data []byte) error { func writeFile(dir, rel string, data []byte) error {
path := filepath.Join(dir, filepath.FromSlash(rel)) path := filepath.Join(dir, filepath.FromSlash(rel))

View file

@ -358,6 +358,72 @@ func TestBuildRejectsSwitchToUnknownLanguage(t *testing.T) {
} }
} }
// TestBuildFeedMakesImagesAbsolute checks that src links get the full address like href links.
func TestBuildFeedMakesImagesAbsolute(t *testing.T) {
files := baseFiles()
files["news/pic.md"] = "Title: Pic\nDate: 2026-11-15T12:00:00+0100\n-\n![the logo](/logo.svg)\n"
out, err := runBuild(t, files)
if err != nil {
t.Fatal(err)
}
mustContain(t, "feed", readOut(t, out, "news/feed.xml"), `src="https://kmalloc.org/logo.svg"`)
}
// TestBuildNewsPageMustBeAFolderIndex checks that the feed has a sound address.
func TestBuildNewsPageMustBeAFolderIndex(t *testing.T) {
files := baseFiles()
delete(files, "content/en/news/index.md")
files["content/en/news.md"] = page("news", "News", "", "{{news_list}}\n")
_, err := runBuild(t, files)
if err == nil || !strings.Contains(err.Error(), "content/en/news.md") || !strings.Contains(err.Error(), "index") {
t.Errorf("Build() error = %v, want it to say that content/en/news.md must be an index", err)
}
}
// TestBuildBlockNameInATitleIsNotAnError checks that a generated list that shows
// the text of a block placeholder (in a news title) is not mistaken for a
// placeholder that is not alone in its paragraph.
func TestBuildBlockNameInATitleIsNotAnError(t *testing.T) {
files := baseFiles()
files["news/tpl.md"] = "Title: Use {{latest_news}} in a page\nDate: 2026-11-15T12:00:00+0100\n-\nA note.\n"
out, err := runBuild(t, files)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
mustContain(t, "home", readOut(t, out, "index.html"), "Use {{latest_news}} in a page")
}
// TestBuildPlaceholdersAndCode checks the three ways a page can use a placeholder
// next to code: a value in a code span (the pages show the GPG fingerprint that
// way), a fenced block that shows a placeholder as written, and an escaped one.
func TestBuildPlaceholdersAndCode(t *testing.T) {
files := baseFiles()
files["content/en/people.md"] = page("people", "People", "", "Value `{{lang}}` and escaped \\{{lang}}.\n\n```\n{{lang}} {{nope}}\n```\n\nAnd {{lang}}.\n")
out, err := runBuild(t, files)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
got := readOut(t, out, "people.html")
mustContain(t, "people", got, "<code>en</code>", "escaped {{lang}}.", "{{lang}} {{nope}}", "And en.")
}
// TestBuildEscapedPlaceholders checks the escape with block placeholders: an
// escaped one inside a sentence or alone in a paragraph is shown, not expanded
// and not an error, and a code span can show a placeholder as written.
func TestBuildEscapedPlaceholders(t *testing.T) {
files := baseFiles()
files["content/en/people.md"] = page("people", "People", "", "See \\{{news_list}} here.\n\n\\{{latest_news}}\n\nAnd `\\{{lang}}`.\n")
out, err := runBuild(t, files)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
got := readOut(t, out, "people.html")
mustContain(t, "people", got, "See {{news_list}} here.", "<p>{{latest_news}}</p>", "<code>{{lang}}</code>")
if strings.Contains(got, "<ul>") {
t.Error("an escaped block placeholder was expanded")
}
}
// TestBuildAssetsCopied checks the assets listed in site.json. // TestBuildAssetsCopied checks the assets listed in site.json.
func TestBuildAssetsCopied(t *testing.T) { func TestBuildAssetsCopied(t *testing.T) {
out, err := runBuild(t, baseFiles()) out, err := runBuild(t, baseFiles())
@ -388,7 +454,7 @@ func TestBuildFeed(t *testing.T) {
feed := readOut(t, out, "news/feed.xml") feed := readOut(t, out, "news/feed.xml")
mustContain(t, "feed", feed, mustContain(t, "feed", feed,
"<guid isPermaLink=\"true\">https://kmalloc.org/news/2026/11/shop.html</guid>", "<guid isPermaLink=\"true\">https://kmalloc.org/news/2026/11/shop.html</guid>",
`&lt;a href="https://kmalloc.org/people.html"&gt;people&lt;/a&gt;`) `<content:encoded><![CDATA[<p>The programme is out. See <a href="https://kmalloc.org/people.html">people</a>.</p>`)
} }
// TestBuildErrorsOnMissingTemplate checks that a missing layout template is named. // TestBuildErrorsOnMissingTemplate checks that a missing layout template is named.
@ -400,3 +466,33 @@ func TestBuildErrorsOnMissingTemplate(t *testing.T) {
t.Errorf("Build() error = %v, want it to name pages/manifesto", err) t.Errorf("Build() error = %v, want it to name pages/manifesto", err)
} }
} }
// TestBuildMenuComesFromTheSite checks that a page with an ID motorino has never
// heard of can be linked and marked from a template, with no change to the code.
func TestBuildMenuComesFromTheSite(t *testing.T) {
files := baseFiles()
files["content/en/labs.md"] = page("labs", "Labs", "", "Experiments.\n")
files["templates/header.html"] = `<header>
<a href="{{url_labs}}"{{cur_labs}}>Labs</a>
<a href="{{url_news}}"{{cur_news}}>News</a>
</header>
`
out, err := runBuild(t, files)
if err != nil {
t.Fatal(err)
}
mustContain(t, "labs.html", readOut(t, out, "labs.html"),
`<a href="/labs.html" aria-current="page">Labs</a>`, `<a href="/news/">News</a>`)
mustContain(t, "index.html", readOut(t, out, "index.html"), `<a href="/labs.html">Labs</a>`)
}
// TestBuildPagesBesidesHomeAndNewsAreOptional checks that only the home page and
// the news section are required: the rest of the menu is the site's choice.
func TestBuildPagesBesidesHomeAndNewsAreOptional(t *testing.T) {
files := baseFiles()
delete(files, "content/en/people.md")
delete(files, "content/it/persone.md")
if _, err := runBuild(t, files); err != nil {
t.Errorf("Build() without a people page: %v", err)
}
}

View file

@ -9,15 +9,13 @@ import (
"git.kmalloc.org/squid/motorino/internal/render" "git.kmalloc.org/squid/motorino/internal/render"
) )
// menuPages are the IDs of the pages in the menu; the current one is marked.
var menuPages = []string{"home", "manifesto", "people", "services", "news"}
// linkedPages are the IDs the templates link to with url_<id>.
var linkedPages = []string{"manifesto", "people", "services", "news", "history"}
// blockNames are the placeholders that expand to generated HTML. Until they // blockNames are the placeholders that expand to generated HTML. Until they
// are expanded they stand for themselves, so a page can hold them. // are expanded they stand for themselves, so a page can hold them.
var blockNames = []string{"news_list", "latest_news", "categories_list"} var blockNames = []string{"news_list", "latest_news", "categories_list", "news_blog", "news_archive"}
// sitePrefix goes before the title of a page that has no title of its own in the
// browser, such as a post.
const sitePrefix = "kmalloc.org: "
// document describes one output page for the shared page frame. // document describes one output page for the shared page frame.
type document struct { type document struct {
@ -78,8 +76,10 @@ func (b *builder) languageContext(lang string) map[string]string {
for k, label := range l.Nav { for k, label := range l.Nav {
ctx["nav_"+k] = render.Escape(label) ctx["nav_"+k] = render.Escape(label)
} }
for _, id := range linkedPages { for _, id := range b.pageIDs {
ctx["url_"+id] = b.pageAddress(lang, id) if _, ok := b.byKey[pageKey{id, b.defaultLang}]; ok {
ctx["url_"+id] = b.pageAddress(lang, id)
}
} }
ctx["history_attr"] = "" ctx["history_attr"] = ""
if _, ok := b.byKey[pageKey{"history", lang}]; !ok { if _, ok := b.byKey[pageKey{"history", lang}]; !ok {
@ -105,49 +105,62 @@ func (b *builder) documentContext(d document) map[string]string {
ctx["doc_q"] = strings.NewReplacer(":", "%3A", "/", "%2F").Replace(docURL) ctx["doc_q"] = strings.NewReplacer(":", "%3A", "/", "%2F").Replace(docURL)
title := d.title title := d.title
if d.prefixed { if d.prefixed {
title = "kmalloc.org: " + title title = sitePrefix + title
} }
ctx["title_html"] = render.EscapeAttr(title) ctx["title_html"] = render.EscapeAttr(title)
ctx["page_title"] = render.Escape(d.title) ctx["page_title"] = render.Escape(d.title)
ctx["description_attr"] = render.EscapeAttr(d.description) ctx["description_attr"] = render.EscapeAttr(d.description)
for _, k := range menuPages { ctx["og_type"] = "website"
ctx["cur_"+k] = "" ctx["article_meta"] = ""
if d.nav == k { for _, id := range b.pageIDs {
ctx["cur_"+k] = ` aria-current="page"` ctx["cur_"+id] = ""
if d.nav == id {
ctx["cur_"+id] = ` aria-current="page"`
} }
} }
ctx["lang_links"], ctx["alt_links"] = b.languageLinks(d) ctx["lang_links"], ctx["alt_links"] = b.languageLinks(d)
return ctx return ctx
} }
// languageLinks returns the links to the other languages for the menu and the // langTarget is a page in another language that a page links to.
// alternate links for the head. A page without translations links to the home type langTarget struct{ lang, path string }
// of the first other language that has one.
func (b *builder) languageLinks(d document) (links, alternates string) { // languageTargets returns the pages in the other languages that a page links
type target struct{ lang, path string } // to: its translations, with the address a Switch key gives instead of the
var twins []target // translation, or, when it has none, the home of the first other language that
// has one.
func (b *builder) languageTargets(d document) []langTarget {
var twins []langTarget
if d.id != "" { if d.id != "" {
for _, l := range b.cfg.Languages { for _, l := range b.cfg.Languages {
if p, ok := b.byKey[pageKey{d.id, l}]; ok && l != d.lang { if p, ok := b.byKey[pageKey{d.id, l}]; ok && l != d.lang {
twins = append(twins, target{l, p.Path}) twins = append(twins, langTarget{l, p.Path})
} }
} }
} }
if len(twins) == 0 { if len(twins) == 0 {
for _, l := range b.cfg.Languages { for _, l := range b.cfg.Languages {
if p, ok := b.byKey[pageKey{"home", l}]; ok && l != d.lang { if p, ok := b.byKey[pageKey{"home", l}]; ok && l != d.lang {
twins = append(twins, target{l, p.Path}) twins = append(twins, langTarget{l, p.Path})
break break
} }
} }
} }
for i, t := range twins {
if override, ok := d.switches[t.lang]; ok {
twins[i].path = override
}
}
return twins
}
// languageLinks returns the links to the other languages for the menu and the
// alternate links for the head.
func (b *builder) languageLinks(d document) (links, alternates string) {
twins := b.languageTargets(d)
anchors := make([]string, len(twins)) anchors := make([]string, len(twins))
for i, t := range twins { for i, t := range twins {
href := t.path anchors[i] = fmt.Sprintf(`<a href="%s" hreflang="%s" lang="%s">%s</a>`, render.EscapeAttr(t.path), t.lang, t.lang, render.Escape(b.langs[t.lang].Strings["lang_name"]))
if override, ok := d.switches[t.lang]; ok {
href = override
}
anchors[i] = fmt.Sprintf(`<a href="%s" hreflang="%s" lang="%s">%s</a>`, render.EscapeAttr(href), t.lang, t.lang, render.Escape(b.langs[t.lang].Strings["lang_name"]))
} }
links = strings.Join(anchors, "\n&middot;\n") links = strings.Join(anchors, "\n&middot;\n")

51
internal/build/date.go Normal file
View file

@ -0,0 +1,51 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"fmt"
"strconv"
"strings"
"time"
"git.kmalloc.org/squid/motorino/internal/site"
)
// longDate writes a date for a reader, in the language l, from the strings
// date_long (a layout with {d}, {month} and {y}) and month_01 to month_12. The
// day, the month and the year are those of the date as written, not converted
// to another time zone. A language without the layout, or without the name of
// the month, gets the ISO date.
func longDate(date time.Time, l site.Lang) string {
layout := l.Strings["date_long"]
month := l.Strings[fmt.Sprintf("month_%02d", int(date.Month()))]
if layout == "" || month == "" {
return date.Format(dateOnly)
}
return strings.NewReplacer(
"{d}", strconv.Itoa(date.Day()), "{month}", month, "{y}", strconv.Itoa(date.Year()),
).Replace(layout)
}
// monthYear is the label of an archive month in the language l, from the
// strings date_month_year (a layout with {month} and {y}) and month_01 to
// month_12; without them it is the year and the month, "2026-11".
func monthYear(year, month int, l site.Lang) string {
layout := l.Strings["date_month_year"]
name := l.Strings[fmt.Sprintf("month_%02d", month)]
if layout == "" || name == "" {
return fmt.Sprintf("%04d-%02d", year, month)
}
return strings.NewReplacer("{month}", name, "{y}", strconv.Itoa(year)).Replace(layout)
}
// readingText is the reading time of a post in the language l, from the string
// reading_time (a sentence with {n}); without the string it is empty and no
// reading time is shown.
func readingText(minutes int, l site.Lang) string {
layout := l.Strings["reading_time"]
if layout == "" {
return ""
}
return strings.ReplaceAll(layout, "{n}", strconv.Itoa(minutes))
}

View file

@ -0,0 +1,73 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"testing"
"time"
"git.kmalloc.org/squid/motorino/internal/site"
)
// dateLang returns a language with the strings that write a date.
func dateLang(layout string, months ...string) site.Lang {
l := site.Lang{Code: "xx", Strings: map[string]string{}}
if layout != "" {
l.Strings["date_long"] = layout
}
for i, m := range months {
l.Strings["month_"+string(rune('0'+(i+1)/10))+string(rune('0'+(i+1)%10))] = m
}
return l
}
// allMonths lists twelve month names for a test language.
var allMonths = []string{"gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"}
// TestLongDate checks the long date in a language, as written in the date.
func TestLongDate(t *testing.T) {
date, err := time.Parse("2006-01-02T15:04:05-0700", "2026-11-05T23:30:00-0500")
if err != nil {
t.Fatal(err)
}
if got := longDate(date, dateLang("{d} {month} {y}", allMonths...)); got != "5 novembre 2026" {
t.Errorf("longDate() = %q, want the day without a zero, the month name and the year", got)
}
if got := longDate(date, dateLang("{month} {d}, {y}", allMonths...)); got != "novembre 5, 2026" {
t.Errorf("longDate() with another layout = %q", got)
}
}
// TestLongDateFallsBack checks the ISO date when the language has no layout or a month.
func TestLongDateFallsBack(t *testing.T) {
date, _ := time.Parse("2006-01-02T15:04:05-0700", "2026-11-14T12:00:00+0100")
if got := longDate(date, dateLang("")); got != "2026-11-14" {
t.Errorf("no layout: longDate() = %q, want the ISO date", got)
}
if got := longDate(date, dateLang("{d} {month} {y}", "uno", "due")); got != "2026-11-14" {
t.Errorf("a missing month name: longDate() = %q, want the ISO date", got)
}
}
// TestMonthYear checks the label of an archive month.
func TestMonthYear(t *testing.T) {
l := dateLang("{d} {month} {y}", allMonths...)
l.Strings["date_month_year"] = "{month} {y}"
if got := monthYear(2026, 11, l); got != "novembre 2026" {
t.Errorf("monthYear() = %q", got)
}
if got := monthYear(2026, 3, dateLang("")); got != "2026-03" {
t.Errorf("monthYear() without strings = %q, want 2026-03", got)
}
}
// TestReadingText checks the reading time sentence and its absence.
func TestReadingText(t *testing.T) {
l := site.Lang{Strings: map[string]string{"reading_time": "{n} min di lettura"}}
if got := readingText(3, l); got != "3 min di lettura" {
t.Errorf("readingText() = %q", got)
}
if got := readingText(3, site.Lang{Strings: map[string]string{}}); got != "" {
t.Errorf("readingText() without a string = %q, want empty", got)
}
}

View file

@ -3,6 +3,7 @@
package build package build
import ( import (
"regexp"
"strings" "strings"
"time" "time"
@ -10,10 +11,20 @@ import (
"git.kmalloc.org/squid/motorino/internal/site" "git.kmalloc.org/squid/motorino/internal/site"
) )
// siteRelative matches a link that starts at the root of the site, such as
// href="/people.html", but not one that is relative to the protocol (href="//host/x").
var siteRelative = regexp.MustCompile(`\b(href|src)="/([^/])`)
// cdata wraps text in a CDATA section; the end marker inside the text is split
// over two sections, so it cannot close the first one.
func cdata(text string) string {
return "<![CDATA[" + strings.ReplaceAll(text, "]]>", "]]]]><![CDATA[>") + "]]>"
}
// feed returns the RSS 2.0 feed of the latest limit posts, which must be sorted // feed returns the RSS 2.0 feed of the latest limit posts, which must be sorted
// newest first. Each item carries the full text with absolute links. The // newest first. Each item has the description as a plain summary and the full
// channel is described by the language l, and newsURL is the address of the // text, with absolute links, in content:encoded. The channel is described by
// news page, such as "/news/". // the language l, and newsURL is the address of the news page, such as "/news/".
func feed(base string, l site.Lang, newsURL string, posts []renderedPost, limit int) string { func feed(base string, l site.Lang, newsURL string, posts []renderedPost, limit int) string {
if limit < len(posts) { if limit < len(posts) {
posts = posts[:limit] posts = posts[:limit]
@ -21,13 +32,22 @@ func feed(base string, l site.Lang, newsURL string, posts []renderedPost, limit
var items strings.Builder var items strings.Builder
for _, p := range posts { for _, p := range posts {
link := base + p.Path link := base + p.Path
text := strings.ReplaceAll(p.HTML, `href="/`, `href="`+base+`/`) text := siteRelative.ReplaceAllString(p.HTML, `$1="`+base+`/$2`)
items.WriteString("<item>\n") items.WriteString("<item>\n")
items.WriteString("<title>" + render.Escape(p.Title) + "</title>\n") items.WriteString("<title>" + render.Escape(p.Title) + "</title>\n")
items.WriteString("<link>" + link + "</link>\n") items.WriteString("<link>" + link + "</link>\n")
items.WriteString(`<guid isPermaLink="true">` + link + "</guid>\n") items.WriteString(`<guid isPermaLink="true">` + link + "</guid>\n")
items.WriteString("<pubDate>" + p.Date.Format(time.RFC1123Z) + "</pubDate>\n") items.WriteString("<pubDate>" + p.Date.Format(time.RFC1123Z) + "</pubDate>\n")
items.WriteString("<description>" + render.Escape(text) + "</description>\n") if p.Description != "" {
items.WriteString("<description>" + render.Escape(p.Description) + "</description>\n")
}
if p.Author != "" {
items.WriteString("<dc:creator>" + render.Escape(p.Author) + "</dc:creator>\n")
}
for _, c := range p.Categories {
items.WriteString("<category>" + render.Escape(c.Name) + "</category>\n")
}
items.WriteString("<content:encoded>" + cdata(text) + "</content:encoded>\n")
items.WriteString("</item>\n") items.WriteString("</item>\n")
} }
built := "" built := ""
@ -36,7 +56,7 @@ func feed(base string, l site.Lang, newsURL string, posts []renderedPost, limit
} }
url := base + newsURL url := base + newsURL
return `<?xml version="1.0" encoding="utf-8"?> return `<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel> <channel>
<title>kmalloc.org: ` + render.Escape(l.Nav["news"]) + `</title> <title>kmalloc.org: ` + render.Escape(l.Nav["news"]) + `</title>
<link>` + url + `</link> <link>` + url + `</link>

View file

@ -18,7 +18,7 @@ func feedLang() site.Lang {
// TestFeedItems checks the channel, the item fields, the limit and the dates. // TestFeedItems checks the channel, the item fields, the limit and the dates.
func TestFeedItems(t *testing.T) { func TestFeedItems(t *testing.T) {
items := []renderedPost{ items := []renderedPost{
{Post: testPost("new", "2026-11-14", "New <post>", ""), HTML: `<p>See <a href="/people.html">people</a>.</p>`}, {Post: testPost("new", "2026-11-14", "New <post>", "A summary & more.", "FLUG", "Linux Day"), HTML: `<p>See <a href="/people.html">people</a>.</p>`},
{Post: testPost("old", "2026-11-12", "Old", ""), HTML: "<p>Old text.</p>"}, {Post: testPost("old", "2026-11-12", "Old", ""), HTML: "<p>Old text.</p>"},
{Post: testPost("older", "2026-11-10", "Older", ""), HTML: "<p>Older text.</p>"}, {Post: testPost("older", "2026-11-10", "Older", ""), HTML: "<p>Older text.</p>"},
} }
@ -35,7 +35,11 @@ func TestFeedItems(t *testing.T) {
`<guid isPermaLink="true">https://kmalloc.org/news/2026/11/new.html</guid>`, `<guid isPermaLink="true">https://kmalloc.org/news/2026/11/new.html</guid>`,
"<pubDate>Sat, 14 Nov 2026 12:00:00 +0100</pubDate>", "<pubDate>Sat, 14 Nov 2026 12:00:00 +0100</pubDate>",
"<lastBuildDate>Sat, 14 Nov 2026 12:00:00 +0100</lastBuildDate>", "<lastBuildDate>Sat, 14 Nov 2026 12:00:00 +0100</lastBuildDate>",
`&lt;a href="https://kmalloc.org/people.html"&gt;`, `xmlns:content="http://purl.org/rss/1.0/modules/content/"`,
"<description>A summary &amp; more.</description>",
`<content:encoded><![CDATA[<p>See <a href="https://kmalloc.org/people.html">people</a>.</p>]]></content:encoded>`,
"<category>FLUG</category>",
"<category>Linux Day</category>",
} { } {
if !strings.Contains(got, want) { if !strings.Contains(got, want) {
t.Errorf("feed does not contain %q\n%s", want, got) t.Errorf("feed does not contain %q\n%s", want, got)
@ -49,6 +53,44 @@ func TestFeedItems(t *testing.T) {
} }
} }
// TestFeedCarriesTheFullTextAsContent checks that the text is in content:encoded and the
// description stays a plain summary, and that the end marker of a CDATA section in a text
// does not close it.
func TestFeedCarriesTheFullTextAsContent(t *testing.T) {
post := testPost("a", "2026-11-14", "A", "Short.")
post.Author = "Matteo Bini"
items := []renderedPost{{Post: post, HTML: "<pre>a ]]> b</pre>\n<p>Text.</p>"}}
got := feed("https://kmalloc.org", feedLang(), "/news/", items, 5)
mustContain(t, "feed", got, "<description>Short.</description>", "<dc:creator>Matteo Bini</dc:creator>",
`xmlns:dc="http://purl.org/dc/elements/1.1/"`, "<pre>a ]]]]><![CDATA[> b</pre>")
if strings.Contains(got, "<description><") || strings.Contains(got, "&lt;pre&gt;") {
t.Errorf("the description holds markup:\n%s", got)
}
var parsed struct {
Channel struct {
Items []struct {
Content string `xml:"http://purl.org/rss/1.0/modules/content/ encoded"`
} `xml:"item"`
} `xml:"channel"`
}
if err := xml.Unmarshal([]byte(got), &parsed); err != nil {
t.Fatalf("feed is not well-formed XML: %v", err)
}
if len(parsed.Channel.Items) != 1 || parsed.Channel.Items[0].Content != "<pre>a ]]> b</pre>\n<p>Text.</p>" {
t.Errorf("content:encoded = %+v, want the text back as written", parsed.Channel.Items)
}
}
// TestFeedLeavesProtocolRelativeLinks checks that "//host/x" is not given the site address.
func TestFeedLeavesProtocolRelativeLinks(t *testing.T) {
items := []renderedPost{{Post: testPost("a", "2026-11-14", "A", ""), HTML: `<img src="//cdn.example/x.png"> <a href="/p.html">p</a>`}}
got := feed("https://kmalloc.org", feedLang(), "/news/", items, 5)
mustContain(t, "feed", got, `src="//cdn.example/x.png"`, `href="https://kmalloc.org/p.html"`)
if strings.Contains(got, "https://kmalloc.org//cdn") {
t.Error("a protocol-relative link got the site address")
}
}
// TestFeedWithoutPosts checks that an empty feed is still a valid channel. // TestFeedWithoutPosts checks that an empty feed is still a valid channel.
func TestFeedWithoutPosts(t *testing.T) { func TestFeedWithoutPosts(t *testing.T) {
got := feed("https://kmalloc.org", feedLang(), "/news/", nil, 20) got := feed("https://kmalloc.org", feedLang(), "/news/", nil, 20)

View file

@ -46,6 +46,12 @@ func readOut(t *testing.T, out, rel string) string {
return string(data) return string(data)
} }
// readOutMaybe returns the content of a file of the output, or the error when it is missing.
func readOutMaybe(out, rel string) (string, error) {
data, err := os.ReadFile(filepath.Join(out, filepath.FromSlash(rel)))
return string(data), err
}
// mustContain fails the test for each want that is not in got. // mustContain fails the test for each want that is not in got.
func mustContain(t *testing.T, name, got string, wants ...string) { func mustContain(t *testing.T, name, got string, wants ...string) {
t.Helper() t.Helper()
@ -94,12 +100,14 @@ func baseFiles() map[string]string {
"content/en/manifesto.md": page("manifesto", "Manifesto", "Layout: manifesto\n", "## 01 — Antifascist\n\nText one.\n\n## Our commitments\n\nText two.\n"), "content/en/manifesto.md": page("manifesto", "Manifesto", "Layout: manifesto\n", "## 01 — Antifascist\n\nText one.\n\n## Our commitments\n\nText two.\n"),
"content/en/manifesto-history.md": page("history", "Manifesto history", "", "Versions.\n"), "content/en/manifesto-history.md": page("history", "Manifesto history", "", "Versions.\n"),
"content/en/people.md": page("people", "People", "", "## squid\n\nI run it.\n"), "content/en/people.md": page("people", "People", "", "## squid\n\nI run it.\n"),
"content/en/projects.md": page("projects", "Projects", "", "## motorino\n\nThis.\n"),
"content/en/services.md": page("services", "Services", "Switch: it=/it/servizi.html\n", "Write to **{{contact_shown}}**.\n"), "content/en/services.md": page("services", "Services", "Switch: it=/it/servizi.html\n", "Write to **{{contact_shown}}**.\n"),
"content/en/news/index.md": page("news", "News", "", "{{news_list}}\n\n{{categories_list}}\n"), "content/en/news/index.md": page("news", "News", "", "{{news_list}}\n\n{{categories_list}}\n"),
"content/it/index.md": page("home", "kmalloc.org", "Layout: bare\n", "{{latest_news}}\n"), "content/it/index.md": page("home", "kmalloc.org", "Layout: bare\n", "{{latest_news}}\n"),
"content/it/manifesto.md": page("manifesto", "Manifesto", "Layout: manifesto\n", "## 01 — Antifascisti\n\nTesto.\n"), "content/it/manifesto.md": page("manifesto", "Manifesto", "Layout: manifesto\n", "## 01 — Antifascisti\n\nTesto.\n"),
"content/it/cronologia-manifesto.md": page("history", "Cronologia", "", "Versioni.\n"), "content/it/cronologia-manifesto.md": page("history", "Cronologia", "", "Versioni.\n"),
"content/it/persone.md": page("people", "Persone", "", "Io.\n"), "content/it/persone.md": page("people", "Persone", "", "Io.\n"),
"content/it/progetti.md": page("projects", "Progetti", "", "## motorino\n\nQuesto.\n"),
"content/it/servizi.md": page("services", "Servizi", "", "Scrivi a {{contact_shown}}.\n"), "content/it/servizi.md": page("services", "Servizi", "", "Scrivi a {{contact_shown}}.\n"),
"content/es/manifesto.md": page("manifesto", "Manifiesto", "Layout: manifesto\nNote: Traduccion de la version 1.0.\nManifestoVersion: 1.0\nManifestoDate: 2026-11-12\n", "## 01 — Antifascistas\n\nTexto.\n"), "content/es/manifesto.md": page("manifesto", "Manifiesto", "Layout: manifesto\nNote: Traduccion de la version 1.0.\nManifestoVersion: 1.0\nManifestoDate: 2026-11-12\n", "## 01 — Antifascistas\n\nTexto.\n"),

501
internal/build/gemini.go Normal file
View file

@ -0,0 +1,501 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
"git.kmalloc.org/squid/motorino/internal/content"
"git.kmalloc.org/squid/motorino/internal/gemtext"
"git.kmalloc.org/squid/motorino/internal/mdx"
"git.kmalloc.org/squid/motorino/internal/render"
)
// GeminiOptions tell BuildGemini where the site is and where to write the capsule.
type GeminiOptions struct {
// SiteDir is the folder of the site, the same one Build reads.
SiteDir string
// OutDir is the folder the gemtext is written to; it is created when missing.
OutDir string
// Version is the version of motorino.
Version string
// Now is the moment the capsule is built at, as in Options.
Now time.Time
// Drafts publishes the drafts and the posts dated in the future too.
Drafts bool
// Clean empties OutDir before the capsule is written, as Options.Clean does.
Clean bool
// Language is the language served at the root of the capsule, the others
// living under /<code>/. It is the language the server serves by default
// (Agate's --lang). Empty means gemini_language of site.json, then the
// default language of the site.
Language string
}
// GeminiResult summarises a Gemini build.
type GeminiResult struct {
// Pages is the number of gemtext files written.
Pages int
// Stale lists the files that were already in the output folder and that this
// build did not write; see Result.Stale.
Stale []string
}
// gemNews is the address of the news in the capsule. They exist once, whatever
// the language of the root, so it is always /news/.
const gemNews = "/news/"
// metaName is the file that tells the server the language of each folder; the
// server reads it and never serves it.
const metaName = ".meta"
// gemini holds one Gemini build, on top of the loaded site.
type gemini struct {
*builder
// root is the language served at the root.
root string
// pageAddr is the address of every page in the capsule, by ID and language.
pageAddr map[pageKey]string
// addr maps an address of the website to the address in the capsule.
addr map[string]string
// usedAssets lists the assets some page points to: only those are copied.
usedAssets map[string]bool
// gemFiles holds the output, and gemOwners what produces each file.
gemFiles map[string]string
gemOwners map[string]string
}
// BuildGemini reads the site in opts.SiteDir and writes it as a Gemini capsule
// to opts.OutDir: the pages of each language, the news once under /news/ and a
// .meta file for the server. Everything is rendered in memory first, so a
// failure in the content leaves the folder untouched.
func BuildGemini(opts GeminiOptions) (GeminiResult, error) {
b, err := load(Options{SiteDir: opts.SiteDir, OutDir: opts.OutDir, Version: opts.Version, Now: opts.Now, Drafts: opts.Drafts})
if err != nil {
return GeminiResult{}, err
}
g := &gemini{
builder: b, pageAddr: map[pageKey]string{}, addr: map[string]string{}, usedAssets: map[string]bool{},
gemFiles: map[string]string{}, gemOwners: map[string]string{},
}
if g.root, err = g.rootLanguage(opts.Language); err != nil {
return GeminiResult{}, err
}
g.mapAddresses()
for _, p := range b.pages {
if err := g.renderPage(p); err != nil {
return GeminiResult{}, err
}
}
steps := []func() error{g.renderPosts, g.renderCategories, g.renderArchives, g.renderFeed}
for _, step := range steps {
if err := step(); err != nil {
return GeminiResult{}, err
}
}
pages := len(g.gemFiles)
if err := g.publish(metaName, "the language file of the server", g.meta()); err != nil {
return GeminiResult{}, err
}
assets, err := g.assets()
if err != nil {
return GeminiResult{}, err
}
files := make(map[string][]byte, len(g.gemFiles)+len(assets))
for rel, text := range g.gemFiles {
files[rel] = []byte(text)
}
for rel, data := range assets {
files[rel] = data
}
stale, err := writeOutput(opts.OutDir, opts.SiteDir, opts.Clean, files)
if err != nil {
return GeminiResult{}, err
}
return GeminiResult{Pages: pages, Stale: stale}, nil
}
// rootLanguage returns the language at the root of the capsule: the option,
// else gemini_language of site.json, else the default language. It must be a
// language of the site that has a home page.
func (g *gemini) rootLanguage(option string) (string, error) {
code, from := option, "the language option"
if code == "" {
code, from = g.cfg.Strings["gemini_language"], "gemini_language in site.json"
}
if code == "" {
return g.defaultLang, nil
}
if _, ok := g.langs[code]; !ok {
return "", fmt.Errorf("%s: %q is not one of the languages of the site", from, code)
}
if _, ok := g.byKey[pageKey{"home", code}]; !ok {
return "", fmt.Errorf("%s: the language %q has no home page, so it cannot be the root of the capsule", from, code)
}
return code, nil
}
// meta returns the content of the .meta file. The server gives a document the
// language of the first line that matches it, so the lines come from the most
// specific to the most general:
//
// - a line for the file of every post written in another language than the news;
// - for every language other than the one at the root, and for the news when they
// are in another language, two lines for the folder. A glob does not cross
// folders, so each folder has a line for itself and one for what is below it.
func (g *gemini) meta() string {
var out strings.Builder
for _, p := range g.posts {
if p.Language != g.newsLang {
fmt.Fprintf(&out, "%s:;lang=%s\n", gemFile(gemPostAddress(p)), p.Language)
}
}
line := func(folder, lang string) {
fmt.Fprintf(&out, "%s/*:;lang=%s\n%s/**/*:;lang=%s\n", folder, lang, folder, lang)
}
for _, lang := range g.cfg.Languages {
if lang != g.root {
line(lang, lang)
}
}
if g.newsLang != g.root {
line(strings.Trim(gemNews, "/"), g.newsLang)
}
return out.String()
}
// pageGemAddress returns the address in the capsule of a page of lang whose
// address in the website is html: the root language at the root, the others
// under their code, .gmi for .html.
func (g *gemini) pageGemAddress(lang, html string) string {
p := html
if lang != g.defaultLang {
p = strings.TrimPrefix(p, "/"+lang)
}
if strings.HasSuffix(p, ".html") {
p = strings.TrimSuffix(p, ".html") + ".gmi"
}
if lang != g.root {
p = "/" + lang + p
}
return p
}
// gemPostAddress, gemCategoryAddress, gemYearAddress and gemMonthAddress are the
// addresses in the capsule of a post, a category and the archives.
func gemPostAddress(p content.Post) string { return strings.TrimSuffix(p.Path, ".html") + ".gmi" }
func gemCategoryAddress(slug string) string { return gemNews + "category/" + slug + ".gmi" }
func gemYearAddress(year int) string { return fmt.Sprintf("%s%04d/", gemNews, year) }
func gemMonthAddress(year, month int) string {
return fmt.Sprintf("%s%04d/%02d/", gemNews, year, month)
}
// mapAddresses fills the maps from the addresses of the website to those of the capsule.
func (g *gemini) mapAddresses() {
for _, p := range g.pages {
address := g.pageGemAddress(p.Lang, p.Path)
if p.ID == "news" && p.Lang == g.newsLang {
address = gemNews
}
g.pageAddr[pageKey{p.ID, p.Lang}] = address
g.addr[p.Path] = address
}
for _, p := range g.posts {
g.addr[p.Path] = gemPostAddress(p)
}
for _, c := range g.categories {
g.addr[categoryPath(c.Slug)] = gemCategoryAddress(c.Slug)
}
for _, y := range groupArchive(g.posts) {
g.addr[g.yearPath(y.year)] = gemYearAddress(y.year)
for _, m := range y.months {
g.addr[g.monthPath(m.year, m.month)] = gemMonthAddress(m.year, m.month)
}
}
g.addr[g.newsPath+"feed.xml"] = gemNews + "feed.gmi"
}
// publish stores a file of the capsule; source names what produced it.
func (g *gemini) publish(rel, source, text string) error {
if other, taken := g.gemOwners[rel]; taken {
return fmt.Errorf("%s and %s are both published as %s", other, source, rel)
}
g.gemOwners[rel] = source
g.gemFiles[rel] = text
return nil
}
// gemFile returns the file of an address of the capsule: a folder gets index.gmi.
func gemFile(address string) string {
rel := strings.TrimPrefix(address, "/")
if rel == "" || strings.HasSuffix(rel, "/") {
rel += "index.gmi"
}
return rel
}
// scheme matches the start of an address that names a protocol.
var scheme = regexp.MustCompile(`^([A-Za-z][A-Za-z0-9+.-]*):`)
// externalSchemes are the protocols a link may use to leave the capsule.
var externalSchemes = map[string]bool{
"http": true, "https": true, "mailto": true, "gemini": true, "xmpp": true, "irc": true, "ircs": true, "matrix": true,
"bitcoin": true, "magnet": true, "tel": true, "sms": true,
}
// resolve turns an address found in the HTML of the page at base into the
// address to write in the capsule. An address with a protocol stays as it is
// when the protocol is a known one, and is an error otherwise; an address in the
// website is mapped to its page or file, and a file is copied by the end of the
// build. An address that starts with one of the paths of gemini_web_paths in
// site.json is one the website serves by other means: it becomes a link to the
// website. A fragment alone is dropped, and so is the fragment of an address: a
// gemini link cannot point inside a document.
func (g *gemini) resolve(href, base string) (string, error) {
if strings.HasPrefix(href, "#") {
return "", nil
}
if m := scheme.FindStringSubmatch(href); m != nil {
if !externalSchemes[strings.ToLower(m[1])] {
return "", fmt.Errorf("the protocol %q is not supported", m[1])
}
return href, nil
}
if strings.HasPrefix(href, "//") {
return "", errors.New("an address that starts with // is not supported")
}
target, _, _ := strings.Cut(href, "#")
if !strings.HasPrefix(target, "/") {
folder := path.Dir(base)
if strings.HasSuffix(base, "/") {
folder = strings.TrimSuffix(base, "/")
}
joined := path.Join("/", folder, target)
if strings.HasSuffix(target, "/") && joined != "/" {
joined += "/"
}
target = joined
}
if address, ok := g.addr[target]; ok {
return address, nil
}
name := strings.TrimPrefix(target, "/")
for asset := range g.cfg.Assets {
if path.Clean(asset) == name {
g.usedAssets[asset] = true
return target, nil
}
}
for _, prefix := range strings.Split(g.cfg.Strings["gemini_web_paths"], ",") {
if prefix = strings.TrimSpace(prefix); prefix != "" && strings.HasPrefix(target, prefix) {
return g.cfg.Strings["base_url"] + target, nil
}
}
return "", errors.New("no page or file of the site has this address (a path the website serves by other means goes in gemini_web_paths of site.json)")
}
// convert converts the HTML of a page or post to gemtext. base is the address
// of the page in the website, lang its language.
func (g *gemini) convert(name, html, base, lang string) (string, error) {
text, err := gemtext.Convert(html, gemtext.Options{
Link: func(href string) (string, error) { return g.resolve(href, base) },
Block: func(block string) (string, error) { return g.block(block, lang) },
})
if err != nil {
return "", fmt.Errorf("%s: %w", name, err)
}
return strings.TrimRight(text, "\n"), nil
}
// markdown replaces the placeholders of a source, converts it to HTML and puts
// the element <gemblock> where a generated list goes, for convert to expand. A
// list with nothing in it leaves no element, and the heading of an empty list of
// categories or of the archive goes with it.
func (g *gemini) markdown(name, source string, ctx map[string]string) (string, error) {
text, err := mdx.SubstituteMarkdown(name, source, ctx)
if err != nil {
return "", err
}
html, _, err := mdx.Render(text)
if err != nil {
return "", fmt.Errorf("%s: %w", name, err)
}
lang := ctx["lang"]
html, err = replaceBlocks(name, html, func(block string) (string, error) {
text, err := g.block(block, lang)
if err != nil || text == "" {
return "", err
}
return `<gemblock name="` + block + `"></gemblock>`, nil
})
if err != nil {
return "", err
}
return render.Unescape(html), nil
}
// assets reads the files some page points to.
func (g *gemini) assets() (map[string][]byte, error) {
names := make([]string, 0, len(g.usedAssets))
for name := range g.usedAssets {
names = append(names, name)
}
sort.Strings(names)
out := map[string][]byte{}
for _, name := range names {
source := g.cfg.Assets[name]
data, err := os.ReadFile(filepath.Join(g.opts.SiteDir, filepath.FromSlash(source)))
if err != nil {
return nil, fmt.Errorf("asset %q: %w", name, err)
}
rel := path.Clean(name)
if other, taken := g.gemOwners[rel]; taken {
return nil, fmt.Errorf("the asset %q and %s are both published as %s", name, other, rel)
}
out[rel] = data
}
return out, nil
}
// heading is the first level heading of a page.
func heading(title string) string { return "# " + strings.Join(strings.Fields(title), " ") }
// withoutTitle prepares a body converted to gemtext to go under the heading of
// the title. A leading heading that says the title is dropped, since the page
// has it already, and the other first level headings go one level down: a page
// has one.
func withoutTitle(title, body string) string {
lines := strings.Split(body, "\n")
if first, ok := strings.CutPrefix(lines[0], "# "); ok && (first == title || strings.HasSuffix(title, ": "+first)) {
lines = lines[1:]
}
fenced := false
for i, line := range lines {
switch {
case strings.HasPrefix(line, "```"):
fenced = !fenced
case !fenced && strings.HasPrefix(line, "# "):
lines[i] = "#" + line
}
}
return strings.Join(lines, "\n")
}
// navigation returns the links every page ends with: the home and the entries
// of the menu of the language, in the order its file lists them, then the
// other languages. An entry whose page the language lacks is left out.
func (g *gemini) navigation(d document) string {
l := g.langs[d.lang]
var menu []string
if home, ok := g.pageAddr[pageKey{"home", d.lang}]; ok {
label := l.Strings["back"]
if label == "" {
label = g.byKey[pageKey{"home", d.lang}].Title
}
menu = append(menu, gemtext.Link(home, label))
}
for _, key := range l.NavOrder {
address, ok := g.pageAddr[pageKey{key, d.lang}]
if !ok && key == "news" {
address, ok = gemNews, true
}
if ok {
menu = append(menu, gemtext.Link(address, l.Nav[key]))
}
}
parts := []string{strings.Join(menu, "\n")}
var others []string
for _, t := range g.languageTargets(d) {
if address, err := g.resolve(t.path, d.path); err == nil && address != "" {
others = append(others, gemtext.Link(address, g.langs[t.lang].Strings["lang_name"]))
}
}
if len(others) > 0 {
parts = append(parts, strings.Join(others, "\n"))
}
return strings.Join(parts, "\n\n")
}
// translationNote returns the line, and the link to report a problem, that a
// machine translated language puts at the top of its pages; empty for the others.
func (g *gemini) translationNote(lang string) string {
l := g.langs[lang]
note := l.Strings["translation_note"]
if note == "" {
return ""
}
label, issues := l.Strings["translation_note_a"], g.cfg.Strings["issues_url"]
if label == "" || issues == "" {
return gemtext.Text(note)
}
return gemtext.Text(note+" "+label+".") + "\n" + gemtext.Link(issues, label)
}
// compose joins the parts of a page with a blank line between them, leaving out
// the empty ones, and ends the text with a line break.
func compose(parts ...string) string {
var kept []string
for _, part := range parts {
if part = strings.Trim(part, "\n"); part != "" {
kept = append(kept, part)
}
}
return strings.Join(kept, "\n\n") + "\n"
}
// renderPage converts a page and writes it with the navigation.
func (g *gemini) renderPage(p content.Page) error {
d := document{
lang: p.Lang, id: p.ID, nav: p.ID, path: p.Path, title: p.Title,
description: p.Description, switches: p.Switch, source: p.Source,
}
ctx := g.documentContext(d)
version, date := g.cfg.Strings["manifesto_version"], g.cfg.Strings["manifesto_date"]
if p.ManifestoVersion != "" {
version = p.ManifestoVersion
}
if p.ManifestoDate != "" {
date = p.ManifestoDate
}
ctx["manifesto_version"], ctx["manifesto_date"] = render.Escape(version), render.Escape(date)
source := p.Body
if p.Layout == "manifesto" {
var err error
if source, err = mdx.NoEmDash(source); err != nil {
return fmt.Errorf("%s: %w", p.Source, err)
}
}
html, err := g.markdown(p.Source, source, ctx)
if err != nil {
return err
}
body, err := g.convert(p.Source, html, p.Path, p.Lang)
if err != nil {
return err
}
var intro []string
if p.Layout == "manifesto" {
intro = []string{g.manifestoLine(p.Lang, version, date), gemtext.Text(p.Note)}
}
parts := append([]string{heading(p.Title), g.translationNote(p.Lang)}, intro...)
parts = append(parts, withoutTitle(p.Title, body), g.navigation(d))
return g.publish(gemFile(g.pageAddr[pageKey{p.ID, p.Lang}]), p.Source, compose(parts...))
}
// manifestoLine is the line that says the version and the date of the manifesto.
func (g *gemini) manifestoLine(lang, version, date string) string {
line := strings.TrimSpace(g.langs[lang].Strings["version"] + " " + version)
if date != "" {
line += ", " + date
}
return gemtext.Text(line)
}

View file

@ -0,0 +1,249 @@
// 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)
}

View file

@ -0,0 +1,523 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"io/fs"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"testing"
)
// buildGem builds the Gemini output of the site made of files, with the options changed by tune.
func buildGem(t *testing.T, files map[string]string, tune func(*GeminiOptions)) (string, error) {
t.Helper()
out := filepath.Join(t.TempDir(), "capsule")
opts := GeminiOptions{SiteDir: writeFiles(t, files), OutDir: out, Version: "1.2.3", Now: fixedNow, Language: "it"}
if tune != nil {
tune(&opts)
}
_, err := BuildGemini(opts)
return out, err
}
// gemSite is the base site with a services page that has a table and an image, a Spanish news page left out and
// the news index of the English pages.
func gemSite(extra map[string]string) map[string]string {
files := withFiles(map[string]string{
"lang/en.json": blogLangEn, "lang/it.json": blogLangIt,
"content/en/services.md": page("services", "Services", "", "Write to **{{contact_shown}}**.\n\n<table class=\"data\">\n<thead><tr><th>Service</th><th>Kept</th></tr></thead>\n<tbody><tr><td>Mail</td><td>30 days</td></tr></tbody>\n</table>\n\n![The mark of kmalloc](/logo.svg)\n"),
"content/en/news/index.md": page("news", "News", "Layout: bare\n", "# News\n\n{{news_list}}\n\n## Categories\n\n{{categories_list}}\n\n## Archive\n\n{{news_archive}}\n\n[Feed](/news/feed.xml)\n"),
"news/shop.md": "Title: A shop\nAuthor: squid\nDate: 2026-11-14T12:00:00+0100\nCategories: FLUG\nDescription: About the shop.\nMastodon: https://social.kmalloc.org/@squid/115\n-\nThere is a shop. Mail me at squid@kmalloc.org or visit [the site](https://kmalloc.org/).\n",
})
for k, v := range extra {
if v == "" {
delete(files, k)
continue
}
files[k] = v
}
return files
}
// gemFiles returns every file of the output, as slash paths relative to it.
func gemFiles(t *testing.T, out string) []string {
t.Helper()
var files []string
err := filepath.WalkDir(out, func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return err
}
rel, _ := filepath.Rel(out, path)
files = append(files, filepath.ToSlash(rel))
return nil
})
if err != nil {
t.Fatal(err)
}
sort.Strings(files)
return files
}
// TestGeminiTree checks which files the capsule has, with Italian at the root.
func TestGeminiTree(t *testing.T) {
out, err := buildGem(t, gemSite(nil), nil)
if err != nil {
t.Fatalf("BuildGemini() error = %v", err)
}
have := map[string]bool{}
for _, f := range gemFiles(t, out) {
have[f] = true
}
for _, want := range []string{
"index.gmi", "manifesto.gmi", "persone.gmi", "servizi.gmi", "cronologia-manifesto.gmi", "progetti.gmi",
"en/index.gmi", "en/manifesto.gmi", "en/people.gmi", "en/services.gmi", "es/manifesto.gmi",
"news/index.gmi", "news/feed.gmi", "news/2026/11/shop.gmi", "news/2026/11/status.gmi", "news/2026/09/gnu-linux-day-2026.gmi",
"news/category/flug.gmi", "news/category/linux-day.gmi", "news/2026/index.gmi", "news/2026/11/index.gmi", "news/2026/09/index.gmi",
".meta", "logo.svg",
} {
if !have[want] {
t.Errorf("the capsule has no %s", want)
}
}
for f := range have {
if strings.HasSuffix(f, ".html") || strings.HasPrefix(f, "it/") || strings.HasPrefix(f, "en/news/") || strings.HasPrefix(f, "es/news/") || strings.HasSuffix(f, "robots.txt") {
t.Errorf("the capsule has %s, which it should not", f)
}
}
}
// TestGeminiRootLanguage checks the language at the root: the option, site.json, then the default language.
func TestGeminiRootLanguage(t *testing.T) {
plain, err := buildGem(t, gemSite(nil), func(o *GeminiOptions) { o.Language = "" })
if err != nil {
t.Fatal(err)
}
mustContain(t, "index.gmi", readOut(t, plain, "index.gmi"), "# kmalloc.org")
if !exists(t, plain, "it/index.gmi") || exists(t, plain, "en/index.gmi") {
t.Error("without a choice the default language is at the root and the others under their code")
}
files := gemSite(map[string]string{"site.json": strings.Replace(baseFiles()["site.json"], `"languages"`, `"gemini_language": "it", "languages"`, 1)})
fromSite, err := buildGem(t, files, func(o *GeminiOptions) { o.Language = "" })
if err != nil {
t.Fatal(err)
}
if !exists(t, fromSite, "en/index.gmi") || exists(t, fromSite, "it/index.gmi") {
t.Error("gemini_language in site.json does not put Italian at the root")
}
for name, tune := range map[string]func(*GeminiOptions){"xx": func(o *GeminiOptions) { o.Language = "xx" }, "es has no home": func(o *GeminiOptions) { o.Language = "es" }} {
if _, err := buildGem(t, gemSite(nil), tune); err == nil {
t.Errorf("language %s: no error", name)
}
}
}
// TestGeminiMeta checks the .meta file that sets the language of the documents.
func TestGeminiMeta(t *testing.T) {
out, err := buildGem(t, gemSite(nil), nil)
if err != nil {
t.Fatal(err)
}
want := "en/*:;lang=en\nen/**/*:;lang=en\nes/*:;lang=es\nes/**/*:;lang=es\nnews/*:;lang=en\nnews/**/*:;lang=en\n"
if got := readOut(t, out, ".meta"); got != want {
t.Errorf(".meta =\n%q\nwant\n%q", got, want)
}
plain, err := buildGem(t, gemSite(nil), func(o *GeminiOptions) { o.Language = "en" })
if err != nil {
t.Fatal(err)
}
want = "it/*:;lang=it\nit/**/*:;lang=it\nes/*:;lang=es\nes/**/*:;lang=es\n"
if got := readOut(t, plain, ".meta"); got != want {
t.Errorf(".meta with English at the root =\n%q\nwant\n%q (the news are in the language of the root: no lines for them)", got, want)
}
for _, f := range gemFiles(t, out) {
if strings.HasSuffix(f, ".gmi") && strings.Contains(readOut(t, out, f), ".meta") {
t.Errorf("%s mentions .meta", f)
}
}
}
// TestGeminiPages checks the headings, the layouts and the navigation of a page.
func TestGeminiPages(t *testing.T) {
out, err := buildGem(t, gemSite(nil), nil)
if err != nil {
t.Fatal(err)
}
services := readOut(t, out, "en/services.gmi")
mustContain(t, "en/services.gmi", services, "# Services\n", "Write to squid (at) kmalloc.org.\n",
"```\nService Kept\n------- -------\nMail 30 days\n```\n", "=> /logo.svg The mark of kmalloc\n")
if strings.Contains(services, "<") || strings.Contains(services, "**") {
t.Errorf("the page has markup left:\n%s", services)
}
mustContain(t, "en/services.gmi navigation", services,
"=> /en/ kmalloc.org\n", "=> /en/manifesto.gmi Manifesto\n", "=> /en/people.gmi People\n", "=> /en/services.gmi Services\n", "=> /news/ News\n",
"=> /servizi.gmi Italiano\n")
manifesto := readOut(t, out, "es/manifesto.gmi")
mustContain(t, "es/manifesto.gmi", manifesto, "# Manifiesto\n", "Version 1.0, 2026-11-12\n", "Traduccion de la version 1.0.\n", "=> /es/manifesto.gmi Manifiesto\n")
home := readOut(t, out, "index.gmi")
mustContain(t, "index.gmi", home, "=> /news/ Tutte le news\n", "=> /news/2026/11/shop.gmi 2026-11-14 A shop\n", "=> /en/ English\n")
if strings.HasPrefix(home, "# ") && !strings.Contains(home, "kmalloc.org") {
t.Errorf("home has no title:\n%s", home)
}
}
// TestGeminiHomesLinkToTheNews checks that every home links to the one copy of the news.
func TestGeminiHomesLinkToTheNews(t *testing.T) {
out, err := buildGem(t, gemSite(nil), nil)
if err != nil {
t.Fatal(err)
}
for _, rel := range []string{"index.gmi", "en/index.gmi"} {
mustContain(t, rel, readOut(t, out, rel), "=> /news/ ", "=> /news/2026/11/shop.gmi 2026-11-14 A shop\n")
}
}
// TestGeminiNews checks a post, the index, a category, the archive and the feed.
func TestGeminiNews(t *testing.T) {
out, err := buildGem(t, blogGem(), nil)
if err != nil {
t.Fatal(err)
}
post := readOut(t, out, "news/2026/11/shop.gmi")
mustContain(t, "post", post, "# A shop\n", "14 November 2026 · squid · FLUG · 1 min read\n", "About the shop.\n",
"There is a shop. Mail me at squid@kmalloc.org or visit the site.\n", "=> https://kmalloc.org/ the site\n",
"=> /news/category/flug.gmi FLUG\n", "=> https://social.kmalloc.org/@squid/115 Reply on Mastodon\n",
"=> /news/2026/11/status.gmi Older: Status page\n", "=> /news/ All news\n", "=> /en/manifesto.gmi Manifesto\n")
if strings.Contains(post, "Newer:") {
t.Error("the newest post has a link to a newer one")
}
gnu := readOut(t, out, "news/2026/09/gnu-linux-day-2026.gmi")
mustContain(t, "older post", gnu, "=> /en/people.gmi people\n", "=> /news/2026/11/status.gmi Newer: Status page\n")
index := readOut(t, out, "news/index.gmi")
mustContain(t, "index", index, "# News\n",
"=> /news/2026/11/shop.gmi 2026-11-14 A shop\nAbout the shop.\n", "=> /news/2026/11/status.gmi 2026-11-12 Status page\n",
"=> /news/category/flug.gmi FLUG (2)\n", "=> /news/2026/ 2026 (3)\n", "=> /news/2026/11/ November 2026 (2)\n", "=> /news/feed.gmi Feed\n")
category := readOut(t, out, "news/category/flug.gmi")
mustContain(t, "category", category, "# Category: FLUG\n", "=> /news/2026/11/shop.gmi 2026-11-14 A shop\n", "=> /news/2026/09/gnu-linux-day-2026.gmi 2026-09-18 GNU/Linux Day 2026\n")
month := readOut(t, out, "news/2026/11/index.gmi")
mustContain(t, "month", month, "# Archive: November 2026\n", "=> /news/2026/ Archive of the year 2026\n", "A shop")
if strings.Contains(month, "GNU/Linux") {
t.Error("the archive of November lists a post of September")
}
year := readOut(t, out, "news/2026/index.gmi")
mustContain(t, "year", year, "# Archive: 2026\n", "=> /news/2026/09/ September 2026 (1)\n", "=> /news/2026/11/ November 2026 (2)\n")
}
// blogGem is the site of the tests of the news pages.
func blogGem() map[string]string { return gemSite(nil) }
// TestGeminiFeed checks the feed: newest first, a date before each title.
func TestGeminiFeed(t *testing.T) {
out, err := buildGem(t, blogGem(), nil)
if err != nil {
t.Fatal(err)
}
feed := readOut(t, out, "news/feed.gmi")
want := "=> /news/2026/11/shop.gmi 2026-11-14 A shop\n=> /news/2026/11/status.gmi 2026-11-12 Status page\n=> /news/2026/09/gnu-linux-day-2026.gmi 2026-09-18 GNU/Linux Day 2026\n"
mustContain(t, "feed", feed, "# kmalloc.org: News\n", want)
if first := strings.Index(feed, "A shop"); first > strings.Index(feed, "Status page") {
t.Error("the feed is not newest first")
}
}
// TestGeminiNeverHasBrokenLinksOrEmptyFiles walks the whole capsule: no empty file, and every
// link that stays in the capsule leads to a file.
func TestGeminiNeverHasBrokenLinksOrEmptyFiles(t *testing.T) {
out, err := buildGem(t, blogGem(), nil)
if err != nil {
t.Fatal(err)
}
link := regexp.MustCompile(`(?m)^=> (\S+)`)
for _, f := range gemFiles(t, out) {
data, err := os.ReadFile(filepath.Join(out, filepath.FromSlash(f)))
if err != nil {
t.Fatal(err)
}
if len(strings.TrimSpace(string(data))) == 0 && f != ".meta" {
t.Errorf("%s is empty", f)
}
if !strings.HasSuffix(f, ".gmi") {
continue
}
for _, m := range link.FindAllStringSubmatch(string(data), -1) {
target := m[1]
if strings.Contains(target, ":") || !strings.HasPrefix(target, "/") {
if !strings.Contains(target, ":") {
t.Errorf("%s has the relative link %s", f, target)
}
continue
}
rel := strings.TrimPrefix(target, "/")
if rel == "" || strings.HasSuffix(rel, "/") {
rel += "index.gmi"
}
if !exists(t, out, rel) {
t.Errorf("%s links to %s, which is not in the capsule", f, target)
}
}
}
}
// TestGeminiLinksMustResolve checks that a link to nothing stops the build and names the page and the address.
func TestGeminiLinksMustResolve(t *testing.T) {
for name, extra := range map[string]map[string]string{
"page": {"content/en/people.md": page("people", "People", "", "See [nowhere](/nowhere.html).\n")},
"post": {"news/broken.md": "Title: Broken\nDate: 2026-11-01T12:00:00+0100\n-\nSee [nowhere](/nowhere.html).\n"},
"unsafe scheme": {"content/en/people.md": page("people", "People", "", "[x](javascript:alert(1))\n")},
} {
t.Run(name, func(t *testing.T) {
_, err := buildGem(t, gemSite(extra), nil)
if err == nil || !strings.Contains(err.Error(), "nowhere.html") && !strings.Contains(err.Error(), "javascript") {
t.Errorf("error = %v, want one that names the address", err)
}
})
}
}
// TestGeminiOnlyReferencedAssets checks that an asset is copied when a page uses it and not otherwise.
func TestGeminiOnlyReferencedAssets(t *testing.T) {
out, err := buildGem(t, gemSite(map[string]string{"content/en/services.md": page("services", "Services", "", "No image.\n")}), nil)
if err != nil {
t.Fatal(err)
}
if exists(t, out, "logo.svg") {
t.Error("an asset no page uses is copied")
}
}
// TestGeminiDraftsAndFuture checks that hidden posts stay out of the capsule, as in the site.
func TestGeminiDraftsAndFuture(t *testing.T) {
out, err := buildGem(t, gemSite(postNews()), nil)
if err != nil {
t.Fatal(err)
}
for _, f := range gemFiles(t, out) {
if strings.Contains(f, "a-draft") || strings.Contains(f, "in-future") || strings.Contains(f, "secret") {
t.Errorf("the capsule has %s, but the post is hidden", f)
}
}
with, err := buildGem(t, gemSite(postNews()), func(o *GeminiOptions) { o.Drafts = true })
if err != nil {
t.Fatal(err)
}
if !exists(t, with, "news/2026/11/a-draft.gmi") {
t.Error("a build with drafts lacks the draft")
}
}
// TestGeminiBlockMustStandAlone checks the rule of the generated blocks.
func TestGeminiBlockMustStandAlone(t *testing.T) {
_, err := buildGem(t, gemSite(map[string]string{"content/en/people.md": page("people", "People", "", "Text {{news_list}} text.\n")}), nil)
if err == nil || !strings.Contains(err.Error(), "news_list") {
t.Errorf("error = %v, want one about {{news_list}}", err)
}
}
// TestGeminiWebPaths checks that a path the website serves by other means can stay a link to the website.
func TestGeminiWebPaths(t *testing.T) {
files := gemSite(map[string]string{
"site.json": strings.Replace(baseFiles()["site.json"], `"languages"`, `"gemini_web_paths": "/~, /pastes/", "languages"`, 1),
"content/en/people.md": page("people", "People", "", "Pages of the [people](/~squid/) and the [pastes](/pastes/x).\n"),
})
out, err := buildGem(t, files, nil)
if err != nil {
t.Fatalf("BuildGemini() error = %v", err)
}
mustContain(t, "en/people.gmi", readOut(t, out, "en/people.gmi"),
"=> https://kmalloc.org/~squid/ people\n", "=> https://kmalloc.org/pastes/x pastes\n")
if _, err := buildGem(t, gemSite(map[string]string{"content/en/people.md": page("people", "People", "", "[x](/~squid/)\n")}), nil); err == nil {
t.Error("a path of the website is a link when the site did not say so")
}
}
// TestGeminiHeadings checks that a page has one first level heading and that code is left alone.
func TestGeminiHeadings(t *testing.T) {
out, err := buildGem(t, gemSite(map[string]string{
"content/en/people.md": page("people", "People", "", "# People\n\n# Other\n\n```\n# a comment in code\n```\n"),
}), nil)
if err != nil {
t.Fatal(err)
}
got := readOut(t, out, "en/people.gmi")
mustContain(t, "en/people.gmi", got, "# People\n\n## Other\n\n```\n# a comment in code\n```\n")
if strings.Count(got, "# People") != 1 {
t.Errorf("the title is repeated:\n%s", got)
}
home := readOut(t, out, "en/index.gmi")
mustContain(t, "en/index.gmi", home, "# kmalloc.org\n\n## Technology.\n")
}
// TestGeminiKeepsLinksToOtherProtocols checks the protocols that leave the capsule as they are.
func TestGeminiKeepsLinksToOtherProtocols(t *testing.T) {
out, err := buildGem(t, gemSite(map[string]string{"content/en/people.md": page("people", "People", "",
"[Mail](mailto:squid@kmalloc.org) [Pay](bitcoin:bc1qxyz) [Chat](xmpp:squid@kmalloc.org) [Capsule](gemini://example.org/)\n")}), nil)
if err != nil {
t.Fatal(err)
}
mustContain(t, "en/people.gmi", readOut(t, out, "en/people.gmi"),
"=> mailto:squid@kmalloc.org Mail\n", "=> bitcoin:bc1qxyz Pay\n", "=> xmpp:squid@kmalloc.org Chat\n", "=> gemini://example.org/ Capsule\n")
}
// TestGeminiTitleThatSaysTheHeading checks that a heading the title already says is not repeated, as in
// the real site where the page title is "kmalloc.org: Services" and the text starts with "# Services".
func TestGeminiTitleThatSaysTheHeading(t *testing.T) {
out, err := buildGem(t, gemSite(map[string]string{
"content/en/people.md": page("people", "kmalloc.org: People", "", "# People\n\nText.\n"),
}), nil)
if err != nil {
t.Fatal(err)
}
got := readOut(t, out, "en/people.gmi")
if !strings.HasPrefix(got, "# kmalloc.org: People\n\nText.\n") {
t.Errorf("en/people.gmi starts with\n%q", got)
}
}
// TestGeminiFeedTitleIsNotPrefixedTwice checks the feed title when the news page title has the prefix already.
func TestGeminiFeedTitleIsNotPrefixedTwice(t *testing.T) {
out, err := buildGem(t, gemSite(map[string]string{
"content/en/news/index.md": page("news", "kmalloc.org: News", "", "{{news_list}}\n"),
}), nil)
if err != nil {
t.Fatal(err)
}
if got := readOut(t, out, "news/feed.gmi"); !strings.HasPrefix(got, "# kmalloc.org: News\n") {
t.Errorf("news/feed.gmi starts with %q", got)
}
}
// TestGeminiTranslationNote checks that the note of a machine translated language reads as one sentence.
func TestGeminiTranslationNote(t *testing.T) {
files := gemSite(map[string]string{
"lang/es.json": strings.Replace(baseFiles()["lang/es.json"], `"translation_note": "Traduccion automatica."`, `"translation_note": "Traduccion automatica, avisa si ves un error:"`, 1),
})
out, err := buildGem(t, files, nil)
if err != nil {
t.Fatal(err)
}
mustContain(t, "es/manifesto.gmi", readOut(t, out, "es/manifesto.gmi"),
"Traduccion automatica, avisa si ves un error: avisar.\n=> https://git.kmalloc.org/squid/kmalloc.org/issues avisar\n")
}
// TestGeminiMetaForPostsInAnotherLanguage checks that a post with its own Language has a line of its own in
// .meta, with the real path of its file and before the lines of the folders: the server uses the first line that
// matches a file, and the line of the news folder would hide a line that comes after it.
func TestGeminiMetaForPostsInAnotherLanguage(t *testing.T) {
files := gemSite(map[string]string{
"news/ciao.md": "Title: Ciao\nDate: 2026-11-05T09:00:00+0100\nLanguage: it\n-\nUn testo.\n",
"news/hola.md": "Title: Hola\nDate: 2026-11-04T09:00:00+0100\nLanguage: es\n-\nUn texto.\n",
"news/same.md": "Title: Same\nDate: 2026-11-03T09:00:00+0100\nLanguage: en\n-\nText.\n",
})
out, err := buildGem(t, files, nil)
if err != nil {
t.Fatalf("BuildGemini() error = %v", err)
}
want := "news/2026/11/ciao.gmi:;lang=it\nnews/2026/11/hola.gmi:;lang=es\n" +
"en/*:;lang=en\nen/**/*:;lang=en\nes/*:;lang=es\nes/**/*:;lang=es\nnews/*:;lang=en\nnews/**/*:;lang=en\n"
if got := readOut(t, out, ".meta"); got != want {
t.Errorf(".meta =\n%q\nwant\n%q", got, want)
}
for _, rel := range []string{"news/2026/11/ciao.gmi", "news/2026/11/hola.gmi"} {
if !exists(t, out, rel) {
t.Errorf("the .meta names %s, which is not in the capsule", rel)
}
}
// With English at the root and the news in English too, only the posts in other languages have a line.
plain, err := buildGem(t, files, func(o *GeminiOptions) { o.Language = "en" })
if err != nil {
t.Fatalf("BuildGemini() error = %v", err)
}
want = "news/2026/11/ciao.gmi:;lang=it\nnews/2026/11/hola.gmi:;lang=es\n" +
"it/*:;lang=it\nit/**/*:;lang=it\nes/*:;lang=es\nes/**/*:;lang=es\n"
if got := readOut(t, plain, ".meta"); got != want {
t.Errorf(".meta with English at the root =\n%q\nwant\n%q", got, want)
}
}
// TestGeminiNoNewsAtAll checks that a site without posts says so once, in the news list, and that the empty
// lists leave their headings out.
func TestGeminiNoNewsAtAll(t *testing.T) {
index := "# News\n\n{{news_list}}\n\n## Categories\n\n{{categories_list}}\n\n## Archive\n\n{{news_archive}}\n"
out, err := buildGem(t, gemSite(map[string]string{
"content/en/news/index.md": page("news", "News", "Layout: bare\n", index),
"news/shop.md": "", "news/gnu-linux-day-2026.md": "", "news/status.md": "",
}), nil)
if err != nil {
t.Fatalf("BuildGemini() error = %v", err)
}
got := readOut(t, out, "news/index.gmi")
if n := strings.Count(got, "No news yet."); n != 1 {
t.Errorf("the index says there is no news %d times, want once:\n%s", n, got)
}
for _, heading := range []string{"## Categories", "## Archive"} {
if strings.Contains(got, heading) {
t.Errorf("the index keeps the heading %q of an empty list:\n%s", heading, got)
}
}
mustContain(t, "news/index.gmi", got, "# News\n")
mustContain(t, "news/feed.gmi", readOut(t, out, "news/feed.gmi"), "# kmalloc.org: News\n")
home := readOut(t, out, "index.gmi")
if strings.Contains(home, "Tutte le news") {
t.Errorf("the home links to the news list of a site with no news:\n%s", home)
}
mustContain(t, "index.gmi", home, "# kmalloc.org\n")
}
// TestGeminiOnlyOnePost checks that a single post has no link to a newer or an older one, but has the one to all the news.
func TestGeminiOnlyOnePost(t *testing.T) {
out, err := buildGem(t, gemSite(map[string]string{"news/gnu-linux-day-2026.md": "", "news/status.md": ""}), nil)
if err != nil {
t.Fatalf("BuildGemini() error = %v", err)
}
post := readOut(t, out, "news/2026/11/shop.gmi")
for _, absent := range []string{"Older", "Newer"} {
if strings.Contains(post, absent) {
t.Errorf("the only post has %q:\n%s", absent, post)
}
}
mustContain(t, "the post", post, "=> /news/ All news\n")
}
// TestGeminiUnusualTitle checks that a title with quotes, an ampersand, a tag and non Latin letters is written as it is.
func TestGeminiUnusualTitle(t *testing.T) {
title := `Say "hi" & <b>go</b> ünï 日本語`
out, err := buildGem(t, gemSite(map[string]string{
"news/odd.md": "Title: " + title + "\nDate: 2026-11-15T09:00:00+0100\nCategories: Odd\n-\nText.\n",
}), nil)
if err != nil {
t.Fatalf("BuildGemini() error = %v", err)
}
mustContain(t, "the post", readOut(t, out, "news/2026/11/odd.gmi"), "# "+title+"\n")
mustContain(t, "news/feed.gmi", readOut(t, out, "news/feed.gmi"), "=> /news/2026/11/odd.gmi 2026-11-15 "+title+"\n")
mustContain(t, "news/index.gmi", readOut(t, out, "news/index.gmi"), "=> /news/2026/11/odd.gmi 2026-11-15 "+title+"\n")
mustContain(t, "the post below", readOut(t, out, "news/2026/11/shop.gmi"), "Newer: "+title)
for _, rel := range []string{"news/2026/11/odd.gmi", "news/index.gmi", "news/category/odd.gmi"} {
if strings.Contains(readOut(t, out, rel), "&lt;") || strings.Contains(readOut(t, out, rel), "&amp;") {
t.Errorf("%s has an HTML escape in the title", rel)
}
}
}
// TestGeminiLeavesTheLogoOut checks that the logo of the home page leaves no link line and no copied file.
func TestGeminiLeavesTheLogoOut(t *testing.T) {
out, err := buildGem(t, gemSite(map[string]string{
"content/en/services.md": page("services", "Services", "", "No image.\n"),
"content/en/index.md": page("home", "kmalloc.org", "Layout: bare\n", "<img class=\"logo\" src=\"/logo.svg\" alt=\"kmalloc\" width=\"112\">\n\n<h1 class=\"hero\">Technology.</h1>\n\n{{latest_news}}\n"),
}), nil)
if err != nil {
t.Fatalf("BuildGemini() error = %v", err)
}
home := readOut(t, out, "en/index.gmi")
if strings.Contains(home, "logo.svg") {
t.Errorf("the home links to the logo:\n%s", home)
}
if !strings.HasPrefix(home, "# kmalloc.org\n\n## Technology.\n") {
t.Errorf("the home starts with %q", home)
}
if exists(t, out, "logo.svg") {
t.Error("the logo is copied, and nothing points to it")
}
}

View file

@ -24,12 +24,13 @@ type renderedPost struct {
} }
// newsItem returns the <li> of a post for a list: the date, the title as a // newsItem returns the <li> of a post for a list: the date, the title as a
// link and, when withDescription is set and there is one, the description. News // link and, when withDescription is set and there is one, the description. A
// are written in English, so on any other language the link says so. // post is written in its own language, so on a page in another language the
// link says which one.
func newsItem(p content.Post, lang string, withDescription bool) string { func newsItem(p content.Post, lang string, withDescription bool) string {
mark := "" mark := ""
if lang != "en" { if p.Language != "" && p.Language != lang {
mark = ` hreflang="en" lang="en"` mark = fmt.Sprintf(` hreflang="%s" lang="%s"`, p.Language, p.Language)
} }
day := p.Date.Format(dateOnly) day := p.Date.Format(dateOnly)
item := fmt.Sprintf(`<li><time datetime="%s">%s</time> <a href="%s"%s>%s</a>`, item := fmt.Sprintf(`<li><time datetime="%s">%s</time> <a href="%s"%s>%s</a>`,
@ -40,6 +41,64 @@ func newsItem(p content.Post, lang string, withDescription bool) string {
return item + "</li>" return item + "</li>"
} }
// newsPanel returns a post for a list of the news as a note panel: first a small
// line with the date and the categories, then the title in bold with a full stop
// (unless it ends with one, a question mark or an exclamation mark), then the
// description, then the link to read more, which names the post for a reader that
// hears the links out of context. lang is the language of the page; the link of a
// post in another language says so. Without the string read_more the title is the
// link and there is no link to read more, unless needMore is set: the blog list has
// no other link to the post, so there it is an error.
func (b *builder) newsPanel(p content.Post, lang string, needMore bool) (string, error) {
l := b.langs[lang]
more := l.Strings["read_more"]
if more == "" && needMore {
return "", fmt.Errorf("lang/%s.json: the language has no read_more string, which the blog list needs", lang)
}
mark := ""
if p.Language != "" && p.Language != lang {
mark = fmt.Sprintf(` hreflang="%s" lang="%s"`, p.Language, p.Language)
}
meta := fmt.Sprintf(`<time datetime="%s">%s</time>`, p.Date.Format(dateOnly), render.Escape(longDate(p.Date, l)))
if links := categoryLinks(p); links != "" {
meta += " &middot; " + links
}
title := p.Title
if !strings.HasSuffix(title, ".") && !strings.HasSuffix(title, "?") && !strings.HasSuffix(title, "!") {
title += "."
}
var out strings.Builder
out.WriteString("<p class=\"note\" role=\"note\">\n")
fmt.Fprintf(&out, "<span class=\"meta\">%s</span><br>\n", meta)
if more == "" {
fmt.Fprintf(&out, "<strong><a href=\"%s\"%s>%s</a></strong>\n", p.Path, mark, render.Escape(title))
} else {
fmt.Fprintf(&out, "<strong>%s</strong>\n", render.Escape(title))
}
if p.Description != "" {
fmt.Fprintf(&out, "%s\n", render.Escape(p.Description))
}
if more != "" {
fmt.Fprintf(&out, "<a href=\"%s\" aria-label=\"%s: %s\"%s>%s</a>.\n", p.Path, render.EscapeAttr(more), render.EscapeAttr(p.Title), mark, render.Escape(more))
}
out.WriteString("</p>")
return out.String(), nil
}
// newsPanels returns the panels of the posts of a category or of an archive,
// one after the other, or an empty string when there are none.
func (b *builder) newsPanels(posts []content.Post, lang string) (string, error) {
panels := make([]string, len(posts))
for i, p := range posts {
panel, err := b.newsPanel(p, lang, false)
if err != nil {
return "", err
}
panels[i] = panel
}
return strings.Join(panels, "\n"), nil
}
// newsList returns the full list of the news as a <ul>, or an empty string // newsList returns the full list of the news as a <ul>, or an empty string
// when there are no posts. // when there are no posts.
func newsList(posts []content.Post, lang string) string { func newsList(posts []content.Post, lang string) string {

View file

@ -23,7 +23,7 @@ func testPost(slug, day, title, description string, cats ...string) content.Post
} }
return content.Post{ return content.Post{
Slug: slug, Path: content.PostPath(date, slug), Title: title, Description: description, Slug: slug, Path: content.PostPath(date, slug), Title: title, Description: description,
Date: date, Categories: categories, Source: "news/" + slug + ".md", Language: "en", Date: date, Categories: categories, Source: "news/" + slug + ".md",
} }
} }

Some files were not shown because too many files have changed in this diff Show more