Wowpedia
Advertisement

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 m = {}
m.data = mw.loadData("Module:API_info/multi/data")

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

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

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

function m:GetInfobox(args, apiName, multiNames)
	-- cannot use table api on multiNames from mw.loadData()
	local api = {apiName}
	if multiNames then
		for _, name in ipairs(multiNames) do
			table.insert(api, name)
		end
	end
	local t = {}
	table.insert(t, '{| class="darktable" cellpadding=5')
	if #api <= 3 then
		for _, name in pairs(api) do
			local prefixName = name
			name = GetNonPrefixName(name)
			table.insert(t, "\n|-\n| ")
			local flavorText = self:GetFlavorText(args, name) or ""
			table.insert(t, string.format("%s ", flavorText: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:GetElinkText(args, name) or ""))
		end
	else
		for _, name in pairs(api) do
			local prefixName = name
			name = GetNonPrefixName(name)
			table.insert(t, "\n|-\n| ")
			table.insert(t, string.format("%s || ", self:GetElinkText(args, name) or ""))
			table.insert(t, string.format("%s || ", FormatCodeOrLink(prefixName)))
			table.insert(t, string.format("%s || ", self:GetFlavorText(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)
			end
		end
	end
	table.insert(t, "\n|}")
	return table.concat(t)
end

return m
Advertisement