Switch PDF generation from Typst to WeasyPrint to match typst-templates layout.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-18 14:48:29 +03:30
parent 6159e46da8
commit e3980c5777
10 changed files with 725 additions and 58 deletions

View File

@@ -1,5 +1,6 @@
-- Keep YAML author for Typst/PDF (sidebar + document metadata),
-- but strip it from HTML so the page does not show more than the PDF body.
-- Keep YAML author/title for print-chrome.lua injection,
-- but strip Quarto's visible title block (we render our own for PDF;
-- screen readers still get the document <title>).
function Meta(meta)
if quarto.doc.is_format("html") then
meta.author = nil
@@ -7,3 +8,25 @@ function Meta(meta)
end
return meta
end
function Pandoc(doc)
if not quarto.doc.is_format("html") then
return doc
end
-- Drop Pandoc title-block header nodes if present in blocks
local out = {}
for _, block in ipairs(doc.blocks) do
local skip = false
if block.t == "Header" and block.level == 1 and doc.meta.title ~= nil then
local title = pandoc.utils.stringify(doc.meta.title)
if pandoc.utils.stringify(block.content) == title then
skip = true
end
end
if not skip then
table.insert(out, block)
end
end
doc.blocks = out
return doc
end

209
filters/print-chrome.lua Normal file
View File

