Module:ComicsInfobox/Citizenship

local Plural = function( 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

local CitizenshipCheck = function( values, table ) local output = "" local valueUpper = "" local valueLower = "" local pagename = vars.Pagename for i, value in ipairs( values ) do     if type( value ) ~= nil then valueUpper = HF.firstToUpper( HF.trim( value ) ) valueLower = string.lower( HF.trim( value ) ) if type( table.valid[valueUpper] ) ~= nil and table.valid[valueUpper] == true then output = output .. HF.CategoryLink( valueUpper, pagename, valueUpper ) .. ", "       elseif type( table.substitutes[valueLower] ) == "string" then output = output .. HF.CategoryLink( table.substitutes[valueLower], pagename, valueUpper ) .. ", "       else output = output .. value .. ", "       end else output = output .. value end end return output end

function CharacterInfobox.Race( field, vars ) local output   = "" local raceTable = mw.loadData('Module:CharacterInfoboxRace') 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 .. 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 .. 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 .. 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 .. CategoryLink( Plural( race ), vars.Pagename, race ) output = output .. 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 CharacterInfobox.Citizenship( field, vars ) local output   = "" local ctznTable = mw.loadData('Module:CharacterInfoboxCitizenship') 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 .. 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 .. 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 .. 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 .. CategoryLink( Plural( citizenship ), vars.Pagename, citizenship ) output = output .. 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