Module:Convert : Tameẓla gar ileqman

Contenu supprimé Contenu ajouté
SalemB (mmeslay | attekki)
Asebtar amaynut: -- Convert a value from one unit of measurement to another. -- Example: {{convert|123|lb|kg}} --> 123 pounds (56 kg) -- See en:Template:Convert/Transwiki guide if copying to...
 
Sami At Ferḥat (mmeslay | attekki)
Aucun résumé des modifications
Ajerriḍ 259:
end
 
local function wanted_category(catcatkey, catsort, want_warning)
-- Return catmessage category if it is wanted in current namespace,
-- otherwise return nil''.
local cat
-- This is so tracking categories only include pages that need correction.
local title = mw.title.getCurrentTitle()
if title then
Ligne 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.
Ligne 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
(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)
Ligne 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
Ligne 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
Ligne 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
Ligne 606 ⟶ 616 :
}
 
local function make_per(unitcode, unit_table, ulookup)
-- Return true, t where t is a per unit with unit codes expanded to unit tables,
-- or return false, t where t is an error message table.
local result = {
unitcode = unitcode,
utype = unit_table.utype,
per = {} }
}
override_from(result, unit_table, { 'invert', 'iscomplex', 'default', 'link', 'symbol', 'symlink' })
result.symbol_raw = (result.symbol or false) -- to distinguish between a defined exception and a metatable calculation
Ligne 675 ⟶ 689 :
end
unitcode = unitcode:gsub('_', ' '):gsub('&nbsp;', ' '):gsub(' +', ' ')
local function call_make_per(t)
return make_per(unitcode, t,
function (ucode) return lookup(parms, ucode, 'no_combination', utable, fails, depth) end
)
end
local t = utable[unitcode]
if t then
Ligne 696 ⟶ 715 :
end
if t.per then
return call_make_per(t)
return make_per(t, function (ucode) return lookup(parms, ucode, 'no_combination', utable, fails, depth) end)
end
local combo = t.combination -- nil or a table of unitcodes
Ligne 716 ⟶ 735 :
end
local result = shallow_copy(t)
result.unitcode = unitcode
if result.prefixes then
result.si_name = ''
Ligne 734 ⟶ 754 :
if t and t.prefixes then
local result = shallow_copy(t)
result.unitcode = unitcode
result.si_name = parms.opt_sp_us and si.name_us or si.name
result.si_prefix = si.prefix or prefix
result.scale = t.scale * 10 ^ (si.exponent * t.prefixes)
return true, setmetatable(result, unit_prefixed_mt)
end
end
end
-- Accept any unit with an engineering notation prefix like "e6cuft"
-- (million cubic feet), but not chained prefixes like "e3e6cuft",
-- and not if the unit is a combination or multiple,
-- and not if the unit has an offset or is a built-in.
-- Only en digits are accepted.
local has_plus = unitcode:find('+', 1, true)
if not has_plus then
local exponent, baseunit = unitcode:match('^e(%d+)(.*)')
if exponent then
local engscale = text_code.eng_scales[exponent]
if engscale then
local success, result = lookup(parms, baseunit, 'no_combination', utable, fails, depth)
if success and not (result.offset or result.builtin or result.engscale) then
result.defkey = unitcode -- key to lookup default exception
result.engscale = engscale
result.scale = result.scale * 10 ^ tonumber(exponent)
return true, result
end
end
end
Ajerriḍ 768:
local err_is_fatal
local combo = collection()
if has_plusunitcode:find('+', 1, true) then
err_is_fatal = true
for item in (unitcode .. '+'):gmatch('%s*(.-)%s*%+') do
Ajerriḍ 805:
if success or err_is_fatal then
return success, result
end
end
-- Accept any unit with an engineering notation prefix like "e6cuft"
-- (million cubic feet), but not chained prefixes like "e3e6cuft",
-- and not if the unit is a combination or multiple,
-- and not if the unit has an offset or is a built-in.
-- Only en digits are accepted.
local exponent, baseunit = unitcode:match('^e(%d+)(.*)')
if exponent then
local engscale = text_code.eng_scales[exponent]
if engscale then
local success, result = lookup(parms, baseunit, 'no_combination', utable, fails, depth)
if success and not (result.offset or result.builtin or result.engscale) then
result.unitcode = unitcode -- 'e6cuft' not 'cuft'
result.defkey = unitcode -- key to lookup default exception
result.engscale = engscale
result.scale = result.scale * 10 ^ tonumber(exponent)
return true, result
end
end
end
Ligne 814 ⟶ 833 :
-- Engineering notation (apart from at start and which has been stripped before here),
-- is not supported so do not make a per unit if find text like 'e3' in unitcode.
local success, result = make_percall_make_per({ per = {top, bottom} }, function (ucode) return lookup(parms, ucode, 'no_combination', utable, fails, depth) end)
if success then
return true, result
Ligne 1 360 ⟶ 1 379 :
-- Before processing, the input text is cleaned:
-- * Any thousand separators (valid or not) are removed.
-- * Any sign (andis optionalreplaced followingwith '-' (if whitespacenegative) isor replaced'' with(otherwise).
-- '-' (if negative) or '' (otherwise).
-- That replaces Unicode minus with '-'.
-- If successful, the returned info table contains named fields:
Ligne 1 421 ⟶ 1 439 :
local valstr
for _, prefix in ipairs({ '-', MINUS, '&minus;' }) do
-- Including '-' meanssets inputsisnegative likein '-case 2'input (withis space)a arefraction accepted aslike '-2-3/4'.
-- It also sets isnegative in case input is a fraction like '-2-3/4'.
local plen = #prefix
if clean:sub(1, plen) == prefix then
valstr = clean:sub(plen + 1)
if valstr:match('^%s') then -- "- 1" is invalid but "-1 - 1/2" is ok
return false, { 'cvt_bad_num', text }
end
break
end
Ligne 1 591 ⟶ 1 611 :
-- 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)
if char == '&' 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
text = text .. ' '
end
end
return text
if i == 1 then
return ' ' .. text
end
return text .. ' '
end
local PLUS = '+ '
preunit1 = preunit1 or ''
local trim1 = strip(preunit1)
Ligne 1 619 ⟶ 1 640 :
return nil
end
if trim1 == '+' then
return withspace(withspace(preunit1, 1), -1)
return PLUS
end
return withspace(preunit1, true)
end
preunit1 = withspace(preunit1)
preunit2 = preunit2 or ''
local trim2 = strip(preunit2)
if trim1 == '' and trim2 == '+' then
if trim2 == '' or trim2 == '+' then
return nil, nil
return PLUS, PLUS
end
end
if trim1 ~= '+' then
preunit1 = withspace(preunit1, 1)PLUS
end
if trim2 == '&#32;' then -- trick to make preunit2 empty
preunit2if trim1 == '' nilthen
return nil, nil
elseif trim2 == '' then
end
preunit2 = preunit1
elseif trim2 ~== '+' then
preunit2 = withspace(preunit2, 1)PLUS
elseif trim2 == '&#32;' then -- trick to make preunit2 empty
preunit2 = nil
else
preunit2 = withspace(preunit2)
end
return preunit1, preunit2
Ligne 1 666 ⟶ 1 696 :
 
