Module:TournamentTabs

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

To edit the documentation or categories for this module, click here.

Automated creation of tournament tabs templates without the user needing to use if statements or anything. See {{TournamentTabs}} for usage.

Dependencies: {{GetEventPageInfo}}m, {{SubpageSettings}}m, {{TabsHeader}}m, {{AutomatedTabs}}m

Eventually {{GetEventPageInfo}}m should be merged into this but not until it's no longer needed standalone.


local util_args = require('Module:ArgsUtil')
local util_title = require('Module:TitleUtil')

local settings = require('Module:SubpageSettings').tournaments
local pageinfo = require('Module:GetEventPageInfo')
local tabs = require('Module:AutomatedTabs')

local p = {}
function p.makeArgsIntoTables(args)
	local args2 = mw.clone(args) -- since we might have to modify some keys if they are numbers
	for key, value in pairs(args2) do
		key_str = tostring(key) or key
		if not p.doWeSkip(key) then
			args[key_str] = mw.text.split(value,'%s*%f[,%%],%s*')
			for k, v in ipairs(args[key_str]) do
				args[key_str][k] = args[key_str][k]:gsub('%%,',','):gsub('_',' ')
			end
		end
	end
end

function p.doWeSkip(key)
	if settings.dontconvert[key] then
		return true
	end
	for k, v in pairs(settings.dontconvert_patterns) do
		if string.find(key, v, 1, true) then
			return true
		end
	end
	return false
end

-- making json
function p.jsonFromArgs(args)
	if util_args.castAsBool(args.oldMD) then
		p.oldMD(args)
	end
	local tbl = {}
	p.jsonRecursion(tbl, args, nil)
	return tbl
end

function p.oldMD(args)
	-- oldMD is an arg to make a shortcut for every Match Details artificially becoming named
	-- Match Details & VODs
	for _, event in ipairs(args.events) do
		args[event .. '_names_Match Details'] = 'Match Details'
	end
	args['names_Match Details'] = 'Match Details'
	args['events_names_Match Details'] = 'Match Details'
	return
end

function p.jsonRecursion(tbl, args, path)
	-- names are made ready for output now but links are kept in canonical form until output
	local thispath = path or 'events'
	local links = mw.clone(args[thispath])
	p.processLinks(links)
	local names = mw.clone(args[thispath .. '_names'] or links)
	p.processNames(args, thispath, names, links)
	tbl.links = links
	tbl.names = names
	if not util_args.castAsBool(args[thispath .. '_nooverview']) then
		tbl.links[1] = 'Overview'
	end
	for k, v in ipairs(links) do
		local newpath = tabs.newIndex(path, v)
		if args[newpath] and not tbl[v] then
			tbl[v] = {}
		 	p.jsonRecursion(tbl[v],args,newpath)
		end
	end
	-- if we don't specify if it's specificaly not an overview page
	-- then we probably don't want any subpage for the first tab
	if not util_args.castAsBool(args[thispath .. '_nooverview']) then
		tbl.links[1] = ''
	end
	tbl.path = thispath -- for maintenance cargo table to be used when automating things
	return tbl
end

function p.processLinks(links)
	for k, link in ipairs(links) do
		links[k] = settings.lookup[mw.ustring.lower(link)] or link
	end
	return
end

function p.processNames(args, path, names, links)
	for key, name in ipairs(names) do
		names[key] = args[path .. '_names_' .. links[key]] or settings.names[name] or name
	end
	return
end

-- make navbox args
function p.getNavboxData(args)
	local navboxdata = {}
	for i, navbox in ipairs(args.navboxes) do
		local tm = args['navbox' .. i .. '_titlematch'] and args['navbox' .. i .. '_titlematch'][1]
		navboxdata[i] = {
			title = navbox,
			args = {},
			events = args['navbox' .. i .. '_events'] or ((not tm) and { showAll = true } or {}),
			titlematch = tm
		}
		for k,v in pairs(args['navbox' .. i .. '_keys'] or {}) do
			navboxdata[i].args[v] = args['navbox' .. i .. '_values'][k]
		end
	end
	return navboxdata
end

-- link manipulation
function p.linkAdjustments(args)
	local tbl = {}
	tbl.fr = p.makeFindReplace(args.titlefind, args.titlereplace)
	tbl.cd = p.makeCurrentdata(args.currentdata_pages, args.currentdata_values, args.currentdata_force)
	return tbl
end

function p.makeFindReplace(find, replace)
	-- in case the tournament has different base urls for different sections
	if find and replace then
		return { find = find, replace = replace }
	else
		return { find = {}, replace = {} }
	end
end

function p.makeCurrentdata(pages, values, force)
	-- current data makes a different subpage from the overview focused
	-- typically find-replace is ignored when it's trying to replace the first
	-- link in a list. but on rare occasions we won't want this, when we're actually
	-- tricking it into focusing a different thing in the very first line
	-- specifically this may be the case when there's no overview and it start with qualifiers.
	-- in this case we can use the currentdata_force argument, which is a list of booleans.
	local data = { force = {} }
	if not pages or not values then return data end
	force = force or {}
	for i, page in ipairs(pages) do
		data[page] = util_title.concatSubpage(page,values[i])
		data.force[page] = util_args.castAsBool(force[i])
	end
	return data
end

function p.getCategories()
	local title = mw.title.getCurrentTitle()
	if title.nsText == 'Template' then
		return '[[Category:Tournament Tabs]]'
	end
	return
end

-- main
function p.fromArgs(frame)
	local args = util_args.merge(true)
	if not args.basepage then
		error('Missing basepage parameter')
	end
	-- process data
	if not args.events then args.events = args.Overview end
	p.makeArgsIntoTables(args)
	local datadiv = pageinfo.getEventPageInfo(frame) -- for vardefines
	
	local data = {
		linkadjustments = p.linkAdjustments(args),
		navboxdata = args.navboxes and p.getNavboxData(args),
		corrdata = args.showCorr and args,
		after = p.getCategories(),
		basepage = args.basepage,
		tabstype = args.tabstype or 'header'
	}
	local json = args.events and p.jsonFromArgs(args)
	return tabs.main(json, data), datadiv
end

return p