bavbavhaus.net/pandoc/bavbavhaus.lua

54 lines
1.7 KiB
Lua
Raw Normal View History

2024-05-21 00:52:25 +02:00
local l = require("pandoc.logging")
local read = pandoc.read
assert(#arg == 2, "\n" ..
"[ERROR] usage: pandoc lua collect_deps.lua <index file> <output file")
local index_file, output_file = table.unpack(arg)
local f = assert(io.open(index_file), "\n" ..
"[ERROR] could not open " .. index_file .. " for reading.")
local data = f:read("a")
f:close()
local visited = {}
local lines = {}
local all_target = "all: "
local all_prereqs = {}
local sitemap_target = "public_html/sitemap.html.part: "
local sitemap_prereqs = {}
local function collect (fp)
local f = assert(io.open(fp), "\n" ..
"[ERROR] could not open " .. fp .. " for reading.")
visited[fp] = true
local target = fp:gsub("(.+)%.md", "public_html/%1.html")
table.insert(all_prereqs, target)
table.insert(sitemap_prereqs, fp)
local prereqs = { fp }
local data = f:read("a")
f:close()
read(data):walk({ Link = function (link)
if not link.target:find(".%.md$") then return end
local f = io.open(link.target)
if f == nil then return else f:close() end
if visited[link.target] then return end
local prereq = link.target:gsub("(.+)%.md", "public_html/%1.html.part")
table.insert(prereqs, prereq)
collect(link.target)
end })
table.insert(prereqs, "public_html/sitemap.html.part")
target = ("%s: "):format(target)
table.insert(lines, table.concat({ target, table.concat(prereqs, " ") }))
end
collect(arg[1])
table.insert(lines, 1, table.concat({ all_target, table.concat(all_prereqs, " ") }))
table.insert(lines, table.concat({ sitemap_target, table.concat(sitemap_prereqs, " ") }))
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()