bavbavhaus.net/pandoc/bavbavhaus.net.lua

235 lines
6.5 KiB
Lua

local path = pandoc.path
local MetaList = pandoc.MetaList
local Link = pandoc.Link
INPUT_FILES = (function(prog)
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)
local insert = table.insert
local output_files = {}
for _,filename in ipairs(INPUT_FILES) do
local basename = path.split_extension(filename)
insert(output_files, (pattern):format(basename))
end
return output_files
end)("public_html/%s.html")
print("[INFO] bavbavhaus.net:")
do
for i,input_file in ipairs(INPUT_FILES) do
print(" " .. INPUT_FILES[i] .. " -> " .. OUTPUT_FILES[i])
end
end
print("[INFO] bavbavhaus.net: reading pages")
PAGES = (function()
local input_format = "markdown+wikilinks_title_before_pipe"
local reader_options = pandoc.ReaderOptions({})
local pages = MetaList({})
function pages:get(input_file)
return self:find_if(function(page)
return page["meta"]["input_file"] == input_file
end)
end
for i,input_file in ipairs(INPUT_FILES) do
local f = io.open(input_file)
if not f then goto continue end
local page = pandoc.read(f:read("*a"), input_format, reader_options)
f:close()
pages:insert(page)
::continue::
end
return pages
end)()
print("[INFO] bavbavhaus.net: updating metadata (initial)")
do
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 pandoc.Inlines(pandoc.Str(input_file))
return meta
end,
})
end
end
print("[INFO] bavbavhaus.net: scanning pages for internal links")
BACKLINKS, FORELINKS = (function(pages)
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)
print("[INFO] bavbavhaus.net: updating index page")
do
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
end
print("[INFO] bavbavhaus.net: updating link targets")
do
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
end
print("[INFO] bavbavhaus.net: generating sitemap")
SITEMAP = (function()
local sitemap = MetaList({})
for _,page in ipairs(PAGES) do
local meta = page["meta"]
sitemap:insert(Link(meta["title"], meta["url"]))
end
return sitemap
end)()
print("[INFO] bavbavhaus.net: processing citations")
do
local csl = "pandoc/csl/chicago-fullnote-sl"
for i,page in ipairs(PAGES) do
PAGES[i] = page:walk({
Meta = function(meta)
meta["csl"] = csl
return meta
end,
Pandoc = function(doc)
return pandoc.utils.citeproc(doc)
end
})
end
end
print("[INFO] bavbavhaus.net: updating metadata (sitemap)")
do
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: writing partial pages")
function get_template(path)
local f = io.open(path)
if not f then return end
local template = f:read("*a")
f:close()
return pandoc.template.compile(template)
end
local write = pandoc.write
local output_format = "html5"
local WriterOptions = pandoc.WriterOptions
local template = get_template("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
local backlink_page = PAGES:get(backlink)
local writer_options = WriterOptions({
identifier_prefix = "back/" .. backlink,
template = template
})
local text = pandoc.write(backlink_page, output_format, writer_options)
backlinks:insert(RawBlock("html5", text))
end
PAGES[i] = page:walk({
Meta = function(meta)
meta["backlinks"] = backlinks
return meta
end
})
::continue::
end
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
local forelink_page = PAGES:get(forelink)
local writer_options = WriterOptions({
identifier_prefix = "fore/" .. forelink,
template = template
})
local text = pandoc.write(forelink_page, output_format, writer_options)
forelinks:insert(RawBlock("html5", text))
end
PAGES[i] = page:walk({
Meta = function(meta)
meta["forelinks"] = forelinks
return meta
end
})
::continue::
end
print("[INFO] bavbavhaus.net: writing pages")
for _,page in ipairs(PAGES) do
local f = io.open(page["meta"]["output_file"], "w")
f:write(pandoc.write(page, output_format, WriterOptions({
template = get_template("pandoc/templates/bavbavhaus.net.html5")
})))
f:close()
end
end