Wowpedia
Advertisement

local m = {}

local levels = {
	protected = "It can only be called from [[secure code|Blizzard code]].",
	hw = "It requires a hardware event (user input).",
	nocombat = "It cannot be called from insecure code while in combat.",
	noinstance = "This only works outdoors and not in instanced content (dungeons/raids/battlegrounds/arena).",
	noscript = "It cannot be called ''directly'' from [[MACRO_script|/script]], loadstring() and WeakAuras.",
	nofreetrial = "This is not available to Free Trial / non-subscriber accounts.",
}

-- https://stackoverflow.com/a/7615129/1297035
local function strsplit(input, sep)
	local t = {}
	for s in string.gmatch(input, "([^"..sep.."]+)") do
		table.insert(t, s)
	end
	return t
end

local function contains(tbl, value)
	for _, v in pairs(tbl) do
		if value == v then
			return true
		end
	end
end

local function GetInfoText(f, protectedTypes)
	local t = {}
	for _, v in pairs(protectedTypes) do
		if levels[v] then
			local tag = string.format("'''<span class=\"apiret\">%s</span>'''", v:upper())
			table.insert(t, string.format("* %s - %s", tag, levels[v]))
		end
	end
	-- tried {{{1}}} but then it wouldnt support formatted text fonts
	if f.args.info ~= "{{{info}}}" then
		table.insert(t, "* "..f.args.info)
	end
	return table.concat(t, "\n")
end

local unrestrictedAmbox = {
	title = "Ambox",
	args = {
		border = "green",
		type = "'''This function appears to not require HW events.'''",
		image = "[[Image:Inv_misc_toy_10.png|32px|link=]]",
	}
}

local deprecatedAmbox = {
	title = "Ambox",
	args = {
		border = "yellow",
		type = "'''This function is deprecated.'''",
		image = "[[Image:Spell_holy_borrowedtime.png|32px|link=]]",
	}
}

local function GetAmbox(f)
	local protectedTypes = strsplit(f.args.type, ",")
	if contains(protectedTypes, "unrestricted") then
		return f:expandTemplate(unrestrictedAmbox)
	elseif contains(protectedTypes, "deprecated") then
		if f.args.info ~= "{{{info}}}" then
			deprecatedAmbox.args.type = deprecatedAmbox.args.type.." "..f.args.info
		else
			deprecatedAmbox.args.type = deprecatedAmbox.args.type.." It no longer appears to do anything."
		end
		return f:expandTemplate(deprecatedAmbox)
	end
	local template = {
		title = "Ambox",
		args = {
			border = "blue",
			type = "'''This function is [[:Category:API functions/Protected|restricted]].'''",
			--style = "margin-right: 10em; margin-left: 0",
			info = GetInfoText(f, protectedTypes),
		}
	}
	if contains(protectedTypes, "protected") then
		template.args.image = [=[<div style="position: relative; padding: 0 5px 2px 0">[[Image:Icon-terminal.svg|48px|link=]]<div style="position:absolute; bottom: 0; right: 0">[[File:Ambox padlock red.svg|32px|link=]]</div></div>]=]
	else
		template.args.image = "[[Image:Icon-addon-48x48.png|link=]]"
	end
	return f:expandTemplate(template)
end

function m.main(f)
	return GetAmbox(f)
end

return m
Advertisement