Module:Set gallery

From Yugipedia
Jump to: navigation, search
Module documentation[view] [edit] [history] [purge]

Implements {{Set gallery}}.

See also Module:Set gallery/functions and Module:Set gallery/data.

--<pre>
--[[
    ###########
    # Var list:
    ###########
    ->  G                       //  ;
    ->  A                       //  getArgs;
    ->  F                       //  Auxiliary functions;
    -------
    # Data:
    ->  data                    //  Loads Module:Set gallery/data values;
    ->  rg_list                 //  Region code list;
    ->  region_list             //  Region list;
    ->  ln_list                 //  Language code list;
    ->  language_list           //  Language list;
    ->  ed_list                 //  Edition code list;
    ->  edition_list            //  Edition list;
    ->  medium_list             //  Medium list;
    ->  rarity_list             //  Rarity list;
    ----------
    # General:
    ->  parameters              //  Table with the parameters values;
    ->  PAGENAME                //  {{PAGENAME}};
    ->  NAMESPACE               //  {{NAMESPACE}};
    -----------
    # Set info:
    ->  _rg                     //  Region code;
    ->  _region                 //  Region name;
    ->  _ln                     //  Language code;
    ->  _language               //  Language name;
    ->  _ed                     //  Edition code;
    ->  _edition                //  Edition name;
    ->  _medium                 //  Medium (OCG or TCG);
    ->  _nameSet                //  Set name;
    ->  _nameSet_localized      //  Localized set name;
    ->  _abbr                   //  {{{abbr}}};
    ->  _rarity                 //  {{{rarity}}};
    ->  _alt                    //  {{{alt}}};
    -------
    # Page:
    ->  debug                   //  Debug text;
    ->  categories              //  Page categories;
    ->  header                  //  Page header;
    ->  gallery                 //  The page gallery to display;
    --------
    # Flags:
    ->  notEnglish              //  Boolean;
    ->  italics                 //  Italicize;
    ------
    # Aux:
    ->  nameSet_parts           //  Array with «_nameSet» split by «(»;
    ->  gallery_table           //  Table containing {{{1}}} entries by «\n»;
    
    ################
    # Function list:
    ################
    -----
    # MW:
    ->  split                   //  mw.text.split;
    -------
    # Main:
    ->  main                    //  For {{Set gallery}};
    ->  
    ------
    # Aux: (from F)
    ->  localized_name          //  Get localized name;
    ->  categories              //  Generate categories;
    ->  header                  //  Generate page header;
    ->  gallery                 //  Generate the gallery;
]]
----------
--  Start:
----------
local G = {};

--------------
--  Load data:
local data          = mw.loadData( 'Module:Set gallery/data' );
--  # Assign:
local rg_list       = data.rg_list;
local region_list   = data.region_list;
local ln_list       = data.ln_list;
local language_list = data.language_list;
local ed_list       = data.ed_list;
local edition_list  = data.edition_list;
local medium_list   = data.medium_list;

-----------------
--  Load modules:
local A = require( 'Module:Arguments' ).getArgs;
local F = require( 'Module:Set gallery/functions' );    --  Load auxiliary functions.

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

