Module:CardGameCite

local p = {}

local yesno = require('Module:Yesno') local datatable = mw.loadData('Module:Cardgames/data')

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', 'Category', cat) end

local function makeWikitextError(template, msg) local ret = string.format(       ' Template:%s error: %s. ',        template, msg    ) if currentTitle.namespace == 0 then ret = ret .. makeCategoryLink('Pages with template parameter errors') 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 ', 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] = ' – ' 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", set, text) end end local link, cardname = args.link, args.cardname args.link, args.cardname = nil, nil if link or cardname then if link and not cardname then return makeWikitextError(               template, '"link" cannot be used without "cardname"'            ) end local fmt = ' (Card: %s) ' if not link then ret[#ret + 1] = fmt:format(cardname) else local cardlink if args.nourl then if link == cardname then cardlink = string.format('%s', cardname) else cardlink = string.format('%s', link, cardname) end else cardlink = string.format(                   '[%s%s %s]', data.urlprefix, link, cardname                ) end ret[#ret + 1] = fmt:format(cardlink) 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(           'Templates with unrecognized parameters'        ) ret[#ret + 1] = string.format(           ' Warning: Call to Template:%s' ..            ' contains unrecognized parameter "%s"', template, key        ) end

return table.concat(ret) .. table.concat(warnings) end

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

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(               'Pages using deprecated unnamed parameter 1 for card game set'            ) else error('the "set" parameter is required') end end end

local function generateTextFromSet(args, warnings) if not args.text then local stop = args.set:find('%s*%(')       if stop then            args.text = args.set:sub(1, stop - 1)        else            args.text = args.set        end    end end

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(               'Pages using deprecated unnamed parameter 2 for card game set'            ) else generateTextFromSet(args, warnings) end end end

local function processTextFFGTCG(args, warnings) generateTextFromSet(args) if args.text == 'Star Wars: The Card Game Core Set' then args.text = 'Core Set' end end

local function processCardnameFFGTCG(args, warnings) if not args.cardname then if args[2] then args.cardname = args[2] args[2] = nil warnings[#warnings + 1] = makeCategoryLink(               'Pages using deprecated parameter 2 to Template:FFGTCG'            ) end end end

local function urlToLink(args, warnings) if not args.link then if args.url then args.link = args.url args.url = nil warnings[#warnings + 1] = makeCategoryLink(               'Pages using deprecated url parameter to Template:SWGTCG'            ) end end end

local function processSetTextSWPM(args, warnings) processSet(args, warnings) if 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 .. ' (TCG)' elseif not args.text then generateTextFromSet(args, warnings) 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

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, processCardnameFFGTCG) 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, urlToLink) end

function p.SWPM(frame) return main(frame, 'SWPM', processSetTextSWPM) end

function p.Topps(frame) return main(frame, 'Topps', processSet, generateTextFromSet) end

function p.YJCCG(frame) return main(frame, 'YJCCG', processSet, generateTextFromSet) end

return p