Module:Utils: Difference between revisions

m
no edit summary
No edit summary
mNo edit summary
 
(20 intermediate revisions by the same user not shown)
Line 1: Line 1:
local utils = {}
local utils = {}


function utils.tableEmpty( tab )
function utils.strip( str )
return next( tab ) == nil
return mw.ustring.gsub(str, '^%s*(.-)%s*$', '%1')
end
 
function utils.empty( val )
return val == nil or val == ''
end
end


function utils.defaultIfEmpty( val, default )
function utils.defaultIfEmpty( val, default )
if val == nil or val == '' or ( type( val ) == 'table' and utils.tableEmpty( val ) ) then
if val == nil or val == '' then
return default
return default
else
else
Line 20: Line 24:
return nil
return nil
end
end
end
function generateErrorWikitext( message, type, category, template )
local wikitext = [[''']]
.. string.upper( type )
.. [[:''' ]]
.. message
if template ~= nil then
wikitext = wikitext .. ' (Template: [[Template:'
.. template
.. '|'
.. template
.. ']])'
end
local categorylink = '[[Category:' .. category .. ']]'
return wikitext, categorylink
end
end


Line 29: Line 51:
local template = utils.defaultIfEmpty( select( 4, ... ), nil )
local template = utils.defaultIfEmpty( select( 4, ... ), nil )
-- Style data  
-- Generate  data  
etype = string.upper( etype )
local wikitext, categorylink = generateErrorWikitext( message, etype .. ' error', category, template )
-- Add custom styling
wikitext = [[<span class="error halopedia-template-error" style="color: red;">]] .. wikitext .. [[</span>]]
-- Add a warning to the edit preview
-- Add a warning to the edit preview
if template == nil then
mw.addWarning( wikitext )
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
-- Create and return the text to display on the page itself
return [[<span class="template-error" style="color: red;">''']]
return wikitext .. ' ' .. categorylink
.. etype
end
.. [[ ERROR:''' ]]
 
.. message
function utils.warning( ... )
.. '</span> [[Category:'
-- Collect necessary information to display error
.. category
local message = tostring( utils.defaultIfEmpty( select( 1, ... ), 'This template issued a warning. 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 )
-- Generate  data
local wikitext, categorylink = generateErrorWikitext( message, wtype .. ' warning', category, template )
-- Add custom styling
wikitext = [[<span class="warning halopedia-template-warning" style="color: orange;">]] .. wikitext .. [[</span>]]
-- Add the warning to the edit preview
mw.addWarning( wikitext )
-- Create and return the text to add to the page. In this case, we only add
-- the category - no message, as warnings should be silent to the reader
return categorylink
end
end


return utils
return utils