From c761a53728e10725aa505d2dfec5521b0a37a0ae Mon Sep 17 00:00:00 2001 From: dschlueter Date: Tue, 6 Oct 2026 05:55:25 +0200 Subject: [PATCH] fix: Kasten-CSS des Pandoc-Filters wirkt auch mit -H und in EPUB-Kapiteln MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - CSS in pandoc/callouts.css (eine Quelle), vom Filter neben sich gelesen - HTML: -]] +-- callouts.css next to this filter is the single source of the box styles. +local function read_file(path) + local fh = io.open(path, "r") + if not fh then + return nil + end + local text = fh:read("a") + fh:close() + return text +end + +local CSS_PATH = (PANDOC_SCRIPT_FILE and PANDOC_SCRIPT_FILE:match("^(.*[/\\])") or "") .. "callouts.css" +local CSS = read_file(CSS_PATH) +if not CSS then + io.stderr:write("callouts.lua: " .. CSS_PATH .. " nicht gefunden – Kästen ohne Rahmen\n") +end + +-- EPUB: a stylesheet in metadata `css` replaces Pandoc's default epub.css, so combine both in a temp file. +local function epub_stylesheet() + local default = "" + local pipe = io.popen("pandoc --print-default-data-file epub.css 2>/dev/null") + if pipe then + default = pipe:read("a") or "" + pipe:close() + end + local path = os.tmpname() -- small temp file; Pandoc reads it after the filter has finished + local fh = io.open(path, "w") + if not fh then + return nil + end + fh:write(default, "\n", CSS) + fh:close() + return path +end local lang = "de" @@ -79,15 +104,23 @@ local function Meta(meta) lang = l end end - if is_html() then - local includes = meta["header-includes"] - if includes == nil then - includes = pandoc.MetaList({}) - elseif includes.t ~= "MetaList" then - includes = pandoc.MetaList({ includes }) + if CSS and FORMAT:match("epub") then + local css = meta.css + local path = nil + if css == nil then -- no own stylesheet: default epub.css + ours; otherwise just add ours + path = epub_stylesheet() end - includes:insert(pandoc.MetaBlocks({ pandoc.RawBlock("html", CSS) })) - meta["header-includes"] = includes + if css == nil then + css = pandoc.List({}) + elseif pandoc.utils.type(css) ~= "List" then + css = pandoc.List({ css }) + end + if path then + css:insert(pandoc.MetaString(path)) + else + css:insert(pandoc.MetaString(CSS_PATH)) + end + meta.css = css end return meta end @@ -126,7 +159,16 @@ local function Div(div) return pandoc.BlockQuote(div.content) end +-- HTML: the stylesheet goes into the body, because a command-line -H replaces header-includes from a filter. +local function Pandoc(doc) + if CSS and FORMAT:match("html") and not FORMAT:match("epub") then + doc.blocks:insert(1, pandoc.RawBlock("html", "")) + end + return doc +end + return { { Meta = Meta }, { BlockQuote = BlockQuote, Div = Div }, + { Pandoc = Pandoc }, }