Difference between revisions of "Module:Video game release"

From blackwiki
Jump to navigation Jump to search
blackwiki>Ferret
(Silly error)
m (43 revisions imported)
 
(12 intermediate revisions by 4 users not shown)
Line 3: Line 3:
 
local getArgs = require('Module:Arguments').getArgs
 
local getArgs = require('Module:Arguments').getArgs
 
local cd = require('Module:CountryData')
 
local cd = require('Module:CountryData')
 +
local list = require('Module:List');
 
local p = {}
 
local p = {}
  
local labels  = {
+
local knownargs = {
    ['NA'] = "[[North America|NA]]",
+
['format'] = true,
    ['EU'] = "[[Europe|EU]]",
+
['class'] = true,
    ['EUR'] = "[[Europe|EU]]",
+
['style'] = true,
    ['AU'] = "[[Australasia|AU]]",
+
['list_style'] = true,
    ['AUS'] = "[[Australasia|AU]]",
+
['item_style'] = true,
    ['PAL'] = "[[PAL region|PAL]]",
+
['item1_style'] = true,
    ['JP'] = "[[Japan|JP]]",
+
['indent'] = true
    ['JPN'] = "[[Japan|JP]]",
+
}
    ['RU'] = "[[Russia|RU]]",
+
 
    ['RUS'] = "[[Russia|RU]]",
+
local labels = {
    ['BR'] = "[[Brazil|BR]]",
+
['NA'] = "[[North America|NA]]",
    ['BRA'] = "[[Brazil|BR]]",
+
['EU'] = "[[Europe|EU]]",
    ['INT'] = "[[International version|INT]][[Category:Pages using vgrelease with deprecated parameters]]",
+
['EUR'] = "[[Europe|EU]]",
    ['SEA'] = "[[Southeast Asia|SEA]]",
+
['AU'] = "[[Australasia|AU]]",
    ['AS'] = "[[Asia|AS]]",
+
['AUS'] = "[[Australasia|AU]]",
    ['SA'] = "[[South America|SA]]",
+
['PAL'] = "[[PAL region|PAL]]",
    ['OC'] = "[[Oceania|OC]]",
+
['SEA'] = "[[Southeast Asia|SEA]]",
    ['WW'] = "<abbr title=\"Worldwide\">WW</abbr>",
+
['AS'] = "[[Asia|AS]]",
    ['?'] = "<abbr title=\"Unknown\">?</abbr>"
+
['SA'] = "[[South America|SA]]",
 +
['OC'] = "[[Oceania|OC]]",
 +
['WW'] = "<abbr title=\"Worldwide\">WW</abbr>"
 
}
 
}
  
 
local function getLocalLabel(alias)
 
local function getLocalLabel(alias)
local label = labels[string.upper(alias)]  
+
local label = labels[string.upper(alias)]
  
 
return label
 
return label
Line 37: Line 40:
 
local function getCountryData(frame, alias)
 
local function getCountryData(frame, alias)
 
local ualias = string.upper(alias)
 
local ualias = string.upper(alias)
+
 
if(countryData[ualias] == nil) then
+
if (countryData[ualias] == nil) then
local cdtable = cd.gettable(frame,alias,{})
+
local cdtable = cd.gettable(frame, alias, {})
 
countryData[ualias] = cdtable['alias']
 
countryData[ualias] = cdtable['alias']
 
end
 
end
  
 
return countryData[ualias]
 
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
 
end
  
 
function p.main(frame)
 
function p.main(frame)
 
local args = getArgs(frame)
 
local args = getArgs(frame)
local out = "<ul style=\"list-style: none none; line-height: inherit; margin: 0px;\">"
+
local listformat = args['format']
+
if (listformat == nil or listformat == "") then
 +
listformat = "unbulleted"
 +
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.
 
-- 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
+
if (args[3] ~= nil and args[4] == nil) then
out = out .. "<li><span style=\"font-size:95%;\">[["
+
local item = "<span style=\"font-size:95%;\">[["
if(args[1] ~= nil) then
+
if (args[1] ~= nil) then
out = out .. args[1]
+
item = item .. args[1]
 
end
 
end
out = out .. "|"
+
item = item .. "|"
if(args[2] ~= nil) then
+
if (args[2] ~= nil) then
out = out .. args[2]
+
item = item .. args[2]
 
end
 
end
out = out .. "]]:</span> " .. args[3] .. "[[Category:Pages using vgrelease with two parameter region]]</li>"
+
item = item .. "]]:</span> " .. args[3] .. "[[Category:Pages using vgrelease with two parameter region]]"
-- Old syntax "Blank region" use case, where param 1 is empty, and param 2 is the date.
+
table.insert(items, item)
elseif(args[1] == nil and args[2] ~= nil) then
+
-- Old syntax "Blank region" use case, where param 1 is empty, and param 2 is the date.
out = out .. "<li>" .. args[2] .. "[[Category:Pages using vgrelease without a region]]</li>"
+
elseif (args[1] == nil and args[2] ~= nil) then
-- Normal use cases, region/date pairs in 1/2, 3/4, 5/6, etc.
+
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
 
else
 
local i = 1
 
local i = 1
 
local j = 2
 
local j = 2
    while(args[i] and args[j]) do
+
while (args[i] and args[j]) do
    local label = getLocalLabel(args[i]);
