Module:Test: Difference between revisions

From RPCS3 Wiki
(Trying to get old syntax to also work)
Tag: Reverted
m (Testing changes 2)
Tag: Reverted
Line 33: Line 33:
     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
                 -- Check if the next argument is a date
                 islist = false
                local nextIndex = k + 1
                if nextIndex <= #s then
                    local nextArg = mw.text.trim(mw.text.split(s, '%s*/%s*')[nextIndex])
                    local nextArgIsDate = nextArg:match('^[^%[%]|]+%s*$')
                    if nextArgIsDate then
                        -- If the next argument is a date, assume the previous argument as a label
                        table.insert(res, v .. ' ')
                    else
                        -- If the next argument is not a date, it's an unlabelled date
                        table.insert(res, '')
                        table.insert(res, v)
                    end
                else
                    -- If there are no more arguments, assume the previous argument as a label
                    table.insert(res, v .. ' ')
                end
             end
             end
         end
         end
Line 66: Line 50:


function p.main(frame)
function p.main(frame)
local args = getArgs(frame)
    local args = getArgs(frame)
local listformat = args['format']
    local listformat = args['format']
if (listformat == nil or listformat == "") then
    if (listformat == nil or listformat == "") then
listformat = "unbulleted"
        listformat = "unbulleted"
end
    end
local items = {}
    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:97%;\">[["
        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 vgregion 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 vgregion 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);


-- 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.
                -- Didn't find a local label? Check for country data.
if (args[3] ~= nil and args[4] == nil) then
                if (label == nil) then
local item = "<span style=\"font-size:97%;\">[["
                    if not v:match('^%s*%[') then
if (args[1] ~= nil) then
                        -- Removed line of code relating to fetching external country data
item = item .. args[1]
                    end
end
 
item = item .. "|"
                    -- Found something? Build a sitelink with it.
if (args[2] ~= nil) then
                    if (label ~= nil) then
item = item .. args[2]
                        label = "[[" .. label .. "|" .. v .. "]]"
end
                    else
item = item .. "]]:</span> " .. args[3] .. "[[Category:Pages using vgregion with two parameter region]]"
                        label = v
table.insert(items, item)
                    end
-- Old syntax "Blank region" use case, where param 1 is empty, and param 2 is the date.
                end
elseif (args[1] == nil and args[2] ~= nil) then
                table.insert(labels, label)
local item = args[2] .. "[[Category:Pages using vgregion without a region]]"
            end
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.
            -- Handle unlabelled dates
if (label == nil) then
            if #labels == 0 then
if not v:match('^%s*%[') then
                table.insert(labels, '') -- Insert an empty label
-- Removed line of code relating to fetching external country data
                table.insert(labels, args[j])  -- Insert the date
end
            end


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


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


local out = list.makeList(listformat, items)
    local out = list.makeList(listformat, items)


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


-- Check for invalid parameters
    -- Check for invalid parameters
for k, v in pairs(args) do
    for k, v in pairs(args) do
if (type(k) ~= 'number' and knownargs[k] ~= true) 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
end
        end
end
    end


return out
    return out
end
end


return p
return p

Revision as of 18:15, 17 March 2024

Documentation for this module may be created at Module:Test/doc

require('strict')

local getArgs = require('Module:Arguments').getArgs
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'] = "[[:Category:North America (Release region)|NA]]",
	['EU'] = "[[:Category:Europe (Release region)|EU]]",
	['AU'] = "[[:Category:Australia (Release region)|AU]]",
	['PAL'] = "[[:Category:Europe (Release region)|PAL]]",
	['JP'] = "[[:Category:Japan (Release region)|JP]]",
	['WW'] = "<abbr title=\"Worldwide\">WW</abbr>"
}

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

	return label
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:97%;\">[["
        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 vgregion 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 vgregion 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
                        -- Removed line of code relating to fetching external country data
                    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

            -- Handle unlabelled dates
            if #labels == 0 then
                table.insert(labels, '')  -- Insert an empty label
                table.insert(labels, args[j])  -- Insert the date
            end

            local item = "<span style=\"font-size:97%;\">" .. 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)

    -- Preview message and category
    local parameterMsg = require('Module:If preview')._warning({
        'Unknown parameter "_VALUE_".'
    }) .. "[[Category:Pages using vgregion 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