FANDOM


local Infobox = {}
 
----------------------------
-- Libraries of functions --
----------------------------
 
-- stands for High Frequency
local HF = require('Module:ComicsHF')
-- Parses invocation parameters, trims whitespace, and removes blanks
local getArgs = require('Dev:Arguments').getArgs
-- Common and centralized functions
local ComicsInfobox = require('Module:ComicsInfobox')
 
-----------------------
-- Libraries of data --
-----------------------
-- Various data tables which can be refactored for paradigm
-- Citizenship and Race can be sorted into: 
-- * dataCitizenship_dc
-- * dataCitizenship_marvel
local ctznTable       = mw.loadData('Module:ComicsInfobox/dataCitizenship_dc')
local raceTable       = mw.loadData('Module:ComicsInfobox/dataRace')
 
-- Since this should only be evaluated once per pageview, it's now global
_G.vars = { Pagename = mw.title.getCurrentTitle().text }
 
local function transition(funcName)
  -- This module's initial functions were made for InfoboxBuilder.
  -- As a result, most of them can't be invoked for Portable Infoboxes.
  -- To allow the existing code without rewriting it, this wrapper allows invocation.
	return function (frame)
		local args = getArgs(frame, { trim = true, removeBlanks = true })
		return Infobox[funcName](args, vars)
	end
end
 
local function Plural( s )
  -- Be careful! This function works only for regular plurals!
 
  local last1, last2, last3 = string.sub( s, -1, -1 ), string.sub( s, -2, -1 ), string.sub( s, -3, -1 )
 
  if last1 == "y" then
    s = string.sub( s, 1, -2 ) .. "ies"
  elseif last1 == "s" or last1 == "x" or last1 == "z" or last2 == "ch" or last2 == "sh" then
    s = s .. "es"    
  elseif last1 == "f" then
    s = string.sub( s, 1, -2 ) .. "ves"
  elseif last2 == "fe" then
    s = string.sub( s, 1, -3 ) .. "ves"
  else
    s = s .. "s"
  end
 
  return s
end
 
----------------------
-- Public functions --
----------------------
 
Infobox.Race = transition('_Race')
Infobox.Citizenship = transition('_Citizenship')
 
function Infobox._Race( field, vars )
  local output    = ""
 
  local valid, substitutes = raceTable.valid, raceTable.substitutes
 
  -- Check for multiple Citizenships separated with commas
  local races = HF.explode( ',', field.Value )
 
  for i, race in ipairs( races ) do
    race = HF.trim( race )
 
    -- 1. Check if it's a valid race with a plural form. If yes - add category links
    if type( valid[race] ) == "string" then
      output = output .. HF.CategoryLink( valid[race], vars.Pagename, race ) .. ", "
 
    else
    -- 2. If it's not valid, check for a possible substitution
      local firstLetter, found = string.lower( string.sub( race, 1, 1 ) ), false
 
      if type(substitutes[firstLetter]) == "table" then
       for pattern, sub in pairs( substitutes[firstLetter] ) do
         if string.find( string.lower( race ), pattern ) ~= nil then
           found = true
 
           local subValid  = sub
           local subPlural = valid[subValid]
 
           output = output .. HF.CategoryLink( subPlural, vars.Pagename, subValid ) .. ", "
 
           -- Since we only need one substitution, break loop
           break
         end
       end
      else
        for letter, subs in pairs( substitutes ) do
          for pattern, sub in pairs( subs ) do
            if string.find( string.lower( race ), pattern ) ~= nil then
             found = true
 
             local subValid  = sub
             local subPlural = valid[subValid]
 
             output = output .. HF.CategoryLink( subPlural, vars.Pagename, subValid ) .. ", "
 
             -- Since we only need one substitution, break loop
             break
           end
          end
        end
      end
 
      -- 3. If no substitution is found - fallback to a common grammar rules and add a page to a Category:Needs valid race
      if not found then
        output = output .. HF.CategoryLink( Plural( race ), vars.Pagename, race )
        output = output .. HF.CategoryLink( "Needs valid race", vars.Pagename, "" ) .. ", "
      end
    end
  end
 
  -- Sanitize output, remove the last comma and space
  if string.sub( output, -1, -1 ) == "," then
    output = string.sub( output, 1, -2 )
  end
  if string.sub( output, -2, -1 ) == ", " then
    output = string.sub( output, 1, -3 )
  end
 
  -- If Citizenship2 or CitizenshipRef is provided - concatenate them to the output
  if not HF.isempty( field.Value2 ) then
    output = output .. " " .. field.Value2
  end
 
  if not HF.isempty( field.ValueRef ) then
    output = output .. " " .. field.ValueRef
  end
 
  return output
end
 
function Infobox._Citizenship( field, vars )
  local output    = ""
  local valid, substitutes = ctznTable.valid, ctznTable.substitutes
 
  -- Check for multiple Citizenships separated with commas
  local citizenships = HF.explode( ',', field.Value )
 
  for i, citizenship in ipairs( citizenships ) do
    citizenship = HF.trim( citizenship )
 
    -- 1. Check if it's a valid citizenship with a plural form. If yes - add category links
    if type( valid[citizenship] ) == "string" then
      output = output .. HF.CategoryLink( valid[citizenship], vars.Pagename, citizenship ) .. ", "
 
    else
    -- 2. If it's not valid, check for a possible substitution
      local firstLetter, found = string.lower( string.sub( citizenship, 1, 1 ) ), false
 
      if type(substitutes[firstLetter]) == "table" then
       for pattern, sub in pairs( substitutes[firstLetter] ) do
         if string.find( string.lower( citizenship ), pattern ) ~= nil then
           found = true
 
           local subValid  = sub
           local subPlural = valid[subValid]
 
           output = output .. HF.CategoryLink( subPlural, vars.Pagename, subValid ) .. ", "
 
           -- Since we only need one substitution, break loop
           break
         end
       end
      else
        for letter, subs in pairs( substitutes ) do
          for pattern, sub in pairs( subs ) do
            if string.find( string.lower( citizenship ), pattern ) ~= nil then
             found = true
 
             local subValid  = sub
             local subPlural = valid[subValid]
 
             output = output .. HF.CategoryLink( subPlural, vars.Pagename, subValid ) .. ", "
 
             -- Since we only need one substitution, break loop
             break
           end
          end
        end
      end
 
      -- 3. If no substitution is found - fallback to a common grammar rules and add a page to a Category:Needs valid citizenship
      if not found then
        output = output .. HF.CategoryLink( Plural( citizenship ), vars.Pagename, citizenship )
        output = output .. HF.CategoryLink( "Needs valid citizenship", vars.Pagename, "" ) .. ", "
      end
    end
  end
 
  -- Sanitize output, remove the last comma and space
  if string.sub( output, -1, -1 ) == "," then
    output = string.sub( output, 1, -2 )
  end
  if string.sub( output, -2, -1 ) == ", " then
    output = string.sub( output, 1, -3 )
  end
 
  -- If Citizenship2 or CitizenshipRef is provided - concatenate them to the output
  if not HF.isempty( field.Value2 ) then
    output = output .. " " .. field.Value2
  end
 
  if not HF.isempty( field.ValueRef ) then
    output = output .. " " .. field.ValueRef
  end
 
  return output
end
 
return Infobox

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.