bavbavhaus.net/pandoc/links.lua

32 lines
864 B
Lua
Raw Normal View History

2024-04-12 22:30:16 +02:00
-- local l = require("pandoc.logging")
2024-05-02 20:11:20 +02:00
local read = pandoc.read
2024-04-12 22:30:16 +02:00
2024-05-02 20:11:20 +02:00
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
2024-04-12 22:30:16 +02:00
end
2024-05-02 20:11:20 +02:00
local visited = {}
local function find_links (fp)
local f = assert(io.open(fp), "\n" ..
"[ERROR] could not open " .. fp .. " for reading.")
2024-04-12 22:30:16 +02:00
local data = f:read("a")
f:close()
2024-05-02 20:11:20 +02:00
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 })
2024-04-12 22:30:16 +02:00
end
2024-05-02 20:11:20 +02:00
for i = should_follow and 2 or 1, #arg do
find_links(arg[i])
2024-04-12 22:30:16 +02:00
end