Module:Convert: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
(accept user-defined combination like "LT+ST"; fix adj=on|lk=on bug so do not put hyphens in link target)
(error messages only include a category in wanted namespaces (default 0,10); isolate template parms so they cannot be mistaken for working parms; trim boolean)
Line 13: Line 13:
-- Configuration options to keep magic values in one location.
-- Configuration options to keep magic values in one location.
-- Conversion data and message text are defined in separate modules.
-- Conversion data and message text are defined in separate modules.
local config, maxsigfig
local numdot, numsep -- each must be a single byte for simple regex search/replace
local numdot, numsep -- each must be a single byte for simple regex search/replace
local maxsigfig, warnings
local default_exceptions, link_exceptions, all_units
local default_exceptions, link_exceptions, all_units
local SIprefixes, all_categories, all_messages, customary_units, disp_joins
local SIprefixes, all_categories, all_messages, customary_units, disp_joins
Line 35: Line 35:
if text then
if text then
text = text:lower()
text = text:lower()
if text == '1' or text == 'y' or text == 'yes' or text == 'on' or text == 'true' then
if text == 'on' or text == 'yes' then
return true
return true
end
end
end
end
return false
end
end


Line 77: Line 78:
local function set_config(frame)
local function set_config(frame)
-- Set configuration options from template #invoke or defaults.
-- Set configuration options from template #invoke or defaults.
local args = frame.args
config = frame.args
numdot = args.numdot or '.' -- decimal mark before fractional digits
numdot = config.numdot or '.' -- decimal mark before fractional digits
numsep = args.numsep or ',' -- group separator for numbers (',', '.', '')
numsep = config.numsep or ',' -- group separator for numbers (',', '.', '')
maxsigfig = args.maxsigfig or 14 -- maximum number of significant figures
maxsigfig = config.maxsigfig or 14 -- maximum number of significant figures
warnings = boolean(args.warnings) -- true if want warnings for invalid options
-- Scribunto sets the global variable 'mw'.
-- Scribunto sets the global variable 'mw'.
-- A testing program can set the global variable 'is_test_run'.
-- A testing program can set the global variable 'is_test_run'.
Line 92: Line 92:
spell_module = "ConvertNumeric"
spell_module = "ConvertNumeric"
else
else
local sandbox = boolean(args.sandbox) and '/sandbox' or ''
local sandbox = boolean(config.sandbox) and '/sandbox' or ''
data_module = "Module:Convert/data" .. sandbox
data_module = "Module:Convert/data" .. sandbox
text_module = "Module:Convert/text" .. sandbox
text_module = "Module:Convert/text" .. sandbox
Line 171: Line 171:
if type(text) == 'string' then
if type(text) == 'string' then
return text:match("^%s*(.-)%s*$")
return text:match("^%s*(.-)%s*$")
end
end

local function wanted_category(cat)
-- Return cat if it is wanted in current namespace, otherwise return nil.
-- This is so tracking categories only include pages that need correction.
-- Default wanted namespaces are 0 (article) and 10 (template).
local title = mw.title.getCurrentTitle()
if title then
local namespace = title.namespace
for _, v in ipairs(split(config.nscat or '0,10', ',')) do
if namespace == tonumber(v) then
return cat
end
end
end
end
end
end
Line 188: Line 203:
mcode[3] or '?',
mcode[3] or '?',
mcode[4] or '?')
mcode[4] or '?')
local cat = all_categories[msg[2]] or ''
local cat = wanted_category(all_categories[msg[2]]) or ''
local prefix = all_messages[msg.warning and 'cvt_prefix_warning' or 'cvt_prefix_error'] or ''
local prefix = all_messages[msg.warning and 'cvt_prefix_warning' or 'cvt_prefix_error'] or ''
local suffix = (prefix == '') and '' or '</span>'
local suffix = (prefix == '') and '' or '</span>'
Line 203: Line 218:
-- If enabled, add a warning that will be displayed after the convert result.
-- If enabled, add a warning that will be displayed after the convert result.
-- To reduce output noise, only the first warning is displayed.
-- To reduce output noise, only the first warning is displayed.
if warnings then
if boolean(config.warnings) then
if parms.warnings == nil then
if parms.warnings == nil then
parms.warnings = message({ mcode, text })
parms.warnings = message({ mcode, text })
Line 1,346: Line 1,361:
end
end


local function translate_parms(parms, named_keys)
local function translate_parms(parms, kv_pairs)
-- Update fields in parms by translating parameters to those used at enwiki.
-- Update fields in parms by translating each key:value in kv_pairs to terms
-- used by this module (may involve translating from local language to English).
-- Also, checks are performed which may display warnings, if enabled.
-- Also, checks are performed which may display warnings, if enabled.
-- Return true if successful or return false, t where t is an error message table.
-- Return true if successful or return false, t where t is an error message table.
if kv_pairs.adj and kv_pairs.sing then
for _, loc_name in ipairs(named_keys) do
-- For en.wiki (before translation), warn if attempt to use adj and sing
local loc_value = parms[loc_name]
-- as the latter is a deprecated alias for the former.
if kv_pairs.adj ~= kv_pairs.sing and kv_pairs.sing ~= '' then
add_warning(parms, 'cvt_unknown_option', 'sing=' .. kv_pairs.sing)
end
kv_pairs.sing = nil
end
for loc_name, loc_value in pairs(kv_pairs) do
local en_name = en_option_name[loc_name]
local en_name = en_option_name[loc_name]
if en_name then
if en_name then
Line 1,372: Line 1,395:
add_warning(parms, 'cvt_empty_option', loc_name)
add_warning(parms, 'cvt_empty_option', loc_name)
else
else
-- Using, for example, aliases like |sing=off|adj=on can give
-- loc_value can no longer be nil here (at one time, that could occur
-- loc_value == nil when adj is processed after sing. In that case,
-- with aliases like |sing=off|adj=on), but am retaining safety check.
-- the following gives a slightly misleading but reasonable warning.
local text = loc_value and (loc_name .. '=' .. loc_value) or loc_name
local text = loc_value and (loc_name .. '=' .. loc_value) or loc_name
add_warning(parms, 'cvt_unknown_option', text)
add_warning(parms, 'cvt_unknown_option', text)
Line 1,485: Line 1,507:
-- whitespace entered in the template, and whitespace is used by some
-- whitespace entered in the template, and whitespace is used by some
-- parameters (example: the numbered parameters associated with "disp=x").
-- parameters (example: the numbered parameters associated with "disp=x").
local parms = {} -- arguments passed to template
local parms = {} -- arguments passed to template, after translation
local named_keys = collection() -- numbered table of named keys in parms: needed because cannot iterate parms and add new fields to it
local kv_pairs = {} -- table of input key:value pairs where key is a name; needed because cannot iterate parms and add new fields to it
for k, v in pairs(pframe.args) do
for k, v in pairs(pframe.args) do
if type(k) == 'number' or k == 'test' then -- parameter "test" is reserved for testing and is not translated
parms[k] = v
if type(k) == 'string' then
parms[k] = v
named_keys:add(k)
else
kv_pairs[k] = v
end
end
end
end
local success, msg = translate_parms(parms, named_keys)
local success, msg = translate_parms(parms, kv_pairs)
if not success then return false, msg end
if not success then return false, msg end
local success, valinfo, i = get_values(parms)
local success, valinfo, i = get_values(parms)