Module:HonorTitle

From Pathfinder Wiki
Revision as of 00:32, 27 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 )
	-- in case of an error the function doesn't return anything
	if ( customTitle == nil or customTitle == '' ) then
		local templateVersion = 'name'
		local transHonorname = frame:expandTemplate{ title = 'GetHonorName' }
		mw.log('HonorTitle.setDisplayTitle (transHonorname): '..transHonorname)
		
		local tActVariantInfo = honorUtils.getActVariantInfo( frame )
		local countries = nil
		local authorities = nil
		local subpage = nil
		if ( tActVariantInfo ~= nil ) then
			-- it is a subpage
			templateVersion = templateVersion..',subpage'
			subpage = utilities.localize(tActVariantInfo['subPage'])
			local variantType = tActVariantInfo['variantType']
			local variantNumber = tActVariantInfo['variantNumber']
			if (variantType == 'requirements') then
				local tVariant = honorUtils.getVariantsData( frame, '', variantType, variantNumber)
				if (tVariant == nil or tVariant['requirements'] == nil) then
					mw.log('HonorTitle.setDisplayTitle: There are no Requirements Variants!')
				else
					templateVersion = templateVersion..',authority'
					for key, authority in pairs(tVariant['requirements'][1]['authorities']) do
						if (key == 1) then
							authorities = authority['translated']
						else
							authorities = authorities..' & '..authority['translated']
						end
					end
			    end
			elseif (variantType == 'answerKey') then
				local tVariant = honorUtils.getVariantsData( frame, '', variantType, variantNumber)
				if (tVariant == nil or tVariant['answers'] == nil) then
					mw.log('HonorTitle.setDisplayTitle: There are no answer key Variants!')
				else
					-- get the country data
					if (tVariant['answers'][1]['countries'] ~= nil and
						tVariant['answers'][1]['countries']['split'] ~= nil and
						#tVariant['answers'][1]['countries']['split'] >= 1) then
						templateVersion = templateVersion..',territory'
						for key, country in pairs(tVariant['answers'][1]['countries']['split']) do
							if (countries == nil) then
								countries = country['translated']
							else
								countries = countries..' & '..country['translated']
							end
						end
					end
					-- get the authority data
					if (tVariant['answers'][1]['authority'] ~= nil) then
						-- get req variant data for the authority
						local tReqVariant = honorUtils.getVariantsData(frame, '', 'requirements', tVariant['answers'][1]['authority'])
						if (tReqVariant ~= nil and tReqVariant['requirements'] ~= nil) then
							mw.log('HonorTitle.setDisplayTitle: There are requirement variants!')
							templateVersion = templateVersion..',authority'
							for key, authority in pairs(tReqVariant['requirements'][1]['authorities']) do
								if (authorities == nil) then
									authorities = authority['translated']
								else
									authorities = authorities..' & '..authority['translated']
								end
							end
						else
							-- no requirement variant?!
							mw.log('HonorTitle.setDisplayTitle: There are no requirement variants!')
						end
					end
			    end
			end
		else
			-- it is probably the landing page
			-- we don't need to do anything
		end
		
		customTitle = getTitle(templateVersion, transHonorname, subpage, countries, authorities)
		if (customTitle == nil) then
			-- return nothing so the title doesn't get changed
			return
		end
	end
	return mw.ext.displaytitle.set(customTitle)
end

function getTitle(templateVersion, honorname, subpage, countries, authorities)
	local title = utilities.localize(templateVersion, 'HonorTitle')
	if (title ~= templateVersion) then
		mw.log('HonorTitle.getTitle: Replace $honorname with '..honorname)
		title = mw.ustring.gsub(title, '$honorname', honorname)
		if (subpage ~= nil) then
			mw.log('HonorTitle.getTitle: Replace $subpage with '..subpage)
			title = mw.ustring.gsub(title, '$subpage', subpage)
		end
		if (countries ~= nil) then
			mw.log('HonorTitle.getTitle: Replace $territory with '..countries)
			title = mw.ustring.gsub(title, '$territory', countries)
		end
		if (authorities ~= nil) then
			mw.log('HonorTitle.getTitle: Replace $authority with '..authorities)
			title = mw.ustring.gsub(title, '$authority', authorities)
		end
		return title
	else
		return nil
	end
end

return p