Difference between revisions of "Module:Card type"
(Starting, for {{Card type}}.) |
(Fix on table count. Convert card name to page name sooner (on main). Start _type functions. _link.) |
||
Line 2: | Line 2: | ||
-- @@@ for ideas. | -- @@@ for ideas. | ||
local CardType = {}; | local CardType = {}; | ||
+ | |||
+ | ------------------ | ||
+ | -- Aux functions: | ||
+ | ------------------ | ||
-- Trim function: | -- Trim function: | ||
Line 9: | Line 13: | ||
if s and not v:match( '^%s*$' ) then | if s and not v: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 | ||
+ | |||
+ | 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 | ||
end | end | ||
Line 20: | Line 33: | ||
local counter = 0; | local counter = 0; | ||
for _,v in pairs( t ) do | for _,v in pairs( t ) do | ||
− | |||
− | |||
− | |||
counter = counter + 1; | counter = counter + 1; | ||
end | end | ||
Line 29: | Line 39: | ||
function _error( message ) | function _error( message ) | ||
− | local _error = mw.html.create( 'div' ):addClass( 'error' ):wikitext( 'Error: '..message ):done(); | + | local _error = mw.html.create( 'div' ) |
+ | :tag( 'strong' ):addClass( 'error' ):wikitext( 'Error: '..message ):done() | ||
+ | :AllDone(); | ||
return tostring( _error ); | return tostring( _error ); | ||
end | end | ||
Line 41: | Line 53: | ||
-- Similar to #show parser function. | -- Similar to #show parser function. | ||
-- Returns table with the results. | -- Returns table with the results. | ||
− | function | + | 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 = '-' }; | local result = mw.smw.ask{ '[['..page..']]', '?'..property..'=', mainlabel = '-' }; | ||
Line 55: | Line 67: | ||
return t; | return t; | ||
end | 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 ) | 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 | ||
− | return | + | 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 | end | ||
Line 82: | Line 132: | ||
return _error( 'Empty value!' ); | return _error( 'Empty value!' ); | ||
end | end | ||
+ | local _page = _card:gsub( '#', '' ); | ||
--local track = _redirect( _card ) and _error | --local track = _redirect( _card ) and _error | ||
− | return _type( | + | return _type( _page ); |
end | end | ||
return CardType; | return CardType; |
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 v: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;