Module:Common: Difference between revisions
Jump to navigation
Jump to search
ftb>RZR0 mNo edit summary |
Auburnsummer (talk | contribs) m (1 revision imported: Sprite from Feed the Beast wiki) |
Revision as of 01:43, 27 May 2022
Documentation for this module may be created at Module:Common/doc
local p = {}
p.bullet = " • "
-- checks if a param is given (i. e. neither null nor whitespace)
function p.isGiven(param)
if type(param) == "string" then -- special checks for strings
if string.gsub(param, "%s*", "") == "" then -- remove all whitespace before checking if empty
return false
else
return true
end
elseif param == nil then -- return false if nil
return false
else -- return true in all other cases
return true
end
end
function p.getFrameAndArgs(frame)
frame = frame or {}
local args
-- Make frame.args a fully functional table
if frame.args then
local frame_args = frame.args
frame.args = {}
for k, v in pairs(frame_args) do
frame.args[k] = v
end
end
if frame.args and next(frame.args) ~= nil then
args = frame.args -- use args from #invoke
elseif type(frame.getParent) == "function" then
local pframe = frame:getParent()
-- Make pframe.args a fully functional table
local pframe_args = pframe.args
pframe.args = {}
for k, v in pairs(pframe_args) do
pframe.args[k] = v
end
args = pframe.args -- get args from template call
else
args = frame -- assume directly passed in args
frame = mw.getCurrentFrame()
end
return frame, args
end
function p.unescape(s)
local news = s
for start, stop in s:gmatch("&#()%d*();") do
-- need to be relative to end, otherwise the indexes will be incorrect once the strigh's length changes
start = start-1 - #s
stop = stop-2 - #s
news = news:sub(1, start-3) .. string.char(tonumber(s:sub(start, stop))) .. news:sub(stop+2)
end
return news
end
-- generates a span tag that automatically cycles through the given image list
function p.anim(images, size)
if p.isGiven(images) then
images = p.unescape(images)
local r = ""
r = r .. "<span class=\"animated\" data-imgs=\"" .. images .. "\" "
if p.isGiven(size) then
r = r .. "data-img-size=\"" .. tostring(size) .."\" "
end
r = r .. "><span class=\"active\">[[File:" .. mw.text.split(images, ";")[1]
if p.isGiven(size) then
r = r .. "|" .. tostring(size)
end
r = r .. "]]</span></span>"
return r
end
end
function p.mwAnim(frame)
return p.anim(frame.args.images, frame.args.size)
end
-- generates a Semantic MediaWiki property
function p.smwProp(name, value, display)
if p.isGiven(name) and p.isGiven(value) then
if p.isGiven(display) or display == " " then
return "[[" .. name .. "::" .. value .. "|" .. display .. "]]"
else
return "[[" .. name .. "::" .. value .. "]]"
end
end
return nil
end
-- trims leading and trailing whitespace characters from a given string
function p.trim(str)
if p.isGiven(str) then
return string.gsub(str, "^%s*(.-)%s*$", "%1")
end
return ""
end
-- appends px to a given value, only if it's not already there
function p.px(val)
if tonumber(val) then
return val .. "px"
end
if p.isGiven(val) then
return string.gsub(val, "^(%d+)(?:px)?$", "%1px")
end
return ""
end
-- creates an internal MediaWiki link
function p.link(page, text, anchor)
if p.isGiven(page) then
local out = "[[" .. page
if p.isGiven(anchor) then
out = out .. "#" .. anchor
end
if p.isGiven(text) or (text == ' ') then
out = out .. "|" .. text
end
out = out .. "]]"
return out
end
return ""
end
-- creates an image link
function p.img(page, size, link, alt)
if p.isGiven(page) then
local out = "[[File:" .. page
if p.isGiven(size) then
out = out .. "|" .. p.px(size)
end
if p.isGiven(link) then
out = out .. "|link=" .. link
end
if p.isGiven(alt) then
out = out .. "|" .. alt
end
out = out .. "]]"
return out
end
return ""
end
-- capitalize the first letter of a given string
function p.cap(str)
return (str:gsub("^%l", string.upper))
end
-- parses seconds to a min:sec string
function p.parseTime(seconds)
if (seconds < 10) then
return '0:0' .. seconds
elseif (seconds < 60) then
return '0:' .. seconds
else
local sec = seconds % 60
local min = (seconds - sec) / 60
if (sec < 10) then
return min .. ':0' .. sec
else
return min .. ':' .. sec
end
end
end
-- formats a number with commas
-- http://lua-users.org/wiki/FormattingNumbers
function p.commaValue(n)
local left, num, right = string.match(n, '^([^%d]*%d)(%d*)(.-)$')
return left .. (num:reverse():gsub('(%d%d%d)', '%1,'):reverse()) .. right
end
return p