Module:Race

From Wowpedia
Jump to: navigation, search

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

local p = {}
local races = {}

function p.findIndex(search) 
	for i, race in ipairs(races) do
		if (string.lower(string.gsub(race.link,'%s','')) == search) then
			return i
		end
	end
	for i, race in ipairs(races) do
		for j, alt in ipairs(race.alternates) do
			if (string.lower(string.gsub(alt,'%s','')) == search) then
				return i
			end
		end
	end
	for i, race in ipairs(races) do
		if (string.lower(string.gsub(race.name,'%s','')) == search) then
			return i
		end
	end
	return -1
end

function p.main(frame)
	races = mw.loadData('Module:Race/data')
	local search = string.lower(string.gsub(frame:getParent().args[1],'%s',''))
	local index = p.findIndex(search)
	local output = ''
	local size = frame:getParent().args["size"] or '16px'
	if index == -1 then
		return output
	else
		if size == '32px' then
			output = output .. '<div'
		else
			output = output .. '<span'
		end
		output = output .. ' class="linkicon raceicon RaceLink">'
	end
	local race = races[index]
	
	for i, img in ipairs(race.images) do
		output = output .. '[[File:' .. img .. '|' .. size .. '|' .. race.name .. '|link=' .. race.link .. ']]'
	end
	
	if size == '32px' then
		output = output .. '<br>'
	else
		output = output .. '</span>'
	end
	
	local text = frame:getParent().args[2] or '[[' .. race.link .. '|' .. race.name .. ']]'
	if text ~= 'notext' then
		output = output .. '&nbsp;' .. text
	end
	if size == '32px' then
		output = output .. '</div>'
	end
	return output
end

return p