assert(#arg == 2, "\n" .. "[ERROR] usage: pandoc lua bavbavhaus.lua ") local index_file, output_file = table.unpack(arg) local read = pandoc.read NOTE_PATTERN = "^((.+)%.md)(#?.*)$" PAGE_REPL = "public_html/%2.html" PART_REPL = "public_html/%2.html.part" local all = "all" local makefile = { [all] = {} } local function collect (fp) local f = assert(io.open(fp), "\n" .. "[ERROR] could not open " .. fp .. " for reading.") local data = f:read("a") f:close() local page = fp:gsub(NOTE_PATTERN, PAGE_REPL) table.insert(makefile[all], page) makefile[page] = {} read(data):walk({ Link = function (link) local fp, _slug, _anchor = link.target:match(NOTE_PATTERN) if not fp then return end local f = io.open(fp) if f == nil then return else f:close() end local target_page = link.target:gsub(NOTE_PATTERN, PAGE_REPL) if makefile[target_page] then return end local target_part = link.target:gsub(NOTE_PATTERN, PART_REPL) table.insert(makefile[page], target_part) collect(fp) end }) end collect(index_file) local lines = {} for k, v in pairs(makefile) do local parts = table.concat(v, " ") table.insert(lines, ("%s: %s"):format(k, parts)) end 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()