Wowpedia
Advertisement

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

local bit = require "bit32"
local data
local m = {}

local api_types = {
	a = "api",
	e = "event",
}

-- cant use dots in github search
-- https://docs.github.com/en/github/searching-for-information-on-github/searching-on-github/searching-code#considerations-for-code-search
local function GetApiBaseName(v)
	if v:find("%.") then
		v = v:match(".-%.(.+)")
	end
	return v
end

-- github forks are not searchable unless they have more stars, just cloned it instead
-- https://stackoverflow.com/questions/33626326/how-to-search-a-github-fork-for-code-using-the-github-search-api
local flavors = {
	mainline = {
		label = "mainline",
		flag = 0x1,
		icon = {name = "Shadowlands-Icon-Inline.png", size = 36},
		url = "https://github.com/Gethe/wow-ui-source/search?q=%s",
	},
	vanilla = {
		label = "vanilla",
		flag = 0x2,
		icon = {name = "WoW Icon update.png", size = 36},
		url = "https://github.com/Ketho/wow-ui-source-vanilla/search?q=%s",
	},
	tbc = {
		label = "tbc",
		flag = 0x4,
		icon = {name = "Bc icon.gif", size = 36},
		url = "https://github.com/Ketho/wow-ui-source-tbc/search?q=%s",
	},
}

local function GetData(apiType)
	data = {}
	if api_types[apiType] then
		data[apiType] = mw.loadData("Module:API_info/flavor/"..api_types[apiType])
		return data
	end
end

function m:GetFlavorInfo(apiType, name)
	data = data or GetData(apiType)
	local flags = data[apiType][name]
	if flags then
		local mainline = bit.band(flags, flavors.mainline.flag) > 0
		local vanilla = bit.band(flags, flavors.vanilla.flag) > 0
		local tbc = bit.band(flags, flavors.tbc.flag) > 0
		return flags, mainline, vanilla, tbc
	end
end

local function InsertTableInfo(tbl, name, info)
	local baseName = GetApiBaseName(name)
	table.insert(tbl, {
		icon = info.icon.name,
		iconsize = info.icon.size,
		url = info.url:format(baseName),
		text = info.label,
	})
end

function m:GetFlavors(apiType, name)
	data = data or GetData(apiType)
	local flags, mainline, vanilla, tbc = self:GetFlavorInfo(apiType, name)
	if flags then
		local t = {}
		if mainline then
			InsertTableInfo(t, name, flavors.mainline)
		end
		if tbc then
			InsertTableInfo(t, name, flavors.tbc)
		end
		if vanilla then
			InsertTableInfo(t, name, flavors.vanilla)
		end
		return t
	end
end

return m
Advertisement