Difference between revisions of "Module:HonorUtils"

From Pathfinder Wiki
Line 12: Line 12:
 
tVariants['requirements']['orig'] = getHonorVariants( 'requirement', landingPage, false )
 
tVariants['requirements']['orig'] = getHonorVariants( 'requirement', landingPage, false )
 
tVariants['requirements']['split']  = mw.text.split(tVariants['requirements']['orig'], '|', true)
 
tVariants['requirements']['split']  = mw.text.split(tVariants['requirements']['orig'], '|', true)
 +
mw.log('HonorUtils.getExpandedHonorVariantsV2: '..#tVariants)
 
 
 
tVariants = p.expandHonorVariantsV2( frame, tVariants )
 
tVariants = p.expandHonorVariantsV2( frame, tVariants )

Revision as of 15:39, 10 July 2021

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

local p = {}

function p.getExpandedHonorVariantsV2( frame, landingPage )
	if ( landingPage == nil or landingPage == "" ) then
		landingPage = frame:expandTemplate{ title='GetHonorLandingPage' }
	end
	local tVariants = {}
	tVariants['answers'] = {}
	tVariants['answers']['orig'] = getHonorVariants( 'answer', landingPage, false )
	tVariants['answers']['split'] = mw.text.split(tVariants['answers']['orig'], '|', true)
	tVariants['requirements'] = {}
	tVariants['requirements']['orig'] = getHonorVariants( 'requirement', landingPage, false )
	tVariants['requirements']['split']  = mw.text.split(tVariants['requirements']['orig'], '|', true)
	mw.log('HonorUtils.getExpandedHonorVariantsV2: '..#tVariants)
	
	tVariants = p.expandHonorVariantsV2( frame, tVariants )
	
	return tVariants
end

function p.expandHonorVariantsV2( frame, tVariants )
	if ( tVariants == nil ) then
		mw.log('HonorUtils.expandHonorVariantsV2: No Variants to Expand (nil)')
		return {}
	elseif ( #tVariants == 0 ) then
		mw.log('HonorUtils.expandHonorVariantsV2: No Variants to Expand (0)!')
		return {}
	end
	if ( tVariants['answers'] == nil or #tVariants['answers'] == 0) then
		mw.log('HonorUtils.expandHonorVariantsV2: No Answer Variants to Expand!')
	else
		-- expand the answer Variants
	end
	if ( tVariants['requirements'] == nil or #tVariants['requirements'] == '') then
		mw.log('HonorUtils.expandHonorVariantsV2: No Requirement Variants to Expand!')
	else
		-- expand the requirement Variants
	end
	
end

function p.getExpandedHonorVariants(frame, variantsString, landingPage, targetPage)
	-- check the input variables
	if (frame == nil or landingPage == nil or landingPage == "" or targetPage == nil or targetPage == "") then
		return "fatal error"
	end
	if (variantsString == nil or variantsString == "") then
		local linkTitle = frame:expandTemplate{ title = 'Localize', args = { targetPage } }
		return {
			{
				url = "[[" .. landingPage .. "/" .. targetPage .. frame:expandTemplate{title='GetLangSuffix'} .. "|" .. linkTitle .. "]]"
			}
		}
	end
	
	local t_variants = p.expandHonorVariants(frame, variantsString)
	t_variants = p.translateHonorVariants(frame, t_variants)
	t_variants = p.createHonorVariantsURLs(frame, t_variants, landingPage, targetPage)
	return t_variants
end

function p.expandHonorVariants(frame, variantsString)
	if (variantsString == nil or variantsString == "") then return {} end
	
	local t_variants = mw.text.split( variantsString, ';;', true )
	
	for key, variant in pairs(t_variants) do
		t_variants[key] = {
			orig = variant,
			countries = mw.text.split( variant, ';', true )
		}
	end
	
	return t_variants
end

function p.translateHonorVariants(frame, variants)
	local ret_t_variants = variants
	for varKey, variant in pairs(variants) do
		for countryKey, country in pairs(variant['countries']) do
			local trans = frame:expandTemplate{ title = 'Localize', args = { country, 'Answer Variants' } }
			local t_countryTrans = {
				orig = country,
				translation = trans
			}
			ret_t_variants[varKey]['countries'][countryKey] = t_countryTrans
		end
	end
	return ret_t_variants
end

function p.createHonorVariantsURLs(frame, variants, landingPage, targetPage)
	local t_variants = variants
	local langSuffix = frame:expandTemplate{title='GetLangSuffix'}
	for key, variant in pairs(variants) do
		local linkTitle = ""
		for countryKey, country in pairs(variant['countries']) do
			if (#linkTitle > 0) then
				linkTitle = linkTitle .. " & " .. country['translation']
			else
				linkTitle = country['translation']
			end
		end
		t_variants[key]['url'] ="[[" .. landingPage .. "_(" .. variant['orig'] .. ")/" .. targetPage .. langSuffix .. "|" .. linkTitle .. "]]"
	end
	return t_variants
end

function getHonorVariants(variantType, src, srcIsHonor)
	if (variantType == nil or variantType == '') then
		variantType = 'requirement'
	end
	local cntOfVariants = tonumber(p.getHonorMetadata(src, 'cnt_'..variantType..'_variants', srcIsHonor))
	if (cntOfVariants == nil or cntOfVariants == 0) then
		mw.log('HonorUtils.getHonorVariants: There are no '..variantType..' variants!')
		return ''
	end
	mw.log('HonorUtils.getHonorVariants (cntOfVariants): '..cntOfVariants)
	local actVariant = 0
	local tVariantArguments = {}
	for actVariant = 1, cntOfVariants, 1 do
		mw.log('HonorUtils.getHonorVariants (actVariant): '..actVariant)
		tVariantArguments[actVariant] = variantType .. '_variant_' .. actVariant
	end
	local strVariantArguments = mw.text.listToText(tVariantArguments, ':', ':')
	mw.log('HonorUtils.getHonorVariants (strVariantArguments): '..strVariantArguments)
	local strVariant = p.getHonorMetadata(src, strVariantArguments, srcIsHonor)
	mw.log('HonorUtils.getHonorVariants (strVariant): '..strVariant)
	return strVariant
end

function p.getHonorMetadata(src, metadata, srcIsHonor)
	local frame = mw.getCurrentFrame()
	local lPage = ''
	if (src ~= '' and src ~= nil) then
		if (srcIsHonor == true) then
			lPage = 'AY Honors/' .. src
		else
			lPage = src
		end
	end
	lPage = frame:expandTemplate{ title = 'GetHonorLandingPage', args = {page = lPage}}
	return frame:preprocess( '{{#dpl:|debug=0|title=' .. lPage .. '|include={HonorLanding}:' .. metadata ..'}}' )
end

return p