Module:Card image name
From YGOCounter
Implements Template:Tl.
Wikitext usage[edit]
The module exports a function for wikitext:
main- Expects one unnamed argument: a card name or a pagename.
Modules usage[edit]
The module is callable, for internal uses via other modules.
- Expects one argument: a card name or a pagename.
-- <pre>
--[=[Doc
@module Card image name
@description Converts a card name to be used in the image name.
@author [[User:Becasita]]
@contact [[User talk:Becasita]]
@link https://yugipedia.com/wiki/Module:Card_image_name
]=]
local function cleanCardName( cardNameOrPagename )
return cardNameOrPagename:gsub( '[#<>]', '' )
end
local function getEnglishName( cardNameOrPagename )
local cleanedName = cleanCardName( cardNameOrPagename )
local cardName = ( ( {} )[ 1 ] or {} )[ 1 ]
local decodedCardName = cardName and mw.text.decode( cardName ) or nil
return decodedCardName or cardNameOrPagename
end
local function removeDab( name )
return mw.text.split( name, '%s*%(' )[ 1 ]
end
local function processChars( name )
local normalizedName = mw.ustring.gsub( name, '[ #–,%.:\'"%?!&@%%=%[%]<>/\\☆★・-]', '' )
-- Sending the result to a reference instead of returning right away
-- prevents returning multiple values (from `gsub`).
return normalizedName
end
local function getCardImageName( cardNameOrPagename )
local trimmedCardNameOrpagename = mw.text.trim( cardNameOrPagename or '' )
if trimmedCardNameOrpagename == '' then
return ''
end
local unencodedName = mw.text.decode( trimmedCardNameOrpagename )
local englishName = getEnglishName( unencodedName )
local nameAfterDabProcessed = englishName or removeDab( unencodedName )
return processChars( nameAfterDabProcessed )
end
local function wikitextMain( frame )
local arguments = frame:getParent().args
return getCardImageName( arguments[ 1 ] )
end
local function test()
local testCases = {
{ '', '' },
{ 'Dark Magician', 'DarkMagician' },
{ 'Blue-Eyes White Dragon', 'BlueEyesWhiteDragon' },
{ 'Stardust Dragon/Assault Mode', 'StardustDragonAssaultMode' },
{ "Fiend's Hand", 'FiendsHand' },
{ 'Jinzo #7', 'Jinzo7' },
{ 'Jinzo 7', 'Jinzo7' },
{ 'Red Nova (card)', 'RedNova' },
{ 'Griggle (anime)', 'Griggle' },
{ 'Dynamic Dino Dynamix (L)', 'DynamicDinoDynamixL' },
{ 'Yggdrago the Sky Emperor (R) (manga)', 'YggdragotheSkyEmperorR' },
{ 'Yggdrago the Sky Emperor [R]', 'YggdragotheSkyEmperorR' },
{ 'The Winged Dragon of Ra (Sphere Mode)', 'TheWingedDragonofRa(SphereMode)' },
{ 'CotH', 'CalloftheHaunted' },
{ 'Fiend's Hand', 'FiendsHand' },
{ 'This is not a card name', 'Thisisnotacardname' },
{ 'M∀LICE Pawn White Rabbit', 'MalissPWhiteRabbit' },
{ 'M∀LICE <Pawn> White Rabbit', 'MalissPWhiteRabbit' },
{ 'M∀LICE CODE GWC-06', 'MalissCGWC06' },
{ 'M∀LICE <CODE> GWC-06', 'MalissCGWC06' },
{ 'M∀LICE <CODE> GWC-06', 'MalissCGWC06' },
{ 'Maliss <Q> RED RANSOM', 'MalissQREDRANSOM' }
}
for i, testCase in ipairs( testCases ) do
local inputValue = testCase[ 1 ]
local expectedValue = testCase[ 2 ]
local result = getCardImageName( inputValue )
local testPassed = result == expectedValue
mw.log( ( 'Case %d: %s' ):format( i, testPassed and 'PASSED' or 'FAILED' ) )
if not testPassed then
mw.log( '', ( 'Input: %s' ):format( inputValue ) )
mw.log( '', ( 'Expected: %s' ):format( expectedValue ) )
mw.log( '', ( 'Result: %s' ):format( result ) )
end
end
end
return setmetatable(
{
main = wikitextMain,
test = test,
},
{
__call = function( self, cardNameOrPagename )
return getCardImageName( cardNameOrPagename )
end,
}
)
-- </pre>