Module:PatchNotesTOC

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search

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

local util_args = require('Module:ArgsUtil')
local util_table = require('Module:TableUtil')
local util_text = require('Module:TextUtil')

local m_champion = require('Module:Champion')

local Sprite = require('Module:Sprite').sprite

local outersep = '%s*;%s*'
local innersep = '%s*,%s*'

local h = {}

function h.getDataFromArgs(args)
	local i = 1
	local data = {}
	while args['group' .. i] do
		local group = 'group' .. i
		data[i] = {
			name = args[group],
			types = args[group .. 'types'] and util_text.split(args[group .. 'types'],outersep),
			images = args[group .. 'images'] and util_args.splitAndMap(args[group .. 'images'],outersep, h.splitImages)
		}
		i = i + 1
	end
	return data
end

function h.splitImages(arg)
	return util_text.split(arg,innersep)
end

function h.printTable(data)
	local tbl = mw.html.create('table')
		:addClass('wikitable')
		:addClass('patchnote-toc')
	tbl:tag('tr')
		:tag('th')
			:wikitext(util_table.concat(data, '<hr>', h.printRow))
	return tbl
end

function h.printRow(row)
	local tbl = {
		('[[#%s|%s]]'):format(row.name, row.name),
		h.printImages(row)
	}
	return util_table.concat(tbl,'<br>')
end

function h.printImages(row)
	if not row.types then
		return false
	end
	local tbl = {}
	for i, v in ipairs(row.types) do
		local f = h.getImageType(v)
		tbl[#tbl+1] = util_table.concat(row.images[i],'',f)
	end
	return table.concat(tbl,'')
end

function h.getImageType(v)
	if v == 'Champion' then
		return h.ChampionSprite
	elseif v == 'Item' then
		return h.ItemSprite
	elseif v == 'Summoner' then
		return h.SummonerSprite
	elseif v == 'Rune' then
		return h.printRune
	elseif v == 'Mastery' then
		return h.printMastery
	elseif v =='Stat' then
		return h.printStat
	else
		error('Unrecognized Image Type, allowed values are Champion, Item, Summoner, Rune, Mastery, Stat')
	end
end

function h.ChampionSprite(id)
	return Sprite{
		id,
		link = '#' .. m_champion.championname(id):gsub(' ','_'),
		size = '25',
		type = 'Champion',
		notext = true
	}
end

function h.ItemSprite(id)
	return Sprite{
		id,
		link = '#' .. id:gsub(' ','_'),
		size = '25',
		type = 'Item',
		notext = true,
		autolinksuffix = ''
	}
end

function h.SummonerSprite(id)
	return Sprite{
		id,
		link = '#' .. id:gsub(' ','_'),
		size = '25',
		type = 'Summoner',
		notext = true
	}
end

function h.printRune(id)
	return('[[File:Rune %s.png|25px|link=#%s]]'):format(id, id:gsub(' ','_'))
end

function h.printMastery(id)
	return ('[[File:Mastery %s.png|25px|link=#%s]]'):format(id, id:gsub(' ','_'))
end

function h.printStat(id)
	return ('[[File:%s Mini Icon.png|25px|link=#%s Items]]'):format(id, id:gsub(' ','_'))
end

local p = {}
function p.main(frame)
	local args = util_args.merge(true)
	local data = h.getDataFromArgs(args)
	return h.printTable(data)
end
return p