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:
2026-08-29 05:44:24 -05:00
parent 97c8e340ab
commit 10e930e46d
147 changed files with 30793 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
package pdfinfo
import (
"fmt"
"strings"
"testing"
)
// fakePDF builds a minimal but structurally scannable PDF with a page-tree
// /Count and N /Type /Page objects.
func fakePDF(pages, declared int) []byte {
var objs []string
for i := 1; i <= pages; i++ {
objs = append(objs, fmt.Sprintf("%d 0 obj<</Type/Page/Parent 99 0 R>>endobj", i))
}
objs = append(objs, fmt.Sprintf("99 0 obj<</Type/Pages/Count %d/Kids[1 0 R]>>endobj", declared))
body := strings.Join(objs, "\n")
return []byte("%PDF-1.7\n" + body + "\n%%EOF")
}
func TestPagesCount(t *testing.T) {
for _, n := range []int{1, 2, 7, 23} {
got, err := Pages(fakePDF(n, n))
if err != nil {
t.Fatalf("n=%d: %v", n, err)
}
if got != n {
t.Fatalf("n=%d got %d", n, got)
}
}
}
func TestPagesFallsBackToPageObjects(t *testing.T) {
// No /Pages /Count reachable: count /Type /Page objects instead.
pdf := []byte("%PDF-1.7\n1 0 obj<</Type/Page>>endobj\n2 0 obj<</Type/Page>>endobj\n%%EOF")
got, err := Pages(pdf)
if err != nil || got != 2 {
t.Fatalf("got=%d err=%v", got, err)
}
}
func TestPagesRejectsNonPDF(t *testing.T) {
if _, err := Pages([]byte("hello")); err == nil {
t.Fatal("want error for non-PDF")
}
if _, err := Pages(nil); err == nil {
t.Fatal("want error for empty input")
}
}
func TestPagesDoesNotCountPagesType(t *testing.T) {
// "/Type /Pages" must not match the /Type /Page fallback.
pdf := []byte("%PDF-1.7\n1 0 obj<</Type/Page>>endobj\n2 0 obj<</Type/Pages/Count 5>>endobj\n%%EOF")
got, err := Pages(pdf)
if err != nil || got != 5 {
t.Fatalf("got=%d err=%v", got, err)
}
}