Module:Card image name
Implements {{Card image name}}.
Wikitext usage
The module exports a function for wikitext:
main
- Expects one unnamed argument: a card name or a pagename.
Modules usage
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]]
]=]
local function getEnglishName( cardNameOrpagename )
local query = mw.smw.ask( {
( '[[%s]]' ):format( cardNameOrpagename:gsub( '#', '' ) ),
'?English name=',
limit = 1,
mainlabel = '-'
} )
return ( ( query or {} )[ 1 ] or {} )[ 1 ] or cardNameOrpagename
end
local function removeDab( name )
return mw.text.split( name, '%s*%(' )[ 1 ]
end
local function processChars( name )
local normalizedName = name:gsub( '[ #–,%.:\'"%?!&@%%=%[%]<>/\\☆★・-]', '' )
-- 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' },
}
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>