Module:Convert: Difference between revisions

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
(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:
-- Configuration options to keep magic values in one location.
-- 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 maxsigfig, warnings
local default_exceptions, link_exceptions, all_units
local SIprefixes, all_categories, all_messages, customary_units, disp_joins
Line 35:
if text then
text = text:lower()
if text == '1' or text == 'yon' or text == 'yes' or text == 'on' or text == 'true' then
return true
end
end
return false
end
 
Line 77 ⟶ 78:
local function set_config(frame)
-- Set configuration options from template #invoke or defaults.
local argsconfig = frame.args
numdot = argsconfig.numdot or '.' -- decimal mark before fractional digits
numsep = argsconfig.numsep or ',' -- group separator for numbers (',', '.', '')
maxsigfig = argsconfig.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'.
-- A testing program can set the global variable 'is_test_run'.
Line 92:
spell_module = "ConvertNumeric"
else
local sandbox = boolean(argsconfig.sandbox) and '/sandbox' or ''
data_module = "Module:Convert/data" .. sandbox
text_module = "Module:Convert/text" .. sandbox
Line 171:
if type(text) == 'string' then
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 loc_valuenamespace = parms[loc_name]title.namespace
for _, v in ipairs(split(config.nscat or '0,10', ',')) do
if namespace == tonumber(v) then
return cat
parms[k] = v end
end
end
end
Line 188 ⟶ 203:
mcode[3] or '?',
mcode[4] 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 suffix = (prefix == '') and '' or '</span>'
Line 203 ⟶ 218:
-- If enabled, add a warning that will be displayed after the convert result.
-- To reduce output noise, only the first warning is displayed.
if boolean(config.warnings) then
if parms.warnings == nil then
parms.warnings = message({ mcode, text })
Line 1,346 ⟶ 1,361:
end
 
local function translate_parms(parms, named_keyskv_pairs)
-- Update fields in parms by translating parameterseach tokey:value thosein usedkv_pairs atto enwiki.terms
-- used by this module (may involve translating from local language to English).
-- 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.
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]
if en_name then
Line 1,372 ⟶ 1,395:
add_warning(parms, 'cvt_empty_option', loc_name)
else
-- Using,loc_value forcan example,no aliaseslonger likebe |sing=off|adj=onnil canhere (at one time, that could giveoccur
-- loc_valuewith aliases like |sing=off|adj=on), nilbut whenam adjretaining issafety processed after singcheck. In that case,
-- the following gives a slightly misleading but reasonable warning.
local text = loc_value and (loc_name .. '=' .. loc_value) or loc_name
add_warning(parms, 'cvt_unknown_option', text)
Line 1,485 ⟶ 1,507:
-- whitespace entered in the template, and whitespace is used by some
-- parameters (example: the numbered parameters associated with "disp=x").
local parms = {} -- arguments passed to template, after translation
local named_keyskv_pairs = collection(){} -- numbered table of namedinput keyskey:value inpairs parms:where key is a name; needed because cannot iterate parms and add new fields to it
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( parms[k)] == 'string' thenv
named_keys:add(k)else
kv_pairs[k] = v
end
end
local success, msg = translate_parms(parms, named_keyskv_pairs)
if not success then return false, msg end
local success, valinfo, i = get_values(parms)
Anonymous user