Module:Card type

From Yugipedia
Revision as of 00:52, 27 August 2017 by Becasita (talk | contribs) (Starting, for {{Card type}}.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
--  Module for {{Card type}}.
--  @@@ for ideas. 
local CardType = {};

--  Trim function:
--  Trims white space from front and tail of string.
--  If the input is only white space, returns nil.
function _trim( s )
    if s and not v:match( '^%s*$' ) then
        return mw.text.trim( s ); -- If not nil nor empty.
    end
end

--  Unlink function:
function _unlink( s )
    return _trim( s ) and (s:match( '%[%[:?(.-)[|%]]' ) or _trim( s ));
end

function _count( t )
    local counter = 0;
    for _,v in pairs( t ) do
        counter = counter + 1;
    end
    for _,v in ipairs( t ) do
        counter = counter + 1;
    end
    return counter;
end

function _error( message )
    local _error = mw.html.create( 'div' ):addClass( 'error' ):wikitext( 'Error: '..message ):done();
    return tostring( _error );
end

function _redirect( card )
    
    return
end

--  Show function:
--  Similar to #show parser function.
--  Returns table with the results.
function CardType._show( name, property )
    local page = name:gsub( '#', '' );
    local result =  mw.smw.ask{ '[['..page..']]', '?'..property..'=', mainlabel = '-' };
    
    if _count( result ) == 0 then
        return;
    end
    
    local t = {};
    for key, value in ipairs( result[1][1] ) do
        table.insert( t, _unlink( value ))
    end
    return t; 
end

function _type( card )
    
    return
end

function CardType.main( frame )
    if not mw.smw then
        return _error( 'mw.smw module not found' );
    end
    --  «args» for the args table;
    --  «argsN» for the number or arguments;
    --  «_card» for the card to be processed;
    --  «track» for tracking stuff;
    local args = frame.args; -- Args table:
    
    local argsN = _count( args );
    if argsN > 1 then
        return _error( 'Too many arguments! Use only one!' );
    elseif argsN < 1 then
        return _error( 'No arguments! Use one!' );
    end
    
    local _card = _unlink( args[1] );
    if not _card then
        return _error( 'Empty value!' );
    end
    --local track = _redirect( _card ) and _error
    return _type( _card );
end

return CardType;