Module:Apitooltip

From Warcraft Wiki
Jump to navigation Jump to search

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

-- https://wowpedia.fandom.com/wiki/Template:Apitooltip
local m = {}
local fs_tooltip = '<span class="tttemplatelink">%s</span><span style="display:none">%s</span>'

local ConsoleCategory = {
	[0] = "Debug",
	[1] = "Graphics",
	[2] = "Console",
	[3] = "Combat",
	[4] = "Game",
	--[5] = "Default",
	[6] = "Net",
	[7] = "Sound",
	[8] = "Gm",
	[9] = "Reveal",
	[10] = "None",
}

local function ColorText(text)
	return string.format('<span class="apitype">%s</span>', text)
end

function m.GetCVar(args)
	local t, r = {}, {}
	table.insert(t, string.format('<font color="#71d5ff">%s</font>', args.name))
	if #args.cat > 0 then
		t[1] = t[1]..string.format(" (%s)", ConsoleCategory[tonumber(args.cat)])
	end
	if #args.default > 0 then
		table.insert(r, string.format("Default: <code>%s</code>", ColorText(args.default)))
	end
	if #args.scope > 0 then
		table.insert(r, string.format("Scope: %s", ColorText(args.scope)))
	end
	if next(r) then
		table.insert(t, table.concat(r, ", "))
	end
	if #args.desc > 0 then
		table.insert(t, string.format("<small>%s</small>", args.desc))
	end
	local name = string.format("[[CVar %s|%s]]", args.name, args.name)
	return name, table.concat(t, "<br>")
end

local function GetCVarInfo(name, info)
	-- cannot use unpack()
	local default, category, account, character, help = info[1], info[2], info[3], info[4], info[5]
	local t = {
		name = name,
		default = #default > 0 and default or "",
		cat = category ~= 5 and tostring(category) or "",
		scope = account and "Account" or character and "Character" or "",
		desc = #help > 0 and help or ""
	}
	return t
end

local function GetCVarTooltip(data, name)
	local info = data[1].var[name]
	if info then
		local source = GetCVarInfo(name, info)
		return fs_tooltip:format(m.GetCVar(source))
	end
end

local function GetCommandTooltip(args)
	return fs_tooltip:format(m.GetCVar(args))
end

function m.main(f)
	if f.args.type == "cvar" then
		if #f.args.name > 0 then -- params already given
			return fs_tooltip:format(m.GetCVar(f.args))
		else
			local data = mw.loadData("Module:API_info/cvar/data")
			return GetCVarTooltip(data, f.args[1])
		end
	elseif f.args.type == "command" then
		return GetCommandTooltip(f.args)
	end
end

return m