Module:Sandbox/KleptomaniacViolet/Language families/Autotree

local p = {}

function get_node (title) local title_data = mw.loadData("Module:Sandbox/KleptomaniacViolet/Language families/Data") local result = nil for n, page in pairs(title_data) do		tab = mw.loadData(page).language_nodes if tab[title] ~= nil then result = tab[title] end end return result end

function get_first_node_data (target_node_title, fallback_node_title, page_title, errors) if fallback_node_title ~= "" and page_title == target_node_title then table.insert(errors, "redundant fallback") end if page_title ~= target_node_title then table.insert(errors, "node title mismatch") end

local node_data = get_node(target_node_title) if node_data == nil then if fallback_node_title ~= "" then node_data = get_node(fallback_node_title) end if node_data == nil then table.insert(errors, "no node data") else table.insert(errors, "using fallback") end end return node_data end

function show_errors (frame, errors) local res = "" for i, err in ipairs(errors) do res = res .. " " .. frame:expandTemplate{ title = "User:KleptomaniacViolet/Language family errors/" .. err, args = {} } end return res end

function p.gen_tree (frame) local errors = {} local target_node_title = frame.args["targetnode"] local expected_parent_article = frame.args["expectedparentarticle"] if expected_parent_article == "" then expected_parent_article = nil end local fallback_node_title = frame.args["fallbacknode"] local page_title = frame.args["pagetitle"] local node_data = get_first_node_data(target_node_title, fallback_node_title, page_title, errors) local nodes = {}

local count = 0 while node_data ~= nil do		display = node_data['display'] link = node_data['link'] if link ~= nil then wikitext =  .. mw.text.nowiki(display) ..  else wikitext = mw.text.nowiki(display) end table.insert(nodes, 1, wikitext) target_node_title = node_data['parent'] if target_node_title == nil then node_data = nil elseif count > 30 then node_data = nil table.insert(errors, "tree too deep") else count = count + 1 node_data = get_node(target_node_title) if expected_parent_article ~= nil then if node_data == nil then table.insert(errors, "no parent found but one was expected") elseif node_data['link'] ~= nil then if node_data['title'] ~= expected_parent_article then table.insert(errors, "expected parent mistmatch") end expected_parent_article = nil end end end end local result = "" for n, text in ipairs(nodes) do result = result .. '' if n == #nodes then result = result .. "'''" .. text .. "'''"		else result = result .. text end result = result .. "\n" end for n = 1, #nodes do result = result .. "" end return result .. "\n" .. show_errors(frame, errors) end

function p.gen_children (frame) local errors = {} local target_node_title = frame.args["targetnode"] local fallback_node_title = frame.args["fallbacknode"] local page_title = frame.args["pagetitle"] local node_data = get_first_node_data(target_node_title, fallback_node_title, page_title, errors)

local child_titles = {} if node_data ~= nil then child_titles = node_data['children'] end local children = {} for n, child_title in ipairs(child_titles) do		node_data = get_node(child_title) if node_data ~= nil then display = node_data['display'] link = node_data['link'] if link ~= nil then wikitext =  .. mw.text.nowiki(display) ..  else wikitext = mw.text.nowiki(display) end table.insert(children, 1, wikitext) end end local result = "" for n, text in ipairs(children) do result = result .. "* " .. text .. "\n" end return result .. "\n" .. show_errors(frame, errors) end

function p.get_parent_article (frame) local target_node_title = frame.args[1] local node_data = get_node(target_node_title) local res = nil local count = 0 while node_data ~= nil and res == nil and count < 30 do		count = count + 1 if node_data['link'] ~= nil then res = node_data['title'] end if node_data['parent'] == nil then node_data = nil else node_data = get_node(node_data['parent']) end end if res == nil then res = "" end return res end

return p