We're currently performing an overhaul of our entire tournament results system so results will be missing from team and player pages for a while, and there may be some errors on tournament pages. Please bear with us as we complete this update.

Module:NavboxUtil

From Call of Duty Esports Wiki
Jump to: navigation, search

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

local util_args = require('Module:ArgsUtil')

local p = {}

function p.makeSuffixedLink(link, display, suffix)
	if suffix and suffix ~= '' then
		local newlink, class = p.pickLink(link, suffix)
		return string.format('<span class="%s">[[%s|%s]]</span>',
			class or '',
			newlink or '',
			display or ''
		)
	else
		return string.format('[[%s|%s]]',
			link or'',
			display or ''
		)
	end
end

function p.pickLink(link, suffix)
	local target = p.doesSuffixExist(link, suffix)
	if target then
		return target, ''
	else
		return link, 'no-subpage'
	end
end

function p.doesSuffixExist(link, suffix)
	if not suffix then
		return link
	end
	local target = string.format('%s/%s',link or '',suffix or '')
	local targetpage = mw.title.makeTitle('',target)
	return targetpage.exists and targetpage.text
end

function p.getSuffix(useSuffix, title)
	if not title then title = mw.title.getCurrentTitle() end
	if util_args.castAsBool(useSuffix) then
		titleparts = mw.text.split(title.text,'/')
		table.remove(titleparts,1)
		if #titleparts == 0 then
			return nil
		end
		-- make a table where the first entry is the full suffix
		-- subsequent suffixes are shorter and shorter
		local output = table.concat(titleparts,'/')
		return output
	else
		return nil
	end
end

function p.makeNavbox(args)
	if not args then return nil end
	if not args.group1 and not args.list1 then return nil end
	return mw.getCurrentFrame():expandTemplate{
		title = 'navbox',
		args = args
	}
end

return p