local function get_composite(parms, iparm, in_unit_table)
-- Look for a composite input unit. For example, "{{convert|1|yd|2|ft|3|in}}"
-- would result in a call to this function with
-- iparm = 3 (parms[iparm] = "2", just after the first unit)
Ligne 1 737 ⟶ 1 767 :
-- 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
Ligne 1 745 ⟶ 1 783 :
kv_pairs.sing = nil
end
kv_pairs.comma = kv_pairs.comma or config.comma -- for plwiki who want default comma=5
for loc_name, loc_value in pairs(kv_pairs) do
local en_name = text_code.en_option_name[loc_name]
Ligne 1 770 ⟶ 1 809 :
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 == '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
Ligne 1 828 ⟶ 1 867 :
end
if parms.abbr then
if parms.abbr == 'unit' then
parms.abbr = 'on'
parms.number_word = true
end
parms.abbr_org = parms.abbr -- original abbr, before any flip
elseif parms.opt_hand_hh then
Ligne 1 834 ⟶ 1 877 :
else
parms.abbr = 'out' -- default is to abbreviate output only (use symbol, not name)
end
if parms.opt_order_out then
-- Disable options that do not work in a useful way with order=out.
parms.opt_flip = nil -- override adj=flip
parms.opt_spell_in = nil
parms.opt_spell_out = nil
parms.opt_spell_upper = nil
end
if parms.opt_spell_out and not abbr_entered then
Ligne 1 999 ⟶ 2 049 :
-- The valid_value is not negative and does not use a fraction, and
-- no options requiring further processing of the input are used.
-- Otherwise, return nothing andor callerreturn willfalse, reparseparm1 for caller theto inputinterpret.
-- Testing shows this function is successful for 96% of converts in articles,
-- and that on average it speeds up converts by 8%.
if parms.opt_ri or parms.opt_spell_in then return end
local clean = to_en(strip(parms[1] or ''), parms)
if parms.opt_ri or parms.opt_spell_in or #clean > 10 or not clean:match('^[0-9.]+$') then return end
return false, clean
end
local value = tonumber(clean)
if not value then return end
Ligne 2 021 ⟶ 2 072 :
end
 
