Module:CardGameCite

From Droid Reference Library
Revision as of 16:26, 12 August 2018 by Drl-admin (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

local p = {}

local yesno = require('Module:Yesno') local datatable = mw.loadData('Module: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', '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

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

end

-- Checks for and stores the url parameter in the template parameters -- and also add a warning for the use of the deprecated url parameter. 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 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'
           )
       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 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, urlToLink)

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

return p