mopac-pdf v0: markdown to beautiful PDFs via digest-pinned typst
Go CLI that turns the markdown currency of this stack (Redmine notes, Discourse posts, briefing output) into typeset PDFs: two embedded typst templates (report with title page/TOC/headers, dense brief), front-matter (title/subtitle/author/date/classification/template), GFM tables, and bar charts rendered in pure Go (go-chart) from fenced chart data blocks. Engine is a prebuilt typst 0.15.1 container pinned by digest and run --network none; PDF bytes to stdout or -o. Exit codes 0/1/2. All dev in docker (dev.sh/Makefile); unit tests + golden typst fixtures plus a host-side smoke against the real engine container. Generated with Crush Assisted-by: Crush:glm-5.2
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
// Package frontmatter parses the key: value front-matter block that may
|
||||
// open a mopac-pdf markdown input (delimited by --- lines).
|
||||
package frontmatter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Meta is the parsed front-matter. The well-known keys mopac-pdf templates
|
||||
// consume are Title, Subtitle, Author, Date, Classification and Template;
|
||||
// anything else found in the block is kept in Extra (never an error: the
|
||||
// currency of this stack is loose markdown).
|
||||
type Meta struct {
|
||||
Title string
|
||||
Subtitle string
|
||||
Author string
|
||||
Date string
|
||||
Classification string
|
||||
Template string
|
||||
Extra map[string]string
|
||||
}
|
||||
|
||||
// Split separates an optional front-matter block from the markdown body and
|
||||
// parses it. Input without a leading --- line is all body, empty Meta.
|
||||
func Split(input string) (Meta, string, error) {
|
||||
var meta Meta
|
||||
first, rest, ok := strings.Cut(input, "\n")
|
||||
if strings.TrimRight(first, "\r") != "---" || !ok {
|
||||
return meta, input, nil
|
||||
}
|
||||
end := -1
|
||||
var block []string
|
||||
for i, line := range strings.Split(rest, "\n") {
|
||||
trimmed := strings.TrimRight(line, "\r")
|
||||
if trimmed == "---" || trimmed == "..." {
|
||||
end = i
|
||||
break
|
||||
}
|
||||
block = append(block, trimmed)
|
||||
}
|
||||
if end < 0 {
|
||||
return meta, input, fmt.Errorf("frontmatter: opening --- without closing ---")
|
||||
}
|
||||
body := strings.Join(strings.Split(rest, "\n")[end+1:], "\n")
|
||||
body = strings.TrimPrefix(body, "\n")
|
||||
meta.Extra = map[string]string{}
|
||||
for _, line := range block {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
key, value, ok := strings.Cut(line, ":")
|
||||
if !ok {
|
||||
return meta, "", fmt.Errorf("frontmatter: not a key: value line: %q", line)
|
||||
}
|
||||
key = strings.TrimSpace(key)
|
||||
value = strings.Trim(strings.TrimSpace(value), `"'`)
|
||||
switch strings.ToLower(key) {
|
||||
case "title":
|
||||
meta.Title = value
|
||||
case "subtitle":
|
||||
meta.Subtitle = value
|
||||
case "author":
|
||||
meta.Author = value
|
||||
case "date":
|
||||
meta.Date = value
|
||||
case "classification":
|
||||
meta.Classification = value
|
||||
case "template":
|
||||
meta.Template = value
|
||||
default:
|
||||
meta.Extra[key] = value
|
||||
}
|
||||
}
|
||||
return meta, body, nil
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package frontmatter
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSplitFull(t *testing.T) {
|
||||
meta, body, err := Split("---\ntitle: Q3 Brief\nsubtitle: Ops\nclassification: INTERNAL\ntemplate: brief\nauthor: A B\ndate: 2026-08-29\nextra: kept\n---\n\n# Body\n")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if meta.Title != "Q3 Brief" || meta.Subtitle != "Ops" || meta.Classification != "INTERNAL" ||
|
||||
meta.Template != "brief" || meta.Author != "A B" || meta.Date != "2026-08-29" {
|
||||
t.Fatalf("meta: %+v", meta)
|
||||
}
|
||||
if meta.Extra["extra"] != "kept" {
|
||||
t.Fatalf("extra keys dropped: %+v", meta.Extra)
|
||||
}
|
||||
if body != "# Body\n" {
|
||||
t.Fatalf("body: %q", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitQuoted(t *testing.T) {
|
||||
meta, _, err := Split("---\ntitle: \"The: Quoted Title\"\n---\nx")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if meta.Title != "The: Quoted Title" {
|
||||
t.Fatalf("title: %q", meta.Title)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitNone(t *testing.T) {
|
||||
meta, body, err := Split("# Just markdown\n\nBody.")
|
||||
if err != nil || meta.Title != "" || body != "# Just markdown\n\nBody." {
|
||||
t.Fatalf("meta=%+v body=%q err=%v", meta, body, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitUnclosed(t *testing.T) {
|
||||
_, _, err := Split("---\ntitle: x\n")
|
||||
if err == nil {
|
||||
t.Fatal("want error for unclosed block")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitBadLine(t *testing.T) {
|
||||
_, _, err := Split("---\nnope\n---\n")
|
||||
if err == nil {
|
||||
t.Fatal("want error for non key: value line")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitCommentsBlank(t *testing.T) {
|
||||
_, body, err := Split("---\n# comment\n\ntitle: t\n---\nbody here")
|
||||
if err != nil || body != "body here" {
|
||||
t.Fatalf("body=%q err=%v", body, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user