210 lines
5.8 KiB
Lua
210 lines
5.8 KiB
Lua
-- 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("&", "&")
|
|
s = s:gsub("<", "<")
|
|
s = s:gsub(">", ">")
|
|
s = s:gsub('"', """)
|
|
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
|