Module:Warning: Difference between revisions
Appearance
Content deleted Content added
Formatted warning text and implemented level parameter |
Appended full stop to messages and simplified location handling (by avoiding "[C]: ?") |
||
Line 2: | Line 2: | ||
local wrapper = "%s" -- wikitext formatting |
local wrapper = "%s" -- wikitext formatting |
||
local msg_loc = "Lua warning in %s at line %d: %s" |
local msg_loc = "Lua warning in %s at line %d: %s." |
||
local msg = "Lua warning: %s" |
local msg = "Lua warning: %s." |
||
return function (message, level) |
return function (message, level) |
||
Line 9: | Line 9: | ||
level = level or 1 |
level = level or 1 |
||
if level > 0 then |
if level > 0 then |
||
local location = |
local _, location = pcall(error, '', level+2) |
||
location = |
if location ~= '' then |
||
location = mw.text.split(location:sub(1,-3), ':%f[%d]') |
|||
location = mw.text.split(location[2], ':%f[%d]') |
|||
message = msg_loc:format(location[1], location[2], message) |
message = msg_loc:format(location[1], location[2], message) |
||
else |
else |
Revision as of 23:39, 18 July 2021
![]() | This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
This module simply unifies the formatting of all warning messages similar to Module:Error. Currently, it is plain text, but custom formatting may be applied after discussion in the talk page. Warnings are displayed above the preview when previewing an edit.
Usage
local warn = require('Module:Warning')
warn("Message")
warn(("TypeWarning: %s"):format(warning), level)
See also
local libraryUtil = require('libraryUtil')
local wrapper = "%s" -- wikitext formatting
local msg_loc = "Lua warning in %s at line %d: %s."
local msg = "Lua warning: %s."
return function (message, level)
libraryUtil.checkType('warn', 2, level, 'number', true)
level = level or 1
if level > 0 then
local _, location = pcall(error, '', level+2)
if location ~= '' then
location = mw.text.split(location:sub(1,-3), ':%f[%d]')
message = msg_loc:format(location[1], location[2], message)
else
message = msg:format(message)
end
else
message = msg:format(message)
end
mw.addWarning(wrapper:format(message))
end