Module:Navbox

local p = {}

local item = require('Module:Item').go local strsub = string.sub local split = mw.text.split local trim = mw.text.trim local match = string.match

local args local lang local l10n_table = { ["en"] = { title_v = "View this template", text_v = "V", title_d = "Discuss this template", text_d = "D", title_e = "Edit this template", text_e = "E", },	["fr"] = { title_v = "Voir ce modèle", title_d = "Discuter de ce modèle", title_e = "Modifier ce modèle", text_e = "M" },	["ja"] = { title_v = "このテンプレートを見る", text_v = "閲覧", title_d = "このテンプレートについて議論する", text_d = "議論", title_e = "このテンプレートを編集する", text_e = "編集" },	["pl"] = { title_v = "Pokaż ten szablon", text_v = "p", title_d = "Utwórz dyskusję dla tego szablonu", text_d = "d", title_e = "Edytuj ten szablon", text_e = "e", },	["pt"] = { title_v = "Ver essa predefinição", title_d = "Discutir essa predefinição", title_e = "Editar essa predefinição", },	["ru"] = { title_v = "Просмотр этого шаблона", text_v = "П", title_d = "Обсуждение этого шаблона", text_d = "О", title_e = "Править этот шаблон", text_e = "Р", },	["yue"] = { title_v = "睇呢個模版", text_v = "睇", title_d = "傾呢個模版", text_d = "傾", title_e = "改呢個模版", text_e = "改", },	["uk"] = { title_v = "Переглянути цей шаблон", text_v = "П", title_d = "Обговорити цей шаблон", text_d = "О", title_e = "Редагувати цей шаблон", text_e = "Р", },	["zh"] = { title_v = "查看此模板", text_v = "看", title_d = "讨论此模板", text_d = "论", title_e = "编辑此模板", text_e = "编", } }

-- l10n local function l10n(key) if l10n_table[lang] then return l10n_table[lang][key] or l10n_table["en"][key] end return l10n_table["en"][key] end

local function explode(div,str) if (div == '') then return false end local pos, arr = 0, {} for st,sp in function return string.find(str, div, pos, true) end do		arr[#arr + 1] = trim(string.sub(str, pos, st-1)) pos = sp + 1 end arr[#arr + 1] = trim(string.sub(str, pos)) return arr end

local function returnLink(link) local frame = mw.getCurrentFrame local result = "" if match(link, "%[%[.*%]%]") or match(link, "%{%{.*%}%}") then return link else local result = {} -- "link|text&file#size" local text = link text, result["size"] = unpack(explode("#", text)) text, result["image"] = unpack(explode("&", text)) result[1], result[2] = unpack(explode("|", text)) if not result[2] or result[2] == "" then result[2] = result[1] end result["lang"] = lang return item(frame, result) end end

local function renderList(list, header, paren) local result = ""	if header and header ~= "" then		result = result .. "" .. header .. " "	end	result = result .. ""	if paren then		result = result .. "("	end	-- slightly faster than ipairs	for i = 1, #list do		if type(list[i]) == "table" then			result = result .. "" .. renderList(list[i], nil, true) .. ""		else			result = result .. "" .. returnLink(list[i]) .. ""		end	end	if paren then		result = result .. ")"	end	result = result .. " "	return result end

local function renderH1(list) local result = " " .. list["h1"] .. " "	if list[1]["title"] then result = result .. "" for i = 1, #list do result = result .. renderList(list[i], list[i]["title"], false) end result = result .. " "	else result = result .. renderList(list, "", false) end return result .. " " end

local function vde if not args["template"] or args["template"] == "" then error("The template name is not defined!") end local result = {} result[1] = '[' .. tostring(mw.uri.fullUrl(mw.site.namespaces[10]["canonicalName"] .. ":" .. args["template"])) .. ' ' .. l10n('text_v') .. ' ]'	result[2] = '[' .. tostring(mw.uri.fullUrl(mw.site.namespaces[11]["canonicalName"] .. ":" .. args["template"])) .. ' ' .. l10n('text_d') .. ' ]'	result[3] = '[' .. tostring(mw.uri.fullUrl(mw.site.namespaces[828]["canonicalName"] .. ":Navbox/" .. args[1], "action=edit")) .. ' ' .. l10n('text_e') .. ' ]'	return ' ' .. table.concat(result, '&thinsp;•&thinsp;') .. ' ' end

local function renderHeader(list, isMain) local collapsed = "mw-collapsed" if (args[list["__metadata"]] and args[list["__metadata"]] ~= "") or (args["all"] and args["all"] ~= "") then collapsed = "" end local result = ""	if isMain then		result = result .. vde	end	if list["header"] and list["header"] ~= "" then		result = result .. "" .. list["header"] .. " "	end	result = result .. ""	if not list[1]["header"] then		result = result .. "<div class=\"table\">"	end	for i = 1, #list do		if list[i]["header"] then			result = result .. renderHeader(list[i], false)		elseif list[i]["h1"] then			result = result .. renderH1(list[i])		else			result = result .. renderList(list, nil, false)			break		end	end	if not list[1]["header"] then		result = result .. " "	end	result = result .. "  "	return result end

function p.main(frame) args = require('Dev:Arguments').getArgs(frame) lang = frame:expandTemplate{ title = "lang"} local data = require("Module:Navbox/" .. args[1]) return frame:preprocess(renderHeader(data, true)) end

return p