Module:WPSHIPS utilities
![]() | This Lua module is used on approximately 100,000 pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
ship_name_format
This function attempts to apply appropriate formatting to unformatted ship names. The formats supported are:
- <(prefix)> <name> <(disambiguator)> where:
- <(prefix)> (optional) – one from a list of defined prefixes commonly used for ships (ARA, HMS, USS, etc.); rendered in upright font
- <name> – the ship's proper name; rendered in italic font
- <(disambiguator)> (optional) – typically the ship's hull or pennant number, or other disambiguator; always contained in parentheses; rendered in upright font
- <name> <(disambiguator)> where:
- <name> – the ship's proper name; rendered in italic font
- <(disambiguator)> (required) – the ship's hull or pennant number or a recognized ship-type descriptor; always contained in parentheses; rendered in upright font
- hull and pennant numbers take two basic forms
- uppercase letters, possibly followed by a space or hyphen, followed by digits (SSBN-659, R07, ON 688)
- digits, possibly followed by a space or hyphen, followed by uppercase letters (401B)
- hull and pennant numbers take two basic forms
- <nationality> <ship type> <name> <(disambiguator)> where:
- <nationality> – one- or two- word nation name in adjectival form (Dutch, East Timorese, French, Ottoman, etc.); rendered in upright font
- <ship type> – one from a list of one- to four-word phrases that describes a ship's type (aviso, brig sloop, landing ship medium, ship of the line); rendered in upright font
- <name> – the ship's proper name; rendered in italic font
- <(disambiguator)> (optional) – typically the ship's hull or pennant number, or other disambiguator; always contained in parentheses; rendered in upright font
The function will also attempt to render ship-class names when the name is in the form:
- <name>-class <ship type> where:
- <name> – the class' proper name; rendered in italic font according to the setting of
|sclass=
- <ship type> – a phrase that describes a ship's type; rendered in upright font
- <name> – the class' proper name; rendered in italic font according to the setting of
Lists
The function relies on three lists to accomplish its task. These lists are:
ship_prefix_list
– a list of commonly used ship prefixes initially taken from{{ship prefix}}
; these are usually, but not always wholly uppercasenationality_list
– a list of nation names in their adjectival form; these are always capitalized; always one or two wordsship_type_list
– a list ship-type descriptor phrases; these are usually, but not always wholly lowercase; always one to two words
To add to any of these lists, the form is:
['addition'] = true,
the trailing comma is important. See the instructions that accompany each list for limitations and implementation details.
Usage
The minimal form is:
{{#invoke:WPSHIPS_utilities|ship_name_format|name=}}
Parameters
The function requires one parameter and supports several optional parameters.
- |name= – (required) the name to format
- |dab=none – for use with
|infobox caption=nodab
in{{infobox ship begin}}
, excludes a disambiguator from the formatted name - |sclass=2 – for use with
{{DISPLAYTITLE}}
and with|infobox caption=yes
or|infobox caption=nodab
in{{infobox ship begin}}
, when the class is not named for a member of the class; causes the class name to be rendered in upright font - |adj=off – for use in ship-class articles with
|infobox caption=yes
or|infobox caption=nodab
in{{infobox ship begin}}
, formats the class name in its noun form and excludes the ship type phrase and disambiguator - |showerrs=yes – debug tool; not currently supported in any templates;
Wrapper template
- {{Ship name format}} is a wrapper template to make it easier for editors to label ships in the main body of articles or as input to templates or modules. It supports the same parameters as the module.
- In most cases, {{ship}} is preferred over using this wrapper template or this module.
- It is useful to set the DISPLAYTITLE of redirects, disambiguation pages, or other pages named after ships which do not use one of the other "ships" templates such as {{Infobox ship begin}} to do this. Example:
{{DISPLAYTITLE:{{ship_name_format|name={{FULLPAGENAME}}}}}}
- Other templates and modules should not transclude this template, instead they should call the module directly.
- Wikipedia pages should not transclude this template many times, as it may cause the page to exceed Wikipedia's template limits. Those pages should invoke the module directly.
wpsu.navsource()
This function is the driver for {{navsource}}
. The function assembles an external wikilink from known fragments and positional parameter {{{1}}}
. A ship name provided in positional parameter {{{2}}}
is formatted using ship_name_format()
. New with this implementation of {{navsource}}
is the addition of |showerrs=
. The {{#invoke:}}
in the template defaults to |showerrs=yes
which can be overridden by setting |showerrs=no
in the template.
wpsu.hnsa()
Similar to {{navsource}}
, this code supports {{hnsa}}
by attempting to construct a link to a ship article at the Historic Naval Ships Association website.
The template has the form:
{{hnsa|<page>|<name>}}
where:
- <page> is the name of the page from the url
http://hnsa.org/hnsa-ships/<page>
- <name> (optional) is the name of the ship; if left blank, the template uses the current page title; if a ship name, it is formatted using
do_ship_name_format()
from which this code produces:
[http://hnsa.org/hnsa-ships/<page> <name>] at Historic Naval Ships Association
require('Module:No globals')
local wpsu={}
--[[-------------------------< S H I P P R E F I X L I S T >-----------------------------------------------
This is a list of currently supported ship prefixes. To add to this list the form is:
['prefix'] = length,
the trailing comma is important and of course, the prefix length must match the number of characters actually in
the prefix.
]]
local ship_prefix_list =
{
['ARA'] = 3, -- Armada de la República Argentina
['ARC'] = 3, -- Armada Nacional de la República de Colombia
['CCGS'] = 4, -- Canadian Coast Guard Ship
['CFAV'] = 4, -- Canadian Forces Auxiliary Vessel
['CS'] = 2, -- Cable Ship
['CSS'] = 3, -- Confederate States Ship
['FGS'] = 3, -- Federal German Ship
['GTS'] = 3, -- Gas Turbine Ship
['HDMS'] = 4, -- His/Her Danish Majesty's Ship
['HMAS'] = 4, -- Her/His Majesty's Australian Ship
['HMBS'] = 4, -- Her/His Majesty's Britannic Ship (also: Bahamian, Bermudian, Burmese)
['HMC'] = 3, -- Her/His Majesty's Cutter
['HMCS'] = 4, -- Her/His Majesty's Canadian Ship (also Colonial)
['HMNZS'] = 5, -- Her/His Majesty's New Zealand Ship
['HMQS'] = 4, -- Her/His Majesty's Queensland Ship
['HMS'] = 3, -- Her/His Majesty's Ship
['HMT'] = 3, -- Her/His Majesty's Trawler
['HMVS'] = 4, -- Her/His Majesty's Victorian Ship
['HNLMS'] = 5, -- His/Her Netherlands Majesty’s Ship
['HNoMS'] = 5, -- His/Her Norwegian Majesty's Ship
['HSwMS'] = 5, -- His/Her Swedish Majesty's Ship
['HTMS'] = 4, -- His Thai Majesty's Ship
['INS'] = 3, -- Indian Naval Ship, Israeli Naval Ship
['KDM'] = 3, -- Kongelige Danske Marine
['MF'] = 2, -- Motor Ferry
['MS'] = 2, -- Motor Ship
['MV'] = 2, -- Motor Vessel
['NoCGV'] = 5, -- Norwegian Coast Guard Vessel
['NRP'] = 3, -- Navio da República Portuguesa
['ORP'] = 3, -- Okręt Rzeczypospolitej Polskiej
['PS'] = 2, -- Paddle Steamer
['RFA'] = 3, -- Royal Fleet Auxiliary
['RNLB'] = 4, -- Royal National Lifeboat
['RMAS'] = 4, -- Royal Maritime Auxiliary Service
['RMS'] = 3, -- Royal Mail Ship
['RV'] = 2, -- Research Vessel
['SM'] = 2, -- Seiner Majestät Unterseeboot
['SMS'] = 3, -- Seiner Majestät Schiff
['SS'] = 2, -- Screw Steamer or Steamship
['TCG'] = 3, -- Türkiye Cumhuriyeti Gemisi
['TV'] = 2, -- Training vessel
['USAFS'] = 5, -- United States Air Force ship
['USAS'] = 4, -- United States Army Ship
['USCGC'] = 5, -- United States Coast Guard Cutter
['USNS'] = 4, -- United States Naval Ship
['USRC'] = 4, -- United States Revenue Cutter
['USS'] = 3, -- United States Ship
}
--[[--------------------------< I S _ S E T >------------------------------------------------------------------
Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not an empty string.
]]
local function is_set( var )
return not (var == nil or var == '');
end
--[[-------------------------< S H I P _ N A M E _ F O R M A T >----------------------------------------------
This function applies correct styling to free-form ship names. These names are, for example, ship-article titles
used by templates {{navsource}}, {{Infobox ship begin}}, where the article title is to be rendered with proper
styling.
This function takes a single argument, the ship name, perhaps like this:
{{#invoke:WPSHIPS_utilities|ship_name_format|name={{PAGENAME}}}}
]]
function wpsu.ship_name_format(frame)
-- local pframe = frame:getParent() -- is this needed?
-- local args = {};
--
-- for k, v in pairs( pframe.args ) do
-- args[k] = v;
-- end
if not is_set (frame.name) then return '' end; -- return an empty string if there is no name
local prefix = '';
local name = '';
local dab = '';
if frame.name:match (arg, '^%s*%S*%s+.+%s+%b()%s*$') then -- if name might have a prefix
prefix, name, dab = frame.name:match (arg, '^%s*(%S*)%s+(.+)%s+(%b())%s*$'); -- get the component parts
if ship_prefix_list [prefix] then
name = ship_prefix_list [prefix] .. " ''" .. name .. "'' " .. dab; -- assemble formatted name
end
elseif frame.name:match (arg, '^%s*.+%s+%b()%s*$') then -- if name might not have a prefix
name, dab = frame.name:match (arg, '^%s*(.+)%s+(%b())%s*$'); -- get the component parts
name = " ''" .. name .. "'' " .. dab; -- assemble formatted name
end
return name; -- return the formatted name
end
return wpsu;