Module:Vgregion: Difference between revisions

From RPCS3 Wiki
(Fully switch to stock vgregion code)
Tag: Reverted
(Test version of VGregion)
Tag: Reverted
Line 7: Line 7:


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


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


local function splitLabel(s)
local function splitLabel(s)
local islist = true
    local islist = true
local res = {}
    local res = {}
for k,v in ipairs(mw.text.split(s or '', '%s*/%s*')) do
    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*$')
        local v1 = v:match('^%s*([A-Z][A-Z][A-Z]?)%s*$')
if v1 then
        if v1 then
table.insert(res,v1)
            table.insert(res,v1)
else
        else
local v2 = v:match('^%s*(%[%[[^%[%]|]*|[A-Z][A-Z][A-Z]?%]%])%s*$')
            local v2 = v:match('^%s*(%[%[[^%[%]|]*|[A-Z][A-Z][A-Z]?%]%])%s*$')
if v2 then
            if v2 then
table.insert(res,v2)
                table.insert(res,v2)
else
            else
islist = false
                islist = false
end
            end
end
        end
end
    end
return islist and res or {s}
    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 listformat = args['format']
    local listformat = args['format'] or "unbulleted"
if (listformat == nil or listformat == "") then
    local items = {}
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] and not args[4] then
if (args[3] ~= nil and args[4] == nil) then
        local item = "[["
local item = "<span style=\"font-size:97%;\">[["
        if args[1] then
if (args[1] ~= nil) then
            item = item .. args[1]
item = item .. args[1]
        end
end
        item = item .. "|"
item = item .. "|"
        if args[2] then
if (args[2] ~= nil) then
            item = item .. args[2]
item = item .. args[2]
        end
end
        item = item .. "]]: " .. args[3] .. "[[Category:Pages using vgrelease with two parameter region]]"
item = item .. "]]:</span> " .. args[3] .. "[[Category:Pages using vgrelease with two parameter region]]"
        table.insert(items, item)
table.insert(items, item)
    elseif not args[1] and args[2] then
-- Old syntax "Blank region" use case, where param 1 is empty, and param 2 is the date.
        local item = args[2] .. "[[Category:Pages using vgrelease without a region]]"
elseif (args[1] == nil and args[2] ~= nil) then
        table.insert(items, item)
local item = args[2] .. "[[Category:Pages using vgrelease without a region]]"
    else
table.insert(items, item)
        local i = 1
-- Normal use cases, region/date pairs in 1/2, 3/4, 5/6, etc.
        local j = 2
else
        while args[i] and args[j] do
local i = 1
            local labels = {}
local j = 2
            for k,v in ipairs(splitLabel(args[i])) do
while (args[i] and args[j]) do
                local label = getLocalLabel(v)
local labels = {}
                if not label then
for k,v in ipairs(splitLabel(args[i])) do
                    if not v:match('^%s*%[') then
local label = getLocalLabel(v);
                        label = getCountryData(frame, v)
                    end
                    if label then
                        label = "[[" .. label .. "|" .. v .. "]]"
                    else
                        label = v
                    end
                end
                table.insert(labels, label)
            end
            local item = table.concat(labels, '/') .. ": " .. args[j]
            table.insert(items, item)
            i = i + 2
            j = j + 2
        end
    end


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


-- Found something? Build a sitelink with it.
    local out = list.makeList(listformat, items)
if (label ~= nil) then
label = "[[" .. label .. "|" .. v .. "]]"
else
label = v
end
end
table.insert(labels, label)
end
local item = "<span style=\"font-size:97%;\">" .. table.concat(labels,'/') .. ":</span> " .. args[j]
table.insert(items, item)


i = i + 2
    local parameterMsg = require('Module:If preview')._warning({'Unknown parameter "_VALUE_".'}) .. "[[Category:Pages using vgrelease with named parameters|_VALUE_]]"
j = j + 2
end
end


-- Add known parameters of Module:List to the table
    for k, v in pairs(args) do
for k, v in pairs(args) do
        if type(k) ~= 'number' and not knownargs[k] then
if (knownargs[k] == true) then
            local msg = parameterMsg:gsub('_VALUE_', k)
items[k] = v
            out = out .. msg
end
        end
end
    end


local out = list.makeList(listformat, items)
    return out
 
-- Preview message and category
local parameterMsg = require('Module:If preview')._warning({
'Unknown parameter "_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
end


return p
return p

Revision as of 14:24, 17 March 2024

This module provides the vgregion template the capabilities to store and arrange dates entered.

Attribution and re-use

This module has been adapted from Wikipedia released under the Creative Commons Attribution-ShareAlike License 4.0.


require('strict')

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'] or "unbulleted"
    local items = {}

    if args[3] and not args[4] then
        local item = "[["
        if args[1] then
            item = item .. args[1]
        end
        item = item .. "|"
        if args[2] then
            item = item .. args[2]
        end
        item = item .. "]]: " .. args[3] .. "[[Category:Pages using vgrelease with two parameter region]]"
        table.insert(items, item)
    elseif not args[1] and args[2] then
        local item = args[2] .. "[[Category:Pages using vgrelease without a region]]"
        table.insert(items, item)
    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)
                if not label then
                    if not v:match('^%s*%[') then
                        label = getCountryData(frame, v)
                    end
                    if label then
                        label = "[[" .. label .. "|" .. v .. "]]"
                    else
                        label = v
                    end
                end
                table.insert(labels, label)
            end
            local item = table.concat(labels, '/') .. ": " .. args[j]
            table.insert(items, item)
            i = i + 2
            j = j + 2
        end
    end

    for k, v in pairs(args) do
        if knownargs[k] then
            items[k] = v
        end
    end

    local out = list.makeList(listformat, items)

    local parameterMsg = require('Module:If preview')._warning({'Unknown parameter "_VALUE_".'}) .. "[[Category:Pages using vgrelease with named parameters|_VALUE_]]"

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

    return out
end

return p