bavbavhaus.net/dependencies.lua

35 lines
1.0 KiB
Lua

assert(#arg == 1, "\n" ..
"[ERROR] usage: pandoc lua dependencies.lua <input-file>")
local input_file = arg[1]
local f = assert(io.open(input_file), "\n" ..
"[ERROR] could not open " .. input_file .. " for reading.")
local data = f:read("a")
f:close()
local base, _ = pandoc.path.split_extension(input_file)
io.write(("public_html/%s.html:"):format(base))
local prerequisites = {}
pandoc.read(data):walk({ Link = function(link)
-- early return for external links
if link.target:find("^https?%:%/%/") then return end
if link.target:find("^mailto%:") then return end
local fp, anchor = link.target:match("^(.+%.md)#?(.*)$")
-- early return for non markdown files
if not fp then return end
local f = io.open(fp)
-- early return for broken internal links
if f == nil then return else f:close() end
-- early return for already collected
if prerequisites[fp] then return end
prerequisites[fp] = true
local base, _ = pandoc.path.split_extension(fp)
io.write(" ", ("public_html/%s.html.part"):format(base))
end })