Module:Video game release

From Viki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Video game release/doc

  1 require('Module:No globals')
  2 
  3 local getArgs = require('Module:Arguments').getArgs
  4 local cd = require('Module:CountryData')
  5 local list = require('Module:List');
  6 local p = {}
  7 
  8 local knownargs = {
  9 	['format'] = true,
 10 	['class'] = true,
 11 	['style'] = true,
 12 	['list_style'] = true,
 13 	['item_style'] = true,
 14 	['item1_style'] = true,
 15 	['indent'] = true
 16 }
 17 
 18 local labels = {
 19 	['NA'] = "NA",
 20 	['EU'] = "EU",
 21 	['EUR'] = "EU",
 22 	['AU'] = "AU",
 23 	['AUS'] = "AU",
 24 	['PAL'] = "PAL",
 25 	['SEA'] = "SEA",
 26 	['AS'] = "AS",
 27 	['SA'] = "SA",
 28 	['OC'] = "OC",
 29 	['WW'] = "<abbr title=\"Worldwide\">WW</abbr>"
 30 }
 31 
 32 local function getLocalLabel(alias)
 33 	local label = labels[string.upper(alias)]
 34 
 35 	return label
 36 end
 37 
 38 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.
 39 
 40 local function getCountryData(frame, alias)
 41 	local ualias = string.upper(alias)
 42 
 43 	if (countryData[ualias] == nil) then
 44 		local cdtable = cd.gettable(frame, alias, {})
 45 		countryData[ualias] = cdtable['alias']
 46 	end
 47 
 48 	return countryData[ualias]
 49 end
 50 
 51 local function splitLabel(s)
 52 	local islist = true
 53 	local res = {}
 54 	for k,v in ipairs(mw.text.split(s or '', '%s*/%s*')) do
 55 		local v1 = v:match('^%s*([A-Z][A-Z][A-Z]?)%s*$')
 56 		if v1 then
 57 			table.insert(res,v1)
 58 		else
 59 			local v2 = v:match('^%s*(%[%[[^%[%]|]*|[A-Z][A-Z][A-Z]?%]%])%s*$')
 60 			if v2 then
 61 				table.insert(res,v2)
 62 			else
 63 				islist = false
 64 			end
 65 		end
 66 	end
 67 	return islist and res or {s}
 68 end
 69 
 70 function p.main(frame)
 71 	local args = getArgs(frame)
 72 	local listformat = args['format']
 73 	if (listformat == nil or listformat == "") then
 74 		listformat = "unbulleted"
 75 	end
 76 	local items = {}
 77 
 78 	-- 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.
 79 	if (args[3] ~= nil and args[4] == nil) then
 80 		local item = "<span style=\"font-size:95%;\">[["
 81 		if (args[1] ~= nil) then
 82 			item = item .. args[1]
 83 		end
 84 		item = item .. "|"
 85 		if (args[2] ~= nil) then
 86 			item = item .. args[2]
 87 		end
 88 		item = item .. "]]:</span> " .. args[3] .. "[[Category:Pages using vgrelease with two parameter region]]"
 89 		table.insert(items, item)
 90 		-- Old syntax "Blank region" use case, where param 1 is empty, and param 2 is the date.
 91 	elseif (args[1] == nil and args[2] ~= nil) then
 92 		local item = args[2] .. "[[Category:Pages using vgrelease without a region]]"
 93 		table.insert(items, item)
 94 		-- Normal use cases, region/date pairs in 1/2, 3/4, 5/6, etc.
 95 	else
 96 		local i = 1
 97 		local j = 2
 98 		while (args[i] and args[j]) do
 99 			local labels = {}
100 			for k,v in ipairs(splitLabel(args[i])) do
101 				local label = getLocalLabel(v);
102 
103 				-- Didn't find a local label? Check for country data.
104 				if (label == nil) then
105 					if not v:match('^%s*%[') then
106 						label = getCountryData(frame, v)
107 					end
108 
109 					-- Found something? Build a sitelink with it.
110 					if (label ~= nil) then
111 						label = "[[" .. label .. "|" .. v .. "]]"
112 					else
113 						label = v
114 					end
115 				end
116 				table.insert(labels, label)
117 			end
118 			local item = "<span style=\"font-size:95%;\">" .. table.concat(labels,'/') .. ":</span> " .. args[j]
119 			table.insert(items, item)
120 
121 			i = i + 2
122 			j = j + 2
123 		end
124 	end
125 
126 	-- Add known parameters of Module:List to the table
127 	for k, v in pairs(args) do
128 		if (knownargs[k] == true) then
129 			items[k] = v
130 		end
131 	end
132 
133 	local out = list.makeList(listformat, items)
134 
135 	-- Preview message and category
136 	local parameterMsg = require('Module:If preview')._warning({
137 		'Unknown parameter "_VALUE_".'
138 	}) .. "[[Category:Pages using vgrelease with named parameters|_VALUE_]]"
139 
140 	-- Check for invalid parameters	
141 	for k, v in pairs(args) do
142 		if (type(k) ~= 'number' and knownargs[k] ~= true) then
143 			local msg = parameterMsg:gsub('_VALUE_', k)
144 			out = out .. msg
145 		end
146 	end
147 
148 	return out
149 end
150 
151 return p