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,38 @@
|
||||
// Package pdfinfo extracts basic facts from PDF bytes with a tiny parser —
|
||||
// no third-party PDF library, just enough to count pages in golden tests
|
||||
// and smoke checks.
|
||||
package pdfinfo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Pages returns the page count of a PDF by scanning for the /Type /Pages
|
||||
// objects' /Count entries (the catalog root carries the total; we take the
|
||||
// maximum to be safe with nested page trees).
|
||||
func Pages(pdf []byte) (int, error) {
|
||||
if len(pdf) == 0 || !bytes.HasPrefix(pdf, []byte("%PDF-")) {
|
||||
return 0, fmt.Errorf("pdfinfo: not a PDF (missing %%%%PDF- header)")
|
||||
}
|
||||
re := regexp.MustCompile(`/Type\s*/Pages[^>]*?/Count\s+(\d+)`)
|
||||
best := 0
|
||||
for _, m := range re.FindAllStringSubmatch(string(pdf), -1) {
|
||||
if n, err := strconv.Atoi(m[1]); err == nil && n > best {
|
||||
best = n
|
||||
}
|
||||
}
|
||||
if best == 0 {
|
||||
// Compressed object streams can hide the count; require at least
|
||||
// the header then fall back to /Type /Page occurrences (\b keeps
|
||||
// /Pages from matching).
|
||||
pageRe := regexp.MustCompile(`/Type\s*/Page\b`)
|
||||
best = len(pageRe.FindAll(pdf, -1))
|
||||
if best == 0 {
|
||||
return 0, fmt.Errorf("pdfinfo: no page count found")
|
||||
}
|
||||
}
|
||||
return best, nil
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user