Difference between revisions of "Module:Card type"

From Yugipedia
Jump to: navigation, search
(Fix on table count. Convert card name to page name sooner (on main). Start _type functions. _link.)
m (Fix on _trim.)
Line 11: Line 11:
 
--  If the input is only white space, returns nil.
 
--  If the input is only white space, returns nil.
 
function _trim( s )
 
function _trim( s )
     if s and not v:match( '^%s*$' ) then
+
     if s and not s:match( '^%s*$' ) then
 
         return mw.text.trim( s ); -- If not nil nor empty.
 
         return mw.text.trim( s ); -- If not nil nor empty.
 
     end
 
     end

Revision as of 01:35, 27 August 2017

--  Module for {{Card type}}.
--  @@@ for ideas. 
local CardType = {};

------------------
--  Aux functions:
------------------

--  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 s:match( '^%s*$' ) then
        return mw.text.trim( s ); -- If not nil nor empty.
    end
end

function _link( v, label )
    if type( v ) == 'string' then
        return '[['..v..'|'..(label or mw.text.split( v, ' %(' )[1])..']]';
    end
    if type( v ) == 'table' then
        return v --@@@
    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
    return counter;
end

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

function _redirect( card )
    
    return
end

--  Show function:
--  Similar to #show parser function.
--  Returns table with the results.
function _show( page, property )
    --  At this point, SMW is enabled and «name» is formatted properly (as pagename).
    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

-------------------
--  Main functions:
-------------------
function _monster( card )
    
    return
end

function _spell( card )
    
    return
end

function _trap( card )
    
    return
end

function _counter()
    return _link( 'Counter' );
end


function _type( card )
    local _cardTypeTable = _show( card, 'Card type' );
    local _cardType = _cardTypeTable and table.concat( _cardTypeTable, '\n' ):lower();
    if not _trim( _cardType ) then
        return _error( 'On «_type»; No card type available!' );
    end
    
    if _cardType:match('monster') then
        return _monster( card );
    elseif _cardType:match('spell')  then
        return _spell( card );
    elseif _cardType:match('trap')  then
        return _trap( card );
    elseif _cardType:match('counter') then
        return _counter();
    else
        return _error( 'On «_type»; Non-standard card type!' );
    end
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 _page = _card:gsub( '#', '' );
    --local track = _redirect( _card ) and _error
    return _type( _page );
end

return CardType;