Module:Naval Vessel Register
Appearance
Implements {{Naval Vessel Register}}
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 = '\'\'[[Naval Vessel Register]]\'\'.'
--[[--------------------------< 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 (count, list_seq)
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_seq, sep_list_pair); -- insert separator between two items; returns list_seq[1] then only one item
elseif 2 < count then
list = table.concat (list_seq, sep_list, 1, count - 1); -- concatenate all but last item with plain list separator
list = table.concat ({list, list_seq[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
--[[--------------------------< 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, arg in ipairs (list_t) do
list_t[i] = string.format ('[%s here]', list_t[i]); -- format as an ext link
end
if 1 == #list_t then
return string.format ('%s %s at %s', prefix, list_t[1], postfix); -- and render
else
return string.format ('%s %s at %s', prefix, sep_list_make (#list_t, list_t), postfix); -- and render as separated list
end
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 tagn and namen 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 as 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])); -- go render the nvr page link
end
if 0 == #list_t then
return string.format ('%s the %s', prefix, postfix); -- the generic rendering
elseif 1 == #list_t then
return string.format ('%s %s at %s', prefix, list_t[1], postfix); -- for a single ext link
else
return string.format ('%s %s at %s', prefix, sep_list_make (#list_t, list_t), postfix); -- for multiple ext links
end
end
--[[--------------------------< E X P O R T S >----------------------------------------------------------------
]]
return {
main = main,
}