From 2674a4cd12c9571174dd1f715008d004898832c5 Mon Sep 17 00:00:00 2001 From: urosm Date: Fri, 12 Apr 2024 22:30:16 +0200 Subject: [PATCH] update `pandoc` data dir --- pandoc/bavbavhaus.net.lua | 249 ------------------ pandoc/defaults/bavbavhaus.net.yaml | 8 + pandoc/filters/delink.lua | 5 +- pandoc/filters/insert_links.lua | 38 +++ pandoc/filters/insert_sitemap.lua | 22 ++ pandoc/filters/insert_url.lua | 15 ++ pandoc/filters/localize-quotes.lua | 31 +++ pandoc/filters/update_internal_targets.lua | 32 +++ pandoc/links.lua | 42 +++ pandoc/sitemap.lua | 23 ++ pandoc/templates/bavbavhaus.net.html5 | 31 +-- ...tial.html5 => bavbavhaus.net.inline.html5} | 4 +- 12 files changed, 224 insertions(+), 276 deletions(-) delete mode 100644 pandoc/bavbavhaus.net.lua create mode 100644 pandoc/defaults/bavbavhaus.net.yaml create mode 100644 pandoc/filters/insert_links.lua create mode 100644 pandoc/filters/insert_sitemap.lua create mode 100644 pandoc/filters/insert_url.lua create mode 100644 pandoc/filters/localize-quotes.lua create mode 100644 pandoc/filters/update_internal_targets.lua create mode 100644 pandoc/links.lua create mode 100644 pandoc/sitemap.lua rename pandoc/templates/{bavbavhaus.net.partial.html5 => bavbavhaus.net.inline.html5} (63%) diff --git a/pandoc/bavbavhaus.net.lua b/pandoc/bavbavhaus.net.lua deleted file mode 100644 index d697086..0000000 --- a/pandoc/bavbavhaus.net.lua +++ /dev/null @@ -1,249 +0,0 @@ -INPUT_FILES = (function(prog) - print("[INFO] bavbavhaus.net: collecting input files") - local insert = table.insert - local input_files = {} - local pfile = io.popen(prog) - for filename in pfile:lines() do - insert(input_files, filename) - end - pfile:close() - return input_files -end)("ls -t *.md") - -OUTPUT_FILES = (function(pattern) - print("[INFO] bavbavhaus.net: collecting output files") - local split_extension = pandoc.path.split_extension - local insert = table.insert - local output_files = {} - for _,filename in ipairs(INPUT_FILES) do - local basename = split_extension(filename) - insert(output_files, (pattern):format(basename)) - end - return output_files -end)("public_html/%s.html") - -PAGES = (function() - print("[INFO] bavbavhaus.net: reading pages") - local pages = pandoc.MetaList({}) - function pages:get(input_file) - return self:find_if(function(page) - return page["meta"]["input_file"] == input_file - end) - end - local read = pandoc.read - local ropts = pandoc.ReaderOptions({}) - for _,input_file in ipairs(INPUT_FILES) do - print("[INFO] bavbavhaus.net: reading page: " .. input_file) - local f = io.open(input_file) - if not f then goto continue end - pages:insert(read(f:read("a"), "markdown", ropts)) - f:close() - ::continue:: - end - return pages -end)() - -do - print("[INFO] bavbavhaus.net: updating pages metadata") - for i,page in ipairs(PAGES) do - local input_file = INPUT_FILES[i] - local output_file = OUTPUT_FILES[i] - PAGES[i] = page:walk({ - Meta = function(meta) - meta["input_file"] = input_file - meta["output_file"] = output_file - meta["url"] = input_file - meta["title"] = meta["title"] or input_file - return meta - end, - }) - end -end - -do - print("[INFO] bavbavhaus.net: processing citations") - local csl = "pandoc/csl/chicago-fullnote-sl" - local citeproc = pandoc.utils.citeproc - for i,page in ipairs(PAGES) do - PAGES[i] = page:walk({ - Meta = function(meta) - meta["csl"] = csl - return meta - end, - Pandoc = function(doc) - return citeproc(doc) - end - }) - end -end - -BACKLINKS, FORELINKS = (function(pages) - print("[INFO] bavbavhaus.net: scanning pages for internal links") - local MetaList = pandoc.MetaList - local MetaMap = pandoc.MetaMap - local backlinks = MetaMap({}) - local forelinks = MetaMap({}) - for i,page in ipairs(pages) do - local input_file = page["meta"]["input_file"] - page:walk({ - Link = function(link) - if not PAGES:get(link.target) then return end - if input_file == link.target then return end - if not backlinks[link.target] then - backlinks[link.target] = MetaList({}) - end - if not forelinks[input_file] then - forelinks[input_file] = MetaList({}) - end - if not backlinks[link.target]:includes(input_file) then - backlinks[link.target]:insert(input_file) - end - if not forelinks[input_file]:includes(link.target) then - forelinks[input_file]:insert(link.target) - end - end - }) - end - return backlinks, forelinks -end)(PAGES) - -do - print("[INFO] bavbavhaus.net: updating index page") - local index = "README.md" - FORELINKS[index] = pandoc.MetaList({}) - for i,page in ipairs(PAGES) do - if PAGES[i]["meta"]["input_file"] == index then goto continue end - FORELINKS[index]:insert(page["meta"]["input_file"]) - ::continue:: - end -end - -do - print("[INFO] bavbavhaus.net: updating link targets") - local split_extension = pandoc.path.split_extension - function update_target(target) - return ("%s.html#start"):format(split_extension(target)) - end - local Link = pandoc.Link - for i,page in ipairs(PAGES) do - PAGES[i] = page:walk({ - Meta = function(meta) - meta["url"] = update_target(meta["url"]) - return meta - end, - Link = function(link) - if not PAGES:get(link.target) then return end - link.target = update_target(link.target) - return Link(link.content, link.target) - end - }) - end -end - -SITEMAP = (function() - print("[INFO] bavbavhaus.net: generating sitemap") - local sitemap = pandoc.MetaList({}) - local Link = pandoc.Link - for _,page in ipairs(PAGES) do - local meta = page["meta"] - sitemap:insert(Link(meta["title"], meta["url"])) - end - return sitemap -end)() - -do - print("[INFO] bavbavhaus.net: updating sitemap metadata") - for i,page in ipairs(PAGES) do - PAGES[i] = page:walk({ - Meta = function(meta) - meta["sitemap"] = SITEMAP - return meta - end - }) - end -end - -do - print("[INFO] bavbavhaus.net: updating backlinks metadata") - local compile = pandoc.template.compile - local partial_template = (function(filepath) - local f = io.open(filepath) - if not f then return end - local template = f:read("a") - f:close() - return compile(template) - end)("pandoc/templates/bavbavhaus.net.partial.html5") - local MetaList = pandoc.MetaList - local RawBlock = pandoc.RawBlock - local write = pandoc.write - local WriterOptions = pandoc.WriterOptions - for i,page in ipairs(PAGES) do - local input_file = page["meta"]["input_file"] - if not BACKLINKS[input_file] then goto continue end - local backlinks = MetaList({}) - for _,backlink in ipairs(BACKLINKS[input_file]) do - backlinks:insert(RawBlock("html5", write( - PAGES:get(backlink), - "html5", - WriterOptions({ - identifier_prefix = "back/" .. backlink, - table_of_contents = true, - template = partial_template - }) - ))) - end - PAGES[i] = page:walk({ - Meta = function(meta) - meta["backlinks"] = backlinks - return meta - end - }) - ::continue:: - end - print("[INFO] bavbavhaus.net: updating forelinks metadata") - for i,page in ipairs(PAGES) do - local input_file = page["meta"]["input_file"] - if not FORELINKS[input_file] then goto continue end - local forelinks = MetaList({}) - for _,forelink in ipairs(FORELINKS[input_file]) do - forelinks:insert(RawBlock("html5", write( - PAGES:get(forelink), - "html5", - WriterOptions({ - identifier_prefix = "fore/" .. forelink, - table_of_contents = true, - template = partial_template - }) - ))) - end - PAGES[i] = page:walk({ - Meta = function(meta) - meta["forelinks"] = forelinks - return meta - end - }) - ::continue:: - end -end - -do - print("[INFO] bavbavhaus.net: writing pages") - local compile = pandoc.template.compile - local WriterOptions = pandoc.WriterOptions - local write = pandoc.write - local wopts = WriterOptions({ - table_of_contents = true, - template = (function(filepath) - local f = io.open(filepath) - if not f then return end - local template = f:read("a") - f:close() - return compile(template) - end)("pandoc/templates/bavbavhaus.net.html5") - }) - for i,page in ipairs(PAGES) do - local f = io.open(page["meta"]["output_file"], "w") - f:write(write(page, "html", wopts)) - f:close() - end -end diff --git a/pandoc/defaults/bavbavhaus.net.yaml b/pandoc/defaults/bavbavhaus.net.yaml new file mode 100644 index 0000000..97a2268 --- /dev/null +++ b/pandoc/defaults/bavbavhaus.net.yaml @@ -0,0 +1,8 @@ +--- +toc: true +citeproc: true +csl: chicago-fullnote-sl +filters: + - localize-quotes.lua + - update_internal_targets.lua +... diff --git a/pandoc/filters/delink.lua b/pandoc/filters/delink.lua index 84b0913..478f5bf 100644 --- a/pandoc/filters/delink.lua +++ b/pandoc/filters/delink.lua @@ -1,9 +1,10 @@ SCRIPT_NAME = "delink.lua" os.setlocale("C") + return { { - Link = function(elem) - return elem.content + Link = function (link) + return link.content end } } diff --git a/pandoc/filters/insert_links.lua b/pandoc/filters/insert_links.lua new file mode 100644 index 0000000..400fade --- /dev/null +++ b/pandoc/filters/insert_links.lua @@ -0,0 +1,38 @@ +local l = require("pandoc.logging") +local json = pandoc.json +local MetaMap = pandoc.MetaMap +local MetaList = pandoc.MetaList +local RawBlock = pandoc.RawBlock + +SCRIPT_NAME = "insert_links.lua" +os.setlocale("C") + +return { + { Meta = function (meta) + local input_filepath = PANDOC_STATE["input_files"][1] + local links_filepath = meta["links-file"] or "links.json" + local partials_dirpath = meta["partials-dir"] or "partials" + local f = assert(io.open(links_filepath), "\n" .. + "[ERROR] could not open " .. links_filepath) + local links = json.decode(f:read("a")) + f:close() + if not links[input_filepath] then return end + local prevlinks_meta = MetaList({}) + local nextlinks_meta = MetaList({}) + local links_meta = prevlinks_meta + for _,filepath in ipairs(links[input_filepath]) do + if filepath == json.null then + links_meta = nextlinks_meta + goto continue + end + local partial_filepath = filepath:gsub("^(.+)%.md$", partials_dirpath .. "/%1.html") + local f = assert(io.open(partial_filepath), "\n" .. + "[ERROR] could not open " .. partial_filepath) + links_meta:insert(RawBlock("html5", f:read("a"))) + ::continue:: + end + meta["prevlinks"] = prevlinks_meta + meta["nextlinks"] = nextlinks_meta + return meta + end } +} diff --git a/pandoc/filters/insert_sitemap.lua b/pandoc/filters/insert_sitemap.lua new file mode 100644 index 0000000..88c52e5 --- /dev/null +++ b/pandoc/filters/insert_sitemap.lua @@ -0,0 +1,22 @@ +-- local l = require("pandoc.logging") +local json = pandoc.json +local Link = pandoc.Link + +SCRIPT_NAME = "insert_sitemap.lua" +os.setlocale("C") + +return { + { Meta = function (meta) + local sitemap_filepath = meta["sitemap-file"] or "sitemap.json" + local f = assert(io.open(sitemap_filepath), "\n" .. + "[ERROR] could not open " .. sitemap_filepath) + local sitemap = json.decode(f:read("a")) + f:close() + local sitemap_meta = pandoc.MetaList({}) + sitemap:map(function (item) + sitemap_meta:insert(Link(item, item)) + end) + meta["sitemap"] = sitemap_meta + return meta + end } +} diff --git a/pandoc/filters/insert_url.lua b/pandoc/filters/insert_url.lua new file mode 100644 index 0000000..bcf21b7 --- /dev/null +++ b/pandoc/filters/insert_url.lua @@ -0,0 +1,15 @@ +-- local l = require("pandoc.logging") +local stringify = pandoc.utils.stringify +local Link = pandoc.Link + +SCRIPT_NAME = "insert_url.lua" +os.setlocale("C") + +return { + { Meta = function (meta) + local target = PANDOC_STATE["input_files"][1] + local title = stringify(meta["title"] or target) + meta["url"] = Link(title, target) + return meta + end } +} diff --git a/pandoc/filters/localize-quotes.lua b/pandoc/filters/localize-quotes.lua new file mode 100644 index 0000000..7ab274f --- /dev/null +++ b/pandoc/filters/localize-quotes.lua @@ -0,0 +1,31 @@ +SCRIPT_NAME = "localize-quotes.lua" +os.setlocale("C") + +QUOTEMARKS_LANGMAP = { + de = {'„', '“', '‚', '‘' }, + en = {'“', '”', '‘', '’' }, + it = {'«', '»', '“', '”' }, + sl = {'»', '«', '„', '“' }, +} + +QUOTEMARKS = nil + +return { + { Meta = function (meta) + local lang = pandoc.utils.stringify(meta["lang"] or "en") + QUOTEMARKS = QUOTEMARKS_LANGMAP[lang] + end }, + { Quoted = function (quoted) + if not QUOTEMARKS then return end + if quoted.quotetype == "DoubleQuote" then + quoted.content:insert(1, QUOTEMARKS[1]) + quoted.content:insert( QUOTEMARKS[2]) + return quoted.content + end + if quoted.quotetype == "SingleQuote" then + quoted.content:insert(1, QUOTEMARKS[3]) + quoted.content:insert( QUOTEMARKS[4]) + return quoted.content + end + end } +} diff --git a/pandoc/filters/update_internal_targets.lua b/pandoc/filters/update_internal_targets.lua new file mode 100644 index 0000000..6d928e7 --- /dev/null +++ b/pandoc/filters/update_internal_targets.lua @@ -0,0 +1,32 @@ +-- local l = require("pandoc.logging") +local json = pandoc.json + +SCRIPT_NAME = "update_internal_targets.lua" +os.setlocale("C") + +TARGET_PATTERN = { "^(.+)%.md$", "%1.html#start" } + +local function file_exists(filepath) + local f = io.open(filepath) + if f ~= nil then + f:close() + return true + else + return false + end +end + +return { + { Meta = function (meta) + if meta["url"] then + meta["url"] = meta["url"]:gsub(table.unpack(TARGET_PATTERN)) + end + return meta + end }, + { Link = function (link) + if link.target:find(TARGET_PATTERN[1]) then + link.target = link.target:gsub(table.unpack(TARGET_PATTERN)) + end + return link + end }, +} diff --git a/pandoc/links.lua b/pandoc/links.lua new file mode 100644 index 0000000..79ec187 --- /dev/null +++ b/pandoc/links.lua @@ -0,0 +1,42 @@ +-- local l = require("pandoc.logging") +local read, json = pandoc.read, pandoc.json + +assert(#arg > 1, "\n" .. + "[ERROR] usage: pandoc lua links.lua ...") +local destination_filepath +local input_filepaths = {} +local input_filepaths_keys = {} +for _, option in ipairs(arg) do + if destination_filepath == nil then + destination_filepath = option + else + table.insert(input_filepaths, option) + input_filepaths_keys[option] = { json.null } + end +end + +for _,filepath in ipairs(input_filepaths) do + local f = assert(io.open(filepath), "\n" .. + "[ERROR] could not open " .. filepath) + local data = f:read("a") + f:close() + local visited_map = {} + read(data):walk({ + Link = function (link) + if not input_filepaths_keys[link.target] then return end + if visited_map[link.target] then return end + table.insert(input_filepaths_keys[filepath], link.target) + table.insert(input_filepaths_keys[link.target], 1, filepath) + visited_map[link.target] = true + end + }) +end + +local data = json.encode(input_filepaths_keys) +local f = io.open(destination_filepath) +if f == nil or f:read("a") ~= data then + local f = assert(io.open(destination_filepath, "w"), "\n" .. + "[ERROR] could not open " .. destination_filepath .. " for writing") + f:write(data) + f:close() +end diff --git a/pandoc/sitemap.lua b/pandoc/sitemap.lua new file mode 100644 index 0000000..c290e3a --- /dev/null +++ b/pandoc/sitemap.lua @@ -0,0 +1,23 @@ +-- local l = require("pandoc.logging") +local json = pandoc.json + +assert(#arg > 1, "\n" .. + "[ERROR] usage: pandoc lua sitemap.lua ...") +local destination_filepath +local input_filepaths = {} +for _, option in ipairs(arg) do + if destination_filepath == nil then + destination_filepath = option + else + table.insert(input_filepaths, option) + end +end + +local data = json.encode(input_filepaths) +local f = io.open(destination_filepath) +if f == nil or f:read("a") ~= data then + local f = assert(io.open(destination_filepath, "w"), "\n" .. + "[ERROR] could not open " .. destination_filepath .. " for writing") + f:write(data) + f:close() +end diff --git a/pandoc/templates/bavbavhaus.net.html5 b/pandoc/templates/bavbavhaus.net.html5 index d6a02df..85275ce 100644 --- a/pandoc/templates/bavbavhaus.net.html5 +++ b/pandoc/templates/bavbavhaus.net.html5 @@ -13,46 +13,33 @@ $if(description-meta)$ $else$ $endif$ - bavbavhaus.net/$title$ + bavbavhaus.net/$pagetitle$
$if(sitemap)$ - $endif$ - $for(backlinks)$ -
diff --git a/pandoc/templates/bavbavhaus.net.partial.html5 b/pandoc/templates/bavbavhaus.net.inline.html5 similarity index 63% rename from pandoc/templates/bavbavhaus.net.partial.html5 rename to pandoc/templates/bavbavhaus.net.inline.html5 index f1fc984..f6d7bb9 100644 --- a/pandoc/templates/bavbavhaus.net.partial.html5 +++ b/pandoc/templates/bavbavhaus.net.inline.html5 @@ -1,6 +1,4 @@ -$if(title)$ -

$title$

-$endif$ +

$if(title)$$title$$else$$pagetitle$$endif$

$if(toc)$