Module:Set list/sandbox

-- --  Module for , -- to be used like. -- @@@ for ideas. --- local SetList = {};

- -- Load modules: - local getArgs = require( 'Dev:Arguments' ).getArgs;

-- -- Load data: -- local _D           = {}; local data         = mw.loadData( 'Module:Set list/data' ); local rg_list      = data.rg_list;         --  Region code table. local region_list  = data.region_list;     --  Region name table. local ln_list      = data.ln_list;         --  Language code table. local language_list = data.language_list;  --  Language name table. local rarity_list  = data.rarity_list;     --  Rarity name table.

-- -- Utility functions: -- -- mw functions: local split = mw.text.split;

-- 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

-- Link function: -- If a string is passed, links it; -- uses a label, when given. -- If a table is passed, links every instance of it; -- uses a table of labels, respectively for each entry, when given. function _link( v, ... ) local labels = type( ... or nil ) == 'table' and ... or { ... };  --  Make sure it's a table. local function __link( v, label ) local l = _trim( label ) or (v:match( '%(2011%)' ) or v:match( '%(series%)' )) and v               or split( v, '%s*%(' )[1];        return ('%s'):format( v, l );    end    if type( v ) == 'string' then        return __link( v, labels[1] );    elseif type( v ) == 'table' then        local t = {};        for key, value in ipairs( v ) do            table.insert( t, __link( value, labels[key] ));        end        return t;    else        return v; -- @@@ error    end end

-- Table count function: -- Given a table, counts how many valuee it has. function _count( t ) -- @@@ error for wrong type local counter = 0; for key, value in pairs( t ) do       counter = counter + 1; end return counter; end

function _error( message ) -- @@@ input list of categories. _D.errors  = {}; local _error= mw.html.create( 'div' ) :tag( 'strong' ):addClass( 'error' ):wikitext( ('Error: %s'):format( message ) ):done :allDone; local cat = ''; return ; -- @@@ or error end

--- -- Main functions: --- -- Name function: -- Returns two values: -- The English name; the localized name. function getSetName( name ) local t = split( name, '%s*%(' );   local _name = _trim( t[3] ) and table.concat( t, ' (', 1, 2 ) or t[1]; local _nameLocal = _D.flags['notEnglish'] and getNameLocal( _name ) or nil; return _name, _nameLocal; end

-- Local name function: -- Return the localized name. function getNameLocal( name ) if not mw.smw then return _error( 'mw.smw module not found' ); end local _page = name:gsub( '#', '' ); local _query = { ('%s'):format( _page ), ('?%s name='):format( _D.language ), limit = 1, mainlabel = '-' }; local query = mw.smw.ask( _query ); if not query or _count( query ) == 0 or _count( query[1] ) == 0 then return; end return query[1][1]; end

-- Info function: -- Handle region/language info. function getInfo _D.rg      = _D.args['region'] and _D.args['region']:lower or _error( 'A «region» is always needed!' ); _D.region  = region_list[_D.rg]; _D.ln      = ln_list[_D.rg]; _D.language = language_list[_D.ln]; -- Flags: _D.flags   = {}; _D.flags.notEnglish = _D.ln ~= 'en'; _D.flags.italics   = ((_D.ln == 'ja') or (_D.ln == 'zh') or (_D.ln == 'ko')) and 'normal' or 'italic'; end

-- Page header function: -- Build page header (HTML). function getHeader( setName, setNameLocal ) local CSS = { {  --  First «span» tag (set name). ['font-size']  = '120%', ['font-weight'] = 'bold', ['font-style'] = 'italic' },                   {   --  Second «span» tag (local set name). ['font-weight'] ='bold', ['font-style'] = _D.flags['italics'] }               };    local header = mw.html.create( 'div' ):css( 'text-align', 'center' ) :tag( 'span' ):css( CSS[1] ) :wikitext( _link( setName ) ) :done; if _trim( setNameLocal ) then header:tag( 'br' ):done :tag( 'span' ):css( CSS[2] ) :wikitext( setNameLocal ) :done; end header:tag( 'br' ):done :wikitext( _D.region ) :allDone; return tostring( header ); end

-- Categories function: -- Generate categories. function getCategory( ns ) -- @@@ Make it similar to _link. return ( '' ):format( _D.region, ns ); end

-- Main function: -- To be called through #invoke. function SetList.main( frame ) _D.args        = frame.args --getArgs( frame, { trim = true, removeBlanks = true, parentOnly = true } ); local PAGENAME = mw.title.getCurrentTitle.text;     -- local NAMESPACE = mw.title.getCurrentTitle.nsText;  -- getInfo; local _setName, _setNameLocal = getSetName( _D.args['set'] or PAGENAME ); local header   = getHeader( _setName, _setNameLocal ); local category = getCategory( NAMESPACE ); return table.concat( { _D.rg, _D.region, _D.ln, _D.language, _setName, _setNameLocal or '' }, '\n*' ) .. header..category; end

return SetList; --