Codex Gamicus
Register
Advertisement

Documentation for this module may be created at Module:Common/doc

local p = {}

-- 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
	
	if frame.args and pairs(frame.args)(frame.args) ~= nil then -- next doesn't work on frame.args
		args = frame.args -- use args from #invoke
	elseif type(frame.getParent) == "function" then
		args = frame:getParent().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

return p
Advertisement