- 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
121 lines
3.5 KiB
Go
121 lines
3.5 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// Package build assembles a site from its configuration, content, templates
|
|
// and assets, and writes it to a folder.
|
|
package build
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"git.kmalloc.org/squid/motorino/internal/content"
|
|
"git.kmalloc.org/squid/motorino/internal/render"
|
|
)
|
|
|
|
// dateOnly is the layout of the dates shown in lists.
|
|
const dateOnly = "2006-01-02"
|
|
|
|
// renderedPost is a post together with its body already converted to HTML.
|
|
type renderedPost struct {
|
|
content.Post
|
|
// HTML is the body of the post as HTML, placeholders already replaced.
|
|
HTML string
|
|
}
|
|
|
|
// 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. A
|
|
// 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 {
|
|
mark := ""
|
|
if p.Language != "" && p.Language != lang {
|
|
mark = fmt.Sprintf(` hreflang="%s" lang="%s"`, p.Language, p.Language)
|
|
}
|
|
day := p.Date.Format(dateOnly)
|
|
item := fmt.Sprintf(`<li><time datetime="%s">%s</time> <a href="%s"%s>%s</a>`,
|
|
day, day, p.Path, mark, render.Escape(p.Title))
|
|
if withDescription && p.Description != "" {
|
|
item += ": " + render.Escape(p.Description)
|
|
}
|
|
return item + "</li>"
|
|
}
|
|
|
|
// newsList returns the full list of the news as a <ul>, or an empty string
|
|
// when there are no posts.
|
|
func newsList(posts []content.Post, lang string) string {
|
|
if len(posts) == 0 {
|
|
return ""
|
|
}
|
|
items := make([]string, len(posts))
|
|
for i, p := range posts {
|
|
items[i] = newsItem(p, lang, true)
|
|
}
|
|
return "<ul>\n" + strings.Join(items, "\n") + "\n</ul>"
|
|
}
|
|
|
|
// latestNews returns the <li> items of the first n posts, without the <ul>
|
|
// and without descriptions, for the home page.
|
|
func latestNews(posts []content.Post, lang string, n int) string {
|
|
if n > len(posts) {
|
|
n = len(posts)
|
|
}
|
|
items := make([]string, n)
|
|
for i := 0; i < n; i++ {
|
|
items[i] = newsItem(posts[i], lang, false)
|
|
}
|
|
return strings.Join(items, "\n")
|
|
}
|
|
|
|
// categoriesList returns the list of the categories, sorted by name, each with
|
|
// the number of posts, and the categories themselves. With no categories both
|
|
// are empty.
|
|
func categoriesList(posts []content.Post) (string, []content.Category) {
|
|
counts := map[string]int{}
|
|
byslug := map[string]content.Category{}
|
|
for _, p := range posts {
|
|
for _, c := range p.Categories {
|
|
counts[c.Slug]++
|
|
byslug[c.Slug] = c
|
|
}
|
|
}
|
|
if len(byslug) == 0 {
|
|
return "", nil
|
|
}
|
|
cats := make([]content.Category, 0, len(byslug))
|
|
for _, c := range byslug {
|
|
cats = append(cats, c)
|
|
}
|
|
sort.Slice(cats, func(i, j int) bool {
|
|
a, b := strings.ToLower(cats[i].Name), strings.ToLower(cats[j].Name)
|
|
if a != b {
|
|
return a < b
|
|
}
|
|
return cats[i].Name < cats[j].Name
|
|
})
|
|
items := make([]string, len(cats))
|
|
for i, c := range cats {
|
|
items[i] = fmt.Sprintf(`<li><a href="%s">%s</a> (%d)</li>`, categoryPath(c.Slug), render.Escape(c.Name), counts[c.Slug])
|
|
}
|
|
return "<ul>\n" + strings.Join(items, "\n") + "\n</ul>", cats
|
|
}
|
|
|
|
// categoryPath returns the address of the page of a category.
|
|
func categoryPath(slug string) string {
|
|
return "/news/category/" + slug + ".html"
|
|
}
|
|
|
|
// postsIn returns the posts that have the category with the given address
|
|
// name, in the order they were given.
|
|
func postsIn(posts []content.Post, slug string) []content.Post {
|
|
var out []content.Post
|
|
for _, p := range posts {
|
|
for _, c := range p.Categories {
|
|
if c.Slug == slug {
|
|
out = append(out, p)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|