Moduuli:CardGameCite

Jedipediasta, vapaasta Tähtien sota-tietosanakirjasta tänään, 29. maaliskuuta 2024
Siirry navigaatioonSiirry hakuun

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:CardGameCite/ohje

local p = {}
 
local yesno = require('Moduuli:Yesno')
local datatable = mw.loadData('Moduuli:CardGameCite/data')
local currentTitle = mw.title.getCurrentTitle()
 
local function makeCategoryLink(cat)
    -- "Category" is split out here so that the module isn't put into the
    -- category "%s" when the page is saved.
    return string.format('[[%s:%s]]', 'Luokka', cat)
end
 
local function makeWikitextError(template, msg)
    local ret = string.format(
        '<strong class="error">[[Malline:%s]] virhe: %s.</strong>',
        template, msg
    )
    if currentTitle.namespace == 0 then
        ret = ret .. makeCategoryLink('Sivut, joissa on mallineparametrivirheitä')
    end
    return ret
end
 
function p._main(args, warnings, template)
    --[[
    Args:
    set: name of set article
    text: display text for set article link
    link: text to link card name to (possibly nil)
    cardname: name of card (possibly nil)
    nourl: boolean indicating external/internal link (true for internal)
    scenario (SWGTCG only): scenario name
 
    Warnings: table, possibly empty, of messages regarding deprecated
        parameters
 
    Template: name of the template, for looking up the data on it
    ]]
    local data = datatable[template]
    local ret = {string.format('%s&nbsp;', data.image)}
    -- Access to a local is faster than indexing a table each time
    local title, noSet = data.title, data.noSet
    if title then
        ret[#ret + 1] = title
        if not noSet then
            ret[#ret + 1] = ' &ndash; '
        end
    end
    if not noSet then
        local set, text = args.set, args.text
        args.set, args.text = nil, nil
        if set == text then
            ret[#ret + 1] = string.format("''[[%s]]''", set)
        else
            ret[#ret + 1] = string.format("''[[%s|%s]]''", set, text)
        end
    end
    local link, cardname, archiveurl = args.link, args.cardname, args.archiveurl
    args.link, args.cardname, args.archiveurl = nil, nil, nil
    if link or cardname then
        if link and not cardname then
            return makeWikitextError(
                template, '"link" vaatii parametrin "cardname"'
            )
        end
        local fmt = ' <small class="plainlinks">(Kortti: %s)%s</small>'
        if not link then
            ret[#ret + 1] = fmt:format(cardname, '')
        else
            local cardlink
            local archivelink = ''
            if args.nourl then
                if link == cardname then
                    cardlink = string.format('[[%s]]', cardname)
                else
                    cardlink = string.format('[[%s|%s]]', link, cardname)
                end
            else
                cardlink = string.format(
                    '[%s%s %s]', data.urlprefix, link, cardname
                )
                if archiveurl then
                    archivelink = string.format(' ([%s linkki varmuuskopioon])', archiveurl)
                elseif args.nobackup then
                    archivelink = ' (ei linkkiä varmuuskopioon)'
                    args.nobackup = nil
                elseif mw.title.getCurrentTitle().namespace == 0 or mw.title.getCurrentTitle().namespace == 6 then
                    makeCategoryLink('Sivut, joissa on puuttuvia arkistolinkkejä')
                end
            end
            ret[#ret + 1] = fmt:format(cardlink, archivelink)
        end
    end
    args.nourl = nil
    local scenarioFormat, scenarioName = data.scenario, args.scenario
    if scenarioFormat and scenarioName then
        args.scenario = nil
        ret[#ret + 1] = scenarioFormat:format(scenarioName)
    end
 
    -- At this point, the args table should be empty. Calling next() on it
    -- will return nil if it is indeed empty, or a string key if it is not.
    local key = next(args)
    if key then
        warnings[#warnings + 1] = makeCategoryLink(
            'Mallineet, joissa on tunnistamattomia parametrejä'
        )
        ret[#ret + 1] = string.format(
            ' <span style="color: red;">Varoitus: Kutsussa mallineeseen %s' ..
            ' on tunnistamaton parametri "%s"</span>', template, key
        )
    end
 
    return table.concat(ret) .. table.concat(warnings)
end
 
-- Trim whitespace from template arguments and store them
local function processArgs(frame, ...)
    local args, warnings = {}, {}
    local funcs = {...}
    for k, v in pairs(frame:getParent().args) do
        v = v:match('^%s*(.-)%s*$') -- trim whitespace
        if v ~= '' then
            args[k] = v
        end
    end
    args.nourl = yesno(args.nourl)
    for _, func in ipairs(funcs) do
        func(args, warnings)
    end
    return args, warnings
end
 
-- 
local function archiveWizards(args, warnings)
    if not args.nourl and args.link and args.link:match('wizards.com') then
        args.link = 'http://web.archive.org/web/' .. args.link
    end
end
 
-- Checks for the set parameter in the template parameters,
-- and also add warnings for the use of unnamed parameter 1
-- or the lack of a set parameter when it's required.
local function processSet(args, warnings)
    if not args.set then
        if args[1] then
            args.set = args[1]
            args[1] = nil
            warnings[#warnings + 1] = makeCategoryLink(
                'Sivut, jotka käyttävät nimeämätöntä parametriä korttipelimallineessa'
            )
        else
            error('parametri "set" on pakollinen', 0)
        end
    end
end
 
-- If the text parameter is not present, retrieves the text that will
-- be used for the set after some trimming.
local function generateTextFromSet(args, warnings)
    if not args.text then
        local stop = args.set:find('%s*%(')
        if args.sformat then
            args.text = args.sformat
            args.sformat = nil
        elseif stop then
            args.text = args.set:sub(1, stop - 1)
        else
            args.text = args.set
        end
    end
end
 
-- Checks for the text parameter in the template parameters
-- and also add warnings for the use of unnamed parameter 2
-- or the lack of a text parameter when it's required.
local function processText(args, warnings)
    if not args.text then
        if args[2] then
            args.text = args[2]
            args[2] = nil
            warnings[#warnings + 1] = makeCategoryLink(
                'Sivut, jotka käyttävät nimeämätöntä parametriä korttipelimallineessa'
            )
        else
            generateTextFromSet(args, warnings)
        end
    end
end
 
-- Trim the set text for the FFG Card Game Core Set.
local function processTextFFGTCG(args, warnings)
    generateTextFromSet(args)
    if args.text == 'Star Wars: The Card Game Core Set' then
        args.text = 'Core Set'
    end
end
 
-- Checks for the cardname parameter in the template parameters
-- and also add warnings for the use of unnamed parameter 2
-- or the lack of a text parameter when it's required.
local function processCardname(args, warnings)
    if not args.cardname then
        if args[2] then
            args.cardname = args[2]
            args[2] = nil
            warnings[#warnings + 1] = makeCategoryLink(
                'Sivut, jotka käyttävät nimeämätöntä parametriä korttipelimallineessa'
            )
        end
    end
end

local function processSetTextSWPM(args, warnings)
    processSet(args, warnings)
    if not args.text then
        if args[2] then
            args.text = args[2]
            args[2] = nil
            warnings[#warnings + 1] = makeCategoryLink(
                'Sivut, jotka käyttävät nimeämätöntä parametriä korttipelimallineessa'
            )
        elseif args.set == 'Base Set' then
            args.text = args.set
            args.set = 'Star Wars PocketModel TCG: Base Set'
        elseif args.set == 'Clone Wars'
            or args.set == 'Ground Assault'
            or args.set == 'Order 66'
        then
            args.text = args.set
            args.set = args.set .. ' (PocketModels)'
        else
            generateTextFromSet(args, warnings)
        end
    end
end
 
local function processSetTextFFGXW(args, warnings)
    processSet(args, warnings)
    if not args.text then
        if args[2] then
            args.text = args[2]
            args[2] = nil
            warnings[#warnings + 1] = makeCategoryLink(
                'Sivut, jotka käyttävät nimeämätöntä parametriä korttipelimallineessa'
            )
        elseif args.set == 'Core Set' then
            args.text = args.set
            args.set = 'X-Wing: Core Set'
        else
            generateTextFromSet(args, warnings)
        end
    end
end
 
local function processSetTextFFGXW2(args, warnings)
    processSet(args, warnings)
    if not args.text then
        if args[2] then
            args.text = args[2]
            args[2] = nil
            warnings[#warnings + 1] = makeCategoryLink(
                'Sivut, jotka käyttävät nimeämätöntä parametriä korttipelimallineessa'
            )
        elseif args.set == 'Core Set' then
            args.text = args.set
            args.set = 'X-Wing: Second Edition Core Set'
        else
            generateTextFromSet(args, warnings)
        end
    end
end
 
local function main(frame, template, ...)
    local success, args, warnings = pcall(processArgs, frame, ...)
    if not success then
        return makeWikitextError(template, args)
    end
    return p._main(args, warnings, template)
end
 
-- The following are specific function calls for each template that 
-- uses this Module framework for its template. To add a new template,
-- simply copy the last entry and change the template name. 
function p.TCG(frame)
    return main(frame, 'TCG', processSet, processText, archiveWizards)
end
 
function p.CCG(frame)
    return main(frame, 'CCG', processSet, generateTextFromSet)
end
 
function p.FFGTCG(frame)
    return main(frame, 'FFGTCG', processSet,
                processTextFFGTCG, processCardname)
end
 
function p.JKTCG(frame)
    return main(frame, 'JKTCG', processSet, processText)
end
 
function p.SOTE(frame)
    return main(frame, 'SOTE')
end
 
function p.SOTEEMCC(frame)
    return main(frame, 'SOTEEMCC')
end
 
function p.SWGTCG(frame)
    return main(frame, 'SWGTCG', processSet, processText)
end
 
function p.SWPM(frame)
    return main(frame, 'SWPM', processSetTextSWPM)
end
 
function p.Topps(frame)
    return main(frame, 'Topps', processSet, generateTextFromSet,
                processCardname)
end
 
function p.YJCCG(frame)
    return main(frame, 'YJCCG', processSet, generateTextFromSet)
end
 
function p.SWIA(frame)
    return main(frame, 'SWIA', processSet, processText)
end
 
function p.Armada(frame)
    return main(frame, 'Armada', processSet, processText)
end
 
function p.Destiny(frame)
    return main(frame, 'Destiny', processSet, processText)
end
 
function p.FFGXW(frame)
    return main(frame, 'FFGXW', processSetTextFFGXW)
end
 
function p.FFGXW2(frame)
    return main(frame, 'FFGXW2', processSetTextFFGXW2)
end

function p.Legion(frame)
    return main(frame, 'Legion', processSet, processText)
end
return p