Difference between revisions of "Module:Card table sets"

From Yugipedia
Jump to: navigation, search
(+"cts" class; copy widths from {{Card table set/header}} (this could probably be done better - in a separate style.css page if nothing else))
(From sandbox: Use the data module to get release dates and localized names. Use CSS classes.)
 
Line 12: Line 12:
 
local Reporter = require( 'Module:Reporter' )
 
local Reporter = require( 'Module:Reporter' )
 
local StringBuffer = require( 'Module:StringBuffer' )
 
local StringBuffer = require( 'Module:StringBuffer' )
local getReleaseDate = require( 'Module:GetReleaseDate' )
 
  
 
local LANGUAGE_ENGLISH = DATA.getLanguage( 'English' )
 
local LANGUAGE_ENGLISH = DATA.getLanguage( 'English' )
Line 101: Line 100:
 
end
 
end
  
local function createHeader( id, text, width, maxwidth )
+
local function createHeader( id, text )
local th = mwHtmlCreate( 'th' )
+
return tostring( mwHtmlCreate( 'th' )
 
:attr( 'scope', 'col' )
 
:attr( 'scope', 'col' )
 +
:addClass( 'cts__header--' .. id )
 
:wikitext( text )
 
:wikitext( text )
if width then
+
)
if maxwidth then
 
th:attr( 'style', 'min-width: ' .. tostring( width ) .. 'em; max-width: ' .. tostring( maxwidth ) .. 'em' )
 
else
 
th:attr( 'style', 'width: ' .. tostring( width ) .. 'em' )
 
end
 
end
 
return tostring( th )
 
 
end
 
end
  
 
local function createHeaderRow( languageFull )
 
local function createHeaderRow( languageFull )
 
local tr = mwHtmlCreate( 'tr' )
 
local tr = mwHtmlCreate( 'tr' )
:node( createHeader( 'release', 'Release', 4.5 ) )
+
:node( createHeader( 'release', 'Release' ) )
:node( createHeader( 'number', 'Number', 6 ) )
+
:node( createHeader( 'number', 'Number' ) )
 
:node( createHeader( 'set', 'Set' ) )
 
:node( createHeader( 'set', 'Set' ) )
  
Line 130: Line 123:
 
end
 
end
  
tr:node( createHeader( 'rarity', 'Rarity', 7, 12 ) )
+
tr:node( createHeader( 'rarity', 'Rarity' ) )
  
 
return tostring( tr )
 
return tostring( tr )
Line 141: Line 134:
 
end
 
end
  
local function createDataRow( region, languageFull, line, lineno )
+
local function createDataRow( region, language, line, lineno )
 
local parts = mwText.split( line, '%s*;%s*' )
 
local parts = mwText.split( line, '%s*;%s*' )
  
Line 161: Line 154:
  
 
local tr = mwHtmlCreate( 'tr' )
 
local tr = mwHtmlCreate( 'tr' )
:node( createCell( 'release', setName and getReleaseDate( setName, region ) ) )
+
:node( createCell( 'release', setName and DATA.getReleaseDate( setName, region ) ) )
 
:node( createCell( 'number', cardNumber and formatCardNumber( cardNumber ) ) )
 
:node( createCell( 'number', cardNumber and formatCardNumber( cardNumber ) ) )
 
:node( createCell( 'set', setName and UTIL.italicLink( setName ) ) )
 
:node( createCell( 'set', setName and UTIL.italicLink( setName ) ) )
  
if languageFull ~= LANGUAGE_ENGLISH.full then
+
if language.full ~= LANGUAGE_ENGLISH.full then
 
tr:node(
 
tr:node(
 
createCell(
 
createCell(
 
'set-localized',
 
'set-localized',
UTIL.getName( setName, languageFull )
+
DATA.getName( setName, language )
 
)
 
)
 
)
 
)
Line 203: Line 196:
 
lineno = lineno + 1
 
lineno = lineno + 1
  
setsTable:node( createDataRow( region, language.full, line, lineno ) )
+
setsTable:node( createDataRow( region, language, line, lineno ) )
 
end
 
end
 
end
 
end

Latest revision as of 01:18, 10 January 2020

Module documentation[create]
-- <pre>
--[=[Doc
@module Card table sets
@description Handles sets.
@author [[User:Becasita]]
@contact [[User talk:Becasita]]
]=]

local DATA = require( 'Module:Data' )
local UTIL = require( 'Module:Util' )

local Reporter = require( 'Module:Reporter' )
local StringBuffer = require( 'Module:StringBuffer' )

local LANGUAGE_ENGLISH = DATA.getLanguage( 'English' )

local mwText = mw.text
local mwHtmlCreate = mw.html.create

local reporter;

local function formatCardNumber( cardNumber )
	return cardNumber:match( '?' )
		and cardNumber
		or UTIL.link( cardNumber )
end