+
local labels = {}
   
+
for k,v in ipairs(splitLabel(args[i])) do
    -- Didn't find a local label? Check for country data.
+
local label = getLocalLabel(v);
    if(label == nil) then
+
 
    label = getCountryData(frame, args[i])
+
-- Didn't find a local label? Check for country data.
   
+
if (label == nil) then
    -- Found something? Build a sitelink with it.
+
if not v:match('^%s*%[') then
    if(label ~= nil) then
+
label = getCountryData(frame, v)
    label = "[[" .. label .. "|" .. args[i] .. "]]"
+
end
    else
+
 
    label = args[i]
+
-- Found something? Build a sitelink with it.
 +
if (label ~= nil) then
 +
label = "[[" .. label .. "|" .. v .. "]]"
 +
else
 +
label = v
 +
end
 
end
 
end
 +
table.insert(labels, label)
 
end
 
end
 +
local item = "<span style=\"font-size:95%;\">" .. table.concat(labels,'/') .. ":</span> " .. args[j]
 +
table.insert(items, item)
  
out = out .. "<li><span style=\"font-size:95%;\">" .. label .. ":</span> " .. args[j] .. "</li>"
 
     
 
 
i = i + 2
 
i = i + 2
 
j = j + 2
 
j = j + 2
 
end
 
end
    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
  
    out = out .. "</ul>"
+
local out = list.makeList(listformat, items)
   
+
 
    -- Check for named parameters
+
-- Set message for invalid parameters.
 
local parameterMsg = "[[Category:Pages using vgrelease with named parameters|_VALUE_]]"
 
local parameterMsg = "[[Category:Pages using vgrelease with named parameters|_VALUE_]]"
if(frame:preprocess( "{{REVISIONID}}" ) == "") then
+
 
-- Preview?
+
-- Preview message.
 +
if (frame:preprocess("{{REVISIONID}}") == "") then
 
parameterMsg = "<div class=\"hatnote\" style=\"color:red\"><strong>Warning:</strong> unknown parameter \"_VALUE_\" (this message is shown only in preview).</div>"
 
parameterMsg = "<div class=\"hatnote\" style=\"color:red\"><strong>Warning:</strong> unknown parameter \"_VALUE_\" (this message is shown only in preview).</div>"
 
end
 
end
 +
 +
-- Check for invalid parameters
 
for k, v in pairs(args) do
 
for k, v in pairs(args) do
if(type(k) ~= 'number') then
+
if (type(k) ~= 'number' and knownargs[k] ~= true) then
 
local msg = parameterMsg:gsub('_VALUE_', k)
 
local msg = parameterMsg:gsub('_VALUE_', k)
 
out = out .. msg
 
out = out .. msg
Line 105: Line 149:
 
end
 
end
  
    return out
+
return out
 
end
 
end
  
 
return p
 
return p

Latest revision as of 05:06, 30 September 2020

This module implements the {{Video game release}} and {{Video game release hlist}} templates. The parameter "format" is passed to Module:List to set the output format. Please see the template page for usage instructions and tracking categories.


require('Module:No globals')

local getArgs = require('Module:Arguments').getArgs
local cd = require('Module:CountryData')
local list = require('Module:List');
local p = {}

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

local labels = {
	['NA'] = "[[North America|NA]]",
	['EU'] = "[[Europe|EU]]",
	['EUR'] = "[[Europe|EU]]",
	['AU'] = "[[Australasia|AU]]",
	['AUS'] = "[[Australasia|AU]]",
	['PAL'] = "[[PAL region|PAL]]",
	['SEA'] = "[[Southeast Asia|SEA]]",
	['AS'] = "[[Asia|AS]]",
	['SA'] = "[[South America|SA]]",
	['OC'] = "[[Oceania|OC]]",
	['WW'] = "<abbr title=\"Worldwide\">WW</abbr>"
}

local function getLocalLabel(alias)
	local label = labels[string.upper(alias)]

	return label
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

function p.main(frame)
	local args = getArgs(frame)
	local listformat = args['format']
	if (listformat == nil or listformat == "") then
		listformat = "unbulleted"
	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 = "<span style=\"font-size:95%;\">[["
		if (args[1] ~= nil) then
			item = item .. args[1]
		end
		item = item .. "|"
		if (args[2] ~= nil) then
			item = item .. args[2]
		end
		item = item .. "]]:</span> " .. 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 label = getLocalLabel(v);

				-- Didn't find a local label? Check for country data.
				if (label == nil) then
					if not v:match('^%s*%[') then
						label = getCountryData(frame, v)
					end

					-- Found something? Build a sitelink with it.
					if (label ~= nil) then
						label = "[[" .. label .. "|" .. v .. "]]"
					else
						label = v
					end
				end
				table.insert(labels, label)
			end
			local item = "<span style=\"font-size:95%;\">" .. table.concat(labels,'/') .. ":</span> " .. 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)

	-- Set message for invalid parameters.
	local parameterMsg = "[[Category:Pages using vgrelease with named parameters|_VALUE_]]"

	-- Preview message.
	if (frame:preprocess("{{REVISIONID}}") == "") then
		parameterMsg = "<div class=\"hatnote\" style=\"color:red\"><strong>Warning:</strong> unknown parameter \"_VALUE_\" (this message is shown only in preview).</div>"
	end

	-- 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