Module:Apilink

From Warcraft Wiki
Jump to navigation Jump to search

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

-- https://warcraft.wiki.gg/wiki/Template:Apilink
local m = {}

local function FormatParams(text, color)
	if color then
		return string.format('<span style="font-size:smaller; color:%s">%s</span>', color, text)
	else
		return string.format('<span style="font-size:smaller">%s</span>', text)
	end
end

local function GetFunctionLink(args)
	local t = {}
	local name = args[1]
	table.insert(t, string.format("[[API %s|%s]]", name, name))
	if #args.arg > 0 then
		local params = FormatParams(args.arg, "#ecbc2a")
		table.insert(t, string.format("(%s)", params))
	else
		if args.noparens ~= "1" then
			table.insert(t, "()")
		end
	end
	if #args.ret > 0 then
		local params = FormatParams(args.ret, "#4ec9b0")
		table.insert(t, string.format(" : %s", params))
	end
	return table.concat(t)
end

local function GetWidgetLink(args)
	local t = {}
	local name = args[1]
	local widget, method = name:match("(%w+):(%w+)")
	table.insert(t, string.format("[[API %s %s|%s]]", widget, method, name))
	if #args.arg > 0 then
		local params = FormatParams(args.arg, "#ecbc2a")
		table.insert(t, string.format("(%s)", params))
	else
		if args.noparens ~= "1" then
			table.insert(t, "()")
		end
	end
	if #args.ret > 0 then
		local params = FormatParams(args.ret, "#4ec9b0")
		table.insert(t, string.format(" : %s", params))
	end
	return table.concat(t)
end

local function GetScriptLink(args)
	local t = {}
	local name = args[1]
	table.insert(t, string.format("[[UIHANDLER %s|%s]]", name, name))
	
	local r = {}
	table.insert(r, FormatParams("''self''"))
	if #args.arg > 0 then
		table.insert(r, FormatParams(args.arg , "#ecbc2a"))
	end
	local params = table.concat(r, ", ")
	table.insert(t, string.format("(%s)", params))
	return table.concat(t)
end

local function GetEventLink(args)
	local t = {}
	local name = args[1]
	table.insert(t, string.format("[[%s]]", name, name))
	if #args.payload > 0 then
		local params = FormatParams(args.payload)
		table.insert(t, string.format(": %s", params))
	end
	return table.concat(t)
end

local link_handler = {
	a = GetFunctionLink,
	w = GetWidgetLink,
	h = GetScriptLink,
	e = GetEventLink,
}

function m.main(f)
	return link_handler[f.args.t](f.args)
end

return m