feat: load pages and news with dated paths and categories
This commit is contained in:
parent
97af0c422d
commit
51071e6fb2
7 changed files with 807 additions and 0 deletions
49
internal/content/category.go
Normal file
49
internal/content/category.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// Package content loads the pages and the news of a site from Markdown files
|
||||
// and works out their addresses.
|
||||
package content
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Category is a news category: the name as written and the name used in its
|
||||
// address.
|
||||
type Category struct {
|
||||
// Name is the category as written in the header of a news file.
|
||||
Name string
|
||||
// Slug is the name in the address, such as "linux-day".
|
||||
Slug string
|
||||
}
|
||||
|
||||
// accents maps the accented letters of the Latin alphabets to ASCII, so a
|
||||
// category address stays readable without an extra dependency.
|
||||
var accents = strings.NewReplacer(
|
||||
"à", "a", "á", "a", "â", "a", "ã", "a", "ä", "a", "å", "a", "ā", "a", "ă", "a", "ą", "a",
|
||||
"æ", "ae", "ç", "c", "ć", "c", "č", "c", "ď", "d", "đ", "d",
|
||||
"è", "e", "é", "e", "ê", "e", "ë", "e", "ē", "e", "ė", "e", "ę", "e", "ě", "e",
|
||||
"ğ", "g", "ì", "i", "í", "i", "î", "i", "ï", "i", "ī", "i", "į", "i", "ı", "i",
|
||||
"ł", "l", "ñ", "n", "ń", "n", "ň", "n",
|
||||
"ò", "o", "ó", "o", "ô", "o", "õ", "o", "ö", "o", "ø", "o", "ō", "o", "ő", "o", "œ", "oe",
|
||||
"ř", "r", "ś", "s", "š", "s", "ş", "s", "ß", "ss", "ť", "t", "ț", "t",
|
||||
"ù", "u", "ú", "u", "û", "u", "ü", "u", "ū", "u", "ů", "u", "ű", "u",
|
||||
"ý", "y", "ÿ", "y", "ź", "z", "ž", "z", "ż", "z",
|
||||
)
|
||||
|
||||
// nonSlug matches a run of characters that cannot be part of an address.
|
||||
var nonSlug = regexp.MustCompile(`[^a-z0-9]+`)
|
||||
|
||||
// CategorySlug turns a category name into the name used in its address:
|
||||
// lowercase, without accents, with hyphens between words ("Linux Day" becomes
|
||||
// "linux-day"). A name without any letter or digit is an error.
|
||||
func CategorySlug(name string) (string, error) {
|
||||
slug := nonSlug.ReplaceAllString(accents.Replace(strings.ToLower(name)), "-")
|
||||
slug = strings.Trim(slug, "-")
|
||||
if slug == "" {
|
||||
return "", fmt.Errorf("category %q has no letters or digits", name)
|
||||
}
|
||||
return slug, nil
|
||||
}
|
||||
35
internal/content/category_test.go
Normal file
35
internal/content/category_test.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package content
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestCategorySlug checks the name that goes into a category address.
|
||||
func TestCategorySlug(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{"Linux Day", "linux-day", false},
|
||||
{"FLUG", "flug", false},
|
||||
{"Università", "universita", false},
|
||||
{"Crème brûlée", "creme-brulee", false},
|
||||
{"C++", "c", false},
|
||||
{" Spaced out ", "spaced-out", false},
|
||||
{"Straße", "strasse", false},
|
||||
{"", "", true},
|
||||
{"+++", "", true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := CategorySlug(tt.name)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Fatalf("CategorySlug(%q) error = %v, wantErr %v", tt.name, err, tt.wantErr)
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("CategorySlug(%q) = %q, want %q", tt.name, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
26
internal/content/helpers_test.go
Normal file
26
internal/content/helpers_test.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package content
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// writeFiles writes the files (path relative to a new directory, then content)
|
||||
// and returns the directory.
|
||||
func writeFiles(t *testing.T, files map[string]string) string {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
for name, content := range files {
|
||||
path := filepath.Join(dir, filepath.FromSlash(name))
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return dir
|
||||
}
|
||||
178
internal/content/pages.go
Normal file
178
internal/content/pages.go
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package content
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.kmalloc.org/squid/motorino/internal/header"
|
||||
"git.kmalloc.org/squid/motorino/internal/mdx"
|
||||
)
|
||||
|
||||
// Page is a page of the site in one language.
|
||||
type Page struct {
|
||||
// Lang is the language code, such as "en".
|
||||
Lang string
|
||||
// ID names the page; the translations of a page share it.
|
||||
ID string
|
||||
// Path is the address of the page, such as "/services.html".
|
||||
Path string
|
||||
// Title, Description and Note come from the header.
|
||||
Title, Description, Note string
|
||||
// Layout is "page", "bare" or "manifesto".
|
||||
Layout string
|
||||
// Switch overrides the address of the language links, by language code.
|
||||
Switch map[string]string
|
||||
// Source is the file the page was read from, relative to the site.
|
||||
Source string
|
||||
// Body is the Markdown after the header.
|
||||
Body string
|
||||
}
|
||||
|
||||
// pageKeys are the keys a page header may have.
|
||||
var pageKeys = []string{"Title", "ID", "Description", "Layout", "Note", "Switch"}
|
||||
|
||||
// layouts are the values of the Layout key; the first is the default.
|
||||
var layouts = []string{"page", "bare", "manifesto"}
|
||||
|
||||
// PagePath returns the address of the page file rel, given with slashes and
|
||||
// relative to the folder of its language. An index.md is the address of its
|
||||
// folder; the default language lives at the root and the others under
|
||||
// /<lang>/.
|
||||
func PagePath(defaultLang, lang, rel string) string {
|
||||
rel = strings.TrimSuffix(rel, ".md")
|
||||
var p string
|
||||
switch {
|
||||
case rel == "index":
|
||||
p = "/"
|
||||
case strings.HasSuffix(rel, "/index"):
|
||||
p = "/" + strings.TrimSuffix(rel, "index")
|
||||
default:
|
||||
p = "/" + rel + ".html"
|
||||
}
|
||||
if lang != defaultLang {
|
||||
p = "/" + lang + p
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// LoadPages reads every Markdown file under content/<lang>/ for each language.
|
||||
// The first language is the default. The em dash rule applies to every page
|
||||
// except those with the manifesto layout, which is rewritten when it is built.
|
||||
func LoadPages(dir string, languages []string) ([]Page, error) {
|
||||
if len(languages) == 0 {
|
||||
return nil, fmt.Errorf("no languages")
|
||||
}
|
||||
var pages []Page
|
||||
for _, lang := range languages {
|
||||
folder := path.Join("content", lang)
|
||||
if info, err := os.Stat(filepath.Join(dir, filepath.FromSlash(folder))); err != nil || !info.IsDir() {
|
||||
return nil, fmt.Errorf("%s: the folder of language %q is missing", folder, lang)
|
||||
}
|
||||
found, err := loadLanguagePages(dir, languages[0], lang, folder)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pages = append(pages, found...)
|
||||
}
|
||||
return pages, nil
|
||||
}
|
||||
|
||||
// loadLanguagePages reads the pages of one language and rejects duplicate IDs.
|
||||
func loadLanguagePages(dir, defaultLang, lang, folder string) ([]Page, error) {
|
||||
var pages []Page
|
||||
seen := map[string]string{}
|
||||
root := filepath.Join(dir, filepath.FromSlash(folder))
|
||||
err := filepath.WalkDir(root, func(file string, d fs.DirEntry, err error) error {
|
||||
if err != nil || d.IsDir() || !strings.HasSuffix(file, ".md") {
|
||||
return err
|
||||
}
|
||||
rel, err := filepath.Rel(root, file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rel = filepath.ToSlash(rel)
|
||||
page, err := loadPage(file, path.Join(folder, rel), defaultLang, lang, rel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if other, dup := seen[page.ID]; dup {
|
||||
return fmt.Errorf("%s: the ID %q is also used by %s", page.Source, page.ID, other)
|
||||
}
|
||||
seen[page.ID] = page.Source
|
||||
pages = append(pages, page)
|
||||
return nil
|
||||
})
|
||||
return pages, err
|
||||
}
|
||||
|
||||
// loadPage reads and checks one page file.
|
||||
func loadPage(file, source, defaultLang, lang, rel string) (Page, error) {
|
||||
data, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return Page{}, fmt.Errorf("read %s: %w", source, err)
|
||||
}
|
||||
h, body, err := header.Parse(source, data)
|
||||
if err != nil {
|
||||
return Page{}, err
|
||||
}
|
||||
if err := h.Only(source, pageKeys...); err != nil {
|
||||
return Page{}, err
|
||||
}
|
||||
if err := h.Require(source, "Title", "ID", "Description"); err != nil {
|
||||
return Page{}, err
|
||||
}
|
||||
layout := h.Get("Layout")
|
||||
if layout == "" {
|
||||
layout = layouts[0]
|
||||
}
|
||||
if !contains(layouts, layout) {
|
||||
return Page{}, fmt.Errorf("%s: unknown Layout %q (use %s)", source, layout, strings.Join(layouts, ", "))
|
||||
}
|
||||
if layout != "manifesto" {
|
||||
if err := mdx.RejectEmDash(source, string(data)); err != nil {
|
||||
return Page{}, err
|
||||
}
|
||||
}
|
||||
switches, err := parseSwitch(source, h.Get("Switch"))
|
||||
if err != nil {
|
||||
return Page{}, err
|
||||
}
|
||||
return Page{
|
||||
Lang: lang, ID: h.Get("ID"), Path: PagePath(defaultLang, lang, rel),
|
||||
Title: h.Get("Title"), Description: h.Get("Description"), Note: h.Get("Note"),
|
||||
Layout: layout, Switch: switches, Source: source, Body: body,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// parseSwitch reads "en=/path, de=/path" into a map; an empty value is an
|
||||
// empty map.
|
||||
func parseSwitch(source, value string) (map[string]string, error) {
|
||||
out := map[string]string{}
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return out, nil
|
||||
}
|
||||
for _, entry := range strings.Split(value, ",") {
|
||||
lang, target, ok := strings.Cut(strings.TrimSpace(entry), "=")
|
||||
if !ok || lang == "" || target == "" {
|
||||
return nil, fmt.Errorf("%s: Switch entry %q is not of the form lang=/path", source, strings.TrimSpace(entry))
|
||||
}
|
||||
out[strings.TrimSpace(lang)] = strings.TrimSpace(target)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// contains reports whether list has s.
|
||||
func contains(list []string, s string) bool {
|
||||
for _, item := range list {
|
||||
if item == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
136
internal/content/pages_test.go
Normal file
136
internal/content/pages_test.go
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package content
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestPagePath checks how a page file becomes an address.
|
||||
func TestPagePath(t *testing.T) {
|
||||
tests := []struct {
|
||||
lang, rel, want string
|
||||
}{
|
||||
{"en", "index.md", "/"},
|
||||
{"en", "services.md", "/services.html"},
|
||||
{"en", "news/index.md", "/news/"},
|
||||
{"it", "index.md", "/it/"},
|
||||
{"it", "servizi.md", "/it/servizi.html"},
|
||||
{"it", "news/index.md", "/it/news/"},
|
||||
{"es", "manifesto.md", "/es/manifesto.html"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.lang+"/"+tt.rel, func(t *testing.T) {
|
||||
if got := PagePath("en", tt.lang, tt.rel); got != tt.want {
|
||||
t.Errorf("PagePath(en, %s, %s) = %q, want %q", tt.lang, tt.rel, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// pageFile is a valid page with the given extra header lines and body.
|
||||
func pageFile(extra, body string) string {
|
||||
return "Title: Services\nID: services\nDescription: What we run.\n" + extra + "-\n" + body
|
||||
}
|
||||
|
||||
// TestLoadPages checks the fields of a loaded page.
|
||||
func TestLoadPages(t *testing.T) {
|
||||
dir := writeFiles(t, map[string]string{
|
||||
"content/en/services.md": pageFile("Switch: it=/it/servizi.html, de=/de/\n", "Body.\n"),
|
||||
"content/en/news/index.md": "Title: News\nID: news\nDescription: The news.\n-\nIntro.\n",
|
||||
"content/it/servizi.md": "Title: Servizi\nID: services\nDescription: Cosa offriamo.\nLayout: bare\n-\nCorpo.\n",
|
||||
"content/en/notes/ignored.txt": "not markdown",
|
||||
})
|
||||
pages, err := LoadPages(dir, []string{"en", "it"})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadPages() error = %v", err)
|
||||
}
|
||||
if len(pages) != 3 {
|
||||
t.Fatalf("got %d pages, want 3: %+v", len(pages), pages)
|
||||
}
|
||||
byPath := map[string]Page{}
|
||||
for _, p := range pages {
|
||||
byPath[p.Lang+" "+p.Path] = p
|
||||
}
|
||||
s := byPath["en /services.html"]
|
||||
if s.ID != "services" || s.Title != "Services" || s.Description != "What we run." ||
|
||||
s.Layout != "page" || s.Body != "Body.\n" || s.Source != "content/en/services.md" {
|
||||
t.Errorf("en services = %+v", s)
|
||||
}
|
||||
if s.Switch["it"] != "/it/servizi.html" || s.Switch["de"] != "/de/" {
|
||||
t.Errorf("Switch = %v", s.Switch)
|
||||
}
|
||||
if it := byPath["it /it/servizi.html"]; it.Layout != "bare" || it.Lang != "it" {
|
||||
t.Errorf("it services = %+v", it)
|
||||
}
|
||||
if n := byPath["en /news/"]; n.ID != "news" {
|
||||
t.Errorf("news page = %+v", n)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadPagesRejectsEmDash checks that an em dash in a page is an error.
|
||||
func TestLoadPagesRejectsEmDash(t *testing.T) {
|
||||
dir := writeFiles(t, map[string]string{"content/en/x.md": pageFile("", "Words — more words.\n")})
|
||||
_, err := LoadPages(dir, []string{"en"})
|
||||
if err == nil || !strings.Contains(err.Error(), "content/en/x.md") {
|
||||
t.Errorf("LoadPages() error = %v, want it to name content/en/x.md", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadPagesManifestoKeepsEmDash checks that the manifesto layout is exempt:
|
||||
// its text is the owner's and is rewritten when the page is built.
|
||||
func TestLoadPagesManifestoKeepsEmDash(t *testing.T) {
|
||||
dir := writeFiles(t, map[string]string{
|
||||
"content/en/manifesto.md": "Title: Manifesto\nID: manifesto\nDescription: What we stand for.\nLayout: manifesto\n-\n## 01 — Antifascist\n",
|
||||
})
|
||||
pages, err := LoadPages(dir, []string{"en"})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadPages() error = %v", err)
|
||||
}
|
||||
if len(pages) != 1 || !strings.Contains(pages[0].Body, "—") {
|
||||
t.Errorf("pages = %+v, want one page that keeps its em dash", pages)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadPagesDuplicateID checks that two pages cannot share an ID in a language.
|
||||
func TestLoadPagesDuplicateID(t *testing.T) {
|
||||
dir := writeFiles(t, map[string]string{
|
||||
"content/en/a.md": pageFile("", "A.\n"),
|
||||
"content/en/b.md": pageFile("", "B.\n"),
|
||||
})
|
||||
_, err := LoadPages(dir, []string{"en"})
|
||||
if err == nil || !strings.Contains(err.Error(), "services") {
|
||||
t.Errorf("LoadPages() error = %v, want it to name the duplicate ID", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadPagesHeaderErrors checks the header rules of pages.
|
||||
func TestLoadPagesHeaderErrors(t *testing.T) {
|
||||
tests := []struct {
|
||||
name, file, want string
|
||||
}{
|
||||
{"unknown key", "Title: T\nID: x\nDescription: d\nTitolo: T\n-\nB\n", "Titolo"},
|
||||
{"missing id", "Title: T\nDescription: d\n-\nB\n", "ID"},
|
||||
{"bad layout", "Title: T\nID: x\nDescription: d\nLayout: fancy\n-\nB\n", "fancy"},
|
||||
{"bad switch", "Title: T\nID: x\nDescription: d\nSwitch: nonsense\n-\nB\n", "Switch"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
dir := writeFiles(t, map[string]string{"content/en/x.md": tt.file})
|
||||
_, err := LoadPages(dir, []string{"en"})
|
||||
if err == nil || !strings.Contains(err.Error(), tt.want) || !strings.Contains(err.Error(), "content/en/x.md") {
|
||||
t.Errorf("LoadPages() error = %v, want it to name content/en/x.md and %s", err, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadPagesMissingLanguageFolder checks that a listed language needs its folder.
|
||||
func TestLoadPagesMissingLanguageFolder(t *testing.T) {
|
||||
dir := writeFiles(t, map[string]string{"content/en/x.md": pageFile("", "B.\n")})
|
||||
_, err := LoadPages(dir, []string{"en", "it"})
|
||||
if err == nil || !strings.Contains(err.Error(), "content/it") {
|
||||
t.Errorf("LoadPages() error = %v, want it to name content/it", err)
|
||||
}
|
||||
}
|
||||
203
internal/content/posts.go
Normal file
203
internal/content/posts.go
Normal file
|
|
@ -0,0 +1,203 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package content
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.kmalloc.org/squid/motorino/internal/header"
|
||||
"git.kmalloc.org/squid/motorino/internal/mdx"
|
||||
)
|
||||
|
||||
// Post is a news item.
|
||||
type Post struct {
|
||||
// Slug is the name of the file without its extension.
|
||||
Slug string
|
||||
// Path is the address, /news/YYYY/MM/<slug>.html.
|
||||
Path string
|
||||
// Title, Author and Description come from the header; the description
|
||||
// falls back to the first sentence of the body.
|
||||
Title, Author, Description string
|
||||
// Date is the date as written, with its own offset.
|
||||
Date time.Time
|
||||
// Categories are the categories of the post, in the order written.
|
||||
Categories []Category
|
||||
// Source is the file the post was read from, relative to the site.
|
||||
Source string
|
||||
// Body is the Markdown after the header.
|
||||
Body string
|
||||
}
|
||||
|
||||
// postKeys are the keys a news header may have.
|
||||
var postKeys = []string{"Title", "Author", "Date", "Categories", "Description"}
|
||||
|
||||
// dateLayouts are the accepted spellings of Date, ISO 8601 with an offset.
|
||||
var dateLayouts = []string{"2006-01-02T15:04:05-0700", "2006-01-02T15:04:05Z07:00"}
|
||||
|
||||
// slugName matches a file name made of lowercase words joined by hyphens.
|
||||
var slugName = regexp.MustCompile(`^[a-z0-9]+(?:-[a-z0-9]+)*$`)
|
||||
|
||||
// PostPath returns the address of a news item: the year and the month are
|
||||
// those of date as written, not converted to another time zone.
|
||||
func PostPath(date time.Time, slug string) string {
|
||||
return "/news/" + date.Format("2006/01") + "/" + slug + ".html"
|
||||
}
|
||||
|
||||
// LoadPosts reads news/*.md, newest first. A site without a news folder has
|
||||
// no posts. Two posts with the same address, or two category names with the
|
||||
// same address, are errors.
|
||||
func LoadPosts(dir string) ([]Post, error) {
|
||||
files, err := filepath.Glob(filepath.Join(dir, "news", "*.md"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list news: %w", err)
|
||||
}
|
||||
var posts []Post
|
||||
for _, file := range files {
|
||||
post, err := loadPost(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
posts = append(posts, post)
|
||||
}
|
||||
sort.SliceStable(posts, func(i, j int) bool {
|
||||
if !posts[i].Date.Equal(posts[j].Date) {
|
||||
return posts[i].Date.After(posts[j].Date)
|
||||
}
|
||||
return posts[i].Slug < posts[j].Slug
|
||||
})
|
||||
if err := checkPaths(posts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := checkCategories(posts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return posts, nil
|
||||
}
|
||||
|
||||
// loadPost reads and checks one news file.
|
||||
func loadPost(file string) (Post, error) {
|
||||
base := filepath.Base(file)
|
||||
source := "news/" + base
|
||||
slug := strings.TrimSuffix(base, ".md")
|
||||
if !slugName.MatchString(slug) {
|
||||
return Post{}, fmt.Errorf("%s: the name must be lowercase words joined by hyphens", source)
|
||||
}
|
||||
data, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return Post{}, fmt.Errorf("read %s: %w", source, err)
|
||||
}
|
||||
h, body, err := header.Parse(source, data)
|
||||
if err != nil {
|
||||
return Post{}, err
|
||||
}
|
||||
if err := h.Only(source, postKeys...); err != nil {
|
||||
return Post{}, err
|
||||
}
|
||||
if err := h.Require(source, "Title", "Date"); err != nil {
|
||||
return Post{}, err
|
||||
}
|
||||
if err := mdx.RejectEmDash(source, string(data)); err != nil {
|
||||
return Post{}, err
|
||||
}
|
||||
date, err := parseDate(source, h.Get("Date"))
|
||||
if err != nil {
|
||||
return Post{}, err
|
||||
}
|
||||
categories, err := parseCategories(source, h.Get("Categories"))
|
||||
if err != nil {
|
||||
return Post{}, err
|
||||
}
|
||||
description := h.Get("Description")
|
||||
if description == "" {
|
||||
description = firstSentence(body)
|
||||
}
|
||||
return Post{
|
||||
Slug: slug, Path: PostPath(date, slug), Title: h.Get("Title"), Author: h.Get("Author"),
|
||||
Description: description, Date: date, Categories: categories, Source: source, Body: body,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// parseDate reads Date in any of the accepted layouts.
|
||||
func parseDate(source, value string) (time.Time, error) {
|
||||
for _, layout := range dateLayouts {
|
||||
if date, err := time.Parse(layout, value); err == nil {
|
||||
return date, nil
|
||||
}
|
||||
}
|
||||
return time.Time{}, fmt.Errorf("%s: Date %q is not ISO 8601 with an offset, such as 2026-09-18T17:00:45+0200", source, value)
|
||||
}
|
||||
|
||||
// parseCategories splits a comma separated list into categories.
|
||||
func parseCategories(source, value string) ([]Category, error) {
|
||||
var out []Category
|
||||
for _, name := range strings.Split(value, ",") {
|
||||
name = strings.TrimSpace(name)
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
slug, err := CategorySlug(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", source, err)
|
||||
}
|
||||
out = append(out, Category{Name: name, Slug: slug})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// checkPaths returns an error when two posts have the same address.
|
||||
func checkPaths(posts []Post) error {
|
||||
seen := map[string]string{}
|
||||
for _, p := range posts {
|
||||
if other, dup := seen[p.Path]; dup {
|
||||
return fmt.Errorf("%s and %s have the same address %s", other, p.Source, p.Path)
|
||||
}
|
||||
seen[p.Path] = p.Source
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkCategories returns an error when two different category names give the
|
||||
// same address.
|
||||
func checkCategories(posts []Post) error {
|
||||
names := map[string]string{}
|
||||
for _, p := range posts {
|
||||
for _, c := range p.Categories {
|
||||
if other, ok := names[c.Slug]; ok && other != c.Name {
|
||||
return fmt.Errorf("%s: the categories %q and %q give the same address /news/category/%s.html", p.Source, other, c.Name, c.Slug)
|
||||
}
|
||||
names[c.Slug] = c.Name
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// markup matches the Markdown that is dropped from a fallback description.
|
||||
var (
|
||||
markdownLink = regexp.MustCompile(`\[([^\]]*)\]\([^)]*\)`)
|
||||
markdownMark = regexp.MustCompile("[*_`]")
|
||||
sentenceEnd = regexp.MustCompile(`^(.+?[.!?])(?:\s|$)`)
|
||||
)
|
||||
|
||||
// firstSentence returns the first sentence of the first paragraph of body that
|
||||
// is not a heading or raw HTML, without Markdown markup.
|
||||
func firstSentence(body string) string {
|
||||
for _, block := range strings.Split(body, "\n\n") {
|
||||
block = strings.TrimSpace(block)
|
||||
if block == "" || strings.HasPrefix(block, "#") || strings.HasPrefix(block, "<") {
|
||||
continue
|
||||
}
|
||||
text := markdownLink.ReplaceAllString(strings.Join(strings.Fields(block), " "), "$1")
|
||||
text = markdownMark.ReplaceAllString(text, "")
|
||||
if m := sentenceEnd.FindStringSubmatch(text); m != nil {
|
||||
return m[1]
|
||||
}
|
||||
return text
|
||||
}
|
||||
return ""
|
||||
}
|
||||
180
internal/content/posts_test.go
Normal file
180
internal/content/posts_test.go
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package content
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestPostPath checks that the month comes from the date as written.
|
||||
func TestPostPath(t *testing.T) {
|
||||
tests := []struct {
|
||||
date, slug, want string
|
||||
}{
|
||||
{"2026-11-14T12:00:00+0100", "shop", "/news/2026/11/shop.html"},
|
||||
{"2026-10-31T23:30:00-0500", "x", "/news/2026/10/x.html"},
|
||||
{"2026-09-18T17:00:45+0200", "gnu-linux-day-2026", "/news/2026/09/gnu-linux-day-2026.html"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.date, func(t *testing.T) {
|
||||
date, err := time.Parse("2006-01-02T15:04:05-0700", tt.date)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := PostPath(date, tt.slug); got != tt.want {
|
||||
t.Errorf("PostPath() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ownerSample is the example header the owner gave for the news.
|
||||
const ownerSample = "Title: GNU/Linux Day 2026\nAuthor: Matteo Bini\nDate: 2026-09-18T17:00:45+0200\n" +
|
||||
"Categories: Linux Day, FLUG\n-\nThe programme is out. See you there.\n"
|
||||
|
||||
// TestLoadPostsOwnerSample checks every field of the owner's example.
|
||||
func TestLoadPostsOwnerSample(t *testing.T) {
|
||||
dir := writeFiles(t, map[string]string{"news/gnu-linux-day-2026.md": ownerSample})
|
||||
posts, err := LoadPosts(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadPosts() error = %v", err)
|
||||
}
|
||||
if len(posts) != 1 {
|
||||
t.Fatalf("got %d posts, want 1", len(posts))
|
||||
}
|
||||
p := posts[0]
|
||||
if p.Title != "GNU/Linux Day 2026" || p.Author != "Matteo Bini" || p.Slug != "gnu-linux-day-2026" ||
|
||||
p.Path != "/news/2026/09/gnu-linux-day-2026.html" || p.Source != "news/gnu-linux-day-2026.md" {
|
||||
t.Errorf("post = %+v", p)
|
||||
}
|
||||
if got := p.Date.Format("2006-01-02"); got != "2026-09-18" {
|
||||
t.Errorf("Date = %s, want 2026-09-18", got)
|
||||
}
|
||||
if len(p.Categories) != 2 || p.Categories[0] != (Category{"Linux Day", "linux-day"}) || p.Categories[1] != (Category{"FLUG", "flug"}) {
|
||||
t.Errorf("Categories = %+v", p.Categories)
|
||||
}
|
||||
if p.Description != "The programme is out." {
|
||||
t.Errorf("Description = %q, want the first sentence of the body", p.Description)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadPostsDescription checks that an explicit Description wins and that
|
||||
// the fallback drops the Markdown markup.
|
||||
func TestLoadPostsDescription(t *testing.T) {
|
||||
dir := writeFiles(t, map[string]string{
|
||||
"news/a.md": "Title: A\nDate: 2026-01-01T10:00:00+0100\nDescription: Chosen.\n-\nIgnored. Text.\n",
|
||||
"news/b.md": "Title: B\nDate: 2026-01-02T10:00:00+0100\n-\nThis is **bold** and [a link](https://x.org/). More.\n",
|
||||
})
|
||||
posts, err := LoadPosts(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := map[string]string{}
|
||||
for _, p := range posts {
|
||||
got[p.Slug] = p.Description
|
||||
}
|
||||
if got["a"] != "Chosen." {
|
||||
t.Errorf("a Description = %q", got["a"])
|
||||
}
|
||||
if got["b"] != "This is bold and a link." {
|
||||
t.Errorf("b Description = %q", got["b"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadPostsSortedNewestFirst checks the order and the tie break.
|
||||
func TestLoadPostsSortedNewestFirst(t *testing.T) {
|
||||
dir := writeFiles(t, map[string]string{
|
||||
"news/old.md": "Title: Old\nDate: 2026-01-01T10:00:00+0100\n-\nx\n",
|
||||
"news/new.md": "Title: New\nDate: 2026-03-01T10:00:00+0100\n-\nx\n",
|
||||
"news/mid1.md": "Title: M1\nDate: 2026-02-01T10:00:00+0100\n-\nx\n",
|
||||
"news/mid2.md": "Title: M2\nDate: 2026-02-01T10:00:00+0100\n-\nx\n",
|
||||
})
|
||||
posts, err := LoadPosts(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var order []string
|
||||
for _, p := range posts {
|
||||
order = append(order, p.Slug)
|
||||
}
|
||||
if got, want := strings.Join(order, ","), "new,mid1,mid2,old"; got != want {
|
||||
t.Errorf("order = %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadPostsDateLayouts checks both accepted spellings of the offset.
|
||||
func TestLoadPostsDateLayouts(t *testing.T) {
|
||||
dir := writeFiles(t, map[string]string{
|
||||
"news/a.md": "Title: A\nDate: 2026-05-01T10:00:00+02:00\n-\nx\n",
|
||||
"news/b.md": "Title: B\nDate: 2026-05-02T10:00:00Z\n-\nx\n",
|
||||
})
|
||||
if _, err := LoadPosts(dir); err != nil {
|
||||
t.Errorf("LoadPosts() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadPostsErrors checks the failures that must name the file.
|
||||
func TestLoadPostsErrors(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
files map[string]string
|
||||
want []string
|
||||
}{
|
||||
{"em dash", map[string]string{"news/a.md": "Title: A\nDate: 2026-01-01T10:00:00+0100\n-\nWords — more.\n"}, []string{"news/a.md"}},
|
||||
{"bad date", map[string]string{"news/a.md": "Title: A\nDate: yesterday\n-\nx\n"}, []string{"news/a.md", "Date"}},
|
||||
{"missing date", map[string]string{"news/a.md": "Title: A\n-\nx\n"}, []string{"news/a.md", "Date"}},
|
||||
{"unknown key", map[string]string{"news/a.md": "Title: A\nDate: 2026-01-01T10:00:00+0100\nTitolo: A\n-\nx\n"}, []string{"news/a.md", "Titolo"}},
|
||||
{"bad file name", map[string]string{"news/Not A Slug.md": "Title: A\nDate: 2026-01-01T10:00:00+0100\n-\nx\n"}, []string{"Not A Slug.md"}},
|
||||
{"empty category slug", map[string]string{"news/a.md": "Title: A\nDate: 2026-01-01T10:00:00+0100\nCategories: Ok, +++\n-\nx\n"}, []string{"news/a.md", "+++"}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := LoadPosts(writeFiles(t, tt.files))
|
||||
if err == nil {
|
||||
t.Fatal("LoadPosts() error = nil")
|
||||
}
|
||||
for _, want := range tt.want {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("error %q does not contain %q", err, want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadPostsDuplicateSlugSameMonth checks that two posts cannot share an address.
|
||||
func TestLoadPostsDuplicateSlugSameMonth(t *testing.T) {
|
||||
// Same slug cannot come from two files, so the clash is built with a slug
|
||||
// that two different months would not share but a repeated date would:
|
||||
// the check is on the final path, so an equal path is the failure.
|
||||
posts := []Post{
|
||||
{Slug: "same", Path: "/news/2026/11/same.html", Source: "news/a.md"},
|
||||
{Slug: "same", Path: "/news/2026/11/same.html", Source: "news/b.md"},
|
||||
}
|
||||
err := checkPaths(posts)
|
||||
if err == nil || !strings.Contains(err.Error(), "news/a.md") || !strings.Contains(err.Error(), "news/b.md") {
|
||||
t.Errorf("checkPaths() = %v, want an error naming both files", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadPostsCategoryCollision checks that two names with the same slug fail.
|
||||
func TestLoadPostsCategoryCollision(t *testing.T) {
|
||||
dir := writeFiles(t, map[string]string{
|
||||
"news/a.md": "Title: A\nDate: 2026-01-01T10:00:00+0100\nCategories: C\n-\nx\n",
|
||||
"news/b.md": "Title: B\nDate: 2026-01-02T10:00:00+0100\nCategories: C++\n-\nx\n",
|
||||
})
|
||||
_, err := LoadPosts(dir)
|
||||
if err == nil || !strings.Contains(err.Error(), "C++") || !strings.Contains(err.Error(), "\"C\"") {
|
||||
t.Errorf("LoadPosts() error = %v, want it to name both C and C++", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadPostsNoNewsFolder checks that a site without news is fine.
|
||||
func TestLoadPostsNoNewsFolder(t *testing.T) {
|
||||
posts, err := LoadPosts(t.TempDir())
|
||||
if err != nil || len(posts) != 0 {
|
||||
t.Errorf("LoadPosts() = %v, %v, want no posts and no error", posts, err)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue