bavbavhaus.net/pandoc/bavbavhaus.net.lua

241 lines
7.5 KiB
Lua
Raw Normal View History

2024-03-17 16:49:51 +01:00
local path = pandoc.path
local MetaList = pandoc.MetaList
local Link = pandoc.Link
INPUT_FILES = (function(prog)
2024-03-18 13:15:14 +01:00
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
2024-03-17 16:49:51 +01:00
end)("ls -t *.md")
OUTPUT_FILES = (function(pattern)
2024-03-18 13:15:14 +01:00
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
2024-03-17 16:49:51 +01:00
end)("public_html/%s.html")
print("[INFO] bavbavhaus.net: reading pages")
PAGES = (function()
2024-03-18 13:15:14 +01:00
local pages = 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
2024-03-17 16:49:51 +01:00
end)()
2024-03-18 13:15:14 +01:00
print("[INFO] bavbavhaus.net: updating metadata")
2024-03-17 16:49:51 +01:00
do
2024-03-18 13:15:14 +01:00
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
2024-03-17 16:49:51 +01:00
end
print("[INFO] bavbavhaus.net: scanning pages for internal links")
BACKLINKS, FORELINKS = (function(pages)
2024-03-18 13:15:14 +01:00
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
2024-03-17 16:49:51 +01:00
end)(PAGES)
print("[INFO] bavbavhaus.net: updating index page")
do
2024-03-18 13:15:14 +01:00
local index = "index.md"
FORELINKS[index] = 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
2024-03-17 16:49:51 +01:00
end
print("[INFO] bavbavhaus.net: updating link targets")
do
2024-03-18 13:15:14 +01:00
function update_target(target)
return ("%s.html#start"):format(path.split_extension(target))
end
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
2024-03-17 16:49:51 +01:00
end
print("[INFO] bavbavhaus.net: processing citations")
do
2024-03-18 13:15:14 +01:00
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
2024-03-17 16:49:51 +01:00
end
2024-03-18 13:15:14 +01:00
SITEMAP = (function()
print("[INFO] bavbavhaus.net: generating sitemap")
local sitemap = pandoc.MetaList({})
for _,page in ipairs(PAGES) do
local meta = page["meta"]
sitemap:insert(Link(meta["title"], meta["url"]))
end
return sitemap
end)()
2024-03-17 16:49:51 +01:00
do
2024-03-18 13:15:14 +01:00
print("[INFO] bavbavhaus.net: updating metadata (sitemap)")
for i,page in ipairs(PAGES) do
PAGES[i] = page:walk({
Meta = function(meta)
meta["sitemap"] = SITEMAP
return meta
end
})
end
2024-03-17 16:49:51 +01:00
end
do
2024-03-18 13:15:14 +01:00
print("[INFO] bavbavhaus.net: writing partial pages")
local write = pandoc.write
local WriterOptions = pandoc.WriterOptions
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 RawBlock = pandoc.RawBlock
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::
2024-03-17 16:49:51 +01:00
end
2024-03-18 13:15:14 +01:00
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::
2024-03-17 16:49:51 +01:00
end
2024-03-18 13:15:14 +01:00
print("[INFO] bavbavhaus.net: writing pages")
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")
2024-03-17 16:49:51 +01:00
})
2024-03-18 13:15:14 +01:00
for _,page in ipairs(PAGES) do
local f = io.open(page["meta"]["output_file"], "w")
f:write(write(page, "html", wopts))
f:close()
end
2024-03-17 16:49:51 +01:00
end