Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:USN fleet totals

From Wikipedia, the free encyclopedia

local data = mw.loadData ('Module:USN fleet totals/data');						-- get the detailed list of ship types and their counts, the grand total of active and reserve ships, and the total planned ships
local get_args = require ('Module:Arguments').getArgs;


--[[--------------------------< R O U N D >--------------------------------------------------------------------

rounds <count> to nearest multiple of 5.

]]

local function round (count)
	if 0 == count % 5 then
		return count;															-- <count> is xx0, xx5 so return unmolested
	elseif 2.5 > (count % 5) then
		return count - (count % 5);												-- <count> is xx1, xx2 so return xx0
	else
		return count + (5 - (count % 5));										-- <count> is xx3, xx4 so return xx5
	end
end


--[[--------------------------< U S N _ F L E E T _ T O T A L S >----------------------------------------------

implements {{USN fleet totals}}

This function returns one of three values according to the text sting in {{{1}}} (frame.args[1]):
	active – the 'grand total' (active and reserved fleets) rounded to the nearest multiple of 5					-- {{USN fleet totals|active}}
	planned – the 'planned total' (ships under construction or on order) rounded to the nearest multiple of 5		-- {{USN fleet totals|planned}}
	retire – number of ships to be decommissioned or taken out of service											-- {{USN fleet totals|retire}}
	retire-year – planned retirement year farthest in the future													-- {{USN fleet totals|retire-year}}
	<anything or nothing> – the long string of fleet totals created by Module:USN fleet totals						-- {{USN fleet totals}}

]]

local function USN_fleet_totals (frame)
	if 'active' == frame.args[1] then											-- number of active and reseved fleet ships
		return round (data.grand_total);										-- round to nearest multiple of 5
	elseif 'planned' == frame.args[1] then										-- under construction and on-order ships
		return round (data.planned_total);										-- round to nearest multiple of 5
	elseif 'retire' == frame.args[1] then										-- ships to be decommissioned or taken out of service
		return round (data.retirements_total);									-- round to nearest multiple of 5
	elseif 'retire-year' == frame.args[1] then
		return data.retirements_year_max;
	else																		-- {{{1}}} is anything else
		return data.fleet_totals_str;											-- return the detailed fleet totals
	end
end


--[[--------------------------< _ D E C O M _ E O S _ C O M M O N >--------------------------------------------

common function to implement {{decommission}} and {{end of service}} templates
	
]]

local function _decom_eos_common (args_t, phrases_t, template_name)
	local out_t = {};

	if phrases_t[args_t[1]] then												-- is keyword valid?
		table.insert (out_t, phrases_t[args_t[1]]);								-- yes, get the associated string
	elseif args_t[1] then														-- no, but not nil so emit appropriate error message
		return '<span style="color:#d33"><kbd>{{[[Template:' .. template_name .. '|' .. template_name ..']]}}</kbd>: error: unknown keyword: ' .. args_t[1] .. '</span>';
	else																		-- missing keyword so emit error message
		return '<span style="color:#d33"><kbd>{{[[Template:' .. template_name .. '|' .. template_name ..']]}}</kbd>: error: missing keyword</span>'
	end

	if args_t[2] then															-- did we get this optional parameter?
		local year = mw.text.trim (args_t[2]):match ('^%d%d%d%d$');				-- attempt to get a four digit year

		if year then															-- did we find a 'year'?  TODO: validate? must be this or a future year? never in the past?
			table.insert (out_t, year);											-- add the year to the output
		else																	-- here when a four-digit 'year' not found
			return '<span style="color:#d33"><kbd>{{[[Template:' .. template_name .. '|' .. template_name ..']]}}</kbd>: error: invalid year</span>';
		end
	end

	return table.concat (out_t, ' ');											-- assemble into a big string and done
end


--[[--------------------------< D E C O M M I S S I O N >------------------------------------------------------

implements {{decommission}}

Takes two positional parameters:
	{{{1}}} – (required) one of two keywords that cause the template to return an appropriate phrase; phrases are
				defined in ~/data.  The keywords are 'scheduled' and 'proposed'
	{{{2}}} – (optional) year when decommissioning is expected to occur
	
]]

local function decommission (frame)
	local args_t = get_args (frame);											-- get parameters from the template into a local table
	return _decom_eos_common (args_t, data.decommission_t, 'decommission');		-- call common function to error check the inputs and return the rendered string
end


--[[--------------------------< E N D _ O F _ S E R V I C E >--------------------------------------------------

implements {{end of service}}

Takes two positional parameters:
	{{{1}}} – (required) one of two keywords that cause the template to return an appropriate phrase; phrases are
				defined in ~/data.  The keywords are 'scheduled' and 'proposed'
	{{{2}}} – (optional) year when end of service is expected to occur
	
]]

local function end_of_service (frame)
	local args_t = get_args (frame);											-- get parameters from the template into a local table
	return _decom_eos_common (args_t, data.end_of_service_t, 'end of service');	-- call common function to error check the inputs and return the rendered string
end	


--[[--------------------------< E X P O R T S >----------------------------------------------------------------
]]

return 
	{
	USN_fleet_totals = USN_fleet_totals,
	
	decommission = decommission,
	end_of_service = end_of_service,
	}