Jump to content

Module:Naval Vessel Register

Permanently protected module
From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by Pppery (talk | contribs) at 00:48, 29 July 2025 (Changed protection settings for "Module:Naval Vessel Register": Match template ([Edit=Require template editor access] (indefinite))). The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

require('strict');

local get_args = require ('Module:Arguments').getArgs;
local nvr_url_mod = require ('Module:Naval Vessel Register URL');

local prefix = '[[File:PD-icon.svg|12px|class=noviewer|link=|alt=Public Domain]] This article incorporates [[Copyright status of works by the federal government of the United States|public domain material]] from';
local postfix = 'the \'\'[[Naval Vessel Register]]\'\'.'

local namespace = mw.title.getCurrentTitle().namespace;							-- used for categorization
local category = 0 == namespace and '[[Category:Wikipedia articles incorporating text from the Naval Vessel Register]]' or '';


--[[--------------------------< S E P _ L I S T _ M A K E >----------------------------------------------------

create a separated list according to the number of items in the list (2 or more) – don't call this function when
there is only one item in the list.

]]

local function sep_list_make (list_t)
	local count = #list_t;														-- get the number of items in the list
	local list = '';

	local sep_list_pair = ' and ';												-- for a list of two items
	local sep_list = ', ';														-- and these for lists of three or more items
	local sep_list_end = ', and ';

	if 2 >= count then
		list = table.concat (list_t, sep_list_pair);							-- insert separator between two items; returns list_seq[1] then only one item
	elseif 2 < count then
		list = table.concat (list_t, sep_list, 1, count - 1);					-- concatenate all but last item with plain list separator
		list = table.concat ({list, list_t[count]}, sep_list_end);				-- concatenate last item onto end of <list> with final separator
	end
	
	return list;
end


--[[--------------------------< L I N K _ M A K E >------------------------------------------------------------

call Module:Naval Vessel Register URL to make an ext link to the NVR page associated with <hull> (the ship's hull
designator).  When <title> is set, use that value as the ext link's display value.  When <title> is not set,
instruct NVR url to provide a display label (ship's name with parenthetical hull designator)

]]

local function link_make (hull, title, archive)
	local name = not title and 'nh';											-- <name> set to 'nh' tells NVR url to return ship's name and hull designator as display label; TODO: better rvalue?

	local nvr_link = nvr_url_mod._nvr_url_make ({['id']=hull, ['title']=title, ['name']=name});
	if archive then
		nvr_link = string.format ('%s <span style="font-size: 85%%;">([%s Archived])</span>', nvr_link, archive);
	end

	return nvr_link;
end


--[[--------------------------< R E N D E R >------------------------------------------------------------------

assemble the various parts into the final rendering

]]

local function render (list_t)
	local count = #list_t;
	
	if 0 == count then
		return string.format ('%s %s%s', prefix, postfix, category);			-- the no ext link generic rendering

	else
		return string.format ('%s %s at %s%s',									-- the one-or-more ext link rendering
			prefix,
			1 == count and list_t[1] or sep_list_make (list_t),					-- a single nvr ext link or a separated list of multiple nvr ext links
			postfix,
			category);
	end
end


--[[--------------------------< O L D _ F O R M >--------------------------------------------------------------

TODO: this function to go away

this function allows support for the current (as of 2025-07-20) {{Naval Vessel Register}} template which uses
positional parameters.  The rendering isn't perfect but will suffice until the template is updated to use the
new parameters.  When all old-form templates have been replaced, this function goes away to be replaced with an
error message when positional parameters are encountered.

]]

local function old_form (args_t)
	local list_t = {args_t[1], args_t[2]};										-- make a list
	for i, list_item in ipairs (list_t) do
		list_t[i] = string.format ('[%s here]', list_item);						-- format as an ext link
	end
	
	return render (list_t);														-- and go render the output
end


--[[--------------------------< M A I N >----------------------------------------------------------------------

implements {{Naval Vessel Register}}

{{#invoke:Naval Vessel Register|main}}

has enumerated parameters.  Non-enumerated forms of these parameters are treated as if they had enumerator '1'.
the enumerators are not bounded except that there must be no gaps between sucessive enumerators.

|hulln=		– (required if |titlen= or |archiven= provided) the ship's hull number in a form acceptable to {{NVR url}}
|titlen=	– (optional) any text; when empty or omitted, creates value from {{NVR url}}
|archiven=	– (optional) url linking to an archived copy of the NVR page

The original form of {{Naval Vessel Register}} accepted one or two optional positional parameters (urls).  During
the transition from old to new, this module supports both forms.  After the transition, positional parameters
will cause the module to emit an error message.

]]

local function main (frame)
	local args_t = get_args (frame)

	if args_t[1] then															-- old form with positional parameters; to be discontinued
		return old_form (args_t);
	end

	local unsorted_enumerators_t = {}											-- unsorted  k/v table of enumerators where k is the enumerator and v is always true

	for param, _ in pairs (args_t) do											-- loop through all parameters
		local enumerator = mw.ustring.match (param, "(%d+)$")					-- does this parameter have an enumerator?
		if enumerator then														-- if there is an enumerator
			unsorted_enumerators_t[tonumber(enumerator)] = true					-- add enumerator to the table
		else
			unsorted_enumerators_t[1] = true									-- assume enumerator is 1
			args_t[param .. '1'] = args_t[param];								-- copy unenumerated parameter value to the enumerated parameter
			args_t[param] = nil;												-- unset unenumerated parameter because redundant
		end
	end

	local enumerators_t = {}													-- will hold a sorted sequence of enumerators
	for n, _ in pairs (unsorted_enumerators_t) do								-- loop through the k/v table of enumerators
		table.insert (enumerators_t, n)											-- add the enumerator to the sequence
	end
	table.sort (enumerators_t)													-- and ascending sort

	local list_t = {};															-- list of formatted nvr ext links goes here

	for _, n in ipairs (enumerators_t) do										-- loop through the sorted enumerators
		table.insert (list_t, link_make (args_t['hull'..n], args_t['title'..n], args_t['archive'..n]));	-- make the nvr page link and add to the list of links
	end

	return render (list_t);														-- and go render the output
end


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

return {
	main = main,
	}