Module:Convert: Difference between revisions

m
1 revision imported
(update from sandbox per Template talk:Convert#Module version 17)
m (1 revision imported)
 
(9 intermediate revisions by 3 users not shown)
Line 259:
end
 
local function wanted_category(catcatkey, catsort, want_warning)
-- Return catmessage category if it is wanted in current namespace, otherwise return nil.
-- otherwise return ''.
-- This is so tracking categories only include pages that need correction.
local cat
local title = mw.title.getCurrentTitle()
if title then
Line 268 ⟶ 269:
for _, v in ipairs(split(config.nscat or nsdefault, ',')) do
if namespace == tonumber(v) then
cat = text_code.all_categories[want_warning and 'warning' or catkey]
return cat
if catsort and catsort ~= '' and cat:sub(-2) == ']]' then
cat = cat:sub(1, -3) .. '|' .. mw.text.nowiki(usub(catsort, 1, 20)) .. ']]'
end
break
end
end
end
return cat or ''
end
 
local function message(parms, mcode, is_warning)
-- Return wikitext for an error message, including category if specified
-- for the message type.
Line 341 ⟶ 347:
return success and (revid == '')
end
local want_warning = is_warning and
not config.warnings and -- show unobtrusive warnings if config.warnings not configured
not msg.nowarn -- but use msg settings, not standard warning, if specified
local title = string.gsub(msg[1] or 'Missing message', '$%d+', parts)
local text = want_warning and '*' or msg[2] or 'Missing message'
local cat = wanted_category(text_code.all_categories[msg[3], mcode[2]), or ''want_warning)
local anchor = msg[4] or ''
local fmtkey = ispreview() and 'cvt_format_preview' or (msg.format or 'cvt_format')
(want_warning and 'cvt_format2' or msg.format or 'cvt_format')
local fmt = text_code.all_messages[fmtkey] or 'convert: bug'
return subparm(fmt, title:gsub('"', '"'), text, cat, anchor)
Line 354 ⟶ 364:
function add_warning(parms, level, key, text1, text2) -- for forward declaration above
-- If enabled, add a warning that will be displayed after the convert result.
-- A higher level is more verbose: more kinds of warnings are displayed.
-- To reduce output noise, only the first warning is displayed.
if level <= (tonumber(config.warnings) or level < 01) then
if level <= (tonumber(configparms.warnings) or== 1)nil then
if parms.warnings == nilmessage(parms, { key, text1, text2 }, thentrue)
parms.warnings = message(parms, { key, text1, text2 })
end
end
end
Line 380 ⟶ 389:
success, speller = pcall(get_speller, spell_module)
if not success or type(speller) ~= 'function' then
add_warning(parms, 1, 'cvt_no_spell', 'spell')
return nil
end
Line 581 ⟶ 590:
-- This is never called to determine a unit name or link because per units
-- are handled as a special case.
-- Similarly, the default output is handled elsewhere., and for a symbol
-- this is only called from get_default() for default_exceptions.
__index = function (self, key)
local value
Line 1,603 ⟶ 1,613:
-- p2 is text to insert before the output unit
-- p1 or p2 may be nil to mean "no preunit"
-- Using '+ ' gives output like "5+ feet" (no precedingspace before, but space after).
local function withspace(text, iwantboth)
-- InsertReturn spacetext atwith beginningspace ifbefore i == 1and, or at end if i ==wantboth, -1after.
-- However, no space is insertedadded if there is a space or '&nbsp;' or '-'
-- or '-' at that position ('-' is for adjectival text).
-- There is also no space if text starts with '&'
local current = text:sub(i, i)
-- (e.g. '&deg;' would display a degree symbol with no preceding space).
if current == ' ' or current == '-' then
returnlocal char = text:sub(1, 1)
elseif if trim2char == '&' then
return text -- an html entity can be used to specify the exact display
end
if inot (char == ' ' or char == '-' or char == 1'+') then
currenttext = text:sub(1,' 6)' .. text
else
current = text:sub(-6, -1)
end
if current == '&nbsp;'wantboth then
returnchar = text:sub(-1, -1)
if not (char == ' ' or char == '-' or text:sub(-6, -1) == '&nbsp;') then
end
if i text == 1text .. ' then'
end
return ' ' .. text
end
return text .. ' '
end
local PLUS = '+ '
preunit1 = preunit1 or ''
local trim1 = strip(preunit1)
Line 1,631 ⟶ 1,642:
return nil
end
if trim1 == '+' then
return withspace(withspace(preunit1, 1), -1)
return catPLUS
end
return withspace(withspace(preunit1, 1), -1true)
end
preunit1 = withspace(preunit1, 1)
preunit2 = preunit2 or ''
local trim2 = strip(preunit2)
if trim1 == '' and trim2 == '+' then
if currenttrim2 == ' ' or currenttrim2 == '-+' then
return nilPLUS, nilPLUS
end
preunit1 = PLUS
end
if trim1trim2 ~== '+' then
if trim1 == '' then
preunit1 = withspace(preunit1, 1)
return 'nil, ' .. textnil
end
end
if trim2 == '&#32;' then -- trick to make preunit2 empty
preunit2 = nil
elseif trim2 == '' then
preunit2 = preunit1
elseif trim2 ~== '+' then
preunit2 = withspace(preunit2, 1)PLUS
ifelseif trim2 == '&#32;' then -- trick to make preunit2 empty
preunit2 = nil
else
preunit2 = withspace(preunit2)
end
return preunit1, preunit2
Line 1,749 ⟶ 1,769:
-- Return true if successful or return false, t where t is an error message table.
currency_text = nil -- local testing can hold module in memory; must clear globals
local accept_any_text = {
input = true,
qid = true,
qual = true,
stylein = true,
styleout = true,
tracking = true,
}
if kv_pairs.adj and kv_pairs.sing then
-- For enwiki (before translation), warn if attempt to use adj and sing
Line 1,782 ⟶ 1,810:
en_value = number
else
add_warning(parms, 1, (en_name == 'frac' and 'cvt_bad_frac' or 'cvt_bad_sigfig'), loc_name .. '=' .. loc_value)
end
end
elseif accept_any_text[en_name] then
elseif en_name == 'stylein' or en_name == 'styleout' or en_name == 'qual' or en_name == 'qid' or en_name == 'input' then
en_value = loc_value ~= '' and loc_value or nil -- accept non-empty user text with no validation
if en_name == 'input' then
Line 2,172 ⟶ 2,200:
end
if word and not set_precision(word) then
parms.out_unit = parms.out_unit or word
if set_precision(strip(parms[i])) then
i = i + 1
Line 2,223 ⟶ 2,251:
else
parms.precision = precision
end
for j = i, i + 3 do
local parm = parms[j] -- warn if find a non-empty extraneous parameter
if parm and parm:match('%S') then
add_warning(parms, 1, 'cvt_unknown_option', parm)
break
end
end
return true, in_unit_table
Line 2,898 ⟶ 2,933:
local per = unit_table.per
if per then
local paren1, paren2 = '', '' -- possible parentheses around bottom unit
local unit1 = per[1] -- top unit_table, or nil
local unit2 = per[2] -- bottom unit_table
Line 2,909 ⟶ 2,945:
return symbol -- for exceptions that have the symbol built-in
end
end
if (unit2.symbol):find('⋅', 1, true) then
paren1, paren2 = '(', ')'
end
end
Line 2,948 ⟶ 2,987:
unit_table.sep = ''
end
return result .. paren1 .. linked_id(parms, unit2, key_id2, want_link, '1') .. paren2
end
if multiplier then
Line 3,175 ⟶ 3,214:
return preunit .. id1
end
if parms.opt_also_symbol and not composite and not parms.opt_flip then
local join1 = parms.joins[1]
if join1 == ' (' or join1 == ' [' then
Line 3,642 ⟶ 3,681:
-- An unknown input unit would display the input and an error message
-- with success == true at this point.
-- Also, can have success == false with a message tothat outputoutputs an empty string.
if successparms.input_text then
if parms.have_problemsuccess and not parms.input_texthave_problem then
return parms.input_textresult
end
local cat
return result
if parms.tracking then
-- Add a tracking category using the given text as the category sort key.
-- There is currently only one type of tracking, but in principle multiple
-- items could be tracked, using different sort keys for convenience.
cat = wanted_category('tracking', parms.tracking)
end
return parms.input_text .. (cat or '')
end
return parms.input_textsuccess and parms.input_textresult or message(parms, result)
end