bavbavhaus.net/pandoc/links.lua

32 lines
864 B
Lua

-- local l = require("pandoc.logging")
local read = pandoc.read
assert(#arg > 0, "\n" ..
"[ERROR] usage: pandoc lua links.lua [--follow] <file>...")
local should_follow = arg[1] == "--follow" and true or false
local function file_exists (fp)
local f = io.open(fp)
if f ~= nil then f:close() return true else return false end
end
local visited = {}
local function find_links (fp)
local f = assert(io.open(fp), "\n" ..
"[ERROR] could not open " .. fp .. " for reading.")
local data = f:read("a")
f:close()
read(data):walk({ Link = function (link)
if not file_exists(link.target) then return end
if visited[link.target] then return end
visited[link.target] = true
io.write(link.target .. "\n")
if should_follow then find_links(link.target) end
end })
end
for i = should_follow and 2 or 1, #arg do
find_links(arg[i])
end