@@ -0,0 +1,209 @@
-- Inject typst-templates-style title block + sidebar into HTML for WeasyPrint PDF.
-- Screen CSS hides .print-only; print.css shows them.
local function stringify(v)
if v == nil then
return ""
end
if type(v) == "string" then
return v
end
return pandoc.utils.stringify(v)
end
local function escape_html(s)
s = stringify(s)
s = s:gsub("&", "&amp;")
s = s:gsub("<", "&lt;")
s = s:gsub(">", "&gt;")
s = s:gsub('"', "&quot;")
return s
end
local function meta_list_join(meta_val, sep)
if meta_val == nil then
return ""
end
if pandoc.utils.type(meta_val) == "List" then
local parts = {}
for _, item in ipairs(meta_val) do
local t = stringify(item)
if t ~= "" then
table.insert(parts, t)
end
end
return table.concat(parts, sep or " - ")
end
return stringify(meta_val)
end
local function author_names(meta)
if meta.author == nil then
return ""
end
local authors = meta.author
if pandoc.utils.type(authors) ~= "List" then
authors = { authors }
end
local names = {}
for _, a in ipairs(authors) do
if type(a) == "table" and a.name ~= nil then
table.insert(names, stringify(a.name))
else
local n = stringify(a)
if n ~= "" then
table.insert(names, n)
end
end
end
return table.concat(names, " - ")
end
local function path_exists(path)
if path == nil or path == "" then
return false
end
local f = io.open(path, "rb")
if f then
f:close()
return true
end
return false
end
local function resolve_logo_path(logo)
if logo == nil or logo == "" then
return nil
end
if path_exists(logo) then
return logo
end
local candidates = {}
if quarto.project ~= nil and quarto.project.directory ~= nil then
table.insert(candidates, pandoc.path.join({ quarto.project.directory, logo }))
end
if quarto.doc ~= nil and quarto.doc.input_file ~= nil then
local input_dir = pandoc.path.directory(quarto.doc.input_file)
table.insert(candidates, pandoc.path.join({ input_dir, logo }))
end
local cwd = pandoc.system.get_working_directory()
if cwd ~= nil then
table.insert(candidates, pandoc.path.join({ cwd, logo }))
end
for _, candidate in ipairs(candidates) do
if path_exists(candidate) then
return candidate
end
end
return logo
end
local function file_to_data_uri(path)
if path == nil or path == "" then
return nil
end
local f = io.open(path, "rb")
if not f then
return nil
end
local data = f:read("*a")
f:close()
if data == nil or data == "" then
return nil
end
local ok, encoded = pcall(pandoc.pipe, "base64", { "-w0" }, data)
if not ok or encoded == nil or encoded == "" then
ok, encoded = pcall(pandoc.pipe, "base64", {}, data)
if ok and encoded then
encoded = encoded:gsub("%s+", "")
end
end
if not ok or encoded == nil or encoded == "" then
return nil
end
local mime = "image/png"
if path:match("%.jpe?g$") then
mime = "image/jpeg"
elseif path:match("%.svg$") then
mime = "image/svg+xml"
end
return "data:" .. mime .. ";base64," .. encoded
end
function Meta(meta)
if not quarto.doc.is_format("html") then
return meta
end
local company = escape_html(meta.company or "کیمیا رازی")
local title = escape_html(meta.title or "")
local subtitle = escape_html(meta.subtitle or "")
local logo_path = resolve_logo_path(stringify(meta.logofile or "assets/logo.png"))
local logo_uri = file_to_data_uri(logo_path)
local destinations = escape_html(meta_list_join(meta.destinations, " - "))
local purpose = stringify(meta.purpose)
local report_date = escape_html(meta["report-date"] or meta.date or "")
local authors = escape_html(author_names(meta))
local subtitle_html = ""
if subtitle ~= "" then
subtitle_html = ' <span class="subtitle">| ' .. subtitle .. "</span>"
end
local logo_html = ""
local running_logo = ""
if logo_uri ~= nil then
logo_html = '<img class="logo" src="' .. logo_uri .. '" alt="" />'
running_logo = '<img src="' .. logo_uri .. '" alt="" />'
elseif logo_path ~= nil and logo_path ~= "" then
local src = escape_html(logo_path)
logo_html = '<img class="logo" src="' .. src .. '" alt="" />'
running_logo = '<img src="' .. src .. '" alt="" />'
end
-- Logo first (left), title text second (right) — matches product_test_template.typ
local title_block = string.format([[
<div class="print-title-block print-only">
%s
<div class="title-text">
<p class="company-line">%s%s</p>
<p class="report-title">%s</p>
</div>
</div>
]], logo_html, company, subtitle_html, title)
-- Rotated sidebar labels; page chip comes from print.css @page @bottom-left.
local sidebar = string.format([[
<div class="print-sidebar print-only" aria-hidden="true">
<div class="seg">%s</div>
<div class="seg">%s</div>
</div>
]], destinations, title)
local running = string.format([[
<div class="print-running-header print-only" aria-hidden="true">
%s
<span>%s</span>
<span class="pipe">|</span>
<span>%s</span>
</div>
]], running_logo, company, subtitle)
local travel = ""
if purpose ~= nil and purpose ~= "" then
travel = string.format([[
<div class="print-travel-meta print-only">
<hr class="rule" />
<h2>اطلاعات کلی</h2>
<div class="meta-row"><div class="meta-label">مقصد:</div><div class="meta-value">%s</div></div>
<div class="meta-row"><div class="meta-label">هدف مأموریت:</div><div class="meta-value">%s</div></div>
<div class="meta-row"><div class="meta-label">تیم اعزامی:</div><div class="meta-value">%s</div></div>
<div class="meta-row"><div class="meta-label">تاریخ:</div><div class="meta-value">%s</div></div>
<hr class="rule" />
</div>
]], destinations, escape_html(purpose), authors, report_date)
end
quarto.doc.include_text("before-body", running .. sidebar .. title_block .. travel)
return meta
end

View File

@@ -1,24 +1,5 @@
-- Render ::: {.summary-box} as a Typst block matching HTML table chrome.
-- ::: {.summary-box} is styled by CSS for HTML/PDF (WeasyPrint).
-- No AST rewrite needed.
function Div(el)
if not el.classes:includes("summary-box") then
return nil
end
if not quarto.doc.is_format("typst") then
return nil
end
local inner = pandoc.write(pandoc.Pandoc(el.content), "typst")
inner = inner:gsub("^%s+", ""):gsub("%s+$", "")
local typst = string.format([[
#block(
width: 100%%,
inset: 12pt,
radius: 8pt,
stroke: 0.5pt + rgb("#d0d7de"),
fill: rgb("#f6f8fa"),
)[
%s
]
]], inner)
return pandoc.RawBlock("typst", typst)
return nil
end