Module:Release

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
This is the main module for the following templates:
local p = {}
local h = {}

local Region = require("Module:Region")
local utilsArg = require("Module:UtilsArg")
local utilsMarkup = require("Module:UtilsMarkup")
local utilsTable = require("Module:UtilsTable")

local regionCodes = Region.enum()

function p.Main(frame)
	local args, err = utilsArg.parse(frame:getParent().args, p.Templates.Release)
	local categories = err and err.categoryText or ""
	
	-- List releases using the region flag, unless there is at least one release using a multi-flag (i.e. multi-country) region
	-- In that case, we use abbrevations instead of flags for consistency
	local hasMultiFlags = utilsTable.find(utilsTable.keys(args), function(code)
		local flags = Region.getRegion(code).flags
		return #flags > 1
	end)
	local releases = hasMultiFlags and h.getReleasesWithAbbrs(args) or h.getReleasesWithFlags(args)
	
	-- escape commas in the date format so that the year doesn't get rendered on a separate line in the infobox
	for i in ipairs(releases) do
		releases[i] = string.gsub(releases[i], ",", ",")
	end

	local list = utilsMarkup.list(releases)
	return list, categories
end
function h.getReleasesWithFlags(args)
	local releases = {}
	for i, code in ipairs(regionCodes) do
		local releaseDate = args[code]
		if releaseDate then
			local region = Region.getRegion(code, { flagTooltip = "name" })
			local flag = region.flags[1]
			local releaseWithFlag = string.format("%s %s", flag, releaseDate)
			table.insert(releases, releaseWithFlag)
		end
	end
	return releases
end

function h.getReleasesWithAbbrs(args)
	local releases = {}
	local regionCodesByAbbr = Region.enum({ sortBy = "abbr" })
	for i, code in ipairs(regionCodesByAbbr) do
		local releaseDate = args[code]
		if releaseDate then
			local region = Region.getRegion(code)
			local abbr = region.abbr
			if region.name == "United Kingdom" then -- for UK the tooltip is "United Kingdom" and the abbreviation is "GBR" which doesn't line up - needs a bit more explaining
				abbr = mw.getCurrentFrame():expandTemplate({
					title = "Exp",
					args = {"United Kingdom (Great Britain)", "GBR"}
				})
			end
			local releaseWithAbbr = string.format("'''%s''': %s", abbr, releaseDate)
			table.insert(releases, releaseWithAbbr)
		end
	end
	return releases
end


regionCodes.reference = nil
local params = {}
for i, code in ipairs(regionCodes) do
	params[code] = {
		type = "date",
		desc = "Release date in "..Region.getRegion(code).name,
		trim = true,
		nilIfEmpty = true,
	}
end
local countryCodes, multiCountryCodes = utilsTable.partition(regionCodes, function(str)
	return str:len() == 2
end)
p.Templates = {
	["Release"] = {
		format = "block",
		indent = 1,
		paramOrder = utilsTable.concat(countryCodes, multiCountryCodes),
		params = params,
		boilerplate = {
			tabs = {
				{
					label = "Countries",
					params = countryCodes,
				},
				{
					label = "Multi-Country Regions",
					params = multiCountryCodes,
				},
			}
		}
	}
}

return p