local p = {}
local getArgs = require('Module:Arguments').getArgs
function p.main(frame)
local r = ""
local input = frame.args[1] or ""
local lines = {}
for line in mw.text.gsplit(input, '\n', true) do
table.insert(lines, line)
end
local root = {
name = "ROOT",
children = {},
depth = -1
}
local stack = {root}
for _, line in ipairs(lines) do
local stars, name = line:match("^(%**)%s*(.+)$")
if name then
local depth = stars and #stars or 0
local node = {
name = name,
children = {},
depth = depth
}
while #stack > 0 and stack[#stack].depth >= depth do
table.remove(stack)
end
table.insert(stack[#stack].children, node)
table.insert(stack, node)
end
end
local function render(node, prefix, isLast)
local connector = isLast and "└─ " or "├─ "
r = r .. "<br />" .. prefix .. connector .. node.name
local newPrefix = prefix .. (isLast and "\t" or "│\t")
for i, child in ipairs(node.children) do
render(child, newPrefix, i == #node.children)
end
end
local top = root.children[1]
if top then
r = top.name
for i, child in ipairs(top.children) do
render(child, "", i == #top.children)
end
end
return "<span style=\"tab-size:4\">" .. r .. "</span>"
end
return p