Module:Utils

local utils = {}

function utils.strip( str ) return mw.ustring.gsub(str, '^%s*(.-)%s*$', '%1') end

function utils.empty( val ) return val == nil or val == '' end

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 generateErrorWikitext( message, type, category, template ) -- 	local wikitext = ''' -- 		.. string.upper( type ) -- 		.. ''' -- 		.. message -- 	if template ~= nil then -- 		wikitext = wikitext .. ' (Template: ' -- 			.. template -- 			.. ')' -- 	end -- -- 	local categorylink = '' -- 	-- 	return wikitext, categorylink -- 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 ) -- Generate data local wikitext, categorylink = generateErrorWikitext( message, etype .. ' error', category, template ) -- Add custom styling wikitext =  .. wikitext .. -- Add a warning to the edit preview mw.addWarning( wikitext ) -- Create and return the text to display on the page itself return wikitext .. ' ' .. categorylink end

function utils.warning( ... ) -- Collect necessary information to display error 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 =  .. wikitext .. -- 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

return utils