Module:HonorTitle

From Pathfinder Wiki
Revision as of 17:07, 22 July 2021 by DesignerThan (talk | contribs)

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

local p = {}
local utilities = require ( "Module:Utilities" )
local honorUtils = require ( "Module:HonorUtils" )
local notUseLUAPattern = true

function p.setDisplayTitle( frame, customTitle )
	if ( customTitle == nil or customTitle == '' ) then
		local pageTitle = frame:expandTemplate{ title = 'PAGENAMEwithoutLang' }
		--local pageTitle = mw.title.getCurrentTitle().text
		mw.log('HonorTitle:setDisplayTitle (pageTitle): '..pageTitle)
		local tPageTitle = mw.text.split(pageTitle, '/', notUseLUAPattern)
		local honorname = tPageTitle[2]
		mw.log('HonorTitle:setDisplayTitle (honorname): '..honorname)
		local transHonorname = utilities.localize( honorname, 'Honors' )
		mw.log('HonorTitle:setDisplayTitle (transHonorname): '..transHonorname)
		
		customTitle = transHonorname
		local subPageString = getSubpageString(tPageTitle[3])
		if (subPageString ~= nil) then
			customTitle = customTitle..subPageString
		end
	end
	return mw.ext.displaytitle.set(customTitle)
end

function getSubpageString(honorSubpage)
	local subPageString = nil
	-- check input
	if ( honorSubpage == nil ) then
		mw.log('HonorTitle.getSubpageString: Input value is invalid!')
		return subPageString
	end
	
	-- extract the number if present
	local variantNumber = tonumber(string.match(honorSubpage, '%d+'))
	if (variantNumber == nil) then
		-- if the page has no number then it is mostlikely number one
		-- and if it is an invalid subpage then the number won't be used anyway's
		variantNumber = 1
	end
	-- check for valid subpages
	if ( mw.ustring.find(honorSubpage, 'Requirements', 0, notUseLUAPattern) ~= nil ) then
		mw.log('HonorTitle.getSubpageString: It is the Requirements Variant '..variantNumber)
		local tVariant = honorUtils.getVariantsData( frame, '', 'requirements', variantNumber)
		subPageString = ' - '..utilities.localize('Requirements')..' ('
		for key, authority in pairs(tVariant['requirements'][1]['authorities']) do
			if (key == 1) then
				subPageString = subPageString..authority
			else
				subPageString = subPageString..' & '..authority['translated']
			end
		end
		subPageString = subPageString..')'
	elseif (mw.ustring.find(honorSubpage, 'Answer Key', 0, notUseLUAPattern) ~= nil ) then
		mw.log('HonorTitle.getSubpageString: It is the Answer Key Variant '..variantNumber)
		local tAnsVariant = honorUtils.getVariantsData( frame, '', 'answerKey', variantNumber)
		subPageString = ' - '..utilities.localize('Answer Key')..' ('
		local authoritiesString = nil
		if (tAnsVariant['answers'][1]['authority'] ~= nil) then
			-- get req variant data
			tReqVariant = honorUtils.getVariantsData(frame, '', 'requirements', tVariant['authority'])
			for key, authority in pairs(tReqVariant['requirements'][1]['authorities']) do
				if (authoritiesString == nil) then
					authoritiesString = authority['translated']
				else
					authoritiesString = authoritiesString..' & '..authority['translated']
				end
			end
		end
		local countriesString = nil
		for key, country in pairs(tAnsVariant['answers'][1]['countries']['split']) do
			if (countriesString == nil) then
				countriesString = country['translated']
			else
				countriesString = countriesString..' & '..country['translated']
			end
		end
		subPageString = subPageString..countriesString
		if (authoritiesString ~= nil) then
			subPageString = subPageString..' - '..authoritiesString
		end
		subPageString = subPageString..')'
	else
		mw.log('HonorTitle.getSubpageString: Invalid Subpage ('..honorSubpage..')')
		subPageString = ' '..utilities.localize(honorSubpage)
	end
	return subPageString
end

return p