Module:Helper
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Helper/doc
-- ----------------------------------------
-- <pre> Module:Helper
-- Collection of small helper functions
-- ----------------------------------------
local Self = {} -- table of functions
local ustr = mw.ustring -- quick/short access to ustring table
-- ----------------------------------------
-- Variables
-- ----------------------------------------
local g_IsDesktop = nil
-- ----------------------------------------
-- Helper Functions
-- ----------------------------------------
function Self.GetArgs(frame)
local f
for k, v in pairs(frame.args) do
return frame.args
end
return frame:getParent().args
end
function Self.EscapeMagicCharacters(item)
item = ustr.gsub(item, "&#(%d+);", function(n) return ustr.char(n) end)
item = ustr.gsub(item, "%%", "%%%%")
item = ustr.gsub(item, "^%^", "%%^")
item = ustr.gsub(item, "%$$", "%%$")
item = ustr.gsub(item, "%(", "%%(")
item = ustr.gsub(item, "%)", "%%)")
item = ustr.gsub(item, "%.", "%%.")
item = ustr.gsub(item, "%[", "%%[")
item = ustr.gsub(item, "%]", "%%]")
item = ustr.gsub(item, "%*", "%%*")
item = ustr.gsub(item, "%+", "%%+")
item = ustr.gsub(item, "%-", "%%-")
item = ustr.gsub(item, "%?", "%%?")
return item
end
function Self.IfDesktop(a,b)
if g_IsDesktop == nil then
g_IsDesktop = mw.loadData("Module:Helper/IsDesktop").IsDesktop
end
if a ~= nil then
return g_IsDesktop and a or (b or "")
else
return g_IsDesktop
end
end
function Self.IfMobile(a,b)
if g_IsDesktop == nil then
g_IsDesktop = mw.loadData("Module:Helper/IsDesktop").IsDesktop
end
if a ~= nil then
return not g_IsDesktop and a or (b or "")
else
return not g_IsDesktop
end
end
function Self.Exp(frame)
return Self.Explode(frame.args[1], frame, frame.args[2])
end
function Self.Explode(text, frame, skippre)
--adds a middot between each letter to allow seeing what we are dealing with
frame = frame or mw.getCurrentFrame()
if not skippre then
text = frame:preprocess(text or "")
end
local str = ""
for l in ustr.gmatch(text, ".") do
str = str.."·"..l
end
return "Code: "..str
end
function Self.Dump(o)
--Dumps the contents of a variable into a <pre>
local str = ""
local serialize
serialize = function(o, s)
local t = type(o)
if o == nil then
str = str.."nil"
elseif t == "number" then
str = str..o
elseif t == "string" then
str = str..ustr.format("%q", o)
elseif t == "boolean" then
str = str..(o and "true" or "false")
elseif t == "table" then
str = str.."{\n"
local l = s
s = s.." "
for k, v in pairs(o) do
str = str..s.."["
serialize(k, s)
str = str.."] = "
serialize(v, s)
str = str..",\n"
end
str = str..l.."}"
else
error("cannot serialize a " .. type(v))
end
end
serialize(o, "")
return "<pre>"..str.."</pre>"
end
function Self.HashArgs(...)
local a = {}
local Hash
Hash = function(key, value)
local t = type(value)
if t == "table" then
for k, v in pairs(value) do
Hash(key..tostring(k).."=", v)
end
else
table.insert(a, tostring(key)..tostring(value)..t)
end
end
Hash("", {...})
table.sort(a)
return table.concat(a,",")
end
-- ----------------------------------------
-- Required for Modules to function
-- ----------------------------------------
return Self