Module:Journal-date

From Guild of Archivists
Revision as of 02:50, 24 December 2024 by Pharap (talk | contribs) (Update module to handle D'ni numbers properly.)

Documentation for this module may be created at Module:Journal-date/doc

--
--
--

local module = {}

-- A lookup table for the names of the vaileetee
local vailee_names =
{
	"Leefo",
	"Leebro",
	"Leesahn",
	"Leetar",
	"Leevot",
	"Leevofo",
	"Leevobro",
	"Leevosahn",
	"Leevotar",
	"Leenovoo",
}

-- The 'century' that journal dates refer to
local journal_epoch = 9375

-- Converts an abbreviated hahr to a full hahr
local function abbreviated_hahr_to_full_hahr(abbreviated_hahr)
	return (journal_epoch + abbreviated_hahr)
end

function module.format_journal_date(frame)
	-- Cache the args object to avoid excessive table lookups
	local args = frame.args
	
	-- Check for the presence of the three main parameters
	
	local hahr
	
	if args[1] ~= nil then
		hahr = tonumber(args[1])
	elseif args['hahr'] ~= nil then
		hahr = tonumber(args['hahr'])
	else
		error "Parameter 1 or 'hahr' must be provided."
	end
	
	local vailee
	
	if args[2] ~= nil then
		vailee = tonumber(args[2])
	elseif args['vailee'] ~= nil then
		vailee = tonumber(args['vailee'])
	else
		error "Parameter 2 or 'vailee' must be provided."
	end
	
	local yahr
	
	if args[3] ~= nil then
		yahr = tonumber(args[3])
	elseif args['yahr'] ~= nil then
		yahr = tonumber(args['yahr'])
	else
		error "Parameter 3 or 'yahr' must be provided."
	end
	
	-- Process the three main parameters
	
	-- Ensure that the provided hahr is in range
	if hahr < 0 or hahr > 624 then
		error "Parameter 1 or 'hahr' must be between 0 and 624 inclusive."
	end
	
	-- Cache a formatted string of 'hahr'
	local abbreviated_hahr_string = tostring(hahr)
	
	-- Convert the abbreviated hahr to a full hahr
	local full_hahr = abbreviated_hahr_to_full_hahr(hahr)
	
	-- Cach a formatted string of 'full_hahr'
	local full_hahr_string = tostring(full_hahr)
	
	-- Ensure that the provided vailee is in range
	if vailee < 1 or vailee > 10 then
		error "Parameter 2 or 'vailee' must be between 1 and 10 inclusive."
	end
	
	-- Cache a formatted string of 'vailee'
	local vailee_string = tostring(vailee)
	
	-- Obtain the proper name of the vailee
	local vailee_name = vailee_names[vailee]
	
	-- Ensure that the provided yahr is in range
	if yahr < 1 or yahr > 29 then
		error "Parameter 3 or 'yahr' must be between 1 and 29 inclusive"
	end
	
	-- Cache a formatted string of 'yahr'
	local yahr_string = tostring(yahr)
	
	-- Prepare the result variable
	local text_buffer
	
	-- If the 'dni' arg was provided
	if args["dni"] ~= nil then
		local dni_numerals = require "Module:Dni-numerals"
		local to_dni_string = dni_numerals._number_to_dni_string
		
		text_buffer =
		{
			"{{hover text| ",
			full_hahr_string, "&nbsp;DE ", vailee_name, " ", yahr_string,
			" | ",
			"[[", full_hahr_string, " DE|", "<dni>", to_dni_string(hahr), "</dni>", "]]",
			"&bullet;", "<dni>", to_dni_string(vailee), "</dni>", "&bullet;", "<dni>", to_dni_string(yahr), "</dni>", "}}"
		}
	else
		text_buffer =
		{
			"{{hover text| ",
			full_hahr_string, "&nbsp;DE ", vailee_name, " ", yahr_string,
			" | ",
			"[[", full_hahr_string, " DE|", abbreviated_hahr_string, "]]",
			".", vailee_string, ".", yahr_string, "}}"
		}
	end
	
	return table.concat(text_buffer)
end

return module