local function wikidata_call(parms, operation, ...)
-- Return true, s where s is the result of a Wikidata operation,
-- or return false, t where t is an error message table.
Ligne 2 032 ⟶ 2 083 :
if success then
return status, result
end
if parms.opt_sortable_debug then
-- Use debug=yes to crash if an error while accessing Wikidata.
error('Error accessing Wikidata: ' .. status, 0)
end
return false, { 'cvt_wd_fail' }
Ligne 2 060 ⟶ 2 115 :
if parms.test == 'wikidata' then
local ulookup = function (ucode)
-- Use empty table for parms so it does not accummulateaccumulate results when used repeatedly.
return lookup({}, ucode, 'no_combination')
end
return wikidata_call(parms, '_listunits', ulookup)
end
local success, msg = translate_parms(parms, kv_pairs)
if not success then return false, msg end
if parms.input then
success, msg = wikidata_call(parms, '_adjustparameters', parms, 1)
if not success then return false, msg end
end
local success, i, in_unit, in_unit_table = simple_get_values(parms)
if not success then
if type(i) == 'string' and i:match('^NNN+$') then
-- Some infoboxes have examples like {{convert|NNN|m}} (3 or more "N").
-- Output an empty string for these.
return false, { 'cvt_no_output' }
end
local valinfo
success, valinfo, i = get_values(parms)
Ligne 2 137 ⟶ 2 197 :
end
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
Ligne 2 189 ⟶ 2 249 :
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
Ligne 2 463 ⟶ 2 530 :
end
local sortspan
if sortkey and (parms.opt_sortable_debug or not parms.table_align) then
sortspan = parms.opt_sortable_debug and
'<span data-sort-value="' .. sortkey .. '♠"><span style="border:1px solid;display:inline;" class="sortkey">' .. sortkey .. '♠</span></span>' or
'<span styledata-sort-value="display:none" class="sortkey">' .. sortkey .. '♠"></span>'
parms.join_before = sortspan
end
if parms.table_align then
local sort
if sortkey then
sort = ' data-sort-value="' .. sortkey .. '"'
if parms.opt_sortable_debug then
parms.join_before = '<span style="border:1px solid">' .. sortkey .. '</span>'
end
else
sort = ''
end
local style = 'style="text-align:' .. parms.table_align .. ';'
local sort = sortkey and ' data-sort-value="' .. sortkey .. '"' or ''
local joins = {}
for i = 1, 2 do
Ligne 2 490 ⟶ 2 565 :
-- is "1", or like "1.00", or is a fraction with value < 1;
-- (and more fields shown below, and a calculated 'absvalue' field).
-- or return true, nil if no value specified;
-- or return false, t where t is an error message table.
-- Input info.clean uses en digits (it has been translated, if necessary).
-- Output show uses en or non-en digits as appropriate, or can be spelled.
local invalue
if info then
invalue = info.value
if in_current.builtin == 'hand' then
invalue = info.altvalue
end
end
if invalue == nil or invalue == '' then
return true, nil
end
if out_current.builtin == 'hand' then
return cvt_to_hand(parms, info, in_current, out_current)
end
local invalue = in_current.builtin == 'hand' and info.altvalue or info.value
local outvalue, extra = convert(parms, invalue, info, in_current, out_current)
if parms.need_table_or_sort then
Ligne 2 777 ⟶ 2 842 :
local linked_pages -- to record linked pages so will not link to the same page more than once
 
local function make_linkunlink(link, id, link_keyunit_table)
-- Forget that the given unit has previously been linked (if it has).
-- That is needed when processing a range of inputs or outputs when an id
-- for the first range value may have been evaluated, but only an id for
-- the last value is displayed, and that id may need to be linked.
linked_pages[unit_table.unitcode or unit_table] = nil
end
 
