update `pandoc` data dir

urosm 2024-04-12 22:30:16 +02:00
parent 123d764286
commit 2674a4cd12
12 changed files with 224 additions and 276 deletions

View File

@ -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

View File

@ -0,0 +1,8 @@
---
toc: true
citeproc: true
csl: chicago-fullnote-sl
filters:
- localize-quotes.lua
- update_internal_targets.lua
...

View File

@ -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
}
}

View File

@ -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 }
}

View File

@ -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 }
}

View File

@ -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 }
}

View File

@ -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 }
}

View File

@ -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 },
}

42
pandoc/links.lua 100644
View File

@ -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 <destination file> <input file>...")
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

23
pandoc/sitemap.lua 100644
View File

@ -0,0 +1,23 @@
-- local l = require("pandoc.logging")
local json = pandoc.json
assert(#arg > 1, "\n" ..
"[ERROR] usage: pandoc lua sitemap.lua <destination file> <input file>...")
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

View File

@ -13,46 +13,33 @@ $if(description-meta)$
$else$
<meta name="description" content="bavbavhaus.net" />
$endif$
<title>bavbavhaus.net/$title$</title>
<title>bavbavhaus.net/$pagetitle$</title>
<link rel="icon" href="favicon.svg" />
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<main>
$if(sitemap)$
<article>
<nav class="sitemap">
<h1><a href="/">bavbavhaus.net</a></h1>
<ul>
$for(sitemap)$
<li>$sitemap$</li>
<li>$it$</li>
$endfor$
</ul>
</nav>
</article>
$endif$
$for(backlinks)$
<article class="backlink">
$backlinks$
$for(prevlinks)$
<article class="prevlink">
$it$
</article>
$endfor$
<article id="start">
$if(title)$
<h1><a href="$url$">$title$</a></h1>
$endif$
$if(toc)$
<nav class="toc">
$if(toc-title)$
<h2>$toc-title$</h2>
$endif$
$table-of-contents$
</nav>
$endif$
$body$
${ bavbavhaus.net.inline.html5() }
</article>
$for(forelinks)$
<article class="forelink">
$forelinks$
$for(nextlinks)$
<article class="nextlink">
$it$
</article>
$endfor$
</main>

View File

@ -1,6 +1,4 @@
$if(title)$
<h1><a href="$url$">$title$</a></h1>
$endif$
<h1><a href="$url$">$if(title)$$title$$else$$pagetitle$$endif$</a></h1>
$if(toc)$
<nav class="toc">
$if(toc-title)$