Difference between revisions of "Module:HonorTitle"

From Pathfinder Wiki
(add support for pilots)
 
(23 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
local notUseLUAPattern = true
 
local notUseLUAPattern = true
  
function p.setDisplayTitle( frame, customTitle )
+
function p.setDisplayTitle( frame )
if ( customTitle == nil or customTitle == '' ) then
+
-- in case of an error the function doesn't return anything
--local pageTitle = frame:expandTemplate{ title = 'PAGENAMEwithoutLang' }
+
local customTitle = ''
local pageTitle = mw.title.getCurrentTitle().text
+
if ( frame.args.customTitle == nil or frame.args.customTitle == '' ) then
mw.log('HonorTitle:setDisplayTitle (pageTitle): '..pageTitle)
+
local templateVersion = 'name'
local tPageTitle = mw.text.split(pageTitle, '/', notUseLUAPattern)
+
local locLandingPage = ''
local honorname = tPageTitle[2]
+
if ( frame.args.customLandingPage ~= nil and frame.args.customLandingPage ~= '' ) then
mw.log('HonorTitle:setDisplayTitle (honorname): '..honorname)
+
locLandingPage = frame.args.customLandingPage
local transHonorname = utilities.localize( honorname, 'Honors' )
+
mw.log('HonorTitle.setDisplayTitle (customLandingPage): '..locLandingPage)
mw.log('HonorTitle:setDisplayTitle (transHonorname): '..transHonorname)
+
end
 +
local transHonorname = frame:expandTemplate{ title = 'GetHonorName', args = { page = locLandingPage } }
 +
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
 +
 +
local honorstate = frame.args.state
 +
if ( honorstate == nil or honorstate == '' ) then
 +
-- try to get it from MetaData
 +
mw.log('HonorTitle.setDisplayTitle: Call GetHonorMetaData for state!')
 +
honorstate = frame:expandTemplate{ title = 'GetHonorMetaData', args = { dontTranslate = 'true', parameter = 'state' } }
 +
end
 +
mw.log('HonorTitle.setDisplayTitle (honorstate): '..tostring(honorstate))
 
 
customTitle = transHonorname
+
customTitle = getTitle(templateVersion, transHonorname, subpage, countries, authorities, honorstate)
local subPageString = getSubpageString(tPageTitle[3])
+
if (customTitle == nil) then
if (subPageString ~= nil) then
+
-- return nothing so the title doesn't get changed
customTitle = customTitle..subPageString
+
return
 
end
 
end
 +
else
 +
customTitle = frame.args.customTitle
 
end
 
end
 
return mw.ext.displaytitle.set(customTitle)
 
return mw.ext.displaytitle.set(customTitle)
 
end
 
end
  
function getSubpageString(honorSubpage)
+
function getTitle(templateVersion, honorname, subpage, countries, authorities, honorstate)
local subPageString = nil
+
if honorstate == 'piloting' then
-- check input
+
templateVersion = 'pilot,'..templateVersion
if ( honorSubpage == nil ) then
 
mw.log('HonorTitle.getSubpageString: Input value is invalid!')
 
return subPageString
 
 
end
 
end
 
 
-- extract the number if present
+
local title = utilities.localize(templateVersion, 'HonorTitle')
local variantNumber = tonumber(string.match(honorSubpage, '%d+'))
+
if (title ~= templateVersion) then
if (variantNumber == nil) then
+
mw.log('HonorTitle.getTitle: Replace $honorname with '..honorname)
-- if the page has no number then it is mostlikely number one
+
title = mw.ustring.gsub(title, '$honorname', honorname)
-- and if it is an invalid subpage then the number won't be used anyway's
+
if (subpage ~= nil) then
variantNumber = 1
+
mw.log('HonorTitle.getTitle: Replace $subpage with '..subpage)
end
+
title = mw.ustring.gsub(title, '$subpage', subpage)
-- 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['authorities']) do
 
if (key == 1) then
 
subPageString = subPageString..authority
 
else
 
subPageString = subPageString..' & '..authority['translated']
 
end
 
 
end
 
end
subPageString = subPageString..')'
+
if (countries ~= nil) then
elseif (mw.ustring.find(honorSubpage, 'Answer Key', 0, notUseLUAPattern) ~= nil ) then
+
mw.log('HonorTitle.getTitle: Replace $territory with '..countries)
mw.log('HonorTitle.getSubpageString: It is the Answer Key Variant '..variantNumber)
+
title = mw.ustring.gsub(title, '$territory', countries)
local tAnsVariant = honorUtils.getVariantsData( frame, '', 'answerKey', variantNumber)
 
subPageString = ' - '..utilities.localize('Answer Key')..' ('
 
local authoritiesString = nil
 
if (tAnsVariant['authority'] ~= nil) then
 
-- get req variant data
 
tReqVariant = honorUtils.getVariantsData(frame, '', 'requirements', tVariant['authority'])
 
for key, authority in pairs(tReqVariant['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['countries']['split']) do
 
if (countriesString == nil) then
 
countriesString = country['translated']
 
else
 
countriesString = countriesString..'&'..country['translated']
 
end
 
 
end
 
end
subPageString = subPageString..countriesString
+
if (authorities ~= nil) then
if (authoritiesString ~= nil) then
+
mw.log('HonorTitle.getTitle: Replace $authority with '..authorities)
subPageString = subPageString..' - '..authoritiesString
+
title = mw.ustring.gsub(title, '$authority', authorities)
 
end
 
end
subPageString = subPageString..')'
+
return title
 
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

Latest revision as of 16:18, 3 April 2022

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 )
	-- in case of an error the function doesn't return anything
	local customTitle = ''
	if ( frame.args.customTitle == nil or frame.args.customTitle == '' ) then
		local templateVersion = 'name'
		local locLandingPage = ''
		if ( frame.args.customLandingPage ~= nil and frame.args.customLandingPage ~= '' ) then
			locLandingPage = frame.args.customLandingPage
			mw.log('HonorTitle.setDisplayTitle (customLandingPage): '..locLandingPage)
		end
		local transHonorname = frame:expandTemplate{ title = 'GetHonorName', args = { page = locLandingPage } }
		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
		
		local honorstate = frame.args.state
		if ( honorstate == nil or honorstate == '' ) then
			-- try to get it from MetaData
			mw.log('HonorTitle.setDisplayTitle: Call GetHonorMetaData for state!')
			honorstate = frame:expandTemplate{ title = 'GetHonorMetaData', args = { dontTranslate = 'true', parameter = 'state' } }
		end
		mw.log('HonorTitle.setDisplayTitle (honorstate): '..tostring(honorstate))
		
		customTitle = getTitle(templateVersion, transHonorname, subpage, countries, authorities, honorstate)
		if (customTitle == nil) then
			-- return nothing so the title doesn't get changed
			return
		end
	else
		customTitle = frame.args.customTitle
	end
	return mw.ext.displaytitle.set(customTitle)
end

function getTitle(templateVersion, honorname, subpage, countries, authorities, honorstate)
	if honorstate == 'piloting' then
		templateVersion = 'pilot,'..templateVersion
	end
	
	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