Module:Utils: Difference between revisions
From Halopedia, the Halo wiki
No edit summary |
No edit summary |
||
Line 43: | Line 43: | ||
.. category | .. category | ||
.. ']]' | .. ']]' | ||
end | |||
function utils.warning( ... ) | |||
-- Collect necessary information to display warning | |||
local message = tostring( utils.defaultIfEmpty( select( 1, ... ), 'There was an error with a template. Are the parameters all correct?' ) ) | |||
local wtype = tostring( utils.defaultIfEmpty( select( 2, ... ), 'Template' ) ) | |||
local category = tostring( utils.defaultIfEmpty( select( 3, ... ), 'Pages containing template warnings' ) ) | |||
local template = utils.defaultIfEmpty( select( 4, ... ), nil ) | |||
-- Style data | |||
wtype = string.upper( wtype ) | |||
-- Add a warning to the edit preview | |||
if template == nil then | |||
mw.addWarning( "'''" .. wtype .. " WARNING:''' " .. message ) | |||
else | |||
mw.addWarning( "'''" .. wtype .. " WARNING:''' " .. message .. ' (Template name: "' .. tostring( template ) .. '")' ) | |||
end | |||
-- Return a blank string (so that this function has the same return type | |||
-- signature as the error function - where it returns a string that should | |||
-- be concatenated into the returned wikitext) | |||
return '' | |||
end | end | ||
return utils | return utils |
Revision as of 14:21, April 24, 2021
Documentation for this module may be created at ModuleDoc:Utils
local utils = {}
function utils.defaultIfEmpty( val, default )
if val == nil or val == '' then
return default
else
return val
end
end
function utils.loadDataIfExists( name )
local success, data = pcall( mw.loadData, name )
if success then
return data
else
return nil
end
end
function utils.error( ... )
-- Collect necessary information to display error
local message = tostring( utils.defaultIfEmpty( select( 1, ... ), 'There was an error with a template. Are the parameters all correct?' ) )
local etype = tostring( utils.defaultIfEmpty( select( 2, ... ), 'Template' ) )
local category = tostring( utils.defaultIfEmpty( select( 3, ... ), 'Pages containing template errors' ) )
local template = utils.defaultIfEmpty( select( 4, ... ), nil )
-- Style data
etype = string.upper( etype )
-- Add a warning to the edit preview
if template == nil then
mw.addWarning( "'''" .. etype .. " ERROR:''' " .. message )
else
mw.addWarning( "'''" .. etype .. " ERROR:''' " .. message .. ' (Template name: "' .. tostring( template ) .. '")' )
end
-- Create and return the text to display on the page itself
return [[<span class="template-error" style="color: red;">''']]
.. etype
.. [[ ERROR:''' ]]
.. message
.. '</span> [[Category:'
.. category
.. ']]'
end
function utils.warning( ... )
-- Collect necessary information to display warning
local message = tostring( utils.defaultIfEmpty( select( 1, ... ), 'There was an error with a template. Are the parameters all correct?' ) )
local wtype = tostring( utils.defaultIfEmpty( select( 2, ... ), 'Template' ) )
local category = tostring( utils.defaultIfEmpty( select( 3, ... ), 'Pages containing template warnings' ) )
local template = utils.defaultIfEmpty( select( 4, ... ), nil )
-- Style data
wtype = string.upper( wtype )
-- Add a warning to the edit preview
if template == nil then
mw.addWarning( "'''" .. wtype .. " WARNING:''' " .. message )
else
mw.addWarning( "'''" .. wtype .. " WARNING:''' " .. message .. ' (Template name: "' .. tostring( template ) .. '")' )
end
-- Return a blank string (so that this function has the same return type
-- signature as the error function - where it returns a string that should
-- be concatenated into the returned wikitext)
return ''
end
return utils