Module:Utils: Difference between revisions

m
no edit summary
No edit summary
mNo edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
local 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 )
function utils.defaultIfEmpty( val, default )
Line 19: Line 27:


function generateErrorWikitext( message, type, category, template )
function generateErrorWikitext( message, type, category, template )
local debug_str = ''
if template == nil then
-- Find the first frame whose title doesn't begin with 'Template:'
local prevFrame = mw.getCurrentFrame()
local frame = prevFrame:getParent()
while frame ~= nil and frame:getTitle() ~= nil and mw.ustring.sub( frame:getTitle(), 1, 9 ) == 'Template:' do
prevFrame = frame
frame = prevFrame:getParent()
debug_str = debug_str .. '>' .. prevFrame:getTitle()
end
if frame ~= nil then
debug_str = debug_str .. '>' .. frame:getTitle()
end
-- Use the frame immediately before that as the template that is giving
-- the error
if mw.ustring.sub( prevFrame:getTitle(), 1, 9 ) == 'Template:' then
-- Strip the 'Template:' part out
template = mw.ustring.sub( prevFrame:getTitle(), 10 )
end
end
local wikitext = [[''']]
local wikitext = [[''']]
.. string.upper( type )
.. string.upper( type )
.. [[:''' ]]
.. [[:''' ]]
.. message .. debug_str
.. message
if template ~= nil then
if template ~= nil then
wikitext = wikitext .. ' (Template: [[Template:'
wikitext = wikitext .. ' (Template: [[Template:'