|
|
Line 1: |
Line 1: |
− | -- <pre>
| + | == Summary == |
− | -- @name Card image name
| + | Importing file |
− | -- @description Converts a card name to be used in the file name.
| |
− | -- @see {{Card image name}}
| |
− | -- @author [[User:Becasita]]
| |
− | -- @contact [[User talk:Becasita]]
| |
− | | |
− | -------------------
| |
− | -- Export variable:
| |
− | -------------------
| |
− | local CardImageName = {};
| |
− | | |
− | ----------------
| |
− | -- Load modules:
| |
− | ----------------
| |
− | local getArgs = require( 'Module:Arguments' ).getArgs;
| |
− | local DATA = require( 'Module:Data' );
| |
− | local UTIL = require( 'Module:Util' );
| |
− | | |
− | --------------------
| |
− | -- Module functions:
| |
− | --------------------
| |
− | -- @name getPageName
| |
− | -- @description
| |
− | local function getPageName( card )
| |
− | if not card then
| |
− | return '';
| |
− | end
| |
− | | |
− | local smwName;
| |
− | local query = mw.smw.ask( {
| |
− | ('[[%s]]'):format( card:gsub( '#', '' ) ),
| |
− | '?Page name=',
| |
− | limit = 1,
| |
− | mainlabel = '-'
| |
− | } );
| |
− | | |
− | if not query or UTIL.count( query ) == 0 or UTIL.count( query[1] ) == 0 then
| |
− | return '';
| |
− | end
| |
− | | |
− | return query[ 1 ][ 1 ];
| |
− | end
| |
− | | |
− | local function normalize( pagename )
| |
− | if not pagename then
| |
− | return '';
| |
− | end
| |
− | | |
− | return (mw.text.split( pagename, '%s*%(' )[ 1 ])
| |
− | :gsub( ' ' , '')
| |
− | :gsub( '%-', ''):gsub( '–' , '')
| |
− | :gsub( ',' , ''):gsub( '%.', ''):gsub( ':' , '')
| |
− | :gsub( '\'', ''):gsub( '"' , ''):gsub( '&' , '')
| |
− | :gsub( '%?', ''):gsub( '!' , '')
| |
− | :gsub( '%%', ''):gsub( '=' , '')
| |
− | :gsub( '/' , ''):gsub( '\\', '')
| |
− | :gsub( '☆' , ''):gsub( '・' , '')
| |
− | ;
| |
− | end
| |
− | | |
− | -- @name processArgs
| |
− | -- @description Handles args (template call vs. module call).
| |
− | local function processArgs( v )
| |
− | if UTIL.isString( v ) then
| |
− | -- If used through other modules.
| |
− | return { UTIL.trim( v ) };
| |
− | end
| |
− | | |
− | return getArgs( v, {
| |
− | trim = true,
| |
− | removeBlanks = true,
| |
− | parentOnly = true
| |
− | } );
| |
− | end
| |
− | | |
− | -- @name main
| |
− | -- @description Main function to be invoked. Handles args and execution.
| |
− | function CardImageName.main( frame )
| |
− | local args = processArgs( frame );
| |
− | local pagename = getPageName( args[ 1 ] );
| |
− | local imageName = normalize( pagename or args[ 1 ] );
| |
− | return imageName;
| |
− | end
| |
− | | |
− | ----------
| |
− | -- Return:
| |
− | ----------
| |
− | return CardImageName;
| |
− | -- </pre>
| |