Module:WPSHIPS utilities and Module:WPSHIPS utilities/sandbox: Difference between pages
(Difference between pages)
imported>Trappist the monk No edit summary |
imported>WOSlinker use require('strict') instead of require('Module:No globals') |
||
| Line 2: | Line 2: | ||
local get_args = require ('Module:Arguments').getArgs; | local get_args = require ('Module:Arguments').getArgs; | ||
local styles = require ('Module:WPMILHIST Infobox style'); -- infobox css | local styles = require ('Module:WPMILHIST Infobox style'); -- infobox css | ||
local data = mw.loadData ('Module:WPSHIPS utilities/data'); | local data = mw.loadData ('Module:WPSHIPS utilities/data/sandbox'); | ||
local namespace = mw.title.getCurrentTitle().namespace; -- used for categorization | local namespace = mw.title.getCurrentTitle().namespace; -- used for categorization | ||
local error_map = { -- [1] is error message; [2] is error category | |||
['synonymous'] = {'has synonymous parameter', 'Pages using infobox ship with synonymous parameters'}; | |||
local | |||
synonymous = {'has synonymous parameter', ' | |||
} | } | ||
--[[--------------------------< E R R O R _ M S G | --[[--------------------------< M A K E _ E R R O R _ M S G >-------------------------------------------------- | ||
assembles an error message from message text | assembles an error message from message text, help link, and error category. | ||
]] | ]] | ||
local function | local function make_error_msg (msg, cat, no_cat) | ||
local out = {}; | local out = {}; | ||
local category; | local category; | ||
table.insert (out, '<span style=\"font-size: 100%; font-style: normal;\" class=\"error\">Error: '); | table.insert (out, ' <span style=\"font-size: 100%; font-style: normal;\" class=\"error\">Error: '); | ||
table.insert (out, error_map[msg][1]); | |||
table.insert (out, table.concat ({' ([[:Category:', error_map[msg][2], '|help]])'})); | |||
table.insert (out, table.concat ({' ([[:', | |||
table.insert (out, '</span>'); | table.insert (out, '</span>'); | ||
if (0 == namespace or 10 == namespace) and not no_cat then -- categorize in article space (and template space to take care of broken usages) | if (0 == namespace or 10 == namespace) and not no_cat then -- categorize in article space (and template space to take care of broken usages) | ||
table.insert (out, table.concat ({'[[', | table.insert (out, table.concat ({'[[Category:', error_map[msg][2], ']]'})); | ||
end | end | ||
return table.concat (out); | return table.concat (out); | ||
end | end | ||
| Line 427: | Line 407: | ||
local output = { | local output = { | ||
'[http://www.navsource. | '[http://www.navsource.org/archives/', | ||
pframe.args[1], | pframe.args[1], | ||
'.htm Photo gallery] of ', | '.htm Photo gallery] of ', | ||
| Line 445: | Line 425: | ||
]] | ]] | ||
local function _ship (prefix, name, dab, control, unlinked_prefix, unlinked_whole | local function _ship (prefix, name, dab, control, unlinked_prefix, unlinked_whole) | ||
local error_msg = ''; | local error_msg = ''; | ||
local category = ''; | local category = ''; | ||
| Line 455: | Line 435: | ||
control = control:match ('%d'); -- strip out the hyphen | control = control:match ('%d'); -- strip out the hyphen | ||
end | end | ||
-- dispose of error conditions straight away | -- dispose of error conditions straight away | ||
if not is_set (name) then -- this is the only required parameter | if not is_set (name) then -- this is the only required parameter | ||
error_msg = | 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 | 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 = | 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 | elseif not is_set (prefix) and ('5' == control or '6' == control) then -- prefix required when control value set to expect it | ||
error_msg = | error_msg = ' missing prefix'; | ||
elseif '4' == control then -- displaying only the prefix | elseif '4' == control then -- displaying only the prefix | ||
error_msg = | error_msg = 'invalid control parameter: ' .. control; | ||
elseif is_set (control) then | elseif is_set (control) then | ||
if ('number' ~= type (tonumber (control) | if ('number' ~= type (tonumber (control))) or (1 > tonumber (control) or 6 < tonumber (control)) then -- control must be a number between 1 through 6 | ||
error_msg = | error_msg = 'invalid control parameter: ' .. control; | ||
end | end | ||
elseif not is_set (prefix) and unlinked_prefix then -- prefix required when |up=yes | elseif not is_set (prefix) and unlinked_prefix then -- prefix required when |up=yes | ||
error_msg = | error_msg = ' missing prefix'; | ||
end | end | ||
if is_set (error_msg) then | |||
if 0 == mw.title.getCurrentTitle().namespace then -- only categorize pages in article space | |||
category = '[[Category:WPSHIPS: template ship parameter errors]]'; | |||
end | |||
return '<span style="font-size:100%" class="error">' .. error_msg .. '</span>' .. category; -- return an error message; don't bother with making a link | |||
return error_msg; | |||
end | end | ||
local link_name; | local link_name; | ||
local link = ''; | local link = '[['; | ||
if is_set (prefix) then | if is_set (prefix) then | ||
link = prefix .. ' ' .. name; | link = link .. prefix .. ' ' .. name; -- begin assembling the article name portion of the wikilink | ||
else | else | ||
link = name; | link = link .. name; | ||
end | end | ||
| Line 489: | Line 472: | ||
link = link .. ' (' .. dab .. ')'; -- wrap dab in parentheses | link = link .. ' (' .. dab .. ')'; -- wrap dab in parentheses | ||
end | end | ||
name = "''" .. name .. "''"; -- name is always italicized so do it now | name = "''" .. name .. "''"; -- name is always italicized so do it now | ||
| Line 510: | Line 488: | ||
if not is_set (control) then -- when control not set: prefix, name, and dab | if not is_set (control) then -- when control not set: prefix, name, and dab | ||
if is_set (prefix) then | if unlinked_prefix then -- 'unlinked prefix'? | ||
link = prefix .. ' ' .. link; -- yes, modify link so that prefix is not linked in final render | |||
link_name = name .. dab; | |||
elseif is_set (prefix) then | |||
link_name = prefix .. ' ' .. name .. dab; | link_name = prefix .. ' ' .. name .. dab; | ||
else | else | ||
| Line 521: | Line 502: | ||
link_name = name .. dab; | link_name = name .. dab; | ||
elseif '5' == control then -- prefix and dab | elseif '5' == control then -- prefix and dab | ||
if unlinked_prefix then -- 'unlinked prefix'? | |||
link = prefix .. ' ' .. link; -- yes, modify link so that prefix is not linked in final render | |||
link_name = dab; | |||
else | |||
link_name = prefix .. dab; | link_name = prefix .. dab; | ||
end | |||
elseif '6' == control then -- prefix and name | elseif '6' == control then -- prefix and name | ||
if unlinked_prefix then -- 'unlinked prefix'? | |||
link = prefix .. ' ' .. link; -- yes, modify link so that prefix is not linked in final render | |||
link_name = name; | |||
else | |||
link_name = prefix .. ' ' .. name; | link_name = prefix .. ' ' .. name; | ||
end | |||
end | end | ||
end | end | ||
if unlinked_whole then | if unlinked_whole then | ||
return link_name; | return link_name; | ||
else | else | ||
return | return link .. '|' .. link_name .. ']]'; | ||
end | end | ||
end | end | ||
--[[--------------------------< S H I P >---------------------------------------------------------------------- | --[[--------------------------< S H I P >---------------------------------------------------------------------- | ||
| Line 548: | Line 531: | ||
This function is the externally accessible entry point for template {{ship}}, {{HMS}}, {{USS}}, etc | This function is the externally accessible entry point for template {{ship}}, {{HMS}}, {{USS}}, etc | ||
{{#invoke:WPSHIPS_utilities|ship| | {{#invoke:WPSHIPS_utilities/sandbox|ship|{{{1|}}}|{{{2|}}}|{{{3|}}}|{{{4|}}}|link={{{link|}}}|up={{{up|}}}}} | ||
]] | ]] | ||
local function ship (frame) -- this version not supported from the template yet | local function ship (frame) -- this version not supported from the template yet | ||
local | local prefix = mw.text.trim (frame.args[1] or ''); -- fetch positional parameters into named variables for readability | ||
local name = mw.text.trim (frame.args[2] or ''); -- stripped of leading and trailing whitespace | |||
local dab = mw.text.trim (frame.args[3] or ''); -- and and set to empty string (is that needed?) | |||
local name = | local control = frame.args[4]; | ||
local dab = | local unlinked_prefix = 'yes' == frame.args.up; | ||
local control = | local unlinked_whole = 'no' == frame.args.link; | ||
local unlinked_prefix = 'yes' == | |||
local unlinked_whole = 'no' == | |||
return _ship (prefix, name, dab, control, unlinked_prefix, unlinked_whole); | |||
return _ship (prefix, name, dab, control, unlinked_prefix, unlinked_whole | |||
end | end | ||
| Line 740: | Line 670: | ||
elseif splats == level + 1 then -- number of splats can only increase by one | elseif splats == level + 1 then -- number of splats can only increase by one | ||
level = splats; -- record the new level | level = splats; -- record the new level | ||
table.insert (html_table, '<ul style="list-style: none">'); -- add a new sublist | table.insert (html_table, '<ul style="list-style:none">'); -- add a new sublist | ||
table.insert (html_table, List_item_otag .. item .. '</li>'); -- and the item | table.insert (html_table, List_item_otag .. item .. '</li>'); -- and the item | ||
elseif splats < level then -- from three splats to one splat for example | elseif splats < level then -- from three splats to one splat for example | ||
| Line 869: | Line 799: | ||
]] | ]] | ||
local function synonym_check (args_t, uk_param, us_param, error_flag) | local function synonym_check (args_t, uk_param, us_param)--, error_flag) | ||
if args_t[uk_param] and args_t[us_param] then | if args_t[uk_param] and args_t[us_param] then | ||
args_t[uk_param] = args_t[uk_param] .. make_error_msg ('synonymous'); -- both are set so append error message with category | |||
args_t[uk_param] = args_t[uk_param] .. | args_t[us_param] = args_t[us_param] .. make_error_msg ('synonymous', true); -- but append error message without category | ||
args_t[us_param] = args_t[us_param] .. | |||
return true; -- inform the calling function that it needs to emit maint category | return true; -- inform the calling function that it needs to emit maint category | ||
end | end | ||
-- return error_flag; -- no match so return unmodified <error_flag> | |||
end | end | ||
| Line 921: | Line 850: | ||
return table.concat (lines_t); -- make a big string of line items and done | return table.concat (lines_t); -- make a big string of line items and done | ||
end | end | ||
| Line 955: | Line 863: | ||
local function infobox_ship_career (frame) | local function infobox_ship_career (frame) | ||
local args_t = get_args (frame); -- get a table of all parameters in the template call | local args_t = get_args (frame); -- get a table of all parameters in the template call | ||
local html_out_t = {}; -- html table text goes here | local html_out_t = {}; -- html table text goes here | ||
-- local error_flag = false; -- controls emission of maint category when synonymous parameters are both set | |||
args_t['Hide header'] = args_t['Hide header'] and args_t['Hide header']:lower(); -- set to lowercase if set | args_t['Hide header'] = args_t['Hide header'] and args_t['Hide header']:lower(); -- set to lowercase if set | ||
-- error_flag = synonym_check (args_t, 'Ship stricken', 'Ship struck', error_flag); -- error if both synonymous parameters set | |||
-- error_flag = synonym_check (args_t, 'Ship honours', 'Ship honors', error_flag); | |||
-- error_flag = synonym_check (args_t, 'Ship pennant num', 'Ship hull num', error_flag); | |||
synonym_check (args_t, 'Ship stricken', 'Ship struck'); -- error if both synonymous parameters set | |||
synonym_check (args_t, 'Ship honours', 'Ship honors'); | |||
synonym_check (args_t, 'Ship pennant num', 'Ship hull num'); | |||
if 'yes' ~= args_t['Hide header'] then -- |Hide header=yes then no header | if 'yes' ~= args_t['Hide header'] then -- |Hide header=yes then no header | ||
| Line 977: | Line 884: | ||
if args_t['Ship country'] and args_t['Ship flag'] then | if args_t['Ship country'] and args_t['Ship flag'] then | ||
table.insert (html_out_t, '<tr><th height="30" colspan="2" style="background-color:#B0C4DE | table.insert (html_out_t, '<tr><th height="30" colspan="2" style="background-color:#B0C4DE;text-align:left;padding-left:2px;vertical-align:middle;font-size:110%;">'); | ||
table.insert (html_out_t, _infobox_ship_flag (args_t['Ship flag'])); | table.insert (html_out_t, _infobox_ship_flag (args_t['Ship flag'])); | ||
table.insert (html_out_t, '<span style="padding-left:1em">'); | table.insert (html_out_t, '<span style="padding-left:1em">'); | ||
| Line 984: | Line 891: | ||
table.insert (html_out_t, '</th></tr>\n'); | table.insert (html_out_t, '</th></tr>\n'); | ||
elseif args_t['Ship country'] then | elseif args_t['Ship country'] then | ||
table.insert (html_out_t, '<tr><th height="30" colspan="2" style="background-color:#B0C4DE | table.insert (html_out_t, '<tr><th height="30" colspan="2" style="background-color:#B0C4DE;text-align:center;vertical-align:middle;font-size:110%;">'); | ||
table.insert (html_out_t, args_t['Ship country']); | table.insert (html_out_t, args_t['Ship country']); | ||
table.insert (html_out_t, '</th></tr>\n'); | table.insert (html_out_t, '</th></tr>\n'); | ||
elseif args_t['Ship flag'] then | elseif args_t['Ship flag'] then | ||
table.insert (html_out_t, '<tr><th height="30" colspan="2" style="background-color:#B0C4DE | table.insert (html_out_t, '<tr><th height="30" colspan="2" style="background-color:#B0C4DE;padding-left:2px;">'); | ||
table.insert (html_out_t, _infobox_ship_flag (args_t['Ship flag'])); | table.insert (html_out_t, _infobox_ship_flag (args_t['Ship flag'])); | ||
table.insert (html_out_t, '</th></tr>\n'); | table.insert (html_out_t, '</th></tr>\n'); | ||
| Line 1,011: | Line 918: | ||
local function infobox_ship_characteristics (frame) | local function infobox_ship_characteristics (frame) | ||
local args_t = get_args (frame); -- get a table of all parameters in the template call | local args_t = get_args (frame); -- get a table of all parameters in the template call | ||
local html_out_t = {}; -- html table text goes here | local html_out_t = {}; -- html table text goes here | ||
-- local error_flag = false; -- controls emission of maint category when synonymous parameters are both set | |||
args_t['Hide header'] = args_t['Hide header'] and args_t['Hide header']:lower(); -- set to lowercase if set | args_t['Hide header'] = args_t['Hide header'] and args_t['Hide header']:lower(); -- set to lowercase if set | ||
-- error_flag = synonym_check (args_t, 'Ship armour', 'Ship armor', error_flag); -- error if both synonymous parameters set | |||
-- error_flag = synonym_check (args_t, 'Ship draught', 'Ship draft', error_flag); | |||
synonym_check (args_t, 'Ship armour', 'Ship armor'); -- error if both synonymous parameters set | |||
synonym_check (args_t, 'Ship draught', 'Ship draft'); | |||
if 'yes' ~= args_t['Hide header'] then -- |Hide header=yes then no header | if 'yes' ~= args_t['Hide header'] then -- |Hide header=yes then no header | ||
| Line 1,053: | Line 957: | ||
local function infobox_ship_class_overview (frame) | local function infobox_ship_class_overview (frame) | ||
local args_t = get_args (frame); -- get a table of all parameters in the template call | local args_t = get_args (frame); -- get a table of all parameters in the template call | ||
local html_out_t = {}; -- html table text goes here | local html_out_t = {}; -- html table text goes here | ||
| Line 1,098: | Line 996: | ||
local args_t = get_args (frame); -- get a table of all parameters in the invoke | local args_t = get_args (frame); -- get a table of all parameters in the invoke | ||
local | local year, volume, scan = args_t[1]:match ('(%d%d)(%l)(%d%d%d%d)%.[Pp][Dd][Ff]'); -- get the various parts | ||
if not year then return nil end -- nil when no match so we're done | if not year then return nil end -- nil when no match so we're done | ||
year = tonumber (year); | year = tonumber (year); | ||
| Line 1,177: | Line 1,072: | ||
args_t[1]); -- append <filename> onto the end, and done | args_t[1]); -- append <filename> onto the end, and done | ||
end | end | ||
end | end | ||
| Line 1,289: | Line 1,087: | ||
is_plimsoll_filename = is_plimsoll_filename, | is_plimsoll_filename = is_plimsoll_filename, | ||
navsource = navsource, | navsource = navsource, | ||
set_plimsoll_subtitle = set_plimsoll_subtitle, | set_plimsoll_subtitle = set_plimsoll_subtitle, | ||
set_plimsoll_date = set_plimsoll_date, | set_plimsoll_date = set_plimsoll_date, | ||
set_plimsoll_url = set_plimsoll_url, | set_plimsoll_url = set_plimsoll_url, | ||
ship = ship, -- experiment | ship = ship, -- experiment | ||
ship_name_format = ship_name_format, | ship_name_format = ship_name_format, | ||
unbulleted_list = unbulleted_list, | unbulleted_list = unbulleted_list, | ||