モジュールの解説[作成]
require('strict')

local getArgs = require('Module:Arguments').getArgs
local list = require('Module:List')
local cd = require('Module:CountryData')
local ca = require('Module:Country alias').main
local iso3166code = require('Module:ISO 3166').luacode
local yesno = require('Module:Yesno')

local p = {}

local knownargs = {
	['format'] = true,
	['use_flag'] = true,
	['class'] = true,
	['style'] = true,
	['label_style'] = true,
	['list_style'] = true,
	['item_style'] = true,
	['item1_style'] = true,
	['indent'] = true
}

local labels = {
	['NA']  = "[[北アメリカ|NA]]",
	['EU']  = "[[ヨーロッパ|EU]]",
	['EUR'] = "[[ヨーロッパ|EU]]",
	['AU']  = "[[オーストララシア|AU]]",
	['AUS'] = "[[オーストララシア|AU]]",
	['PAL'] = "[[:en:PAL region|PAL]]",
	['SEA'] = "[[東南アジア|SEA]]",
	['AS']  = "[[アジア|AS]]",
	['SA']  = "[[南アメリカ|SA]]",
	['OC']  = "[[オセアニア|OC]]",
	['WW']  = "<abbr title=\"全世界\">WW</abbr>",
	['INT'] = "<abbr title=\"全世界\">WW</abbr>"
}

local function getLocalLabel(alias)
	return labels[alias]
end

local countryData = {}; -- Used to store country data to avoid the need of repeated calls to Module:CountryData. This saves a little time if the same abbreviation appears multiple times in the template.

local function getCountryData(frame, alias)
	local ualias = string.upper(alias)

	if (countryData[ualias] == nil) then
		local cdtable = cd.gettable(frame, alias, {})
		countryData[ualias] = cdtable['alias']
	end

	return countryData[ualias]
end

local function splitLabel(s)
	local islist = true
	local res = {}
	for k,v in ipairs(mw.text.split(s or '', '%s*/%s*')) do
		local v1 = v:match('^%s*([A-Z][A-Z][A-Z]?)%s*$')
		if v1 then
			table.insert(res,v1)
		else
			local v2 = v:match('^%s*(%[%[[^%[%]|]*|[A-Z][A-Z][A-Z]?%]%])%s*$')
			if v2 then
				table.insert(res,v2)
			else
				islist = false
			end
		end
	end
	return islist and res or {s}
end

-- spanタグで梱包したテキストを返す
-- @param text spanタグ内に挿入するテキスト
-- @param style spanタグのスタイルシート
-- @return (string)spanタグで梱包したテキストを返す
local function span(text, style)
	return "<span style=\"" .. (style or "font-size:95%;") .. "\">" .. text .. "</span>"
end

-- 国コード(ISO3166-2)から旗ファイルを返す
-- @param alias 国コード(ISO3166-2)
-- @return (string)旗ファイルを返す、無ければ空文字を返す
local cacheFlagFile = {}
local function getFlagFile(alias)
	if (alias == nil or alias == "") then
		return ""
	end

	-- aliasにINTまたはWWが有るときは世界の旗を返す、その他を無視する(iso3166codeで探しても見つからない為)。
	if (alias == "INT" or alias == "WW") then
		return "Perlshaper orthographic example1.svg"
	end

	if (cacheFlagFile[alias] == nil) then
		local aliascode = iso3166code({
			[1] = alias,
			['codetype'] = "3"
		})

		if (aliascode ~= "") then
			local status, result = pcall(ca, {
				['args'] = {
					['alias'] = aliascode,
					['flag'] = true
				}
			})
			if status then
				cacheFlagFile[alias] = result
				return result
			end
		end

		cacheFlagFile[alias] = ""
		return ""
	else
		return cacheFlagFile[alias]
	end
end

function p.main(frame)
	local args = getArgs(frame)
	local listformat = args['format']
	if (listformat == nil or listformat == "") then
		listformat = "unbulleted"
	end
	local useflag = yesno(args['use_flag'], false)
	if (useflag == nil) then
		useflag = false
	end
	local items = {}

	-- Old syntax "Two parameter region" use case, where param 1 is an article, param 2 is a label, and param 3 is the date. We assume this case if argument 4 is nil.
	if (args[3] ~= nil and args[4] == nil) then
		local item = "[["
		if (args[1] ~= nil) then
			item = item .. args[1]
		end
		item = item .. "|"
		if (args[2] ~= nil) then
			item = item .. args[2]
		end
		item = item .. "]]:"
		item = span(item) .. " " .. args[3] .. "[[Category:Pages using vgrelease with two parameter region]]"
		table.insert(items, item)
		-- Old syntax "Blank region" use case, where param 1 is empty, and param 2 is the date.
	elseif (args[1] == nil and args[2] ~= nil) then
		local item = args[2] .. "[[Category:Pages using vgrelease without a region]]"
		table.insert(items, item)
		-- Normal use cases, region/date pairs in 1/2, 3/4, 5/6, etc.
	else
		local i = 1
		local j = 2
		while (args[i] and args[j]) do
			local labels = {}
			for k,v in ipairs(splitLabel(args[i])) do
				local uv = string.upper(v)
				local label
				local locallabel = getLocalLabel(uv)
				local flagfile = useflag and getFlagFile(uv) or ""

				local _countrydata
				if not v:match('^%s*%[') then
					_countrydata = getCountryData(frame, v)
				end

				if (flagfile ~= "") then
					if (uv == "INT" or uv == "WW") then
						label = "世界"
					elseif (_countrydata ~= nil) then
						label = _countrydata
					else
						label = ""
					end

					label = "[[File:" .. flagfile .. "|border|25x20px|" .. label .. "の旗|link=" .. label .. "]]"
				elseif (locallabel ~= nil) then
					label = locallabel
				elseif (_countrydata ~= nil) then
					label = "[[" .. _countrydata .. "|" .. v .. "]]"
				else
					label = v
				end

				table.insert(labels, label)
			end
			local item = span( table.concat(labels, '/')) .. " " .. args[j]
			table.insert(items, item)

			i = i + 2
			j = j + 2
		end
	end

	-- Add known parameters of Module:List to the table
	for k, v in pairs(args) do
		if (knownargs[k] == true) then
			items[k] = v
		end
	end

	local out = list.makeList(listformat, items)

	-- Preview message and category
	local parameterMsg = require('Module:If preview')._warning({
		'不明なパラメーター "_VALUE_".'
	}) .. "[[Category:Pages using vgrelease with named parameters|_VALUE_]]"

	-- Check for invalid parameters	
	for k, v in pairs(args) do
		if (type(k) ~= 'number' and knownargs[k] ~= true) then
			local msg = parameterMsg:gsub('_VALUE_', k)
			out = out .. msg
		end
	end

	return out
end

return p