error_msg = '<span>' .. error_msg .. '</span>';
else
error_msg = ''; -- for concatenation
end
end  
return name .. error_msg; -- return name and error message
end  
----------------------------< H N S A >----------------------------------------------------------------------  
Similar to , this code supports by attempting to construct a link to a ship article at the
the Historic Nava Ships Association website.  
The template has the form:  
where:
<page> is the name of the page at http://hnsa.org/hnsa-ships/<page>
<name> (optional) is the name of the ship; if blank, the template uses the current page title; if a ship name, it is formatted
from which this code produces:
<name> at Historic Naval Ships Association  
function wpsu.hnsa
local pframe = :getParent -- get arguments from calling template
local ship_name = '';
local error_msg = '';
local article_title = mw.title.getCurrentTitle.text; -- fetch the article title  
if not is_set (pframe.args[1]) then
return '<span>missing hsna page</span>';
end  
local fmt_params = {['name']='', ['showerrs']=nil};  
if is_set (pframe.args.showerrs) then -- if showerrs set in template, override showerrs in #invoke:
fmt_params.showerrs = pframe.args.showerrs; -- template value
else
fmt_params.showerrs = .args.showerrs; -- invoke value
end  
if is_set (pframe.args[2]) then
fmt_params.name = pframe.args[2];
else
fmt_params.name = article_title; -- use article title
end  
ship_name, error_msg = do_ship_name_format (fmt_params);  
if is_set (error_msg) and is_set (pframe.args[2]) then -- if unable to format the name
local escaped_name = pframe.args[2]:gsub("([%(%)%.%-])", "%%%1"); -- escape some of the Lua magic characters
if pframe.args[2] == article_title or -- is name same as article title?
nil ~= article_title:find ('%f[%a]' .. escaped_name .. '%f[%s]') or -- is name a word or words substring of article title?
nil ~= article_title:find ('%f[%a]' .. escaped_name .. '$') then -- is name a word or words substring that ends article title?
ship_name = "*" .. pframe.args[2] .. "*"; -- non-standard 'name'; perhaps just the name without prefix and dab;
error_msg = ''; -- unset because we think we have a name
end
end
if is_set (error_msg) and 'yes' == fmt_params.showerrs then
error_msg = '<span>' .. error_msg .. '</span>';
else
error_msg = ''; -- unset so it doesn't diplay
end  
local output = {
'pframe.args[1,
'/ ',
ship_name,
'] at Historic Naval Ships Association',
error_msg,
}  
return table.concat (output);
end  
----------------------------< N A V S O U R C E >------------------------------------------------------------  
This version of the template was added as a test vehicle for do_ship_name_format.  
function wpsu.navsource
local pframe = :getParent -- get arguments from calling template
local ship_name = '';
local error_msg = '';
local article_title = mw.title.getCurrentTitle.text; -- fetch the article title  
if not is_set (pframe.args[1]) then
return '<span>missing navsource URLcode</span>';
end  
local fmt_params = {['name']='', ['showerrs']=nil};  
if is_set (pframe.args.showerrs) then -- if showerrs set in template, override showerrs in #invoke:
fmt_params.showerrs = pframe.args.showerrs; -- template value
else
fmt_params.showerrs = .args.showerrs; -- invoke value
end  
if is_set (pframe.args[2]) then
fmt_params.name = pframe.args[2];
else
fmt_params.name = article_title; -- use article title
end  
ship_name, error_msg = do_ship_name_format (fmt_params);  
if is_set (error_msg) and is_set (pframe.args[2]) then -- if unable to format the name
local escaped_name = pframe.args[2]:gsub("([%(%)%.%-])", "%%%1"); -- escape some of the Lua magic characters
if pframe.args[2] == article_title or -- is name same as article title?
nil ~= article_title:find ('%f[%a]' .. escaped_name .. '%f[%s]') or -- is name a word or words substring of article title?
nil ~= article_title:find ('%f[%a]' .. escaped_name .. '$') then -- is name a word or words substring that ends article title?
ship_name = "*" .. pframe.args[2] .. "*"; -- non-standard 'name'; perhaps just the name without prefix and dab;
error_msg = ''; -- unset because we think we have a name
end
end
if is_set (error_msg) and 'yes' == fmt_params.showerrs then
error_msg = '<span>' .. error_msg .. '</span>';
else
error_msg = ''; -- unset so it doesn't diplay
end  
local output = {
'pframe.args[1,
'.htm Photo gallery] of ',
ship_name,
' at NavSource Naval History',
error_msg,
}  
return table.concat (output);
end  
----------------------------< _ S H I P >--------------------------------------------------------------------  
This is a possible replacement for the template . It has better error detection and handling.  
local function _ship (prefix, name, dab, control, unlinked_prefix)
local error_msg = '';  
if not is_set (control) then
control = '';
end
-- dispose of error conditions straight away
if not is_set (name) then -- this is the only required parameter
error_msg = ' missing name';
elseif not is_set (dab) and ('1' == control or '3' == control or '5' == control) then -- dab required when control value set to expect it
error_msg = ' missing disambiguator';
elseif not is_set (prefix) and ('5' == control or '6' == control) then -- prefix required when control value set to expect it
error_msg = ' missing prefix';
elseif '4' == control then -- displaying only the prefix
error_msg = 'invalid control parameter: ' .. control;
elseif is_set (control) then
if 1 > tonumber (control) or 6 < tonumber (control) then -- control must be a number between 1 through 6
error_msg = 'invalid control parameter: ' .. control;
end
elseif not is_set (prefix) and unlinked_prefix then -- prefix required when |up=yes
error_msg = ' missing prefix';
end  
if is_set (error_msg) then
return '<span>' .. error_msg .. '</span>'; -- return an error message; don't bother with making a link
end  
local link_name;
local link = * .. link_name .. *;
end  
----------------------------< W P S U . S H I P >------------------------------------------------------------  
This is a possible replacement for the template . It has better error detection and handling.  
This function is the externally accessible entry point for template , , , etc  
function wpsu.ship -- this version not supported from the template yet
local prefix = mw.text.trim (.args[1] or ''); -- fetch positional parameters into named variables for readability
local name = mw.text.trim (.args[2] or ''); -- stripped of leading and trailing whitespace
local dab = mw.text.trim (.args[3] or ''); -- and and set to empty string (is that needed?)
local control = .args[4];
local unlinked_prefix = 'yes' == .args.up;  
return _ship (prefix, name, dab, control, unlinked_prefix);
end  
----------------------------< L I S T _ E R R O R >----------------------------------------------------------  
Assembles an error message, the original parameter value and, if appropriate a category. The error message precedes
the existing value of the parameter. If the first non-whitespace character in the parameter is a '*', set prefix to
a '*' and sep to '\n'. For line-break lists, set prefix to empty string and sep to '
'.  
Category is appended to the end of the returned value only for pages in article space.  
Inputs:
prefix – a string of characters that precede the error message span; typically '' and '*'
message – the error message to be displayed; goes inside the span
sep – a string of characters that separates the span from the parameter value; typically '\n' and '
'
param_val – the unmodified parameter value
showerrs – a boolean, true to display the error message text  
local function list_error (prefix, message, sep, param_val, showerrs)
local err_msg = '%s<span>list error: %s (help)</span>%s%s%s';
local category = '';  
if 0 == mw.title.getCurrentTitle.namespace then -- only categorize pages in article space
category = '';
end
if true == showerrs then
return string.format (err_msg, prefix, message, sep, param_val, category); -- put it all together
else