------------------
--  Main function:
function G.main( frame )
    local parameters = A(frame, { trim = true, removeBlanks = true, parentOnly = true });  -- Fetch parameter's values.
    --  { '1' or 'gallery' , 'region' , 'set' , 'abbr' , 'rarity' , 'alt', 'debug' }
    local PAGENAME   = mw.title.getCurrentTitle().text;     --  {{PAGENAME}}
    local NAMESPACE  = mw.title.getCurrentTitle().nsText;   --  {{NAMESPACE}}
    
    ------------------------
    --  Region and language:
    local _rg       = parameters['region'] and parameters['region']:lower()   --  Either use {{{region}}},
                      or PAGENAME:match( 'CG%-(%a*)%-?' ):lower();    --  Or fetch from the pagename.
    local _region   = region_list[_rg] or region_list['default'];
    local _ln       = ln_list[_rg];
    local _language = language_list[_ln];
    local _ed       = PAGENAME:match( 'CG%-%a*%-?(%d*%a*)' ):lower();
    local _edition  = edition_list[_ed];    --  NOTE: can be nil!
    local _medium   = medium_list[_rg] or 'TCG';
    
    --  Flags:
    local flag_notEnglish = _ln ~= 'en';
    local flag_italics    = ((_ln == 'ja') or (_ln == 'zh') or (_ln == 'ko')) and 'normal' or 'italic';
    
    -------------
    --  Set info:
    --  # Set name:
    local _nameSet = parameters['set'];
    if _nameSet == nil then
        local nameSet_parts = split( PAGENAME, ' %(' );
        if nameSet_parts[3] == nil or nameSet_parts[3] == '' then
            _nameSet = nameSet_parts[1];  -- Set name doesn't contain dab text.
        else
            _nameSet = nameSet_parts[1] ..' ('.. nameSet_parts[2];  -- Set name contains dab text.
        end
    end
    local _nameSet_localized = F.localized_name( _nameSet, _language );
    
    --  # Set abbreviation:
    local _abbr = parameters['abbr'];
    
    --  # General card info:
    local _rarity = parameters['rarity'];
    local _alt    = parameters['alt'];
    
    ---------------
    --  Categories:
    local categories = F.categories( NAMESPACE, _region, _edition );
    
    ----------------
    --  Page header:
    local header = F.header( frame, _nameSet, _nameSet_localized, _medium, _region, _edition, flag_notEnglish, flag_italics );
    
    --------------------
    --  Process gallery:
    local gallery_table = assert(
                                          (parameters[1] or parameters['1'] or parameters['gallery']) and
                                    split( parameters[1] or parameters['1'] or parameters['gallery'], '\n' ),
                                    'No gallery info given!'
                                );
    local gallery = F.gallery( frame, gallery_table, _rg, _language, _ed, _abbr, _rarity, _alt, flag_notEnglish );
    
    -------------
    --  Tracking:
    local tracking = F.tracking( frame:getParent().args ) or '';
    
    ----------
    --  Debug:
    local debug = mw.html.create( 'ul' )
        :tag('li'):tag('code'):wikitext('PAGENAME'):done():wikitext(': '..PAGENAME):done()
        :tag('li'):tag('code'):wikitext('NAMESPACE'):done():wikitext(': '..NAMESPACE):done()
        :tag('li'):tag('code'):wikitext('_rg'):done():wikitext(': '.._rg):done()
        :tag('li'):tag('code'):wikitext('_region'):done():wikitext(': '.._region):done()
        :tag('li'):tag('code'):wikitext('_ln'):done():wikitext(': '.._ln):done()
        :tag('li'):tag('code'):wikitext('_language'):done():wikitext(': '.._language):done()
        :tag('li'):tag('code'):wikitext('_ed'):done():wikitext(': '.._ed):done()
        :tag('li'):tag('code'):wikitext('_edition'):done():wikitext(': '..tostring(_edition)):done()
        :tag('li'):tag('code'):wikitext('_medium'):done():wikitext(': '.._medium):done()
        :tag('li'):tag('code'):wikitext('_nameSet'):done():wikitext(': '.._nameSet):done()
        :tag('li'):tag('code'):wikitext('_nameSet_localized'):done():wikitext(': '..tostring(_nameSet_localized)):done()
        :tag('li'):tag('code'):wikitext('_rarity'):done():wikitext(': '..tostring(_rarity)):done()
        :tag('li'):tag('code'):wikitext('_alt'):done():wikitext(': '..tostring(_alt)):done()
        :tag('li'):wikitext('<b>Flags</b>'):done()
        :tag('li'):tag('code'):wikitext('flag_notEnglish'):done():wikitext(': '..tostring(flag_notEnglish)):done()
        :tag('li'):tag('code'):wikitext('flag_italics'):done():wikitext(': '..flag_italics):done()
    :done()
    
    -----------------
    --  Return value:
    local set_gallery = header..    --  Page header.
                        gallery..   --  The gallery.
                        categories..--  Categories.
                        tracking;   --  Tracking.
        if parameters['debug'] then
            set_gallery = tostring(debug)..'\n'..set_gallery;
        end
    
    return set_gallery;
end

return G;

--</pre>