motorino/internal/content/posts.go
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

285 lines
8.9 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package content
import (
"fmt"
"net/url"
"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
// HasDescription is true when the header has a Description; without one,
// Description is the first sentence of the body and a page should not show
// it as a summary, because the text starts with it.
HasDescription bool
// Mastodon is the address of the post on Mastodon, where a reader can reply;
// empty when the header has none.
Mastodon string
// Draft is true for a post that is not to be published yet.
Draft bool
// Language is the language the post is written in, from the Language key;
// empty when the header has none, and the builder then uses the language of
// the news.
Language string
// Words is the number of words of the body.
Words int
// Source is the file the post was read from, relative to the site.
Source string
// Body is the Markdown after the header.
Body string
}
// wordsPerMinute is the reading speed the reading time is worked out with.
const wordsPerMinute = 200
// ReadingMinutes returns how many minutes the post takes to read, at least 1.
func (p Post) ReadingMinutes() int {
minutes := (p.Words + wordsPerMinute - 1) / wordsPerMinute
if minutes < 1 {
return 1
}
return minutes
}
// Visible reports whether the post is published at the moment now: it is not a
// draft and its date has come.
func (p Post) Visible(now time.Time) bool {
return !p.Draft && !p.Date.After(now)
}
// postKeys are the keys a news header may have.
var postKeys = []string{"Title", "Author", "Date", "Categories", "Description", "Mastodon", "Draft", "Language"}
// 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"}
// LanguageCode is the shape of a language code: "en", "pt-BR".
var LanguageCode = regexp.MustCompile(`^[a-z]{2,3}(-[A-Za-z0-9]+)?$`)
// 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
}
mastodon, err := parseMastodon(source, h.Get("Mastodon"))
if err != nil {
return Post{}, err
}
draft, err := parseDraft(source, h.Get("Draft"))
if err != nil {
return Post{}, err
}
language := h.Get("Language")
if language != "" && !LanguageCode.MatchString(language) {
return Post{}, fmt.Errorf("%s: Language %q is not a language code like \"en\" or \"pt-BR\"", source, language)
}
description := h.Get("Description")
hasDescription := description != ""
if !hasDescription {
description = firstSentence(body)
}
return Post{
Slug: slug, Path: PostPath(date, slug), Title: h.Get("Title"), Author: h.Get("Author"),
Description: description, HasDescription: hasDescription, Mastodon: mastodon, Draft: draft, Language: language,
Words: len(strings.Fields(body)), Date: date, Categories: categories, Source: source, Body: body,
}, nil
}
// parseMastodon accepts an empty value or an https address with a host and no
// spaces, because the address becomes the target of a link.
func parseMastodon(source, value string) (string, error) {
if value == "" {
return "", nil
}
u, err := url.Parse(value)
if err != nil || u.Scheme != "https" || u.Host == "" || strings.ContainsAny(value, " \t\"'<>") {
return "", fmt.Errorf("%s: Mastodon %q is not an https address, such as https://social.kmalloc.org/@squid/115000000000000000", source, value)
}
return value, nil
}
// parseDraft reads the Draft key: true or yes make a draft; false, no or no key
// do not.
func parseDraft(source, value string) (bool, error) {
switch strings.ToLower(value) {
case "", "false", "no":
return false, nil
case "true", "yes":
return true, nil
}
return false, fmt.Errorf("%s: Draft %q must be true or false", source, value)
}
// 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
seen := map[string]bool{}
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)
}
if seen[slug] {
continue
}
seen[slug] = true
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
}
// markdownLink, markdownMark and sentenceEnd pick the first sentence out of a
// body and drop its Markdown markup, for a description that was not written.
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 ""
}