Module:Sandbox/Johnuniq

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:Sandbox/Johnuniq/doc

Code

-- List country names from Module:Countries/REGION and compare them with
-- what Module:TNTExpandByCountries needs.
-- Could possibly include this in Module:Countries.

local function checkTemplate(title, qid)
	title = 'Template:' .. title
	local t = mw.title.new(title)
	if t then
		local content = t:getContent()
		if content then
			local expected = '{{Label|' .. qid .. '|'
			if expected == content:sub(1, #expected) then
				content = 'good'
			else
				content = mw.text.nowiki(content:gsub('<noinclude>.-</noinclude>', ''))
			end
			return content
		end
	end
	return 'Error: could not read [[' .. title .. ']]'
end

local function list(frame)
	local regions = {
		'Africa',
		'Americas',
		'Arab world',
		'Asia',
		'Caribbean',
		'Central America',
		'Europe',
		'European Union',
		'North America',
		'North America (subcontinent)',
		'Oceania',
		'South America',
		'United Kingdom',
	}
	local lines = {}
	local function output(line)
		lines[#lines+1] = line
	end
	for _, region in ipairs(regions) do
		local moduleTitle = 'Module:Countries/' .. region
		local data = require(moduleTitle)
		local infos = data.infos or data.countries
		if type(infos) ~= 'table' then
			error('Error: no countries info for ' .. region)
		end
		local codes = {}
		for code, _ in pairs(infos) do
			codes[#codes+1] = code
		end
		table.sort(codes)
		output('== ' .. region .. ' ==')
		output('*[[' .. moduleTitle .. ']]')
		output('{| class="wikitable"')
		output('! Code !! Name1 !! qid !! Template:Name1')
		for _, code in ipairs(codes) do
			local info = infos[code]
			local name1 = info[1] or '(missing)'
			local content = checkTemplate(name1, info.qid)
			output('|-')
			output('| ' .. code .. ' || ' .. name1 .. ' || ' .. info.qid .. ' || ' .. content)
		end
		output('|}')
		output('')
	end
	return table.concat(lines, '\n')
end

return {
	list = list,
	main = list,
}