Module:API info/main/api

From Warcraft Watch Secrets
Revision as of 14:41, 21 July 2025 by imported>Surafbrov
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:API info/main/api/doc

local modules = {
	flavor = require("Module:API_info/flavor"),
	elink = require("Module:API_info/elink"),
	patch = require("Module:API_info/patch"),
	group = require("Module:API_info/group"),
	properties = require("Module:API_info/properties"),
	flavorbox = require("Module:API_info/flavor_ambox"),
	infobox = require("Module:API_info/util/infobox"),
}
local m = {}

local comparison = {
	a = "Global_functions/Classic",
	e = "Events/Classic",
}

local PTR_VERSION = "11.2.0"

local function GetDefaultInfobox(args, name)
	local flavors = modules.flavor:GetFlavors(args.t, name)
	local added, removed = modules.patch:GetPatches(args.t, name)
	if flavors or #added > 0 then -- check if we have data on this since {{wowapi}} doesnt guarantee its an API
		local t = {}
		if modules.properties.data[name] then
			table.insert(t, "! Properties")
			for _, v in pairs(modules.properties.data[name]) do
				table.insert(t, string.format('| <div style="font-family:monospace" class="text-blizz">%s</div>', v))
			end
		end
		local flavors = modules.flavor:GetFlavors(args.t, name)
		if flavors then
			table.insert(t, string.format("! Game Types", comparison[args.t]))
			table.insert(t, flavors)
		end
		local elinks = modules.elink:GetElinks(args.t, name)
		if #elinks > 0 then
			table.insert(t, "! Links")
			table.insert(t, elinks)
		end
		table.insert(t, "! Patch")
		table.insert(t, "| Added in "..added)
		if #removed > 0 then
			-- fuck, need to rework classic
			table.insert(t, "| Removed in "..removed)
		end
		return t
	end
end

local function GetInfobox(f, name)
	local infobox
	local isGroupInfobox
	if modules.group:GetData(name) then
		infobox = modules.group:main(f.args, name)
		isGroupInfobox = true
	else
		local defaultInfobox = GetDefaultInfobox(f.args, name)
		if defaultInfobox then
			infobox = modules.infobox:main(defaultInfobox)
		end
	end
	return infobox, isGroupInfobox
end

local ptrAmbox = {
	title = "Ambox",
	args = {
		border = "green",
		image = "[[File:PTR_client.png|32px|link=]]",
		style = "width: auto; margin-left: 0.8em;",
		type = string.format("'''This API only exists on the %s ''[[Public Test Realm]]'''''", PTR_VERSION),
	}
}

local function GetPtrAmbox(f, name)
	local isPtr = modules.patch:IsPTR(f.args.t, name, PTR_VERSION)
	if isPtr then
		return f:expandTemplate(ptrAmbox)
	end
end

local function GetStyle(text, isGroup)
    if isGroup then
        return string.format('<div>%s</div>', text)
    else
        return string.format('<div>%s</div>', text)
    end
end

function m.main(f)
	local PAGENAME = f.args[1]
	local name = PAGENAME:gsub("API ", ""):gsub(" ", "_")
	local text = ""
	local infobox, isGroup = GetInfobox(f, name)
	local flavorbox = modules.flavorbox:GetAmbox(f, name)
	local ptrBox = GetPtrAmbox(f, name)
	if infobox then
		text = text..infobox
	end
	if flavorbox then
		text = text..flavorbox
	end
	if ptrBox then
		text = text..ptrBox
	end
	text = GetStyle(text, isGroup)
	return text
end

return m