update from sandbox per Template talk:Convert |
Created page with "-- Convert a value from one unit of measurement to another. -- Example: {{convert|123|lb|kg}} --> 123 pounds (56 kg) local MINUS = '−' -- Unicode U+2212 MINUS SIGN (UTF-8: e2 88 92) local abs = math.abs local floor = math.floor local format = string.format local log10 = math.log10 local ustring = mw.ustring local ulen = ustring.len local usub = ustring.sub -- Configuration options to keep magic values in one location. -- Conversion data and message text are defined..." Tags: Mobile edit Mobile web edit |
||
Line 1: | Line 1: | ||
-- Convert a value from one unit of measurement to another. | -- Convert a value from one unit of measurement to another. | ||
-- Example: {{convert|123|lb|kg}} --> 123 pounds (56 kg) | -- Example: {{convert|123|lb|kg}} --> 123 pounds (56 kg) | ||
local MINUS = '−' -- Unicode U+2212 MINUS SIGN (UTF-8: e2 88 92) | local MINUS = '−' -- Unicode U+2212 MINUS SIGN (UTF-8: e2 88 92) | ||
Line 411: | Line 410: | ||
-- If no altitude given, use default (zero altitude = sea level). | -- If no altitude given, use default (zero altitude = sea level). | ||
-- Table gives speed of sound in miles per hour at various altitudes: | -- Table gives speed of sound in miles per hour at various altitudes: | ||
-- altitude = -17,499 to | -- altitude = -17,499 to 302,499 feet | ||
-- mach_table[a + 4] = s where | -- mach_table[a + 4] = s where | ||
-- a = (altitude / 5000) rounded to nearest integer (-3 to | -- a = (altitude / 5000) rounded to nearest integer (-3 to 60) | ||
-- s = speed of sound (mph) at that altitude | -- s = speed of sound (mph) at that altitude | ||
-- LATER: Should calculate result from an interpolation between the next | -- LATER: Should calculate result from an interpolation between the next | ||
Line 423: | Line 422: | ||
660.1, 660.1, 660.1, 662.0, 664.3, 666.5, 668.9, 671.1, 673.4, 675.6, -- 11 to 20 | 660.1, 660.1, 660.1, 662.0, 664.3, 666.5, 668.9, 671.1, 673.4, 675.6, -- 11 to 20 | ||
677.9, 683.7, 689.9, 696.0, 702.1, 708.1, 714.0, 719.9, 725.8, 731.6, -- 21 to 30 | 677.9, 683.7, 689.9, 696.0, 702.1, 708.1, 714.0, 719.9, 725.8, 731.6, -- 21 to 30 | ||
737.3, 737.7, 737.7, 736.2, 730.5, 724.6, 718.8, 712.9, 707.0, 701. | 737.3, 737.7, 737.7, 736.2, 730.5, 724.6, 718.8, 712.9, 707.0, 701.1, -- 31 to 40 | ||
695.0, 688.9, 682.8, 676.6, 670.4, 664.1, 657.8, 652.9, 648.3, 643.7, -- 41 to 50 | 695.0, 688.9, 682.8, 676.6, 670.4, 664.1, 657.8, 652.9, 648.3, 643.7, -- 41 to 50 | ||
639.1, 634.4, 629.6, 624.8, 620.0, 615.2, 613.2, 613.2, 613.2, 613.5, -- 51 to 60 | 639.1, 634.4, 629.6, 624.8, 620.0, 615.2, 613.2, 613.2, 613.2, 613.5, -- 51 to 60 | ||
} | } | ||
altitude = altitude or 0 | altitude = altitude or 0 | ||
Line 437: | Line 434: | ||
if a < -3 then | if a < -3 then | ||
a = -3 | a = -3 | ||
elseif a > | elseif a > 60 then | ||
a = | a = 60 | ||
end | end | ||
return mach_table[a + 4] * 0.44704 -- mph converted to m/s | return mach_table[a + 4] * 0.44704 -- mph converted to m/s | ||
Line 444: | Line 441: | ||
-- END: Code required only for built-in units. | -- END: Code required only for built-in units. | ||
------------------------------------------------------------------------ | ------------------------------------------------------------------------ | ||
local function get_range(word) | local function get_range(word) | ||
Line 568: | Line 543: | ||
if key == 'symbol' then | if key == 'symbol' then | ||
value = self.si_prefix .. self._symbol | value = self.si_prefix .. self._symbol | ||
elseif key == 'sym_us' then | elseif key == 'sym_us' then | ||
value = rawget(self, '_sym_us') | value = rawget(self, '_sym_us') | ||
Line 731: | Line 705: | ||
local success, result = lookup(parms, target, what, utable, fails, depth) | local success, result = lookup(parms, target, what, utable, fails, depth) | ||
if not success then return false, result end | if not success then return false, result end | ||
override_from(result, t, { 'customary', 'default', 'link', 'symbol', 'symlink | override_from(result, t, { 'customary', 'default', 'link', 'symbol', 'symlink' }) | ||
local multiplier = t.multiplier | local multiplier = t.multiplier | ||
if multiplier then | if multiplier then | ||
Line 837: | Line 811: | ||
-- and not if the unit has an offset or is a built-in. | -- and not if the unit has an offset or is a built-in. | ||
-- Only en digits are accepted. | -- Only en digits are accepted. | ||
local | local exponent, baseunit = unitcode:match('^e(%d+)(.*)') | ||
if exponent then | if exponent then | ||
local engscale = text_code.eng_scales[exponent] | local engscale = text_code.eng_scales[exponent] | ||
Line 843: | Line 817: | ||
local success, result = lookup(parms, baseunit, 'no_combination', utable, fails, depth) | local success, result = lookup(parms, baseunit, 'no_combination', utable, fails, depth) | ||
if success and not (result.offset or result.builtin or result.engscale) then | if success and not (result.offset or result.builtin or result.engscale) then | ||
result.unitcode = unitcode -- 'e6cuft' not 'cuft' | result.unitcode = unitcode -- 'e6cuft' not 'cuft' | ||
result.defkey = unitcode -- key to lookup default exception | result.defkey = unitcode -- key to lookup default exception | ||
Line 914: | Line 884: | ||
-- The name may be linked and the target of the link must not be changed. | -- The name may be linked and the target of the link must not be changed. | ||
-- Hypothetical examples: | -- Hypothetical examples: | ||
-- [[long ton|ton]] → [[long ton|ton]] (no change) | -- [[wikipedia:long ton|ton]] → [[wikipedia:long ton|ton]] (no change) | ||
-- [[tonne|long ton]] → [[tonne|long-ton]] | -- [[wikipedia:tonne|long ton]] → [[wikipedia:tonne|long-ton]] | ||
-- [[metric ton|long ton]] → [[metric ton|long-ton]] | -- [[wikipedia:metric ton|long ton]] → [[wikipedia:metric ton|long-ton]] | ||
-- [[long ton]] → [[long ton|long-ton]] | -- [[wikipedia:long ton]] → [[wikipedia:long ton|long-ton]] | ||
-- Input can also have multiple links in a single name like: | -- Input can also have multiple links in a single name like: | ||
-- [[United States customary units|U.S.]] [[US gallon|gallon]] | -- [[wikipedia:United States customary units|U.S.]] [[wikipedia:US gallon|gallon]] | ||
-- [[mile]]s per [[United States customary units|U.S.]] [[quart]] | -- [[wikipedia:mile]]s per [[wikipedia:United States customary units|U.S.]] [[wikipedia:quart]] | ||
-- [[long ton]]s per [[short ton]] | -- [[wikipedia:long ton]]s per [[wikipedia:short ton]] | ||
-- Assume that links cannot be nested | -- Assume that links cannot be nested. | ||
-- This uses a simple and efficient procedure that works for most cases. | -- This uses a simple and efficient procedure that works for most cases. | ||
-- Some units (if used) would require more, and can later think about | -- Some units (if used) would require more, and can later think about | ||
Line 963: | Line 933: | ||
end | end | ||
if parts.n == 0 then | if parts.n == 0 then | ||
-- No link | -- No link was found in the original name. | ||
parts:add(hyphenated(name, parts)) | parts:add(hyphenated(name, parts)) | ||
end | end | ||
Line 1,146: | Line 1,116: | ||
local fracfmt = { | local fracfmt = { | ||
{ -- Like {{frac}} (fraction slash). | { -- Like {{frac}} (fraction slash). | ||
'<span class="frac"> | -- 1/2 : sign, numerator, denominator | ||
'<span class="frac"> | -- 1+2/3 : signed_wholenumber, numerator, denominator | ||
'<span class="frac nowrap">%s<sup>%s</sup>⁄<sub>%s</sub></span>', | |||
'<span class="frac nowrap">%s<span class="visualhide"> </span><sup>%s</sup>⁄<sub>%s</sub></span>', | |||
}, | }, | ||
{ -- Like {{sfrac}} ( | { -- Like {{sfrac}} (fraction horizontal bar). | ||
'<span class="sfrac | -- 1//2 : sign, numerator, denominator (sign should probably be before the fraction, but then it can wrap, and html is already too long) | ||
'<span class="sfrac"> | -- 1+2//3 : signed_wholenumber, numerator, denominator | ||
'<span class="sfrac nowrap" style="display:inline-block; vertical-align:-0.5em; font-size:85%%; text-align:center;"><span style="display:block; line-height:1em; padding:0 0.1em;">%s%s</span><span class="visualhide">/</span><span style="display:block; line-height:1em; padding:0 0.1em; border-top:1px solid;">%s</span></span>', | |||
'<span class="sfrac nowrap">%s<span class="visualhide"> </span><span style="display:inline-block; vertical-align:-0.5em; font-size:85%%; text-align:center;"><span style="display:block; line-height:1em; padding:0 0.1em;">%s</span><span class="visualhide">/</span><span style="display:block; line-height:1em; padding:0 0.1em; border-top:1px solid;">%s</span></span></span>', | |||
}, | }, | ||
} | } | ||
Line 1,167: | Line 1,139: | ||
wholestr = nil | wholestr = nil | ||
end | end | ||
local | if wholestr then | ||
local decorated = with_separator(parms, wholestr) | |||
if negative then | |||
decorated = MINUS .. decorated | |||
end | |||
local fmt = fracfmt[style][2] | |||
wikitext = format(fmt, decorated, from_en(numstr), from_en(denstr)) | |||
else | |||
local sign = negative and MINUS or '' | |||
wikitext = format(fracfmt[style][1], sign, from_en(numstr), from_en(denstr)) | |||
end | |||
if do_spell then | if do_spell then | ||
if negative then | if negative then | ||
Line 1,182: | Line 1,158: | ||
end | end | ||
end | end | ||
wikitext = spell_number(parms, inout, wholestr, numstr, denstr) or wikitext | |||
end | end | ||
return wikitext | return wikitext | ||
end | end | ||
Line 1,555: | Line 1,527: | ||
-- v = value of text (text is a number) | -- v = value of text (text is a number) | ||
-- f = true if value is an integer | -- f = true if value is an integer | ||
-- Input can use en digits or digits in local language | -- Input can use en digits or digits in local language, | ||
-- but no Unicode minus, and no fraction. | -- but no separators, no Unicode minus, and no fraction. | ||
if text then | if text then | ||
local number = tonumber(to_en(text)) | local number = tonumber(to_en(text)) | ||
Line 1,696: | Line 1,668: | ||
end | end | ||
local function range_text(range, want_name, parms, before, after, inout | local function range_text(range, want_name, parms, before, after, inout) | ||
-- Return before .. rtext .. after | -- Return before .. rtext .. after | ||
-- where rtext is the text that separates two values in a range. | -- where rtext is the text that separates two values in a range. | ||
local rtext, adj_text, exception | local rtext, adj_text, exception | ||
if type(range) == 'table' then | if type(range) == 'table' then | ||
-- Table must specify range text for ('off' and 'on') or ('input' and 'output'), | -- Table must specify range text for ('off' and 'on') or ('input' and 'output'), | ||
Line 1,717: | Line 1,688: | ||
end | end | ||
end | end | ||
if rtext == '–' and | if rtext == '–' and after:sub(1, #MINUS) == MINUS then | ||
rtext = ' – ' | rtext = ' – ' | ||
end | end | ||
Line 1,795: | Line 1,766: | ||
-- 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. | ||
currency_text = nil -- local testing can hold module in memory; must clear globals | 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 | if kv_pairs.adj and kv_pairs.sing then | ||
-- For enwiki (before translation), warn if attempt to use adj and sing | -- For enwiki (before translation), warn if attempt to use adj and sing | ||
Line 1,807: | Line 1,786: | ||
local en_name = text_code.en_option_name[loc_name] | local en_name = text_code.en_option_name[loc_name] | ||
if en_name then | if en_name then | ||
local en_value | local en_value | ||
if | if en_name == '$' or en_name == 'frac' or en_name == 'sigfig' then | ||
if loc_value == '' then | if loc_value == '' then | ||
add_warning(parms, 2, 'cvt_empty_option', loc_name) | add_warning(parms, 2, 'cvt_empty_option', loc_name) | ||
elseif en_name == '$' then | |||
-- Value should be a single character like "€" for the euro currency symbol, but anything is accepted. | |||
currency_text = (loc_value == 'euro') and '€' or loc_value | |||
else | else | ||
local minimum | local minimum | ||
local number, is_integer = get_number(loc_value) | local number, is_integer = get_number(loc_value) | ||
if | if en_name == 'frac' then | ||
minimum = 2 | minimum = 2 | ||
if number and number < 0 then | if number and number < 0 then | ||
Line 1,824: | Line 1,803: | ||
end | end | ||
else | else | ||
minimum = | minimum = 1 | ||
end | end | ||
if number and is_integer and number >= minimum then | if number and is_integer and number >= minimum then | ||
en_value = number | en_value = number | ||
else | else | ||
add_warning(parms, 1, (en_name == 'frac' and 'cvt_bad_frac' or 'cvt_bad_sigfig'), loc_name .. '=' .. loc_value) | |||
end | end | ||
end | end | ||
elseif | elseif accept_any_text[en_name] then | ||
en_value = loc_value ~= '' and loc_value or nil -- accept non-empty user text with no validation | en_value = loc_value ~= '' and loc_value or nil -- accept non-empty user text with no validation | ||
if | if en_name == 'input' then | ||
-- May have something like {{convert|input=}} (empty input) if source is an infobox | -- May have something like {{convert|input=}} (empty input) if source is an infobox | ||
-- with optional fields. In that case, want to output nothing rather than an error. | -- with optional fields. In that case, want to output nothing rather than an error. | ||
Line 1,853: | Line 1,819: | ||
end | end | ||
else | else | ||
en_value = | en_value = text_code.en_option_value[en_name][loc_value] | ||
if en_value and en_value:sub(-1) == '?' then | if en_value and en_value:sub(-1) == '?' then | ||
en_value = en_value:sub(1, -2) | en_value = en_value:sub(1, -2) | ||
Line 2,205: | Line 2,171: | ||
end | end | ||
if in_unit_table.builtin == 'mach' then | if in_unit_table.builtin == 'mach' then | ||
-- As with old template, a number following Mach as the input unit is the altitude | -- As with old template, a number following Mach as the input unit is the altitude, | ||
-- | -- and there is no way to specify an altitude for the output unit. | ||
-- Could put more code in this function to get any output unit and check for | |||
-- an altitude following that unit. | |||
local success, info = extract_number(parms, parms[i], false, true) | |||
if success then | if success then | ||
i = i + 1 | i = i + 1 | ||
Line 2,441: | Line 2,403: | ||
end | end | ||
if in_builtin == 'mach' or out_builtin == 'mach' then | if in_builtin == 'mach' or out_builtin == 'mach' then | ||
local adjust | |||
local | |||
if in_builtin == 'mach' then | if in_builtin == 'mach' then | ||
inscale = | inscale = speed_of_sound(in_current.altitude) | ||
adjust = outscale / 0.1 | |||
else | |||
outscale = speed_of_sound(out_current.altitude) | |||
adjust = 0.1 / inscale | |||
end | end | ||
return true, { | return true, { | ||
outvalue = invalue * (inscale / outscale), | outvalue = invalue * (inscale / outscale), | ||
Line 2,654: | Line 2,610: | ||
show = format('%.0f', floor((outvalue / n) + 0.5) * n) | show = format('%.0f', floor((outvalue / n) + 0.5) * n) | ||
end | end | ||
else | else | ||
local inclean = info.clean | local inclean = info.clean | ||
Line 2,897: | Line 2,850: | ||
local function make_link(link, id, unit_table) | local function make_link(link, id, unit_table) | ||
-- Return wikilink | -- Return wikilink, possibly abbreviated as in examples: | ||
-- [[Mile|mile]] --> [[mile]] | -- [[wikipedia:Mile|mile]] --> [[wikipedia:mile]] | ||
-- [[Mile|miles]] --> [[mile]]s | -- [[wikipedia:Mile|miles]] --> [[wikipedia:mile]]s | ||
-- However, just id is returned if: | -- However, just id is returned if: | ||
-- * no link given (so caller does not need to check if a link was defined); or | -- * no link given (so caller does not need to check if a link was defined); or | ||
Line 2,925: | Line 2,878: | ||
end | end | ||
local function variable_name(clean, unit_table | local function variable_name(clean, unit_table) | ||
-- | -- For slwiki, a unit name depends on the value. | ||
-- Parameter clean is the unsigned value in en digits, as a string. | -- Parameter clean is the unsigned rounded value in en digits, as a string. | ||
-- | -- Value Source Example for "m" | ||
-- | -- integer 1: name1 meter (also is the name of the unit) | ||
-- integer 2: var{1} metra | |||
-- integer 3 and 4: var{2} metri | |||
-- integer else: var{3} metrov (0 and 5 or more) | |||
-- real/fraction: var{4} metra | |||
-- var{i} means the i'th field in unit_table.varname if it exists and has | |||
-- an i'th field, otherwise name2. | |||
-- Fields are separated with "!" and are not empty. | |||
-- A field for a unit using an SI prefix has the prefix name inserted, | -- A field for a unit using an SI prefix has the prefix name inserted, | ||
-- replacing '#' if found, or before the field otherwise. | -- replacing '#' if found, or before the field otherwise. | ||
if clean | local vname | ||
if clean == '1' then | |||
vname = unit_table.name1 | |||
elseif unit_table.varname then | |||
local i | |||
if clean == '2' then | |||
i = 1 | |||
elseif clean == '3' or clean == '4' then | |||
i = 2 | |||
elseif clean:find('.', 1, true) then | |||
i = 4 | |||
else | else | ||
i = 3 | |||
end | end | ||
if i > 1 and varname == 'pl' then | |||
i = i - 1 | |||
end | |||
vname = split(unit_table.varname, '!')[i] | |||
end | end | ||
if vname | if vname then | ||
local si_name = rawget(unit_table, 'si_name') or '' | local si_name = rawget(unit_table, 'si_name') or '' | ||
local pos = vname:find('#', 1, true) | local pos = vname:find('#', 1, true) | ||
Line 2,961: | Line 2,919: | ||
vname = si_name .. vname | vname = si_name .. vname | ||
end | end | ||
return vname | |||
end | end | ||
return | return unit_table.name2 | ||
end | end | ||
local function linked_id(parms, unit_table, key_id, want_link, clean | local function linked_id(parms, unit_table, key_id, want_link, clean) | ||
-- Return final unit id (symbol or name), optionally with a wikilink, | -- Return final unit id (symbol or name), optionally with a wikilink, | ||
-- and update unit_table.sep if required. | -- and update unit_table.sep if required. | ||
-- key_id is one of: 'symbol', 'sym_us', 'name1', 'name1_us', 'name2', 'name2_us | -- key_id is one of: 'symbol', 'sym_us', 'name1', 'name1_us', 'name2', 'name2_us'. | ||
local abbr_on = (key_id == 'symbol' or key_id == 'sym_us') | local abbr_on = (key_id == 'symbol' or key_id == 'sym_us') | ||
if abbr_on and want_link then | if abbr_on and want_link then | ||
Line 3,003: | Line 2,962: | ||
else | else | ||
key_id2 = key_id | key_id2 = key_id | ||
end | end | ||
local result | local result | ||
Line 3,018: | Line 2,974: | ||
end | end | ||
if want_link and unit_table.link then | if want_link and unit_table.link then | ||
if varname | if abbr_on or not varname then | ||
result = (unit1 and | result = (unit1 and linked_id(parms, unit1, key_id, false, clean) or '') .. result .. linked_id(parms, unit2, key_id2, false, '1') | ||
else | else | ||
result = (unit1 and | result = (unit1 and variable_name(clean, unit1) or '') .. result .. variable_name('1', unit2) | ||
end | end | ||
if omit_separator(result) then | if omit_separator(result) then | ||
Line 3,054: | Line 3,010: | ||
multiplier = '' | multiplier = '' | ||
end | end | ||
local id = unit_table.fixed_name or ((varname and not abbr_on) and variable_name(clean, unit_table | local id = unit_table.fixed_name or ((varname and not abbr_on) and variable_name(clean, unit_table) or unit_table[key_id]) | ||
if omit_separator(id) then | if omit_separator(id) then | ||
unit_table.sep = '' | unit_table.sep = '' | ||
Line 3,115: | Line 3,071: | ||
local inout = unit_table.inout | local inout = unit_table.inout | ||
local info = unit_table.valinfo[which] | local info = unit_table.valinfo[which] | ||
local abbr_org = parms.abbr_org | |||
local adjectival = parms.opt_adjectival | |||
local lk = parms.lk | local lk = parms.lk | ||
local want_link = (lk == 'on' or lk == inout) | local want_link = (lk == 'on' or lk == inout) | ||
local usename = unit_table.usename | |||
local singular = info.singular | local singular = info.singular | ||
local want_name | local want_name | ||
if usename then | |||
if | |||
want_name = true | want_name = true | ||
else | else | ||
if | if abbr_org == nil then | ||
if parms.wantname then | if parms.wantname then | ||
want_name = true | want_name = true | ||
Line 3,165: | Line 3,123: | ||
-- engscale: so "|1|e3kg" gives "1 thousand kilograms" (plural) | -- engscale: so "|1|e3kg" gives "1 thousand kilograms" (plural) | ||
singular = false | singular = false | ||
end | end | ||
key = ( | key = (adjectival or singular) and 'name1' or 'name2' | ||
if parms.opt_sp_us then | if parms.opt_sp_us then | ||
key = key .. '_us' | key = key .. '_us' | ||
Line 3,180: | Line 3,137: | ||
key = parms.opt_sp_us and 'sym_us' or 'symbol' | key = parms.opt_sp_us and 'sym_us' or 'symbol' | ||
end | end | ||
return linked_id(parms, unit_table, key, want_link, info.clean | return linked_id(parms, unit_table, key, want_link, info.clean), want_name | ||
end | end | ||
local function decorate_value(parms, unit_table, which, | local function decorate_value(parms, unit_table, which, number_word) | ||
-- If needed, update unit_table so values will be shown with extra information. | -- If needed, update unit_table so values will be shown with extra information. | ||
-- For consistency with the old template (but different from fmtpower), | -- For consistency with the old template (but different from fmtpower), | ||
-- the style to display powers of 10 includes "display:none" to allow some | -- 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". | -- browsers to copy, for example, "10³" as "10^3", rather than as "103". | ||
local info | local info | ||
local engscale = unit_table.engscale | local engscale = unit_table.engscale | ||
Line 3,203: | Line 3,155: | ||
info.decorated = true | info.decorated = true | ||
if engscale then | if engscale then | ||
local inout = unit_table.inout | local inout = unit_table.inout | ||
local abbr = parms.abbr | local abbr = parms.abbr | ||
if (abbr == 'on' or abbr == inout) and not | if (abbr == 'on' or abbr == inout) and not parms.number_word then | ||
info.show = info.show .. | info.show = info.show .. | ||
'<span style="margin-left:0.2em">×<span style="margin-left:0.1em">' .. | '<span style="margin-left:0.2em">×<span style="margin-left:0.1em">' .. | ||
Line 3,212: | Line 3,163: | ||
'</span></span><s style="display:none">^</s><sup>' .. | '</span></span><s style="display:none">^</s><sup>' .. | ||
from_en(tostring(engscale.exponent)) .. '</sup>' | from_en(tostring(engscale.exponent)) .. '</sup>' | ||
elseif | elseif number_word then | ||
local number_id | local number_id | ||
local | local lk = parms.lk | ||
if | if lk == 'on' or lk == inout then | ||
number_id = make_link(engscale.link, | number_id = make_link(engscale.link, engscale[1]) | ||
else | else | ||
number_id = | number_id = engscale[1] | ||
end | end | ||
-- WP:NUMERAL recommends " " in values like "12 million". | -- WP:NUMERAL recommends " " in values like "12 million". | ||
Line 3,282: | Line 3,233: | ||
-- For simplicity and because more not needed, handle one range item only. | -- For simplicity and because more not needed, handle one range item only. | ||
local prefix2 = make_id(parms, 2, first_unit) .. ' ' | local prefix2 = make_id(parms, 2, first_unit) .. ' ' | ||
result = range_text(range[1], want_name, parms, result, prefix2 .. valinfo[2].show, 'in' | result = range_text(range[1], want_name, parms, result, prefix2 .. valinfo[2].show, 'in') | ||
end | end | ||
return preunit .. result | return preunit .. result | ||
Line 3,324: | Line 3,275: | ||
if range then | if range then | ||
for i = 0, range.n do | for i = 0, range.n do | ||
local | local number_word | ||
if i == range.n then | if i == range.n then | ||
add_unit = false | add_unit = false | ||
number_word = true | |||
end | end | ||
decorate_value(parms, first_unit, i+1, | decorate_value(parms, first_unit, i+1, number_word) | ||
local show = valinfo[i+1].show | local show = valinfo[i+1].show | ||
if add_unit then | if add_unit then | ||
Line 3,372: | Line 3,323: | ||
if range then | if range then | ||
-- For simplicity and because more not needed, handle one range item only. | -- 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, inout | result = range_text(range[1], want_name, parms, result, prefix .. valinfo[2].show, inout) | ||
end | end | ||
return preunit .. result | return preunit .. result | ||
Line 3,392: | Line 3,343: | ||
if range then | if range then | ||
for i = 0, range.n do | for i = 0, range.n do | ||
local | local number_word | ||
if i == range.n then | if i == range.n then | ||
add_unit = false | add_unit = false | ||
number_word = true | |||
end | end | ||
decorate_value(parms, out_current, i+1, | decorate_value(parms, out_current, i+1, number_word) | ||
local show = valinfo[i+1].show | local show = valinfo[i+1].show | ||
if add_unit then | if add_unit then | ||
Line 3,578: | Line 3,529: | ||
local success, result2 = make_result(valinfo[i+1]) | local success, result2 = make_result(valinfo[i+1]) | ||
if not success then return false, result2 end | if not success then return false, result2 end | ||
result = range_text(range[i], want_name, parms, result, result2, inout | result = range_text(range[i], want_name, parms, result, result2, inout) | ||
end | end | ||
end | end | ||
Line 3,592: | Line 3,543: | ||
local out_unit = parms.out_unit | local out_unit = parms.out_unit | ||
if out_unit == nil or out_unit == '' or type(out_unit) == 'function' then | if out_unit == nil or out_unit == '' or type(out_unit) == 'function' then | ||
if bad_input_mcode or parms.opt_input_unit_only then | if bad_input_mcode or parms.opt_input_unit_only then | ||
bad_output = '' | bad_output = '' | ||
Line 3,709: | Line 3,659: | ||
wikitext = wikitext .. parms.warnings | wikitext = wikitext .. parms.warnings | ||
end | end | ||
return true, | return true, wikitext, out_unit_table | ||
end | end | ||
Line 3,763: | Line 3,713: | ||
-- (a table with two strings) to make an SI unit | -- (a table with two strings) to make an SI unit | ||
-- that will be used for the look up | -- that will be used for the look up | ||
-- link = true if result should be | -- link = true if result should be linked | ||
-- sort = 'on' or 'debug' if result should include a sort key in a | -- sort = 'on' or 'debug' if result should include a sort key in a | ||
-- span element ('debug' makes the key visible) | -- span element ('debug' makes the key visible) | ||
Line 3,772: | Line 3,722: | ||
-- text = requested symbol or name of unit, optionally linked | -- text = requested symbol or name of unit, optionally linked | ||
-- scaled_value = input value adjusted by unit scale; used for sort key | -- scaled_value = input value adjusted by unit scale; used for sort key | ||
-- sortspan = span element with sort key like that provided by | -- sortspan = span element with sort key like that provided by ntsh, | ||
-- calculated from the result of converting value | -- calculated from the result of converting value | ||
-- to a base unit with scale 1. | -- to a base unit with scale 1. |