local l = require("pandoc.logging") local read = pandoc.read assert(#arg == 2, "\n" .. "[ERROR] usage: pandoc lua sitemap.lua ") local node_list = {} local graph_map = {} local function collect (fp) local f = assert(io.open(fp), "\n" .. "[ERROR] could not open " .. fp .. " for reading.") table.insert(node_list, fp) graph_map[fp] = { order = #node_list, nodes = {} } local data = f:read("a") f:close() local visited = {} read(data):walk({ Link = function (link) local f = io.open(link.target) if f == nil then return else f:close() end if visited[link.target] then return end visited[link.target] = true table.insert(graph_map[fp]["nodes"], link.target) if not graph_map[link.target] then collect(link.target) end end }) end collect(arg[1]) local L = { SW = ".", L = "<", W = "-", NW = ".", S = "|", X = " ", N = "|", SE = "'", R = ">", E = "-", NE = "'", } local layout = {} local r_wing = {} local l_wing = {} for i,node in ipairs(node_list) do r_wing[i] = {} layout[i] = node l_wing[i] = {} end local function reverse (t) for i=1,#t//2 do t[i],t[#t-i+1] = t[#t-i+1],t[i] end return t end local function find_free_col(wing, a, b) local col = 1 while true do for i=a,b do local cell = wing[i][col] if cell ~= nil and cell ~= L["X"] then goto continue end end do return col end ::continue:: col = col + 1 end return col end for _,node in ipairs(node_list) do local a_node = graph_map[node] for _,node in ipairs(a_node["nodes"]) do local a = a_node["order"] local b = graph_map[node]["order"] local i1 = 1 local ia = math.min(a,b) local ib = math.max(a,b) local iz = #node_list local wing = a < b and r_wing or l_wing local r_first, l_first = true, true local first = a < b and r_first or l_first local col = find_free_col(wing, ia, ib) if col == 1 then col = 2 end local a_row = wing[ia] if a_row[1] == nil or a_row[1] == " " then a_row[1] = a < b and "-" or "<" end for i=1,col-1 do if a_row[i] == nil or a_row[i] == L["X"] then a_row[i] = "-" end end a_row[col] = "." for i=ia+1,ib-1 do for j=1,col-1 do if wing[i][j] == nil then wing[i][j] = " " end end wing[i][col] = "|" end local b_row = wing[ib] if b_row[1] == nil or b_row[1] == " " then b_row[1] = a < b and ">" or "-" end for i=1,col-1 do if b_row[i] == nil or b_row[i] == L["X"] then b_row[i] = "-" end end b_row[col] = "'" end end -- write local data = {} table.insert(data, "") local f = assert(io.open(arg[2], "w"), "\n" .. "[ERROR] could not open " .. arg[2] .. " for writing.") f:write(table.concat(data, "\n")) f:close()