Module:TNTExpandByCountries

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

{{#invoke:TNTExpandByCountries|main|Asia|lang=en|template=CUR Country}} expands to:

{{Translatable template|CUR Country|Afghanistan}}
{{Translatable template|CUR Country|Armenia}}
{{Translatable template|CUR Country|Azerbaijan}}
{{Translatable template|CUR Country|Bahrain}}
[...]

Intended to be used in Translations:Commons:Currency/Asia/3/en and other similar pages.

  • |lang= determines the sort order.
  • |simple= will passed to Module:Countries for generating the list of countries. If not present each group of countries will be shown in a separate section.
  • |hlevel= the section heading level for each group (used only when |simple= is not present).

Code

local countries_main = require('Module:Countries').main

local function translatable(country, template, ttemp)
	if country:sub(1, 4) == 'the ' then
		country = country:sub(5)
	end
	local tt = ttemp == 'no' and '' or 'Translatable template|'
	return '{{' .. tt .. template .. '|' .. country .. '}}\n'
end

local function simple(template, countries_result)
	local text = ''
	for country in countries_result:gmatch('%[%[(.-)|<bdi>') do
		text = text .. translatable(country, template)
	end
	return text
end

local function grouped(template, countries_result, eqs, ttemp)
	local text = ''
	for index, group in ipairs(mw.text.split(countries_result, '<em><bdi>', true)) do
		if index > 1 then  -- the first group contains no useful info
			text = text .. eqs .. group:match('(.-)</bdi>') .. eqs .. '\n'
			for country in group:gmatch('%[%[(.-)|') do
				text = text .. translatable(country, template, ttemp)
			end
		end
	end
	return text
end

local function main(frame)
	local args = frame.args
	args.mode = 'fop'
	local countries_result = countries_main(frame)
	local text
	if args.simple then
		text = simple(args.template, countries_result)
	else
		local hlevel = tonumber(args.hlevel) or 2
		local eqs = string.rep('=', hlevel)
		text = grouped(args.template, countries_result, eqs, args.ttemp)
	end
	return frame:preprocess(text)
end

return {
	main = main,
}