Open main menu

Yugipedia β

Editing Module:Card gallery

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

This page is not enabled for semantic in-text annotations due to namespace restrictions. Details about how to enable the namespace can be found on the configuration help page.

Latest revision Your text
Line 1: Line 1:
 
-- <pre>
 
-- <pre>
-- @name Card gallery
+
-- NOTES:
-- @description Builds a card gallery section for a single region.
+
-- input in the form of:
-- @author [[User:Becasita]]
+
-- «
-- @contact [[User talk:Becasita]]
+
-- <card number>; <set>; <rarity>; <edition>; <alt>
 +
-- »
 +
-- Where "rarity" and "edition" can either be in full form or abbreviated.
 +
-- Example:
 +
-- «
 +
-- {{Card gallery|region=EN|
 +
-- TLM-EN012; The Lost Millennium; SR;  1E // extension::jpg
 +
-- TLM-EN012; The Lost Millennium; UtR; 1E // extension::jpg
 +
-- TLM-EN012; The Lost Millennium; SR;  UE
 +
-- TLM-EN012; The Lost Millennium; UtR; UE
 +
-- SDJ-035; Starter Deck: Joey; C; UE; Reprint // description::Spell
 +
-- S1; Yu-Gi-Oh! True Duel Monsters: Sealed Memories promotional cards; UR
 +
-- »
 +
 
 +
-- Ideas:
 +
-- * propagate:
 +
-- «
 +
-- TLM-EN012; The Lost Millennium; SR;  1E
 +
--        *;                  *; UtR;  *
 +
--        *;                  *; SR;  UE
 +
--        *;                  *; UtR;  *
 +
-- »
 +
-- Avoids repitition. Harder with a file replace script.
 +
 
 +
-- TODO:
 +
-- support for OP / GC / CT / etc. (<card number>; <set>; <rarity>; <edition>; <alt> :: GC|CT)
 +
 
 +
--------------------
 +
-- Global vairables:
 +
--------------------
 +
local CardGallery = {};
 +
local _D          = {}; -- Global data object.
  
 
----------------
 
----------------
 
-- Load modules:
 
-- Load modules:
 
----------------
 
----------------
local DATA = require( 'Module:Data' );
+
local getArgs = require( 'Module:Arguments' ).getArgs;
local UTIL = require( 'Module:Util' );
+
local DATA   = require( 'Module:Data' );
 +
local getName = require( 'Module:Name' ).main;
  
local InfoWrapper = require( 'Module:InfoWrapper' );
+
---------------------
local StringBuffer = require( 'Module:StringBuffer' );
+
-- Utility functions:
 +
---------------------
 +
-- mw functions:
 +
local  split = mw.text.split;
 +
local gsplit = mw.text.gsplit;
 +
local HTML  = mw.html.create;
  
local File = require( 'Module:Card gallery/File' );
+
-- @name _trim
 +
-- Trims white space from front and tail of string.
 +
-- If the input is only white space, returns nil.
 +
local function _trim( s )
 +
if s and not s:match( '^%s*$' ) then
 +
return mw.text.trim( s );
 +
end
 +
end
  
--------------------
+
-- @name _count
-- Module variables:
+
-- @description Counts how many entries a table has.
--------------------
+
local function _count( t )
-- This can be seen as the ast:
+
local counter = 0;
local CardGallery = InfoWrapper( 'Card gallery' );
+
for key, value in pairs( t ) do
 +
counter = counter + 1;
 +
end
 +
return counter;
 +
end
  
-- Parser state:
+
-- @name _link
local
+
-- @description TODO
PAGENAME,
+
local function _link( ... )
NAMESPACE,
+
-- TODO
_frame,
+
return;
_args,
+
end
_region,
 
_language,
 
_type,
 
_title,
 
_debug
 
;
 
local _files = {};
 
  
-- Methods:
+
-- @name _error
function CardGallery:getRegion()
+
-- Generates an error and places it on the error table.
return _region;
+
local function _error( message, default, category )
 +
_D.errors.exists = true;
 +
local err = HTML( 'div' ):css( 'padding-left', '1.6em' )
 +
:tag( 'strong' ):addClass( 'error' )
 +
:wikitext( ('Error: %s'):format( message ) )
 +
:done()
 +
:allDone();
 +
local cat = category and ('[[Category:%s]]'):format( category ) or '';
 +
table.insert(
 +
_D.errors, table.concat( {
 +
tostring( err ), cat
 +
} )
 +
);
 +
return default or '';
 
end
 
end
  
function CardGallery:getLanguage()
+
--------------
return _language;
+
-- File class:
 +
--------------
 +
-- @name File
 +
-- @classAttr counter -> [static] Counts the number of File instances.
 +
local File  = {};
 +
File.__index = File;
 +
File.counter = 0;
 +
 
 +
-- @name new          -> File constructor.
 +
-- @attr exists      -> Control attribute; denotes if a file is to be printed.
 +
-- @attr number      -> The card number (linked).
 +
-- @attr region      -> The region.
 +
-- @attr setEn        -> The English name for the set.
 +
-- @attr setLn        -> The localized name for the set.
 +
-- @attr setAbbr      -> The set abbreviation.
 +
-- @attr rarity      -> The rarity name.
 +
-- @attr r            -> The rarity abbreviation.
 +
-- @attr edition      -> The full edition.
 +
-- @attr ed          -> The edition abbreviation.
 +
-- @attr modifier    -> The card modifier.
 +
-- @attr modifierAbbr -> The card modifier abbreviation (OP|GC|CT).
 +
-- @attr alt          -> The alt value.
 +
-- @attr extension    -> The file extension.
 +
function File.new( std, mod, opt )
 +
-- @attr _standard -> Contains the trimmed input args for the standard input {enum-like}.
 +
-- @attr _modifier -> Contains the trimmed input arg for the modifier (OP|GC|CT).
 +
-- @attr _options  -> Contains the trimmed input args for the options {map-like}.
 +
File.counter = File.counter + 1;
 +
local fileData = {};
 +
fileData.exists  = true;
 +
fileData._standard = std or {};
 +
fileData._modifier = mod or '';
 +
fileData._options  = opt or {};
 +
 
 +
return setmetatable( fileData, File );
 
end
 
end
  
function CardGallery:getType()
+
-- @name setNumber
return _type;
+
-- @description Sets the «number» and «setAbbr» attributes.
 +
function File:setNumber()
 +
local cardNumber = self._standard[ 1 ];
 +
 
 +
if cardNumber == '' then
 +
self.exists = false;
 +
_error( ('No set abbreviation given for file input number «%d»!'):format( File.counter ) );
 +
return '';
 +
end
 +
 
 +
if cardNumber and cardNumber:match( '^%w%w%w%w?%-%w%w%w%w?%w?$' ) then
 +
-- Input like «TLM-EN012».
 +
self.number  = _link( cardNumber );
 +
self.setAbbr = cardNumber:match( '^(%w%w%w%w?)%-%w%w%w%w?%w?$' );
 +
else
 +
-- Input like «S1».
 +
self.number  = nil;
 +
self.setAbbr = cardNumber;
 +
end
 +
 
 +
return self;
 +
end
 +
--[[function File:setSet()
 +
self.
 
end
 
end
 
+
function File:setRarity()
---------------------
+
self.
-- Utility functions:
+
end
---------------------
+
function File:setEdition()
-- mw functions:
+
self.
local HTML = mw.html.create;
+
end
 +
function File:setModifier()
 +
self.
 +
end
 +
function File:setAlt()
 +
self.
 +
end
 +
function File:setExtension()
 +
self.
 +
end]]
  
 
--------------------
 
--------------------
 
-- Module functions:
 
-- Module functions:
 
--------------------
 
--------------------
local function getCardGalleryType( t )
+
-- @name getInfo
if type( t ) == 'string' then
 
return ( {
 
['anime']  = 'Anime',
 
['manga']  = 'Manga',
 
['ruhduel'] = 'Rush Duel',  ['ruh'] = 'Rush Duel', -- "s" is trimmed
 
['game']    = 'Video games', ['vg']  = 'Video games',
 
['other']  = 'Other',
 
} )[
 
mw.text.trim( t ):lower():gsub( "[%s%-s]", '' ):gsub( 'video', '' )
 
]
 
end
 
end
 
 
 
-- @name initInfo
 
 
-- @description Handles generic info.
 
-- @description Handles generic info.
local function initInfo()
+
local function getInfo()
-- Page:
 
local mwTitle  = mw.title.getCurrentTitle();
 
PAGENAME  = mwTitle.text;
 
NAMESPACE = mwTitle.nsText;
 
 
 
 
-- Region and language:
 
-- Region and language:
_region = DATA.getRegion( _args[ 'region' ] ) or CardGallery:error(
+
_D.rg      = DATA.getRg( _D.args[ 'region' ] ) or _error(
('Invalid «region»: %s!'):format( _args[ 'region' ] or '(no region given)' ),
+
('Invalid «region»: «%!'):format( _D.args[ 'region' ] or '(no region given)' ), DATA.getRg( 'en' )
DATA.getRegion( 'en' )
 
 
);
 
);
_language = DATA.getLanguage( _region.index );
+
_D.region  = DATA.getRegion( _D.rg );
 +
_D.ln      = DATA.getLn( _D.rg );
 +
_D.language = DATA.getLanguage( _D.rg );
 +
 +
-- Flags:
 +
_D.flags            = {};
 +
_D.flags.notEnglish = _D.ln ~= 'en'; -- TODO may not be needed!
 +
_D.flags.italics    = not ((_D.ln == 'ja') or (_D.ln == 'zh') or (_D.ln == 'ko')) --[[and 'normal' or 'italic']];
 +
 +
-- Medium:
 +
_D.cg = _D.flags.italics and 'TCG' or 'OCG'; -- TODO Probably not gonna use that.
 +
 +
-- Card and page:
 +
_D.PAGENAME  = mw.title.getCurrentTitle().text;
 +
_D.NAMESPACE = mw.title.getCurrentTitle().nsText;
 +
_D.name      = getName( _D.PAGENAME, _D.language );
  
-- Type of gallery:
 
_type  = getCardGalleryType( _args[ 'type' ] );
 
_title = _args[ 'title' ];
 
_debug = _args[ 'debug' ];
 
 
end
 
end
  
-- @name getFiles
+
-- @name wrapInQuotes
-- @description Assembles the file entries and prepares them to be parsed.
+
-- @description Wraps «name» in proper quotation marks.
local function initFiles()
+
local function wrapInQuotes( name, std )
if not _args[ 1 ] then
+
if not _trim( name ) then
return CardGallery:error(
+
return '';  --  Return empty string.
( _frame.args[ 1 ] and 'Empty' or 'No' ) .. 'input provided for the gallery!'
 
);
 
 
end
 
end
 +
return (std or (_D.ln ~= 'ja' and _D.ln ~= 'zh'))
 +
and table.concat( { '"', name, '"' } )
 +
or table.concat( { '「', name, '」' } )
 +
;
 +
end
  
for inputEntry in mw.text.gsplit( _args[ 1 ], '\n' ) do
+
-- @name printErrors
local entry = UTIL.trim( inputEntry ) and File.factory( CardGallery, inputEntry );
+
-- @dascription Stringifies the errors table.
if entry then
+
local function printErrors()
table.insert( _files, entry );
+
local category = '[[Category:((Card gallery)) transclusion to be checked]]';
end
+
if not _D.errors.exists then
 +
return '';
 
end
 
end
 +
table.insert( _D.errors, category );
 +
return table.concat( _D.errors, '\n' );
 
end
 
end
  
-- @description Builds the mediawiki section header.
+
-- @name buildHeader
local function getMwSectionHeader()
+
-- @description builds the gallery header.
return (
+
local function buildHeader()
_type and '== %s - %s ==' or '== %s =='
+
return HTML( 'div' ):addClass( 'gallery-header' ):attr( 'id', ('%s_section'):format( _D.rg ) )
):format(
+
:tag( 'div' )
(_type or _title) or _region.full, _region.full
+
:wikitext( wrapInQuotes( _D.name ) )
);
+
:done()
 +
:tag( 'div' )
 +
:wikitext( _D.region )
 +
:done()
 +
:allDone();
 
end
 
end
  
-- @description Builds the ToC skeleton.
+
 
local function getToC()
+
local function _buildStandard( arg, modifier )
return tostring(
+
local standard = {};
HTML( 'div' ):addClass( 'card-gallery__toc' )
+
 
:tag( 'ul' )
+
if not _trim( arg ) then
:done()
+
return standard;
:allDone()
+
end
);
+
 +
for value in gsplit( arg, '%s*;%s*' ) do
 +
if value then
 +
table.insert( standard, _trim( value ) or '' );
 +
end
 +
end
 +
 
 +
return standard;
 
end
 
end
  
-- @description Builds the errors' log.
+
local function _buildOptions( arg )
local function getErrors()
+
local options = {};
return tostring(
+
 
HTML( 'div' )
+
if not _trim( arg ) then
:addClass( 'card-gallery__errors' )
+
return options;
:tag( 'ul' )
+
end
:node(
+
 
CardGallery:dumpErrors( function( err )
+
-- TODO
return tostring(
+
for option in gsplit( tempOpt, '%s*;%s*' ) do
HTML( 'li' )
+
local opt = _trim( option );
:tag( 'strong' )
+
if opt then
:wikitext( err )
+
local optTemp = split( opt, '%s*::%s*' );
:done()
+
local optName  = _trim( optTemp[ 1 ] );
:allDone()
+
local optValue = _trim( optTemp[ 2 ] );
)
+
if optName and optValue then
end )
+
options[ optName ] = optValue;
)
+
end
:done()
+
end
:allDone()
+
end
);
+
 
 +
return options;
 
end
 
end
  
-- @description Builds the gallery itself.
+
-- @name splitEntry
local function getGallery()
+
-- @description Splits an input entry into the standard valus, the modifier and the options.
local gallery = StringBuffer()
+
local function splitEntry( entry )
:addLine( '<gallery heights="175px" position="center" captionalign="center">' )
+
if not _trim( entry ) then
;
+
return {}, '', {};
for _, file in ipairs( _files ) do
 
gallery:addLine( file:render() );
 
 
end
 
end
gallery:addLine( '</gallery>' );
 
  
return tostring(
+
local temp1  = split( entry, '%s*//%s*' );
HTML( 'div' )
+
local tempOpt = temp1[ 2 ] or '';
:addClass( 'card-gallery__gallery' )
+
local temp2  = split( temp1[ 1 ], '%s*::%s*' );
:node(
+
local tempMod = temp2[ 2 ];
_debug and tostring(
+
local tempStd = temp2[ 1 ];
HTML( 'pre' ):node( gallery:toString() ):done()
+
 
) or _frame:preprocess(
+
local modifier = _trim( tempMod );
gallery:toString()
+
local standard = _buildStandard( tempStd, modifier );
)
+
local options  = _buildOptions( tempOpt );
)
+
 
:allDone()
+
return standard, modifier, options;
);
+
end
 +
 
 +
-- @name process
 +
-- @description Processes a gallery input entry and converts it into raw wikitext markup.
 +
-- TODO
 +
local function process( entry )
 +
local standard, modifier, options = splitEntry( entry );
 +
local fileObject = File.new( standard, modifier, options ); -- TODO
 +
return table.concat( { -- TODO: TEST
 +
table.concat( standard ),
 +
'modifier: ' .. modifier or 'nil',
 +
type( options ) .. _count( options )
 +
}, '\n' );
 
end
 
end
  
-- @description Aggregates the categories.
+
-- @name buildInnerGallery
local function getCategories()
+
-- TODO
return tostring(
+
local function buildInnerGallery()
HTML( 'div' )
+
if (_D.args[ 1 ] or '') == '' then
:addClass( 'card-gallery__categories' )
+
-- TODO: Error?
:wikitext(
+
return ''
CardGallery:dumpCategories()
+
end
)
+
-- <card number>; <set>; <rarity>; <edition>; <alt> :: <modifier> // <option1>::<value1>; <optionN>::<valueN>
:allDone()
+
local galleryEntries = {};
);
+
for inputEntry in gsplit( _D.args[ 1 ], '\n' ) do
 +
local entry = _trim( inputEntry ) and process( inputEntry );
 +
if entry then
 +
table.insert( galleryEntries, entry );
 +
end
 +
end
 +
 
 +
return table.concat( galleryEntries, '\n' );
 
end
 
end
  
local function render()
+
-- @name buildGallery
local buffer = StringBuffer()
+
-- TODO
:addLine( '<div id="card-gallery--' .. _region.index .. '" class="card-gallery">' )
+
local function buildGallery()
:addLine( getMwSectionHeader() )
+
local inner = buildInnerGallery();
:addLine( getToC() )
+
return table.concat( {
:addLine( getErrors() )
+
'<gallery heights="175px" position="center" captionalign="center">',
:addLine( getGallery() )
+
inner,
:addLine( getCategories() )
+
'</gallery>'
:add( '</div>' )
+
}, '\n' );
;
 
 
return buffer:toString();
 
 
end
 
end
  
 
-- @name main
 
-- @name main
 +
-- @notes exportable
 
-- @description To be called through #invoke.
 
-- @description To be called through #invoke.
local function main( frame )
+
function CardGallery.main( frame )
_frame = frame;
+
_D.errors = {};
_args  = UTIL.getArgs( frame, {
+
_D.args  = getArgs( frame, {
 
trim        = true,
 
trim        = true,
 
removeBlanks = true,
 
removeBlanks = true,
Line 211: Line 354:
 
} );
 
} );
  
initInfo();
+
getInfo();
initFiles();
+
 
 +
local errors  = printErrors();
 +
local header  = buildHeader();
 +
local gallery = buildGallery();
  
return render();
+
return HTML( 'div' ):addClass( 'card-galleries' )
 +
:node( tostring( header ) )
 +
:newline()
 +
:node(  errors )
 +
:newline()
 +
:node( frame:preprocess( gallery ) )
 +
:allDone();
 
end
 
end
  
----------
+
return CardGallery;
-- Return:
 
----------
 
-- @exports
 
return {
 
['main'] = main
 
};
 
-- </pre>
 

Please note that all contributions to Yugipedia are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 International License (see Yugipedia:Licensing for more details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)
Preview page with this template
Below are some commonly used wiki markup codes. Simply click on what you want to use and it will appear in the edit box above.

View this template