Module:HonorUtils

From Pathfinder Wiki
Revision as of 20:30, 6 July 2021 by DesignerThan (talk | contribs)

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

local p = {}

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 = { 'Answer Key' } }
		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( variantsString, ';', 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

return p