模块:World topic

local p = {}

local Navbox = require("Module:Navbox")
local country = {
	["阿布哈兹"] = {""},
	["阿富汗"] = {""},
	["阿尔巴尼亚"] = {""},
	["阿尔及利亚"] = {""},
	["安道尔"] = {""},
	["安哥拉"] = {""},
	["安提瓜和巴布达"] = {""},
	["阿根廷"] = {""},
	["亚美尼亚"] = {""},
	["澳大利亚"] = {""},
	["奥地利"] = {""},
	["阿塞拜疆"] = {""},
	["巴哈马"] = {""},
	["巴林"] = {""},
	["孟加拉"] = {""},
	["巴巴多斯"] = {""},
	["白俄罗斯"] = {""},
	["比利时"] = {""},
	["伯利兹"] = {""},
	["贝宁"] = {""},
	["不丹"] = {""},
	["玻利维亚"] = {""},
	["波黑"] = {"波斯尼亚和黑塞哥维那"},
	["博茨瓦纳"] = {""},
	["巴西"] = {""},
	["文莱"] = {""},
	["保加利亚"] = {""},
	["布基纳法索"] = {""},
	["布隆迪"] = {""},
	["柬埔寨"] = {""},
	["喀麦隆"] = {""},
	["加拿大"] = {""},
	["佛得角"] = {""},
	["中非"] = {"中非共和国"},
	["乍得"] = {""},
	["智利"] = {""},
	["中国"] = {"中华人民共和国"},
	["哥伦比亚"] = {""},
	["科摩罗"] = {""},
	["刚果民主共和国"] = {""},
	["刚果共和国"] = {""},
	["库克群岛"] = {""},
	["哥斯达黎加"] = {""},
	["科特迪瓦"] = {""},
	["克罗地亚"] = {""},
	["古巴"] = {""},
	["塞浦路斯"] = {""},
	["捷克"] = {"捷克共和国"},
	["丹麦"] = {""},
	["吉布提"] = {""},
	["多米尼克"] = {""},
	["多米尼加共和国"] = {""},
	["厄瓜多尔"] = {""},
	["埃及"] = {""},
	["萨尔瓦多"] = {""},
	["赤道几内亚"] = {""},
	["厄立特里亚"] = {""},
	["爱沙尼亚"] = {""},
	["史瓦帝尼"] = {""},
	["埃塞俄比亚"] = {""},
	["斐济"] = {""},
	["芬兰"] = {""},
	["法国"] = {""},
	["加蓬"] = {""},
	["冈比亚"] = {""},
	["格鲁吉亚"] = {""},
	["德国"] = {""},
	["加纳"] = {""},
	["希腊"] = {""},
	["格林纳达"] = {""},
	["危地马拉"] = {""},
	["几内亚"] = {""},
	["几内亚比绍"] = {""},
	["圭亚那"] = {""},
	["海地"] = {""},
	["洪都拉斯"] = {""},
	["匈牙利"] = {""},
	["冰岛"] = {""},
	["印度"] = {""},
	["印度尼西亚"] = {""},
	["伊朗"] = {""},
	["伊拉克"] = {""},
	["爱尔兰"] = {""},
	["以色列"] = {""},
	["意大利"] = {""},
	["牙买加"] = {""},
	["日本"] = {""},
	["约旦"] = {""},
	["哈萨克斯坦"] = {""},
	["肯尼亚"] = {""},
	["基里巴斯"] = {""},
	["科索沃"] = {""},
	["朝鲜"] = {"朝鲜民主主义人民共和国"},
	["韩国"] = {"大韩民国"},
	["科威特"] = {""},
	["吉尔吉斯斯坦"] = {""},
	["老挝"] = {""},
	["拉脱维亚"] = {""},
	["黎巴嫩"] = {""},
	["莱索托"] = {""},
	["利比里亚"] = {""},
	["利比亚"] = {""},
	["列支敦士登"] = {""},
	["立陶宛"] = {""},
	["卢森堡"] = {""},
	["马达加斯加"] = {""},
	["马拉维"] = {""},
	["马来西亚"] = {""},
	["马尔代夫"] = {""},
	["马里"] = {""},
	["马耳他"] = {""},
	["马绍尔群岛"] = {""},
	["毛里塔尼亚"] = {""},
	["毛里求斯"] = {""},
	["墨西哥"] = {""},
	["密克罗尼西亚联邦"] = {""},
	["摩尔多瓦"] = {""},
	["摩纳哥"] = {""},
	["蒙古"] = {""},
	["黑山"] = {""},
	["摩洛哥"] = {""},
	["莫桑比克"] = {""},
	["缅甸"] = {""},
	["纳米比亚"] = {""},
	["瑙鲁"] = {""},
	["尼泊尔"] = {""},
	["荷兰"] = {""},
	["新西兰"] = {""},
	["尼加拉瓜"] = {""},
	["尼日尔"] = {""},
	["尼日利亚"] = {""},
	["纽埃"] = {""},
	["北塞浦路斯"] = {""},
	["北马其顿"] = {""},
	["挪威"] = {""},
	["阿曼"] = {""},
	["巴基斯坦"] = {""},
	["帕劳"] = {""},
	["巴勒斯坦"] = {"巴勒斯坦国"},
	["巴拿马"] = {""},
	["巴布亚新几内亚"] = {""},
	["巴拉圭"] = {""},
	["秘鲁"] = {""},
	["菲律宾"] = {""},
	["波兰"] = {""},
	["葡萄牙"] = {""},
	["德涅斯特河沿岸"] = {""},
	["卡塔尔"] = {""},
	["罗马尼亚"] = {""},
	["俄罗斯"] = {""},
	["卢旺达"] = {""},
	["圣基茨和尼维斯"] = {""},
	["圣卢西亚"] = {""},
	["圣文森特和格林纳丁斯"] = {""},
	["萨摩亚"] = {""},
	["圣马力诺"] = {""},
	["圣多美和普林西比"] = {""},
	["沙特阿拉伯"] = {""},
	["塞内加尔"] = {""},
	["塞尔维亚"] = {""},
	["塞舌尔"] = {""},
	["塞拉利昂"] = {""},
	["新加坡"] = {""},
	["斯洛伐克"] = {""},
	["斯洛文尼亚"] = {""},
	["所罗门群岛"] = {""},
	["索马里"] = {""},
	["索马里兰"] = {""},
	["南非"] = {""},
	["南奥塞梯"] = {""},
	["南苏丹"] = {""},
	["西班牙"] = {""},
	["斯里兰卡"] = {""},
	["苏丹"] = {""},
	["苏里南"] = {""},
	["瑞典"] = {""},
	["瑞士"] = {""},
	["叙利亚"] = {""},
	["台湾"] = {"中华民国"},
	["塔吉克斯坦"] = {""},
	["坦桑尼亚"] = {""},
	["泰国"] = {""},
	["东帝汶"] = {""},
	["多哥"] = {""},
	["汤加"] = {""},
	["特立尼达和多巴哥"] = {""},
	["突尼斯"] = {""},
	["土耳其"] = {""},
	["土库曼斯坦"] = {""},
	["图瓦卢"] = {""},
	["乌干达"] = {""},
	["乌克兰"] = {""},
	["阿联酋"] = {"阿拉伯联合酋长国"},
	["英国"] = {""},
	["美国"] = {""},
	["乌拉圭"] = {""},
	["乌兹别克斯坦"] = {""},
	["瓦努阿图"] = {""},
	["梵蒂冈"] = {""},
	["委内瑞拉"] = {""},
	["越南"] = {""},
	["西撒哈拉"] = {"撒拉威阿拉伯民主共和国"},
	["也门"] = {""},
	["赞比亚"] = {""},
	["津巴布韦"] = {""},
}