local function validateRarities( rarities, lineno )
	local validated = {}

	local position = 0

	for _, r in ipairs( rarities ) do
		if UTIL.trim( r ) then
			position = position + 1

			local rarity = DATA.getRarity( r )

			if rarity then
				table.insert( validated, rarity.full )
			else
				local message = ('No such rarity for `%s`, at non-empty input line %d, at non-empty position %d.')
					:format( r, lineno, position )

				reporter:addError( message )
			end
		end
	end

	return validated
end

local function printRarities( rarities )
	local linked = {}

	for _, rarity in ipairs( rarities ) do
		table.insert( linked, UTIL.link( rarity ) )
	end

	return table.concat( linked, '<br />' )
end

local function setSMWProps( cardNumber, setName, rarities, regionFull )
	if not mw.smw then
		reporter:addError( 'SMW is not available!' )

		return
	end

	local setInfoProp = ('--- %s --- %s --- %s --- %s ---')
		:format(
			cardNumber,
			setName,
			table.concat( rarities, ',' ),
			regionFull
		)

	local jsonRarities = {}

	for _, rarity in ipairs( rarities ) do
		table.insert( jsonRarities, '"' .. rarity .. '"' )
	end

	local setInfoJSONProp = ('{ "number": "%s", "name": "%s", "rarity": [%s], "region": "%s" }')
		:format(
			cardNumber,
			setName,
			table.concat( jsonRarities, ',' ),
			regionFull
		)

	local setRes = mw.smw.set{
		['Set information'] = setInfoProp,
		['Set information (JSON)'] = setInfoJSONProp,
	}

	if not setRes then
		reporter:addError( "Failed to set SMW properties for set information!" )
	end
end

local function createHeader( id, text )
	return tostring( mwHtmlCreate( 'th' )
		:attr( 'scope', 'col' )
		:addClass( 'cts__header--' .. id )
		:wikitext( text )
	)
end

local function createHeaderRow( languageFull )
	local tr = mwHtmlCreate( 'tr' )
		:node( createHeader( 'release', 'Release' ) )
		:node( createHeader( 'number', 'Number' ) )
		:node( createHeader( 'set', 'Set' ) )

	if languageFull ~= LANGUAGE_ENGLISH.full then
		tr:node(
			createHeader(
				'set-localized',
				table.concat{ languageFull, ' name' }
			)
		)
	end

	tr:node( createHeader( 'rarity', 'Rarity' ) )

	return tostring( tr )
end

local function createCell( id, text )
	return tostring( mwHtmlCreate( 'td' )
		:wikitext( text )
	)
end

local function createDataRow( region, language, line, lineno )
	local parts = mwText.split( line, '%s*;%s*' )

	local cardNumber = UTIL.trim( parts[ 1 ] )
	local setName = UTIL.trim( parts[ 2 ] )
	local rarities = UTIL.trim( parts[ 3 ] )
		and validateRarities(
			mwText.split( parts[ 3 ], '%s*,%s*' ),
			lineno
		)
		or {}

	if not setName then
		local message = ('No set name given at non-empty input line %d.')
			:format( lineno )

		reporter:addWarning( message )
	end

	local tr = mwHtmlCreate( 'tr' )
		:node( createCell( 'release', setName and DATA.getReleaseDate( setName, region ) ) )
		:node( createCell( 'number', cardNumber and formatCardNumber( cardNumber ) ) )
		:node( createCell( 'set', setName and UTIL.italicLink( setName ) ) )

	if language.full ~= LANGUAGE_ENGLISH.full then
		tr:node(
			createCell(
				'set-localized',
				DATA.getName( setName, language )
			)
		)
	end

	tr:node( createCell( 'rarity', printRarities( rarities ) ) )

	setSMWProps( cardNumber or '', setName or '', rarities, region.full )

	return tostring( tr )
end


local function main( regionInput, setsInput )
	reporter = Reporter( 'Card table sets' )

	local region = DATA.getRegion( regionInput ) -- TODO: handle incorrect regions (necessary?)

	local language = DATA.getLanguage( regionInput )

	local setsTable = mwHtmlCreate( 'table' )
		:addClass( 'wikitable' )
		:addClass( 'sortable' )
		:addClass( 'card-list' )
		:addClass( 'cts' )
		:node( createHeaderRow( language.full ) )

	if UTIL.trim( setsInput ) then
		local lineno = 0 -- Non-empty lines count.

		for line in mwText.gsplit( setsInput, '%s*\n%s*' ) do
			if UTIL.trim( line ) then
				lineno = lineno + 1

				setsTable:node( createDataRow( region, language, line, lineno ) )
			end
		end
	else
		local message = 'No input given for the sets.'

		local category = '((Card table sets)) transclusions with no input (((1)))'

		reporter
			:addError( message )
			:addCategory( category )
	end

	return StringBuffer()
		:add( reporter:dump() )
		:add( tostring( setsTable ) )
		:toString()
end

return setmetatable( {
	main = function( frame )
		local arguments = frame:getParent().args

		return main( arguments[ 'region' ], arguments[ 1 ] )
	end
}, {
	__call = function( t, ... )
		return main( ... )
	end,
} )
-- </pre>