Module:HonorTitle: Difference between revisions

From Pathfinder Wiki
DesignerThan (talk | contribs)
No edit summary
DesignerThan (talk | contribs)
No edit summary
Line 5: Line 5:


function p.setDisplayTitle( frame, customTitle )
function p.setDisplayTitle( frame, customTitle )
-- in case of an error the function doesn't return anything
if ( customTitle == nil or customTitle == '' ) then
if ( customTitle == nil or customTitle == '' ) then
local templateVersion = 'name'
local pageTitle = frame:expandTemplate{ title = 'PAGENAMEwithoutLang' }
local pageTitle = frame:expandTemplate{ title = 'PAGENAMEwithoutLang' }
--local pageTitle = mw.title.getCurrentTitle().text
mw.log('HonorTitle.setDisplayTitle (pageTitle): '..pageTitle)
mw.log('HonorTitle:setDisplayTitle (pageTitle): '..pageTitle)
local tPageTitle = mw.text.split(pageTitle, '/', notUseLUAPattern)
local tPageTitle = mw.text.split(pageTitle, '/', notUseLUAPattern)
local honorname = tPageTitle[2]
local honorname = tPageTitle[2]
mw.log('HonorTitle:setDisplayTitle (honorname): '..honorname)
mw.log('HonorTitle.setDisplayTitle (honorname): '..honorname)
local transHonorname = utilities.localize( honorname, 'Honors' )
local transHonorname = utilities.localize( honorname, 'Honors' )
mw.log('HonorTitle:setDisplayTitle (transHonorname): '..transHonorname)
mw.log('HonorTitle.setDisplayTitle (transHonorname): '..transHonorname)
-- set it so we have a fallback if something goes wrong so it isn't empty
customTitle = transHonorname
customTitle = transHonorname
local subPageString = getSubpageString(tPageTitle[3])
if (subPageString ~= nil) then
local tActVariantInfo = honorUtils.getActVariantInfo( frame )
customTitle = customTitle..subPageString
local countries = nil
local authorities = nil
local subpage = nil
if ( tActVariantInfo ~= nil ) then
-- it is a subpage
templateVersion = templateVersion..',subpage'
subpage = utilities.localize(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) then
templateVersion = templateVersion..',country'
for key, country in pairs(tAnsVariant['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['requriements'] ~= nil) then
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?!
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
end
end
Line 24: Line 92:
end
end


function getSubpageString(honorSubpage)
function getTitle(templateVersion, honorname, subpage, countries, authorities)
local subPageString = nil
local title = utilities.localize(templateVersion, 'HonorTitle')
-- check input
if (title ~= templateVersion) then
if ( honorSubpage == nil ) then
title = mw.ustring.gsub(title, '$honorname', honorname)
mw.log('HonorTitle.getSubpageString: Input value is invalid!')
title = mw.ustring.gsub(title, '$subpage', subpage)
return subPageString
title = mw.ustring.gsub(title, '$country', countries)
end
title = mw.ustring.gsub(title, '$authority', authorities)
return title
-- extract the number if present
local variantNumber = utilities.getNumber(honorSubpage)
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)
if (tVariant == nil) then
mw.log('HonorTitle.getSubpageString: There are no Requirements Variants!')
return subPageString
end
subPageString = ' - '..utilities.localize('Requirements')..' ('
for key, authority in pairs(tVariant['requirements'][1]['authorities']) do
if (key == 1) then
subPageString = subPageString..authority['translated']
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)
if (tAnsVariant == nil) then
mw.log('HonorTitle.getSubpageString: There are no Answer Key Variants!')
return subPageString
end
subPageString = ' - '..utilities.localize('Answer Key')..' ('
local authoritiesString = nil
if (tAnsVariant['answers'][1]['authority'] ~= nil) then
-- get req variant data
tReqVariant = honorUtils.getVariantsData(frame, '', 'requirements', tAnsVariant['answers'][1]['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
else
mw.log('HonorTitle.getSubpageString: Invalid Subpage ('..honorSubpage..')')
return nil
subPageString = ' - '..utilities.localize(honorSubpage)
end
end
return subPageString
end
end


return p
return p

Revision as of 23:46, 26 July 2021

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 pageTitle = frame:expandTemplate{ title = 'PAGENAMEwithoutLang' }
		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)
		
		-- set it so we have a fallback if something goes wrong so it isn't empty
		customTitle = 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(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) then
						templateVersion = templateVersion..',country'
						for key, country in pairs(tAnsVariant['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['requriements'] ~= nil) then
							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?!
						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
		title = mw.ustring.gsub(title, '$honorname', honorname)
		title = mw.ustring.gsub(title, '$subpage', subpage)
		title = mw.ustring.gsub(title, '$country', countries)
		title = mw.ustring.gsub(title, '$authority', authorities)
		return title
	else
		return nil
	end
end

return p