Wowpedia
Advertisement

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

local flavor_module = require("Module:API_info/flavor")
local elink_module = require("Module:API_info/elink")
local patch_module = require("Module:API_info/patch")
local metrics_module = require("Module:API_info/metrics")
local m = {}
m.data = mw.loadData("Module:API_info/multi/data")

local INFOBOX_STYLE = '<div class="nomobile" style="float:right; clear:right">\n%s\n</div>'

local function GetNonPrefixName(name)
	if name:sub(1, 1) == "#" then
		return name:sub(2)
	else
		return name
	end
end
local function FormatCodeOrLink(name)
	if name:sub(1, 1) == "#" then
		name = name:sub(2)
		return string.format("<code>[[API %s|%s]]</code>", name, name)
	else
		return string.format("<code>%s</code>", name)
	end
end

local function FormatIcon(item)
	item.iconsize = item.iconsize or 16
	local fs = "[[File:%s|%dpx|link=%s]]"
	return fs:format(item.icon, item.iconsize, item.url)
end

function m:GetFlavorIcons(args, name)
	local data = flavor_module:GetFlavors(args.t, name)
	if data then
		local t = {}
		for _, item in pairs(data) do
			table.insert(t, FormatIcon(item))
		end
		return table.concat(t)
	end
end

function m:GetElinkIcons(args, name)
	local data = elink_module:GetElinks(args.t, name)
	if data then
		local t = {}
		for _, item in pairs(data) do
			table.insert(t, FormatIcon(item))
		end
		return table.concat(t, " &nbsp;")
	end
end

function m:GetMetrics(args, name)
	local data = elink_module:GetElinks(args.t, name)
	if data then
		local t = {}
		for _, item in pairs(data) do
			table.insert(t, FormatIcon(item))
		end
		return table.concat(t, " &nbsp;")
	end
end


function m:CreateSmallInfobox(args, api, t)
	for _, name in pairs(api) do
		local prefixName = name
		name = GetNonPrefixName(name)
		table.insert(t, "\n|-\n| ")
		local flavorIcons = self:GetFlavorIcons(args, name) or ""
		table.insert(t, string.format("%s ", flavorIcons:gsub("%]%[", "]<br>[")))
		table.insert(t, string.format("|| %s<br>", FormatCodeOrLink(prefixName)))
		local added, removed = patch_module:GetPatches(args.t, name)
		if #added > 0 then
			table.insert(t, " + "..added)
		end
		if #removed > 0 then
			table.insert(t, "&nbsp;&nbsp; - "..removed)
		end
		table.insert(t, string.format("&nbsp;&nbsp; %s", self:GetElinkIcons(args, name) or ""))
		local metrics = metrics_module:main(args.t, name)
		if metrics then
			table.insert(t, string.format("&nbsp;&nbsp; %s", metrics_module:GetScoreString(metrics)))
		end
	end
end

function m:CreateBigInfobox(args, api, t)
	for _, name in pairs(api) do
		local prefixName = name
		name = GetNonPrefixName(name)
		table.insert(t, "\n|-\n| ")
		table.insert(t, string.format("%s || ", self:GetElinkIcons(args, name) or ""))
		table.insert(t, string.format("%s || ", FormatCodeOrLink(prefixName)))
		table.insert(t, string.format("%s || ", self:GetFlavorIcons(args, name) or ""))
		local added, removed = patch_module:GetPatches(args.t, name)
		if #added > 0 then
			table.insert(t, "|| + "..added)
		end
		if #removed > 0 then
			table.insert(t, "|| - "..removed)
		else
			table.insert(t, "|| ") -- make metrics align
		end
		local metrics = metrics_module:main(args.t, name)
		if metrics then
			table.insert(t, "|| "..metrics_module:GetScoreString(metrics))
		end
	end
end

function m:main(args, apiName, multiNames)
	-- cannot use table api on multiNames from mw.loadData()
	local apiList = {apiName}
	if multiNames then
		for _, name in ipairs(multiNames) do
			table.insert(apiList, name)
		end
	end
	local t = {}
	table.insert(t, '{| class="darktable" cellpadding=5')
	if #apiList <= 3 then
		self:CreateSmallInfobox(args, apiList, t)
	else
		self:CreateBigInfobox(args, apiList, t)
	end
	table.insert(t, "\n|}")
	return INFOBOX_STYLE:format(table.concat(t))
end

return m
Advertisement