Content deleted Content added
update from sandbox per Template talk:Convert#Module version 29 |
move arg extraction to its own function to allow _convert() to be called from other lua modules |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 731:
local success, result = lookup(parms, target, what, utable, fails, depth)
if not success then return false, result end
override_from(result, t, { 'customary', 'default', 'link', 'symbol', 'symlink', 'usename' })
local multiplier = t.multiplier
if multiplier then
Line 837:
-- and not if the unit has an offset or is a built-in.
-- Only en digits are accepted.
local e, exponent, baseunit = unitcode:match('^
if exponent then
local engscale = text_code.eng_scales[exponent]
Line 843:
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.this_number_word = true
unitcode = 'e' .. unitcode:sub(2)
end▼
result.unitcode = unitcode -- 'e6cuft' not 'cuft'
result.defkey = unitcode -- key to lookup default exception
Line 1,142 ⟶ 1,146:
local fracfmt = {
{ -- Like {{frac}} (fraction slash).
'<span class="frac
'<span class="frac
style = 'frac',
},
{ -- Like {{sfrac}} (stacked fraction, that is, horizontal bar).
'<span class="sfrac tion
'<span class="sfrac
style = 'sfrac',
},
Line 2,921 ⟶ 2,925:
end
local function variable_name(clean, unit_table, exp_multiplier, key_id)
--
-- Parameter clean is the unsigned
-- It may represent a number ("1.0") or a fraction ("1+2/3").
▲ -- Fields are separated with "!" and are not empty.
-- A field for a unit using an SI prefix has the prefix name inserted,
-- replacing '#' if found, or before the field otherwise.
if clean:match('[./]') then -- float or fraction
local vname▼
clean = exp_multiplier -- force selection of name for a large integer
vname = unit_table.name1▼
elseif unit_table.varname then▼
▲ if clean == '2' then
elseif clean == '3' or clean == '4' then▼
else
clean = 34.5 -- force selection of name for a float value
end
else
clean = tonumber(clean) * (exp_multiplier or 1)
▲ end
vname = split(unit_table.varname, '!')[i]▼
end
▲ local name1, vname
if vname then▼
if key_id == 'pername' and unit_table.pername then
vname = unit_table.pername
▲ elseif unit_table.varname then
vname = mw.language.getContentLanguage():convertPlural(clean, name1, unpack(splitvname))
else
return clean == 1 and unit_table.name1 or unit_table.name2
end
▲ if vname == name1 then
-- SI prefix (if any) has been inserted by unit_prefixed_mt.
else
local si_name = rawget(unit_table, 'si_name') or ''
local pos = vname:find('#', 1, true)
Line 2,962 ⟶ 2,961:
vname = si_name .. vname
end
end
return
end
local function linked_id(parms, unit_table, key_id, want_link, clean, exp_multiplier)
-- Return final unit id (symbol or name), optionally with a wikilink,
-- and update unit_table.sep if required.
-- key_id is one of: 'symbol', 'sym_us', 'name1', 'name1_us', 'name2', 'name2_us', 'pername'.
local abbr_on = (key_id == 'symbol' or key_id == 'sym_us')
if abbr_on and want_link then
Line 3,005 ⟶ 3,003:
else
key_id2 = key_id
end
key_id2 = unit2.pername and 'pername' or key_id2 -- ukwiki has some units with a different name in "per unitname"
end
local result
Line 3,017 ⟶ 3,018:
end
if want_link and unit_table.link then
if
result = (unit1 and
else
result = (unit1 and
end
if omit_separator(result) then
Line 3,053 ⟶ 3,054:
multiplier = ''
end
local id = unit_table.fixed_name or ((varname and not abbr_on) and variable_name(clean, unit_table, exp_multiplier, key_id) or unit_table[key_id])
if omit_separator(id) then
unit_table.sep = ''
Line 3,114 ⟶ 3,115:
local inout = unit_table.inout
local info = unit_table.valinfo[which]
local lk = parms.lk
local want_link = (lk == 'on' or lk == inout)
local usename = unit_table.usename▼
local singular = info.singular
local want_name
local exp_multiplier
want_name = true
else
if parms.abbr_org == nil then
if parms.wantname then
want_name = true
Line 3,166 ⟶ 3,165:
-- engscale: so "|1|e3kg" gives "1 thousand kilograms" (plural)
singular = false
exp_multiplier = 10^unit_table.engscale.exponent -- '1 gram' and '1 thousand grams', for example, may use different names for the unit in some languages
end
key = (
if parms.opt_sp_us then
key = key .. '_us'
Line 3,180:
key = parms.opt_sp_us and 'sym_us' or 'symbol'
end
return linked_id(parms, unit_table, key, want_link, info.clean, exp_multiplier), want_name
end
local function decorate_value(parms, unit_table, which,
-- If needed, update unit_table so values will be shown with extra information.
-- For consistency with the old template (but different from fmtpower),
-- the style to display powers of 10 includes "display:none" to allow some
-- browsers to copy, for example, "10³" as "10^3", rather than as "103".
-- The engscale table may have entries such as either of the following:
-- ["3"] = { "thousand", exponent = 3 },
-- ["3"] = { name1 = "A", varname = "B!C!D", exponent = 3 },
-- The first option always uses "thousand" as the exponent name.
-- The second option uses one of A, B, C, D as the exponent name, depending on the value.
local info
local engscale = unit_table.engscale
Line 3,198 ⟶ 3,203:
info.decorated = true
if engscale then
-- Range |10|-|20|e3km| gives '10×10³–20×10³' or '10–20 thousand'.
local inout = unit_table.inout
local abbr = parms.abbr
if (abbr == 'on' or abbr == inout) and not (unit_table.this_number_word or parms.number_word) then
info.show = info.show ..
'<span style="margin-left:0.2em">×<span style="margin-left:0.1em">' ..
Line 3,206 ⟶ 3,212:
'</span></span><s style="display:none">^</s><sup>' ..
from_en(tostring(engscale.exponent)) .. '</sup>'
elseif
local number_id
local name = engscale.varname and variable_name(info.clean, engscale) or engscale[1]
if parms.lk == 'on' or parms.lk == inout then
number_id = make_link(engscale.link,
else
number_id =
end
-- WP:NUMERAL recommends " " in values like "12 million".
Line 3,318 ⟶ 3,324:
if range then
for i = 0, range.n do
local
if i == range.n then
add_unit = false
end
decorate_value(parms, first_unit, i+1,
local show = valinfo[i+1].show
if add_unit then
Line 3,386 ⟶ 3,392:
if range then
for i = 0, range.n do
local
if i == range.n then
add_unit = false
end
decorate_value(parms, out_current, i+1,
local show = valinfo[i+1].show
if add_unit then
Line 3,586 ⟶ 3,592:
local out_unit = parms.out_unit
if out_unit == nil or out_unit == '' or type(out_unit) == 'function' then
-- out_unit can be set to a function by adjustparameters in Module:Convert/wikidata.
if bad_input_mcode or parms.opt_input_unit_only then
bad_output = ''
Line 3,705 ⟶ 3,712:
end
local function
-- Do convert, and if needed, do it again with higher default precision.
local parms = { frame = frame or mw.getCurrentFrame() } -- will hold template arguments, after translation
set_config(
local success, result = get_parms(parms,
if success then
if type(result) ~= 'table' then
Line 3,744 ⟶ 3,751:
end
return success and result or message(parms, result)
end
local function main_convert(frame)
return _main_convert(frame.args, frame:getParent().args, frame)
end
Line 3,828 ⟶ 3,839:
end
return { convert = main_convert, _convert = _main_convert, _unit = _unit }
|