From ec34785d2c6127062218cb914bc2e47d2ae6c4c2 Mon Sep 17 00:00:00 2001 From: urosm Date: Fri, 21 Jun 2024 15:29:42 +0200 Subject: [PATCH] update `pandoc` data dir --- pandoc/defaults/bavbavhaus.net.part.yaml | 8 ++-- pandoc/defaults/bavbavhaus.net.yaml | 11 +++-- pandoc/dependencies.lua | 46 ------------------- pandoc/filters/collect_internal_linked.lua | 31 +++++++++++++ pandoc/filters/insert_parts.lua | 22 --------- pandoc/filters/resolve_internal_links.lua | 28 +++++++++++ .../filters/resolve_internal_links.part.lua | 28 +++++++++++ pandoc/filters/update_internal_targets.lua | 16 ------- 8 files changed, 97 insertions(+), 93 deletions(-) delete mode 100644 pandoc/dependencies.lua create mode 100644 pandoc/filters/collect_internal_linked.lua delete mode 100644 pandoc/filters/insert_parts.lua create mode 100644 pandoc/filters/resolve_internal_links.lua create mode 100644 pandoc/filters/resolve_internal_links.part.lua delete mode 100644 pandoc/filters/update_internal_targets.lua diff --git a/pandoc/defaults/bavbavhaus.net.part.yaml b/pandoc/defaults/bavbavhaus.net.part.yaml index 7894ca0..37177a2 100644 --- a/pandoc/defaults/bavbavhaus.net.part.yaml +++ b/pandoc/defaults/bavbavhaus.net.part.yaml @@ -3,9 +3,9 @@ from: markdown toc: true citeproc: true csl: chicago-fullnote-sl -filters: - - localize_quotes.lua - - update_internal_targets.lua template: bavbavhaus.net.part.html5 -to: html +to: html5 +filters: +- localize_quotes.lua +- resolve_internal_links.part.lua ... diff --git a/pandoc/defaults/bavbavhaus.net.yaml b/pandoc/defaults/bavbavhaus.net.yaml index 0a73554..551ac0a 100644 --- a/pandoc/defaults/bavbavhaus.net.yaml +++ b/pandoc/defaults/bavbavhaus.net.yaml @@ -3,10 +3,11 @@ from: markdown toc: true citeproc: true csl: chicago-fullnote-sl -filters: - - localize_quotes.lua - - update_internal_targets.lua - - insert_parts.lua +reference-location: section template: bavbavhaus.net.html5 -to: html +to: html5 +filters: +- localize_quotes.lua +- collect_internal_linked.lua +- resolve_internal_links.lua ... diff --git a/pandoc/dependencies.lua b/pandoc/dependencies.lua deleted file mode 100644 index e840f35..0000000 --- a/pandoc/dependencies.lua +++ /dev/null @@ -1,46 +0,0 @@ -assert(#arg == 2, "\n" .. - "[ERROR] usage: pandoc lua bavbavhaus.lua ") -local index_file, output_file = table.unpack(arg) - -local read = pandoc.read - -NOTE_PATTERN = "^((.+)%.md)(#?.*)$" -PAGE_REPL = "public_html/%2.html" -PART_REPL = "public_html/%2.html.part" - -local all = "all" -local makefile = { [all] = {} } -local function collect (fp) - local f = assert(io.open(fp), "\n" .. - "[ERROR] could not open " .. fp .. " for reading.") - local data = f:read("a") - f:close() - - local page = fp:gsub(NOTE_PATTERN, PAGE_REPL) - table.insert(makefile[all], page) - makefile[page] = {} - - read(data):walk({ Link = function (link) - local fp, _slug, _anchor = link.target:match(NOTE_PATTERN) - if not fp then return end - local f = io.open(fp) - if f == nil then return else f:close() end - local target_page = link.target:gsub(NOTE_PATTERN, PAGE_REPL) - if makefile[target_page] then return end - local target_part = link.target:gsub(NOTE_PATTERN, PART_REPL) - table.insert(makefile[page], target_part) - collect(fp) - end }) -end -collect(index_file) - -local lines = {} -for k, v in pairs(makefile) do - local parts = table.concat(v, " ") - table.insert(lines, ("%s: %s"):format(k, parts)) -end - -local f = assert(io.open(output_file, "w"), "\n" .. - "[ERROR] could not open " .. output_file .. " for writing.") -f:write(table.concat(lines, "\n")) -f:close() diff --git a/pandoc/filters/collect_internal_linked.lua b/pandoc/filters/collect_internal_linked.lua new file mode 100644 index 0000000..71c6b4e --- /dev/null +++ b/pandoc/filters/collect_internal_linked.lua @@ -0,0 +1,31 @@ +SCRIPT_NAME = "collect_internal_linked.lua" +os.setlocale("C") + +local RawBlock = pandoc.RawBlock +local parts = pandoc.MetaList({}) + +return { + { Link = function(link) + -- early return for external links + if link.target:find("^https?%:%/%/") then return end + if link.target:find("^mailto%:") then return end + + local fp, anchor = link.target:match("^(.+%.md)#?(.*)$") + -- early return for non markdown files + if not fp then return end + + local f = io.open(fp) + -- early return for broken internal links + if f == nil then return link.content else f:close() end -- @todo maybe log broken internal links + + local part_fp = fp:gsub("^(.+)%.md$", "public_html/%1.html.part") + local f = assert(io.open(part_fp), "\n" .. + "[ERROR] could not open " .. part_fp .. " for reading.") + local rawblock = RawBlock(FORMAT, f:read("a")); f:close() + parts:insert(rawblock) + end }, + { Meta = function (meta) + meta["parts"] = parts + return meta + end } +} diff --git a/pandoc/filters/insert_parts.lua b/pandoc/filters/insert_parts.lua deleted file mode 100644 index 225216f..0000000 --- a/pandoc/filters/insert_parts.lua +++ /dev/null @@ -1,22 +0,0 @@ -SCRIPT_NAME = "insert_parts.lua" -os.setlocale("C") - -local MetaList = pandoc.MetaList -local RawBlock = pandoc.RawBlock - -return { - { Meta = function (meta) - if not meta["parts"] then return end - if type(meta["parts"]) == "string" then - meta["parts"] = MetaList({ meta["parts"] }) - end - meta["parts"] = meta["parts"]:map(function (item) - local f = assert(io.open(item), "\n" .. - "[ERROR] could not open " .. item .. " for reading.") - local raw_block = RawBlock("html5", f:read("a")) - f:close() - return raw_block - end) - return meta - end } -} diff --git a/pandoc/filters/resolve_internal_links.lua b/pandoc/filters/resolve_internal_links.lua new file mode 100644 index 0000000..72e9219 --- /dev/null +++ b/pandoc/filters/resolve_internal_links.lua @@ -0,0 +1,28 @@ +SCRIPT_NAME = "resolve_internal_links.lua" +os.setlocale("C") + +local Inlines = pandoc.Inlines + +return { + { Link = function(link) + -- early return for external links + if link.target:find("^https?%:%/%/") then return end + if link.target:find("^mailto%:") then return end + + local fp, slug, anchor = link.target:match("^((.+)%.md)#?(.*)$") + -- early return for non markdown files + if not fp then return end + + local f = io.open(fp) + -- early return for broken internal links + if f == nil then return link.content else f:close() end -- @todo maybe log broken internal links + + local internal_link = link:clone() + internal_link.target = ("#%s%s"):format(slug, anchor) + + link.content = "↪" + link.target = ("%s.html#%s"):format(slug, anchor) + + return Inlines({ internal_link, link }) + end } +} diff --git a/pandoc/filters/resolve_internal_links.part.lua b/pandoc/filters/resolve_internal_links.part.lua new file mode 100644 index 0000000..c94b7fb --- /dev/null +++ b/pandoc/filters/resolve_internal_links.part.lua @@ -0,0 +1,28 @@ +SCRIPT_NAME = "resolve_internal_links.lua" +os.setlocale("C") + +local Inlines = pandoc.Inlines + +local page_slug + +return { + { Meta = function(meta) + page_slug = meta["slug"] + end }, + { Link = function(link) + -- early return for external links + if link.target:find("^https?%:%/%/") then return end + if link.target:find("^mailto%:") then return end + + local fp, slug, anchor = link.target:match("^((.+)%.md)#?(.*)$") + -- early return for non markdown files + if not fp then return end + + local f = io.open(fp) + -- early return for broken internal links + if f == nil then return link.content else f:close() end -- @todo maybe log broken internal links + + link.target = ("%s.html#%s%s"):format(page_slug, slug, anchor) + return link + end } +} diff --git a/pandoc/filters/update_internal_targets.lua b/pandoc/filters/update_internal_targets.lua deleted file mode 100644 index a74ee05..0000000 --- a/pandoc/filters/update_internal_targets.lua +++ /dev/null @@ -1,16 +0,0 @@ -SCRIPT_NAME = "update_internal_targets.lua" -os.setlocale("C") - -NOTE_PATTERN = "^((.+)%.md)(#?.*)$" -LINK_REPL = "%2.html%3" - -return { - { Link = function (link) - local fp, _slug, _anchor = link.target:match(NOTE_PATTERN) - if not fp then return end - local f = io.open(fp) - if f == nil then return link.content else f:close() end - link.target = link.target:gsub(NOTE_PATTERN, LINK_REPL) - return link - end } -}