Module:Convert:修订间差异
删除的内容 添加的内容
enwiki>Johnuniq update from sandbox per Template talk:Convert#Module version 33 |
CaelLab-import(留言 | 贡献) 导入自中文维基百科 |
||
第259行:
end
local function wanted_category(
-- Return
-- This is so tracking categories only include pages that need correction.
local title = mw.title.getCurrentTitle()
if title then
第269行 ⟶ 第268行:
for _, v in ipairs(split(config.nscat or nsdefault, ',')) do
if namespace == tonumber(v) then
return cat
end
end
end
end
local function message(parms, mcode
-- Return wikitext for an error message, including category if specified
-- for the message type.
第347行 ⟶ 第341行:
return success and (revid == '')
end
local title = string.gsub(msg[1] or 'Missing message', '$%d+', parts)
local text =
local cat = wanted_category(text_code.all_categories[msg[3]
local anchor = msg[4] or ''
local fmtkey = ispreview() and 'cvt_format_preview' or (msg.format or 'cvt_format')
local fmt = text_code.all_messages[fmtkey] or 'convert: bug'
return subparm(fmt, title:gsub('"', '"'), text, cat, anchor)
第364行 ⟶ 第354行:
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.
-- To reduce output noise, only the first warning is displayed.
if
if
if parms.warnings ==
parms.warnings = message(parms, { key, text1, text2 })
end
end
end
第389行 ⟶ 第380行:
success, speller = pcall(get_speller, spell_module)
if not success or type(speller) ~= 'function' then
add_warning(parms, 1, 'cvt_no_spell
return nil
end
第411行 ⟶ 第402行:
-- If no altitude given, use default (zero altitude = sea level).
-- Table gives speed of sound in miles per hour at various altitudes:
-- altitude = -17,499 to
-- mach_table[a + 4] = s where
-- a = (altitude / 5000) rounded to nearest integer (-3 to
-- s = speed of sound (mph) at that altitude
-- LATER: Should calculate result from an interpolation between the next
-- lower and higher altitudes in table, rather than rounding to nearest.
-- From: http://www.aerospaceweb.org/question/atmosphere/q0112.shtml
local mach_table = { -- a =
第421行 ⟶ 第414行:
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
737.3, 737.7, 737.7, 736.2, 730.5, 724.6, 718.8, 712.9, 707.0, 701.
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
}
altitude = altitude or 0
local a = (altitude < 0) and -altitude or altitude
a = floor(a / 5000 + 0.5)
if altitude < 0 then
a = -a
end
if a < -3 then
a = -3
elseif a > 60 then
a = 60
end
return mach_table[a + 4] * 0.44704 -- mph converted to m/s
end
-- END: Code required only for built-in units.
------------------------------------------------------------------------
local function get_range(word)
第571行 ⟶ 第535行:
if key == 'symbol' then
value = self.si_prefix .. self._symbol
elseif key == 'sym_us' then
value = rawget(self, '_sym_us')
第618行 ⟶ 第581行:
-- 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
__index = function (self, key)
local value
第734行 ⟶ 第696行:
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
local multiplier = t.multiplier
if multiplier then
第790行 ⟶ 第749行:
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.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
第799行 ⟶ 第780行:
local err_is_fatal
local combo = collection()
if
err_is_fatal = true
for item in (unitcode .. '+'):gmatch('%s*(.-)%s*%+') do
第836行 ⟶ 第817行:
if success or err_is_fatal then
return success, result
end
end
第873行 ⟶ 第831行:
end
end
if not parms.
-- Want the "what links here" list for the extra_module to show only cases
-- where an extra unit is used, so do not require it
-- or if looking up a range word which cannot be a unit.
if not extra_units then
第1,152行 ⟶ 第1,110行:
local fracfmt = {
{ -- Like {{frac}} (fraction slash).
-- 1/2 : sign, numerator, denominator
-- 1+2/3 : signed_wholenumber, numerator, denominator
'<span class="frac nowrap">%s<sup>%s</sup>⁄<sub>%s</sub></span>',
'<span class="frac nowrap">%s<sup>%s</sup>⁄<sub>%s</sub></span>',
},
{ -- Like {{sfrac}} (
-- 1//2 : sign, numerator, denominator (sign should probably be before the fraction, but then it can wrap, and html is already too long)
-- 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 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>',
},
}
第1,173行 ⟶ 第1,133行:
wholestr = nil
end
if wholestr then
local decorated = with_separator(parms, wholestr)
if negative then
decorated = MINUS .. decorated
end
local fmt = fracfmt[style][2]
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 negative then
第1,188行 ⟶ 第1,152行:
end
end
end
return wikitext
end
第1,412行 ⟶ 第1,372行:
-- Before processing, the input text is cleaned:
-- * Any thousand separators (valid or not) are removed.
-- * Any sign
-- '-' (if negative) or '' (otherwise).
-- That replaces Unicode minus with '-'.
-- If successful, the returned info table contains named fields:
第1,472行 ⟶ 第1,433行:
local valstr
for _, prefix in ipairs({ '-', MINUS, '−' }) do
-- Including '-'
-- 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)
break
end
第1,561行 ⟶ 第1,520行:
-- v = value of text (text is a number)
-- f = true if value is an integer
-- Input can use en digits or digits in local language
-- but no separators, no Unicode minus, and no fraction.
if text then
local number = tonumber(to_en(text))
第1,644行 ⟶ 第1,603行:
-- 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
local function withspace(text,
--
-- However, no space is
-- or '-' at that position ('-' is for adjectival text).
local current = text:sub(i, i)
if current == ' ' or current == '-' then
end
if
else
current = text:sub(-6, -1)
end
if
end
if i == 1 then
return ' ' .. text
end
return text .. ' '
end
preunit1 = preunit1 or ''
local trim1 = strip(preunit1)
第1,673行 ⟶ 第1,631行:
return nil
end
return withspace(withspace(preunit1, 1), -1)
end
preunit2 = preunit2 or ''
local trim2 = strip(preunit2)
if trim1 == '
return nil, nil
end
if trim1 ~= '+' then
preunit1 =
end
if trim2 == ' ' then -- trick to make preunit2 empty
elseif trim2 == '' then
preunit2 = preunit1
elseif trim2
preunit2 =
end
return preunit1, preunit2
end
local function range_text(range, want_name, parms, before, after, inout
-- Return before .. rtext .. after
-- where rtext is the text that separates two values in a range.
local rtext, adj_text, exception
if type(range) == 'table' then
-- Table must specify range text for ('off' and 'on') or ('input' and 'output'),
第1,723行 ⟶ 第1,671行:
end
end
if rtext == '–' and
rtext = ' – '
end
第1,809行 ⟶ 第1,757行:
kv_pairs.sing = nil
end
for loc_name, loc_value in pairs(kv_pairs) do
local en_name = text_code.en_option_name[loc_name]
if en_name then
local en_value
if
if loc_value == '' then
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
local minimum
local number, is_integer = get_number(loc_value)
if en_name == '
minimum = 2
if number and number < 0 then
第1,830行 ⟶ 第1,777行:
end
else
minimum =
end
if number and is_integer and number >= minimum then
en_value = number
else
add_warning(parms, 1, (en_name == 'frac' and 'cvt_bad_frac' or 'cvt_bad_sigfig'), loc_value)
end
end
elseif
en_value = loc_value ~= '' and loc_value or nil -- accept non-empty user text with no validation
if
-- 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.
parms.input_text = loc_value -- keep
end
else
en_value =
if en_value and en_value:sub(-1) == '?' then
en_value = en_value:sub(1, -2)
第2,096行 ⟶ 第2,025行:
-- 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
return false, clean
end
第2,116行 ⟶ 第2,046行:
end
local function wikidata_call(
-- Return true, s where s is the result of a Wikidata operation,
-- or return false, t where t is an error message table.
第2,127行 ⟶ 第2,057行:
if success then
return status, result
end
return false, { 'cvt_wd_fail' }
第2,162行 ⟶ 第2,088行:
return lookup({}, ucode, 'no_combination')
end
return wikidata_call(
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(
if not success then return false, msg end
end
第2,216行 ⟶ 第2,142行:
end
if in_unit_table.builtin == 'mach' then
-- 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
i = i + 1
第2,245行 ⟶ 第2,167行:
end
end
if
parms.out_unit =
if set_precision(strip(parms[i])) then
i = i + 1
第2,297行 ⟶ 第2,219行:
else
parms.precision = precision
end
return true, in_unit_table
第2,452行 ⟶ 第2,367行:
end
if in_builtin == 'mach' or out_builtin == 'mach' then
local adjust
if in_builtin == 'mach' then
inscale =
else
outscale = speed_of_sound(out_current.altitude)
adjust = 0.1 / inscale
end
return true, {
outvalue = invalue * (inscale / outscale),
第2,584行 ⟶ 第2,493行:
end
local sortspan
if sortkey and (parms.opt_sortable_debug or not parms.table_align) then
sortspan = parms.opt_sortable_debug and
'
'<span
parms.join_before = sortspan
end
if parms.table_align then
local style = 'style="text-align:' .. parms.table_align .. ';'
local sort = sortkey and ' data-sort-value="' .. sortkey .. '"' or ''
local joins = {}
for i = 1, 2 do
第2,665行 ⟶ 第2,566行:
show = format('%.0f', floor((outvalue / n) + 0.5) * n)
end
else
local inclean = info.clean
第2,936行 ⟶ 第2,834行:
end
local function variable_name(clean, unit_table
--
-- 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,
-- replacing '#' if found, or before the field otherwise.
local vname
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
i = 3
end
vname = split(unit_table.varname, '!')[i]
end
local si_name = rawget(unit_table, 'si_name') or ''
local pos = vname:find('#', 1, true)
第2,972行 ⟶ 第2,872行:
vname = si_name .. vname
end
return vname
end
return
end
local function linked_id(parms, unit_table, key_id, want_link, clean
-- 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
local abbr_on = (key_id == 'symbol' or key_id == 'sym_us')
if abbr_on and want_link then
第2,990行 ⟶ 第2,891行:
local per = unit_table.per
if per then
local unit1 = per[1] -- top unit_table, or nil
local unit2 = per[2] -- bottom unit_table
第3,002行 ⟶ 第2,902行:
return symbol -- for exceptions that have the symbol built-in
end
end
end
第3,014行 ⟶ 第2,911行:
else
key_id2 = key_id
end
local result
第3,029行 ⟶ 第2,923行:
end
if want_link and unit_table.link then
if
result = (unit1 and linked_id(parms, unit1, key_id, false, clean) or '') .. result .. linked_id(parms, unit2, key_id2, false, '1')
else
result = (unit1 and variable_name(clean, unit1) or '') .. result .. variable_name('1', unit2)
end
if omit_separator(result) then
第3,047行 ⟶ 第2,941行:
unit_table.sep = ''
end
return result
end
if multiplier then
第3,065行 ⟶ 第2,959行:
multiplier = ''
end
local id = unit_table.fixed_name or ((varname and not abbr_on) and variable_name(clean, unit_table
if omit_separator(id) then
unit_table.sep = ''
第3,126行 ⟶ 第3,020行:
local inout = unit_table.inout
local info = unit_table.valinfo[which]
local abbr_org = parms.abbr_org
local adjectival = parms.opt_adjectival
local lk = parms.lk
local want_link = (lk == 'on' or lk == inout)
local usename = unit_table.usename
local singular = info.singular
local want_name
if usename then
want_name = true
else
if
if parms.wantname then
want_name = true
第3,176行 ⟶ 第3,072行:
-- engscale: so "|1|e3kg" gives "1 thousand kilograms" (plural)
singular = false
end
key = (
if parms.opt_sp_us then
key = key .. '_us'
第3,191行 ⟶ 第3,086行:
key = parms.opt_sp_us and 'sym_us' or 'symbol'
end
return linked_id(parms, unit_table, key, want_link, info.clean
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".
local info
local engscale = unit_table.engscale
第3,214行 ⟶ 第3,104行:
info.decorated = true
if engscale then
local inout = unit_table.inout
local abbr = parms.abbr
if (abbr == 'on' or abbr == inout) and not
info.show = info.show ..
elseif
local number_id
local lk = parms.lk
if
number_id = make_link(engscale.link,
else
number_id =
end
-- WP:NUMERAL recommends " " in values like "12 million".
第3,279行 ⟶ 第3,168行:
return preunit .. id1
end
if parms.opt_also_symbol and not composite
local join1 = parms.joins[1]
if join1 == ' (' or join1 == ' [' then
第3,293行 ⟶ 第3,182行:
-- For simplicity and because more not needed, handle one range item only.
local prefix2 = make_id(parms, 2, first_unit) .. ' '
result = range_text(range[1], want_name, parms, result, prefix2 .. valinfo[2].show, 'in'
end
return preunit .. result
第3,335行 ⟶ 第3,224行:
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
第3,383行 ⟶ 第3,272行:
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, inout
end
return preunit .. result
第3,403行 ⟶ 第3,292行:
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
第3,589行 ⟶ 第3,478行:
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, inout
end
end
第3,602行 ⟶ 第3,491行:
local bad_input_mcode = in_unit_table.bad_mcode -- nil if input unit is a valid convert unit
local out_unit = parms.out_unit
if out_unit == nil or out_unit == '
if bad_input_mcode or parms.opt_input_unit_only then
bad_output = ''
else
success, out_unit = get_default(in_unit_table.valinfo[1].value, in_unit_table)
parms.out_unit = out_unit
if not success then
第3,720行 ⟶ 第3,607行:
wikitext = wikitext .. parms.warnings
end
return true,
end
local function
-- Do convert, and if needed, do it again with higher default precision.
local parms = { frame = frame
set_config(
local success, result = get_parms(parms,
if success then
if type(result) ~= 'table' then
第3,747行 ⟶ 第3,634行:
-- 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
if
if
return
end
return result
end
return parms.input_text and parms.input_text or message(parms, result)
end
第3,808行 ⟶ 第3,678行:
lk = options.link and 'on' or nil,
opt_sp_us = options.us and true or nil,
opt_ignore_error = true, -- do not add pages using this function to 'what links here' for Module:Convert/extra
opt_sortable_on = options.sort == 'on' or options.sort == 'debug',
opt_sortable_debug = options.sort == 'debug',
第3,857行 ⟶ 第3,726行:
end
return { convert = main_convert
| |||