function p.main(frame)
	local pframe = frame:getParent()
	local config = frame.args
	local args = pframe.args
	
	return p._main(args)
end

function p._main(args)
	if mw.title.getCurrentTitle() == "Template:World topic" then
		state = "autocollapse"
	else
		state = args.state
	end
	if args[1] ~= nil or args.prefix ~= nil and (args[2] == nil or ags.suffix == nil) then
		titlemid = ""
	end
	if args.title ~= nil then
		title = args.title
	else
		title = "[["..(args[1] or args.prefix or "")..(titlemid or "").."世界"..(args.suffix or "").."]]"
	end
	if args.group1 ~= nil and args.group1 ~= "" then
		group1 = args.group1
	end
	local sorter = {}
	for n in pairs(country) do
		table.insert(sorter, n)
	end
	table.sort(sorter)

	-- parse link overrides, e.g. |link_United_States=Foo sets override["United States"] = "Foo"
	local override = {}
	for k,v in pairs(args) do
		local country = mw.ustring.match(k, "^link_(.*)")
		if country then
			country = mw.ustring.gsub(country, "_", " ")
			override[country] = v
		end
	end
	
	local redlinks = args.noredlinks == nil
	local list1 = table.concat(links(args[1], args[2], args.prefix, args.suffix, sorter, redlinks, override) )
	local navarguments = {
		name = args.name or "World topic",
		state = state,
		navbar = args.navbar or "Tnavbar",
		border = args.border,
		title = title,
		image = args.image,
		titlestyle = args.titlestyle,
		bodystyle = args.bodystyle,
		abovestyle = args.abovestyle,
		belowstyle = args.belowstyle,
		groupstyle = args.groupstyle,
		liststyle = args.liststyle,
		listclass = "hlist",
		above = args.above,
		group1 = group1,
		list1 = list1
	}
	return Navbox._navbox(navarguments)
end

function links(args1, args2, argsprefix, argssuffix, sorter, redlinks, override)
	local list = {}
	local prefix = (args1 or argsprefix or "") .. " "
	local suffix = args2 or argssuffix or ""
	for x, y in pairs(sorter) do
		local target = nil
		if redlinks then
			if override[y] then
				target = override[y]
			elseif y == "Georgia" or y == "Palestine" then
				if mw.title.new(prefix..country[y][1]..suffix).exists then
					target = prefix..country[y][1]..suffix
				else
					target = prefix..y..suffix
				end
			else
				target = prefix..linktarget(y, country[y][1])..suffix
			end
		else -- noredlinks
			if override[y] then
				if mw.title.new(override[y]).exists then
					target = override[y]
				end
			elseif mw.title.new(prefix..linktarget(y, country[y][1])..suffix).exists then
				target = prefix..linktarget(y, country[y][1])..suffix
			elseif y == "Georgia" or y == "Palestine" then
				if mw.title.new(prefix..y..suffix).exists then
					target = prefix..y..suffix
				end
			end
		end
		if target then
			table.insert(list, li("[["..target.."|"..y.."]]" ) )
		end
	end
	return list
end

function li(text)
	local li = mw.html.create("li")
	li
		:wikitext(text)
		:done()
	return tostring(li)
end

function linktarget(x, y)
	if y ~= nil and y == "" then
		return x
	else
		return y
	end
end

return p