-- local l = require("pandoc.logging") local read, json = pandoc.read, pandoc.json assert(#arg > 1, "\n" .. "[ERROR] usage: pandoc lua links.lua ...") local destination_filepath = arg[1] local filepaths_arr = {} local filepaths_map = {} for i = 2, #arg do table.insert(filepaths_arr, arg[i]) filepaths_map[arg[i]] = {} end for _,filepath in ipairs(filepaths_arr) do local f = assert(io.open(filepath), "\n" .. "[ERROR] could not open " .. filepath) local data = f:read("a") f:close() local visited_map = {} read(data):walk({ Link = function (link) if not filepaths_map[link.target] then return end if visited_map[link.target] then return end table.insert(filepaths_map[filepath], link.target) visited_map[link.target] = true end }) end local data = json.encode(filepaths_map) local f = io.open(destination_filepath) if f == nil or f:read("a") ~= data then local f = assert(io.open(destination_filepath, "w"), "\n" .. "[ERROR] could not open " .. destination_filepath .. " for writing") f:write(data) f:close() end