This repository has been archived on 2026-09-16. You can view files and clone it, but cannot push or open issues or pull requests.
typst_reporter/modules/model.typ

899 lines
20 KiB
Plaintext

// =============================================================================
// BEFFROI REPORT MODEL
// =============================================================================
//
// Generic corporate report renderer.
//
// Expected use:
//
// typst compile \
// --input report=reports/report.yaml \
// report.typ report.pdf
//
// =============================================================================
// -----------------------------------------------------------------------------
// ----- Helpers
// -----------------------------------------------------------------------------
#let dict-get(dict, key, default) = {
if key in dict {
dict.at(key)
} else {
default
}
}
#let as-content(value) = {
if value == none {
[]
} else {
str(value)
}
}
#let theme-color(theme, key) = rgb(str(dict-get(theme, key, "#000000")))
#let severity-color(theme, value) = {
let value = lower(str(value))
if value == "critical" or value == "high" {
theme-color(theme, "critical")
} else if value == "warning" or value == "medium" {
theme-color(theme, "warning")
} else if value == "ok" or value == "success" or value == "resolved" {
theme-color(theme, "success")
} else {
theme-color(theme, "info")
}
}
#let report-type-label(value) = {
let value = lower(str(value))
if value == "technical" {
"Technical report"
} else if value == "briefing" {
"Stakeholder briefing"
} else {
str(value)
}
}
#let badge(
body,
color: none,
) = {
box(
inset: (x: 5pt, y: 2pt),
radius: 2pt,
fill: color.lighten(85%),
stroke: 0.6pt + color.lighten(45%),
text(
size: 7.5pt,
weight: "bold",
fill: color,
upper(str(body)),
),
)
}
#let metadata-line(theme, label, value) = {
if value != none and str(value) != "" {
stack(
spacing: 1.5pt,
text(
size: 8.5pt,
fill: theme-color(theme, "muted"),
weight: "bold",
)[#label],
text(
size: 8.5pt,
fill: theme-color(theme, "ink"),
)[#as-content(value)],
)
}
}
#let section-title(theme, body) = {
block(
above: 1.1em,
below: 0.55em,
width: 100%,
)[
#text(
size: 13pt,
weight: "bold",
fill: theme-color(theme, "accent"),
)[#body]
#v(2.5pt)
#line(
length: 100%,
stroke: 0.7pt + theme-color(theme, "line"),
)
]
}
#let subsection-title(theme, body) = {
block(
above: 0.8em,
below: 0.35em,
)[
#text(
size: 10.5pt,
weight: "bold",
fill: theme-color(theme, "ink"),
)[#body]
]
}
#let callout(
theme,
body,
title: none,
kind: "info",
breakable: true,
) = {
let color = severity-color(theme, kind)
block(
width: 100%,
inset: 8pt,
breakable: breakable,
radius: 2pt,
fill: color.lighten(91%),
stroke: (
left: 2.5pt + color,
top: 0.5pt + color.lighten(65%),
right: 0.5pt + color.lighten(65%),
bottom: 0.5pt + color.lighten(65%),
),
)[
#if title != none {
text(
size: 9pt,
weight: "bold",
fill: color,
)[ #title ]
v(3pt)
}
#body
]
}
#let code-block(theme, body) = {
block(
width: 100%,
inset: 7pt,
radius: 2pt,
fill: rgb("#f2f3f4"),
stroke: 0.5pt + theme-color(theme, "line"),
)[
#text(
font: dict-get(theme, "mono_font", "DejaVu Sans Mono"),
size: 8.2pt,
fill: theme-color(theme, "ink"),
)[
#body
]
]
}
// -----------------------------------------------------------------------------
// ----- Generic content renderer
// -----------------------------------------------------------------------------
#let render-block(theme, block-data) = {
let kind = dict-get(block-data, "type", "text")
if kind == "text" {
par[
#as-content(dict-get(block-data, "text", ""))
]
} else if kind == "bullets" {
let items = dict-get(block-data, "items", ())
list(
tight: true,
..items.map(item => [#as-content(item)]),
)
} else if kind == "steps" {
let items = dict-get(block-data, "items", ())
enum(
tight: false,
..items.map(item => [#as-content(item)]),
)
} else if kind == "callout" {
callout(
theme,
[#as-content(dict-get(block-data, "text", ""))],
title: dict-get(block-data, "title", none),
kind: dict-get(block-data, "kind", "info"),
)
} else if kind == "code" {
code-block(
theme,
as-content(dict-get(block-data, "text", "")),
)
} else if kind == "subsection" {
subsection-title(
theme,
as-content(dict-get(block-data, "title", "")),
)
for child in dict-get(block-data, "content", ()) {
render-block(theme, child)
v(0.4em)
}
} else {
callout(
theme,
[
Unknown report block type:
#raw(str(kind))
],
title: "Renderer warning",
kind: "warning",
)
}
}
// -----------------------------------------------------------------------------
// ----- Findings
// -----------------------------------------------------------------------------
#let render-findings(theme, findings) = {
for finding in findings {
let title = dict-get(finding, "title", "Finding")
let severity = dict-get(finding, "severity", "info")
let status = dict-get(finding, "status", none)
let evidence = dict-get(finding, "evidence", none)
let assessment = dict-get(finding, "assessment", none)
block(
width: 100%,
breakable: false,
inset: 8pt,
stroke: 0.5pt + theme-color(theme, "line"),
radius: 2pt,
)[
#grid(
columns: (1fr, auto, auto),
gutter: 5pt,
align: (left, center, center),
text(
weight: "bold",
size: 10pt,
fill: theme-color(theme, "ink"),
)[#title],
badge(
severity,
color: severity-color(theme, severity),
),
if status != none {
badge(
status,
color: severity-color(theme, status),
)
} else {
[]
},
)
#if evidence != none {
v(5pt)
text(
size: 8pt,
weight: "bold",
fill: theme-color(theme, "muted"),
)[EVIDENCE]
v(2pt)
text(
size: 9pt,
fill: theme-color(theme, "ink"),
)[#as-content(evidence)]
}
#if assessment != none {
v(5pt)
text(
size: 8pt,
weight: "bold",
fill: theme-color(theme, "muted"),
)[ASSESSMENT]
v(2pt)
text(
size: 9pt,
fill: theme-color(theme, "ink"),
)[#as-content(assessment)]
}
]
v(6pt)
}
}
// -----------------------------------------------------------------------------
// ----- Timeline
// -----------------------------------------------------------------------------
#let render-timeline(theme, entries) = {
let rows = (
(
table.header(
[Time],
[Event],
[Notes],
),
)
)
for entry in entries {
let time = str(dict-get(entry, "time", ""))
rows.push(
(
[#text(size: 8pt)[#time]],
[#as-content(dict-get(entry, "event", ""))],
[#as-content(dict-get(entry, "notes", ""))],
),
)
}
table(
columns: (31mm, 1.1fr, 2fr),
inset: 5pt,
stroke: 0.45pt + theme-color(theme, "line"),
fill: (x, y) => {
if y == 0 {
theme-color(theme, "paper_soft")
} else {
none
}
},
align: (left, left, left),
..rows.flatten(),
)
}
// -----------------------------------------------------------------------------
// ----- Diagnostic hypotheses
// -----------------------------------------------------------------------------
#let confidence-bar(theme, value) = {
let value = int(value)
let color = if value >= 80 {
theme-color(theme, "critical")
} else if value >= 50 {
theme-color(theme, "warning")
} else {
theme-color(theme, "info")
}
grid(
columns: (1fr, 12mm),
gutter: 6pt,
align: (left + horizon, right + horizon),
block(
width: 100%,
height: 5pt,
radius: 2.5pt,
fill: theme-color(theme, "paper_soft"),
stroke: 0.4pt + theme-color(theme, "line"),
)[
#block(
width: value * 1%,
height: 100%,
radius: 2.5pt,
fill: color,
)
],
text(size: 8pt, weight: "bold", fill: color)[#(str(value) + "%")],
)
}
#let diagnostic-list(theme, title, items) = {
if items.len() > 0 {
text(size: 7.5pt, weight: "bold", fill: theme-color(theme, "muted"))[#title]
v(2pt)
list(
tight: true,
..items.map(item => text(size: 8.5pt)[#as-content(item)]),
)
}
}
#let render-diagnostics(theme, diagnostics) = {
let scale = dict-get(diagnostics, "confidence_scale", (:))
let interpretation = dict-get(scale, "interpretation", none)
let hypotheses = dict-get(diagnostics, "hypotheses", ())
let leading = dict-get(diagnostics, "current_leading_assessment", (:))
let unsupported = dict-get(diagnostics, "not_yet_supported", ())
if interpretation != none {
callout(
theme,
[#as-content(interpretation)],
title: "How to read confidence",
kind: "info",
)
v(7pt)
}
for hypothesis in hypotheses {
let id = dict-get(hypothesis, "id", "Dx")
let diagnosis = dict-get(hypothesis, "diagnosis", "Diagnostic hypothesis")
let confidence = dict-get(hypothesis, "confidence_percent", 0)
let status = dict-get(hypothesis, "status", none)
let scope = dict-get(hypothesis, "scope", none)
let evidence-for = dict-get(hypothesis, "evidence_for", ())
let evidence-against = dict-get(hypothesis, "evidence_against", ())
let hypothesis-interpretation = dict-get(hypothesis, "interpretation", none)
let tests = dict-get(hypothesis, "tests_to_confirm", ())
block(
width: 100%,
breakable: false,
inset: 8pt,
stroke: 0.5pt + theme-color(theme, "line"),
radius: 2pt,
)[
#grid(
columns: (auto, 1fr, auto),
gutter: 6pt,
align: (left + horizon, left + horizon, right + horizon),
badge(id, color: theme-color(theme, "accent")),
text(size: 10pt, weight: "bold")[#diagnosis],
if status != none {
badge(status, color: severity-color(theme, status))
} else {
[]
},
)
#v(5pt)
#confidence-bar(theme, confidence)
#if scope != none {
v(3pt)
text(size: 7.5pt, fill: theme-color(theme, "muted"))[
SCOPE: #upper(str(scope))
]
}
#if evidence-for.len() > 0 {
v(6pt)
diagnostic-list(theme, "EVIDENCE FOR", evidence-for)
}
#if evidence-against.len() > 0 {
v(5pt)
diagnostic-list(theme, "EVIDENCE AGAINST / LIMITATIONS", evidence-against)
}
#if hypothesis-interpretation != none {
v(5pt)
text(size: 7.5pt, weight: "bold", fill: theme-color(theme, "muted"))[INTERPRETATION]
v(2pt)
text(size: 8.5pt)[#as-content(hypothesis-interpretation)]
}
#if tests.len() > 0 {
v(5pt)
diagnostic-list(theme, "TESTS TO CONFIRM / REFUTE", tests)
}
]
v(6pt)
}
if leading.len() > 0 {
let statement = dict-get(leading, "statement", none)
let confidence = dict-get(leading, "confidence_percent", none)
callout(
theme,
[
#if confidence != none {
confidence-bar(theme, confidence)
v(5pt)
}
#as-content(statement)
],
title: "Current leading assessment",
kind: "warning",
)
}
if unsupported.len() > 0 {
v(7pt)
diagnostic-list(theme, "NOT YET SUPPORTED", unsupported)
}
}
// -----------------------------------------------------------------------------
// ----- Action plan
// -----------------------------------------------------------------------------
#let render-actions(theme, actions) = {
for (index, action) in actions.enumerate() {
let title = dict-get(action, "title", "Action")
let status = dict-get(action, "status", "pending")
let body = dict-get(action, "text", none)
let commands = dict-get(action, "commands", ())
grid(
columns: (8mm, 1fr, auto),
gutter: 5pt,
align: (center, left, center),
box(
width: 6mm,
height: 6mm,
radius: 50%,
fill: theme-color(theme, "accent"),
align(center + horizon)[
#text(
size: 7pt,
weight: "bold",
fill: white,
)[
#(index + 1)
]
],
),
text(
size: 10pt,
weight: "bold",
fill: theme-color(theme, "ink"),
)[
#title
],
badge(
status,
color: severity-color(theme, status),
),
)
if body != none {
block(
above: 2pt,
inset: (left: 13mm),
)[
#text(
size: 9pt,
fill: theme-color(theme, "ink"),
)[
#as-content(body)
]
]
}
for command in commands {
block(
inset: (left: 13mm, top: 4pt),
)[
#code-block(theme, as-content(command))
]
}
v(8pt)
}
}
// -----------------------------------------------------------------------------
// ----- Header / Footer
// -----------------------------------------------------------------------------
#let report-header(theme, report, logo) = {
let organization = dict-get(report, "organization", "Beffroi")
let report-type = dict-get(report, "report_type", none)
let descriptor = if report-type == none {
"Report"
} else {
report-type-label(report-type)
}
grid(
columns: (1fr, auto),
align: (left, right + horizon),
[
#text(
size: 7.5pt,
weight: "bold",
fill: theme-color(theme, "muted"),
)[
#organization — #descriptor
]
],
image(
logo,
height: 8mm,
fit: "contain",
),
)
v(3pt)
line(
length: 100%,
stroke: 0.5pt + theme-color(theme, "line"),
)
}
#let report-footer(theme, report) = {
let id = dict-get(report, "id", none)
grid(
columns: (1fr, auto),
align: (left, right),
text(
size: 7pt,
fill: theme-color(theme, "muted"),
)[
#if id != none {
[Report #id]
}
],
text(
size: 7pt,
fill: theme-color(theme, "muted"),
)[
Page #context counter(page).display()
],
)
}
// -----------------------------------------------------------------------------
// ----- Main model
// -----------------------------------------------------------------------------
#let report-model(
report: (:),
theme: (:),
logo: "/assets/beffroi.png",
body,
) = {
set page(
paper: "a4",
margin: (
top: 20mm,
bottom: 18mm,
left: 20mm,
right: 20mm,
),
header: context report-header(theme, report, logo),
footer: context report-footer(theme, report),
)
set text(
font: dict-get(theme, "font", "DejaVu Sans"),
size: 9.5pt,
fill: theme-color(theme, "ink"),
lang: "en",
)
set par(
justify: false,
leading: 0.58em,
spacing: 0.55em,
)
// ---------------------------------------------------------------------------
// Title block
// ---------------------------------------------------------------------------
let title = dict-get(report, "title", "Report")
let subtitle = dict-get(report, "subtitle", none)
let report-type = dict-get(report, "report_type", none)
let status = dict-get(report, "status", none)
let severity = dict-get(report, "severity", none)
block(
width: 100%,
below: 12pt,
)[
#text(
size: 22pt,
weight: "bold",
fill: theme-color(theme, "accent"),
)[
#title
]
#if subtitle != none {
v(3pt)
text(
size: 11pt,
fill: theme-color(theme, "muted"),
)[
#subtitle
]
}
#if report-type != none or status != none or severity != none {
v(7pt)
if report-type != none {
badge(
report-type-label(report-type),
color: theme-color(theme, "accent"),
)
h(4pt)
}
if status != none {
badge(
status,
color: severity-color(theme, status),
)
}
if status != none and severity != none {
h(4pt)
}
if severity != none {
badge(
severity,
color: severity-color(theme, severity),
)
}
}
]
// ---------------------------------------------------------------------------
// Metadata
// ---------------------------------------------------------------------------
let metadata = dict-get(report, "metadata", (:))
if metadata.len() > 0 {
block(
width: 100%,
below: 7pt,
inset: 8pt,
fill: theme-color(theme, "paper_soft"),
stroke: 0.5pt + theme-color(theme, "line"),
radius: 2pt,
)[
#grid(
columns: (1fr, 1fr),
row-gutter: 6pt,
column-gutter: 10pt,
..metadata
.pairs()
.map(pair => metadata-line(
theme,
pair.at(0),
pair.at(1),
))
)
]
}
// ---------------------------------------------------------------------------
// Executive summary
// ---------------------------------------------------------------------------
let summary = dict-get(report, "summary", none)
if summary != none {
section-title(theme, "Executive summary")
callout(
theme,
[#as-content(summary)],
kind: dict-get(report, "severity", "info"),
)
}
// ---------------------------------------------------------------------------
// Timeline
// ---------------------------------------------------------------------------
let timeline = dict-get(report, "timeline", ())
if timeline.len() > 0 {
section-title(theme, "Timeline")
render-timeline(theme, timeline)
}
// ---------------------------------------------------------------------------
// Findings
// ---------------------------------------------------------------------------
let findings = dict-get(report, "findings", ())
if findings.len() > 0 {
section-title(theme, "Findings")
render-findings(theme, findings)
}
// ---------------------------------------------------------------------------
// Diagnostic hypotheses
// ---------------------------------------------------------------------------
let diagnostics = dict-get(report, "diagnostics", (:))
let hypotheses = dict-get(diagnostics, "hypotheses", ())
if hypotheses.len() > 0 {
section-title(theme, "Diagnostic hypotheses")
render-diagnostics(theme, diagnostics)
}
// ---------------------------------------------------------------------------
// Arbitrary sections
// ---------------------------------------------------------------------------
let sections = dict-get(report, "sections", ())
for section in sections {
section-title(
theme,
as-content(
dict-get(section, "title", "Section"),
),
)
for block-data in dict-get(section, "content", ()) {
render-block(theme, block-data)
v(0.45em)
}
}
// ---------------------------------------------------------------------------
// Action plan
// ---------------------------------------------------------------------------
let actions = dict-get(report, "actions", ())
if actions.len() > 0 {
section-title(
theme,
dict-get(report, "actions_title", "Action plan"),
)
render-actions(theme, actions)
}
// ---------------------------------------------------------------------------
// Closing state
// ---------------------------------------------------------------------------
let closing = dict-get(report, "closing", none)
if closing != none {
section-title(theme, "Current state")
callout(
theme,
[#as-content(closing)],
title: "Stopping point",
kind: dict-get(report, "severity", "info"),
breakable: false,
)
}
}