-- 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 = ' | ' .. subtitle .. ""
end
local logo_html = ""
local running_logo = ""
if logo_uri ~= nil then
logo_html = ''
running_logo = '
'
elseif logo_path ~= nil and logo_path ~= "" then
local src = escape_html(logo_path)
logo_html = '
'
running_logo = '
'
end
-- Logo first (left), title text second (right) — matches product_test_template.typ
local title_block = string.format([[
%s%s
%s