motorino/internal/content/posts.go

203 lines
6 KiB
Go

// 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 ""
}