local function make_link(link, id, unit_table)
-- Return wikilink "[[link|id]]", possibly abbreviated as in examples:
-- [[Mile|mile]] --> [[mile]]
Ligne 2 784 ⟶ 2 857 :
-- * no link given (so caller does not need to check if a link was defined); or
-- * link has previously been used during the current convert (to avoid overlinking).
local link_key
-- Linking with a unit uses the unit table as the link key, which fails to detect
if unit_table then
-- overlinking for conversions like the following (each links "mile" twice):
link_key = unit_table.unitcode or unit_table
-- {{convert|1|impgal/mi|USgal/mi|lk=on}}
else
-- {{convert|1|l/km|impgal/mi USgal/mi|lk=on}}
link_key = link
link_key = link_key or link -- use key if given (the key, but not the link, may be known when need to cancel a link record)
end
if not link or link == '' or linked_pages[link_key] then
return id
Ligne 2 832 ⟶ 2 906 :
else
i = 3
end
if i > 1 and varname == 'pl' then
i = i - 1
end
vname = split(unit_table.varname, '!')[i]
Ligne 2 862 ⟶ 2 939 :
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
Ligne 2 873 ⟶ 2 951 :
return symbol -- for exceptions that have the symbol built-in
end
end
if (unit2.symbol):find('⋅', 1, true) then
paren1, paren2 = '(', ')'
end
end
Ligne 2 912 ⟶ 2 993 :
unit_table.sep = ''
end
return result .. paren1 .. linked_id(parms, unit2, key_id2, want_link, '1') .. paren2
end
if multiplier then
Ligne 3 077 ⟶ 3 158 :
local inout = unit_table.inout
local abbr = parms.abbr
if (abbr == 'on' or abbr == inout) and not parms.number_word then
info.show = info.show ..
'<span style="margin-left:0.2em">×<span style="margin-left:0.1em">' ..
from_en('10') ..
'</span></span><s style="display:none">^</s><sup>' ..
from_en(tostring(engscale.exponent)) .. '</sup>'
elseif number_word then
local number_id
Ligne 3 139 ⟶ 3 220 :
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
Ligne 3 184 ⟶ 3 265 :
local range = parms.range
if range and not add_unit then
unlink(first_unit)
linked_pages[first_unit] = nil -- so the final and only id will be linked, if wanted
end
local id = range and make_id(parms, range.n + 1, first_unit) or id1
Ligne 3 221 ⟶ 3 302 :
-- Processing required for each output unit.
-- Return block of text to represent output (value/unit).
local inout = out_current.inout -- normally 'out' but can be 'in' for order=out
local id1, want_name = make_id(parms, 1, out_current)
local sep = out_current.sep -- set by make_id
Ligne 3 242 ⟶ 3 324 :
if range then
-- For simplicity and because more not needed, handle one range item only.
result = range_text(range[1], want_name, parms, result, prefix .. valinfo[2].show, 'out'inout)
end
return preunit .. result
Ligne 3 251 ⟶ 3 333 :
local range = parms.range
if range and not add_unit then
unlink(out_current)
linked_pages[out_current] = nil -- so the final and only id will be linked, if wanted
end
local id = range and make_id(parms, range.n + 1, out_current) or id1
local extra, was_hyphenated = hyphenated_maybe(parms, want_name, sep, id, 'out'inout)
if was_hyphenated then
add_unit = false
Ligne 3 275 ⟶ 3 357 :
result = show
else
result = range_text(range[i], want_name, parms, result, show, 'out'inout)
end
end
Ligne 3 292 ⟶ 3 374 :
-- for a single output (which is not a combination or a multiple);
-- or return false, t where t is an error message table.
if parms.opt_order_out and in_unit_table.unitcode == out_unit_table.unitcode then
out_unit_table.valinfo = collection()
out_unit_table.valinfo = in_unit_table.valinfo
local range = parms.range
else
for i = 1, (range and (range.n + 1) or 1) do
out_unit_table.valinfo = collection()
local success, info = cvtround(parms, in_unit_table.valinfo[i], in_unit_table, out_unit_table)
for _, v in ipairs(in_unit_table.valinfo) do
if not success then return false, info end
local success, info = cvtround(parms, v, in_unit_table, out_unit_table)
out_unit_table.valinfo:add(info)
if not success then return false, info end
out_unit_table.valinfo:add(info)
end
end
return true, process_one_output(parms, out_unit_table)
Ligne 3 306 ⟶ 3 391 :
-- for an output which is a multiple (like 'ftin');
-- or return false, t where t is an error message table.
local inout = out_unit_table.inout -- normally 'out' but can be 'in' for order=out
local multiple = out_unit_table.multiple -- table of scaling factors (will not be nil)
local combos = out_unit_table.combination -- table of unit tables (will not be nil)
Ligne 3 312 ⟶ 3 398 :
local disp = parms.disp
local want_name = (abbr_org == nil and (disp == 'or' or disp == 'slash')) or
not (abbr == 'on' or abbr == 'out'inout or abbr == 'mos')
local want_link = (parms.lk == 'on' or parms.lk == 'out'inout)
local mid = parms.opt_flip and parms.mid or ''
local sep1 = '&nbsp;'
Ligne 3 329 ⟶ 3 415 :
local tfrac, thisvalue, strforce
local out_current = combos[i]
out_current.inout = 'out'inout
local scale = multiple[i]
if i == 1 then -- least significant unit ('in' from 'ftin')
Ligne 3 411 ⟶ 3 497 :
end
local strval
local inoutspell_inout = (i == #combos or outvalue == 0) and 'out'inout or '' -- trick so the last value processed (first displayed) has uppercase, if requested
if strforce and outvalue == 0 then
sign = '' -- any sign is in strforce
Ligne 3 417 ⟶ 3 503 :
elseif tfrac then
local wholestr = (thisvalue > 0) and tostring(thisvalue) or nil
strval = format_fraction(parms, inoutspell_inout, false, wholestr, tfrac.numstr, tfrac.denstr, do_spell)
else
strval = (thisvalue == 0) and from_en('0') or with_separator(parms, format(fmt, thisvalue))
if do_spell then
strval = spell_number(parms, inoutspell_inout, strval) or strval
end
end
Ligne 3 444 ⟶ 3 530 :
local success, result2 = make_result(valinfo[i+1])
if not success then return false, result2 end
result = range_text(range[i], want_name, parms, result, result2, 'out'inout)
end
end
Ligne 3 456 ⟶ 3 542 :
local success, bad_output
local bad_input_mcode = in_unit_table.bad_mcode -- nil if input unit is a valid convert unit
local invalue1 = in_unit_table.valinfo[1].value
local out_unit = parms.out_unit
if out_unit == nil or out_unit == '' or type(out_unit) == 'function' then
if bad_input_mcode or parms.opt_input_unit_only then
bad_output = ''
else
local getdef = type(out_unit) == 'function' and out_unit or get_default
success, out_unit = get_default(invalue1, in_unit_table)
success, out_unit = getdef(in_unit_table.valinfo[1].value, in_unit_table)
parms.out_unit = out_unit
if not success then
Ligne 3 480 ⟶ 3 566 :
end
end
local lhs, rhs
local flipped = parms.opt_flip and not bad_input_mcode
if bad_output then
local parts = {}
rhs = (bad_output == '') and '' or message(parms, bad_output)
for part = 1, 2 do
elseif parms.opt_input_unit_only then
-- The LHS (parts[1]) is normally the input, but is the output if flipped.
rhs = ''
-- Process LHS first so it will be linked, if wanted.
else
-- Linking to the same item is suppressed in the RHS to avoid overlinking.
iflocal (partcombos == 1-- andnil not(for 'ft' or flipped'ftin'), or (parttable ==of 2unit andtables flipped)(for then'm ft')
if not out_unit_table.multiple then -- nil/false ('ft' or 'm ft'), or table of factors ('ftin')
parts[part] = process_input(parms, in_unit_table)
combos = out_unit_table.combination
elseif bad_output then
end
parts[part] = (bad_output == '') and '' or message(parms, bad_output)
local frac = parms.frac -- nil or denominator of fraction for output values
else
if frac then
local outputs = {}
local-- combosApply fraction --to nilthe unit (forif 'ft'only or 'ftin'one), or tableto ofnon-SI unit tablesunits (forif 'ma ft'combination),
-- except that if a precision is also specified, the fraction only applies to
if not out_unit_table.multiple then -- nil/false ('ft' or 'm ft'), or table of factors ('ftin')
-- the hand unit; that allows the following result:
combos = out_unit_table.combination
-- {{convert|156|cm|in hand|1|frac=2}} → 156 centimetres (61.4 in; 15.1½ hands)
end
-- However, the following is handled elsewhere as a special case:
local frac = parms.frac -- nil or denominator of fraction for output values
-- {{convert|156|cm|hand in|1|frac=2}} → 156 centimetres (15.1½ hands; 61½ in)
if frac then
if combos then
-- Apply fraction to the unit (if only one), or to non-SI units (if a combination),
local precision = parms.precision
-- except that if a precision is also specified, the fraction only applies to
--for the hand_, unit; that allows thein followingipairs(combos) result:do
if unit.builtin == 'hand' or (not precision and not unit.prefixes) then
-- {{convert|156|cm|in hand|1|frac=2}} → 156 centimetres (61.4 in; 15.1½ hands)
unit.frac = frac
-- However, the following is handled elsewhere as a special case:
-- {{convert|156|cm|hand in|1|frac=2}} → 156 centimetres (15.1½ hands; 61½ in)
if combos then
local precision = parms.precision
for _, unit in ipairs(combos) do
if unit.builtin == 'hand' or (not precision and not unit.prefixes) then
unit.frac = frac
end
end
else
out_unit_table.frac = frac
end
else
out_unit_table.frac = frac
end
end
local out_first
local outputs = {}
local imax = combos and #combos or 1 -- 1 (single unit) or number of unit tables
forif iimax == 1, imax dothen
parms.opt_order_out = nil -- only useful with an output combination
local success, item
end
local out_current = combos and combos[i] or out_unit_table
if not flipped and not parms.opt_order_out then
out_current.inout = 'out'
-- Process left side first so any duplicate links (from lk=on) are suppressed
if i == 1 then
-- on right. Example: {{convert|28|e9pc|e9ly|abbr=off|lk=on}}
out_first = out_current
lhs = process_input(parms, in_unit_table)
if imax > 1 and out_current.builtin == 'hand' then
end
out_current.out_next = combos[2] -- built-in hand can influence next unit in a combination
for i = 1, imax do
end
local success, item
local out_current = combos and combos[i] or out_unit_table
out_current.inout = 'out'
if i == 1 then
if imax > 1 and out_current.builtin == 'hand' then
out_current.out_next = combos[2] -- built-in hand can influence next unit in a combination
end
if out_currentparms.multipleopt_order_out then
out_current.inout = 'in'
success, item = make_output_multiple(parms, in_unit_table, out_current)
else
success, item = make_output_single(parms, in_unit_table, out_current)
end
if not success then return false, item end
table.insert(outputs, item)
end
if parmsout_current.opt_input_unit_onlymultiple then
success, item = make_output_multiple(parms, in_unit_table, out_current)
parts[part] = ''
else
success, item = make_output_single(parms, in_unit_table, out_current)
local sep = parms.table_joins and parms.table_joins[2] or parms.join_between
parts[part] = table.concat(outputs, sep)
end
if not success then return false, item end
outputs[i] = item
end
if parms.opt_order_out then
lhs = outputs[1]
table.remove(outputs, 1)
end
local sep = parms.table_joins and parms.table_joins[2] or parms.join_between
rhs = table.concat(outputs, sep)
end
if flipped or not lhs then
local input = process_input(parms, in_unit_table)
if flipped then
lhs = rhs
rhs = input
else
lhs = input
end
end
if parms.join_before then
parts[1]lhs = parms.join_before .. parts[1]lhs
end
local wikitext
if bad_input_mcode then
if bad_input_mcode == '' then
wikitext = parts[1]lhs
else
wikitext = parts[1]lhs .. message(parms, bad_input_mcode)
end
elseif parms.table_joins then
wikitext = parms.table_joins[1] .. parts[1]lhs .. parms.table_joins[2] .. parts[2]rhs
else
wikitext = parts[1]lhs .. parms.joins[1] .. parts[2]rhs .. parms.joins[2]
end
if parms.warnings and not bad_input_mcode then
Ligne 3 564 ⟶ 3 663 :
end
 
-- entry point from another module
local function main_convert(frame)
local function _convert(frame, pframe)
-- Do convert, and if needed, do it again with higher default precision.
local parms = { frame = frame } -- will hold template arguments, after translation
set_config(frame.args)
local success, result = get_parms(parms, frame:getParent()pframe and pframe.args or {})
if success then
if type(result) ~= 'table' then
Ligne 3 588 ⟶ 3 688 :
-- 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
 
-- entry point from template
local function main_convert(frame)
return _convert(frame, frame:getParent())
end
 
Ligne 3 680 ⟶ 3 792 :
end
 
return { convert = main_convert, _unit = _unit, _convert = _convert }