feat: motorino new writes a draft, motorino build -drafts previews it
All checks were successful
check / check (pull_request) Successful in 30s
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
This commit is contained in:
parent
3f62c79603
commit
633da1c46b
2 changed files with 268 additions and 4 deletions
58
internal/content/new.go
Normal file
58
internal/content/new.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package content
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.kmalloc.org/squid/motorino/internal/mdx"
|
||||
)
|
||||
|
||||
// NewPost writes the file of a new post in dir/news and returns its path
|
||||
// relative to dir. The name comes from the title, the date is now with its own
|
||||
// offset, the category is News and the post is a draft, so that it is not
|
||||
// published until Draft is taken out. The author line is written only when
|
||||
// author is not empty. An existing file is never written over.
|
||||
func NewPost(dir, title, author string, now time.Time) (string, error) {
|
||||
title = strings.TrimSpace(title)
|
||||
if title == "" || strings.ContainsAny(title, "\r\n") {
|
||||
return "", fmt.Errorf("the title must be a single line of text")
|
||||
}
|
||||
if err := mdx.RejectEmDash("the title", title); err != nil {
|
||||
return "", err
|
||||
}
|
||||
slug, err := CategorySlug(title)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("the title %q has no letters or digits to make a file name from", title)
|
||||
}
|
||||
rel := "news/" + slug + ".md"
|
||||
path := filepath.Join(dir, filepath.FromSlash(rel))
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
return "", fmt.Errorf("create %s: %w", filepath.Dir(path), err)
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("Title: " + title + "\n")
|
||||
if author != "" {
|
||||
b.WriteString("Author: " + author + "\n")
|
||||
}
|
||||
b.WriteString("Date: " + now.Format("2006-01-02T15:04:05-0700") + "\n")
|
||||
b.WriteString("Categories: News\n")
|
||||
b.WriteString("Description:\n")
|
||||
b.WriteString("Draft: true\n-\n\n")
|
||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
|
||||
if err != nil {
|
||||
if os.IsExist(err) {
|
||||
return "", fmt.Errorf("%s already exists", rel)
|
||||
}
|
||||
return "", fmt.Errorf("create %s: %w", rel, err)
|
||||
}
|
||||
defer file.Close()
|
||||
if _, err := file.WriteString(b.String()); err != nil {
|
||||
return "", fmt.Errorf("write %s: %w", rel, err)
|
||||
}
|
||||
return rel, nil
|
||||
}
|
||||
78
internal/content/new_test.go
Normal file
78
internal/content/new_test.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package content
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// newsNow is the moment the new post tests run at.
|
||||
var newsNow = time.Date(2026, 11, 16, 10, 45, 12, 0, time.FixedZone("CET", 3600))
|
||||
|
||||
// TestNewPostWritesADraftThatLoads checks the file, its name and its header.
|
||||
func TestNewPostWritesADraftThatLoads(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
rel, err := NewPost(dir, "Il manifesto è cambiato", "squid", newsNow)
|
||||
if err != nil {
|
||||
t.Fatalf("NewPost() error = %v", err)
|
||||
}
|
||||
if rel != "news/il-manifesto-e-cambiato.md" {
|
||||
t.Errorf("NewPost() = %q, want news/il-manifesto-e-cambiato.md", rel)
|
||||
}
|
||||
data, err := os.ReadFile(filepath.Join(dir, filepath.FromSlash(rel)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range []string{"Title: Il manifesto è cambiato\n", "Author: squid\n", "Date: 2026-11-16T10:45:12+0100\n", "Categories: News\n", "Draft: true\n", "\n-\n"} {
|
||||
if !strings.Contains(string(data), want) {
|
||||
t.Errorf("the file does not contain %q\n%s", want, data)
|
||||
}
|
||||
}
|
||||
posts, err := LoadPosts(dir)
|
||||
if err != nil || len(posts) != 1 {
|
||||
t.Fatalf("LoadPosts() = %v, %v", posts, err)
|
||||
}
|
||||
if p := posts[0]; !p.Draft || p.Title != "Il manifesto è cambiato" || p.Author != "squid" || p.Visible(newsNow.Add(time.Hour)) {
|
||||
t.Errorf("the new post is %+v, want a hidden draft with the title and the author", p)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewPostWithoutAuthor checks that no author line is written without one.
|
||||
func TestNewPostWithoutAuthor(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
rel, err := NewPost(dir, "No author", "", newsNow)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, _ := os.ReadFile(filepath.Join(dir, filepath.FromSlash(rel)))
|
||||
if strings.Contains(string(data), "Author:") {
|
||||
t.Errorf("an Author line without an author:\n%s", data)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewPostRefuses checks the titles and the files it will not write over.
|
||||
func TestNewPostRefuses(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if _, err := NewPost(dir, "Twice", "", newsNow); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tests := []struct{ name, title, want string }{
|
||||
{"existing file", "Twice", "already exists"},
|
||||
{"empty title", " ", "title"},
|
||||
{"no letters", "!!!", "no letters"},
|
||||
{"line break", "One\nTwo", "title"},
|
||||
{"em dash", "One — two", "dash"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
rel, err := NewPost(dir, tt.title, "", newsNow)
|
||||
if err == nil || !strings.Contains(err.Error(), tt.want) {
|
||||
t.Errorf("NewPost(%q) = %q, %v, want an error with %q", tt.title, rel, err, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue