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,61 @@
|
||||
// Package chart renders ```chart data blocks into PNG assets using the
|
||||
// permissive go-chart library (pure Go). v0 proves one chart type end to
|
||||
// end: bar.
|
||||
package chart
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/wcharczuk/go-chart/v2"
|
||||
"github.com/wcharczuk/go-chart/v2/drawing"
|
||||
|
||||
"ukrrs.com/mopac/pdf/internal/markdown"
|
||||
)
|
||||
|
||||
// accent matches the shipped templates (#1f4e79).
|
||||
var accent = drawing.Color{R: 31, G: 78, B: 121, A: 255}
|
||||
|
||||
// Bar renders a bar chart to PNG bytes. Unknown chart types are an error:
|
||||
// v0 is deliberately one type, proven end to end.
|
||||
func Bar(c *markdown.Chart) ([]byte, error) {
|
||||
if c == nil || len(c.Labels) == 0 {
|
||||
return nil, fmt.Errorf("chart: no data points")
|
||||
}
|
||||
if c.Type != "bar" {
|
||||
return nil, fmt.Errorf("chart: unsupported type %q (v0: bar only)", c.Type)
|
||||
}
|
||||
values := make([]chart.Value, len(c.Labels))
|
||||
for i, label := range c.Labels {
|
||||
values[i] = chart.Value{
|
||||
Value: c.Values[i],
|
||||
Label: label,
|
||||
Style: chart.Style{FillColor: accent, StrokeColor: accent, StrokeWidth: 0.5},
|
||||
}
|
||||
}
|
||||
yfmt := chart.ValueFormatter(chart.FloatValueFormatter)
|
||||
if c.Unit != "" {
|
||||
unit := c.Unit
|
||||
yfmt = func(v interface{}) string {
|
||||
return chart.FloatValueFormatter(v) + " " + unit
|
||||
}
|
||||
}
|
||||
graph := chart.BarChart{
|
||||
Title: c.Title,
|
||||
Background: chart.Style{
|
||||
Padding: chart.Box{Top: 24, Left: 12, Right: 12, Bottom: 8},
|
||||
},
|
||||
XAxis: chart.Style{FontSize: 9, FontColor: drawing.Color{R: 60, G: 60, B: 60, A: 255}},
|
||||
YAxis: chart.YAxis{ValueFormatter: yfmt, Style: chart.Style{FontSize: 8}},
|
||||
BarWidth: 48,
|
||||
BarSpacing: 26,
|
||||
Height: 320,
|
||||
Width: 260 + len(values)*74,
|
||||
Bars: values,
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
if err := graph.Render(chart.PNG, &buf); err != nil {
|
||||
return nil, fmt.Errorf("chart: render: %w", err)
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package chart
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
_ "image/png"
|
||||
"testing"
|
||||
|
||||
"ukrrs.com/mopac/pdf/internal/markdown"
|
||||
)
|
||||
|
||||
func TestBarPNG(t *testing.T) {
|
||||
c := &markdown.Chart{
|
||||
Type: "bar",
|
||||
Title: "Revenue",
|
||||
Unit: "$K",
|
||||
Labels: []string{"Q1", "Q2", "Q3"},
|
||||
Values: []float64{10, 20, 15},
|
||||
}
|
||||
png, err := Bar(c)
|
||||
if err != nil {
|
||||
t.Fatalf("render: %v", err)
|
||||
}
|
||||
if !bytes.HasPrefix(png, []byte{0x89, 'P', 'N', 'G'}) {
|
||||
t.Fatalf("not a PNG: % x", png[:4])
|
||||
}
|
||||
img, _, err := image.Decode(bytes.NewReader(png))
|
||||
if err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
b := img.Bounds()
|
||||
if b.Dx() < 300 || b.Dy() < 200 {
|
||||
t.Fatalf("unexpected dimensions %dx%d", b.Dx(), b.Dy())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBarRejectsUnknownType(t *testing.T) {
|
||||
c := &markdown.Chart{Type: "pie", Labels: []string{"a"}, Values: []float64{1}}
|
||||
if _, err := Bar(c); err == nil {
|
||||
t.Fatal("pie must be rejected in v0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBarRejectsEmpty(t *testing.T) {
|
||||
if _, err := Bar(nil); err == nil {
|
||||
t.Fatal("nil chart must be rejected")
|
||||
}
|
||||
c := &markdown.Chart{Type: "bar"}
|
||||
if _, err := Bar(c); err == nil {
|
||||
t.Fatal("chart without data must be rejected")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user