Module:AutomatedTabs

local util = require('Module:Util') local util_table = require('Module:TableUtil') local tabsheader = require('Module:TabsHeader') local corr = require('Module:CorrespondingPageBox').makeBox

local p = {} function p.main(json, linkadjustments, navboxdata, corr, basepage, after) local frame = mw.getCurrentFrame local title = p.getCanonicalTitle(linkadjustments, basepage) local thisevent, tabstable = p.whereAreWe(json, title, basepage) local text = {} if navboxdata then p.makeNavboxes(frame, text, navboxdata, thisevent) end n = #text + 2 if json then p.makeTabs(text, tabstable, linkadjustments) end if corr then table.insert(text, n, p.makeCorrBox(corr, title)) end if after then text[#text+1] = after end local output = table.concat(text,'') return output end

-- making parts of the page function p.getCanonicalTitle(linkadjustments, basepage) local title = mw.title.getCurrentTitle titlestr = title.nsText == 'Template' and basepage or title.prefixedText if not linkadjustments then return titlestr end return p.findReplace(titlestr, linkadjustments.fr.replace, linkadjustments.fr.find) end

function p.makeNavboxes(frame, text, data, thisevent) for i, navboxdata in ipairs(data) do		if navboxdata.events.showAll or util_table.keyOf(navboxdata.events, thisevent) then text[#text+1] = frame:expandTemplate({				title = navboxdata.title,				args = navboxdata.args			}) end end return end

function p.makeCorrBox(args, title) local i = tonumber(args.showCorr) if i then elseif args.corr_titlematch then for k, v in ipairs(args.corr_titlematch) do			if string.find(title,v,1,true) then i = k			end end else i = 1 end return corr({		find = mw.text.split(args.corr_find[i] or ,'%s*;%s*'),		replace = mw.text.split(args.corr_replace[i] or ,'%s*;%s*'),		display = args.corr_display and mw.text.split(args.corr_display[i] or ,'%s*;%s*'),		tournament = args.corr_tournament and mw.text.split(args.corr_tournament[i] or ,'%s*;%s*'),		targetpage = args.targetpage,	}) end

function p.makeTabs(text, tabstable, linkadjustments) for k, v in ipairs(tabstable) do		if linkadjustments then p.linksToOutput(v.links, linkadjustments) end text[#text+1] = tabsheader.fromTables(v.names, v.links, v.this, {}) end return end

function p.linksToOutput(links, linkadjustments) for k, link in ipairs(links or {}) do		links[k] = p.linkToOutput(link, linkadjustments.fr, linkadjustments.cd, k)	end return end

-- where are we function p.whereAreWe(json, titlestr, basepagestr) local title = mw.text.split(titlestr,'/') local basepage = mw.text.split(basepagestr,'/') local tbl = {} local i = #basepage + 1 local thisevent = title[i] or 'Overview' local t = json local j = 1 while t do		tbl[j] = { links = p.constructLinks(t.links,basepagestr), names = t.names, this = util_table.keyOf(t.links, title[i]) or 1 }		basepagestr = util.concatSubpage(basepagestr,t.links[tbl[j].this]) if t[title[i]] then t = t[title[i]] i = i + 1 else t = t['Overview'] end j = j + 1 end return thisevent, tbl end

function p.constructLinks(links,path) local tbl = {} for i, link in ipairs(links) do		tbl[i] = util.concatSubpage(path, link) end return tbl end

-- Processing utils function p.newIndex(base, extension) -- concatenate the two but don't add a / if the 2nd argument is empty if not base and extension == 'Overview' then return 'events/Overview' elseif not base or base == '' or base == 'events' then return extension elseif extension == '' or not extension then return base else return base .. '/' .. extension end end

function p.linkToOutput(link, fr, cd, k)	link = p.findReplace(link, fr.find, fr.replace) link = (k~= 1) and cd[link] or link return link end

function p.findReplace(link, find, replace) for k, v in ipairs(find) do		link = string.gsub(link, util.escape(v), util.escape(replace[k])) end return link end

return p