Module:AutomatedTabs

From The Vault - Fallout Wiki
Jump to: navigation, search

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


local util = require('Module:Util')
local util_table = require('Module:TableUtil')
local util_title = require('Module:TitleUtil')
local tabsheader = require('Module:TabsHeader')
local corr = require('Module:CorrespondingPageBox').makeBox
local sep = '%s*;%s*'

local p = {}
function p.main(json, linkadjustments, navboxdata, corrdata, 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, title) end
	n = #text + 2
	if json then
		p.makeTabs(text, tabstable, linkadjustments)
	end
	if corrdata then table.insert(text, n, p.makeCorrBox(corrdata, title)) end
	if after then text[#text+1] = after end
	local output = table.concat(text,'')
	frame:callParserFunction{ name = '#vardefine:hasmoduletabs', args = 'yes' }
	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, title)
	for i, navboxdata in ipairs(data) do
		if navboxdata.events.showAll or util_table.keyOf(navboxdata.events or {}, thisevent) or (navboxdata.titlematch and title:find(navboxdata.titlematch, 1, true)) 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) or p.getPageMatch(args.corr_titlematch,title,1)
	if not i or p.getPageMatch(args.corr_notitlematch and mw.text.split(args.corr_notitlematch[i] or '',sep), title) then
		return ''
	end
	return corr({
		find = mw.text.split(args.corr_find[i] or '',sep),
		replace = mw.text.split(args.corr_replace[i] or '',sep),
		display = args.corr_display and mw.text.split(args.corr_display[i] or '',sep),
		tournament = args.corr_tournament and mw.text.split(args.corr_tournament[i] or '',sep),
		targetpage = args.targetpage,
	})
end

function p.getPageMatch(matches, title, default)
	if not matches then return default end
	for k, v in ipairs(matches) do
		if v ~= '' and string.find(title,v,1,true) then
			return k
		end
	end
	return nil -- this is actually supposed to be nil, not default. default only applies when matches is nil.
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_title.concatSubpage(basepagestr,t.links[tbl[j].this])
		if t[title[i]] then
			t = t[title[i]]
			i = i + 1
		elseif tbl[j].this == 1 then
			t = t['Overview']
		else
			t = nil
		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_title.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)
	-- allow editors to give currentdata using either actual title or canonical title
	-- in the unlikely case that there's a cd entry for both canonical & actual
	-- we wouldn't want to change it twice. so make sure we didn't already change it when
	-- we do the second replacement. first replacement will be canonical, second is actual.
	-- the "proper" way to do this would be only replace canonical but this might be confusing.
	local used_cd
	if k ~= 1 and cd[link] then
		link = cd[link]
		used_cd = true
	end
	link = p.findReplace(link, fr.find, fr.replace)
	link = (k~= 1) and not used_cd 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