মডিউল:Convert: সংশোধিত সংস্করণের মধ্যে পার্থক্য

বিষয়বস্তু বিয়োগ হয়েছে বিষয়বস্তু যোগ হয়েছে
হালনাগাদ করা হয়েছে
আলোচনা ছাড়া এটি হালনাগাদ করবেন না
১৫ নং লাইন:
-- Conversion data and message text are defined in separate modules.
local config, maxsigfig
local numdot -- must be '.' or ',' or a character which works in a regex as used here
local numsep, numsep_remove, numsep_remove2
local data_codedefault_exceptions, link_exceptions, all_units
local text_code
local varname -- can be a code to use variable names that depend on value
২৩ নং লাইন:
local to_en_table -- to translate an input string of digits in local language to en
-- Use translation_table in convert/text to change the following.
local en_default -- true uses lang=en unless convert has lang=local or local digits
local group_method = 3 -- code for how many digits are in a group
local per_word = 'per' -- for units like "liters per kilometer"
local plural_suffix = 's' -- only other useful value is probably '' to disable plural unit names
local omitsep -- true to omit separator before local symbol/name
 
-- All units should be defined in the data module. However, to cater for quick changes
৩৫ ⟶ ৩৩ নং লাইন:
local extra_module -- name of module with extra units
local extra_units -- nil or table of extra units from extra_module
 
-- Some options in the invoking template can set variables used later in the module.
local currency_text -- for a user-defined currency symbol: {{convert|12|$/ha|$=€}} (euro replaces dollar)
 
local function from_en(text)
৫৮ ⟶ ৫৩ নং লাইন:
-- and no separators (they have to be removed here to handle cases like
-- numsep = '.' and numdot = ',' with input "1.234.567,8").
if to_en_tablenumsep_remove ~= '' then
text = ustring.gsub(text, '%d', to_en_table)
end
if numsep_remove then
text = text:gsub(numsep_remove, '')
end
if numsep_remove2 then
text = text:gsub(numsep_remove2, '')
end
if numdot ~= '.' then
text = text:gsub(numdot, '.')
end
return text
end
 
local function decimal_mark(text)
-- Return ',' if text probably is using comma for decimal mark, or has no decimal mark.
-- Return '.' if text probably is using dot for decimal mark.
-- Otherwise return nothing (decimal mark not known).
if not text:find('[.,]') then return ',' end
text = text:gsub('^%-', ''):gsub('%+%d+/%d+$', ''):gsub('[Ee]%-?%d+$', '')
local decimal =
text:match('^0?([.,])%d+$') or
text:match('%d([.,])%d?%d?$') or
text:match('%d([.,])%d%d%d%d+$')
if decimal then return decimal end
if text:match('%.%d+%.') then return ',' end
if text:match('%,%d+,') then return '.' end
end
 
local add_warning, with_separator -- forward declarations
local function to_en_with_check(text, parms)
-- Version of to_en() for a wiki using numdot = ',' and numsep = '.' to check
-- text (an input number as a string) which might have been copied from enwiki.
-- For example, in '1.234' the '.' could be a decimal mark or a group separator.
-- From viwiki.
if to_en_table then
text = ustring.gsub(text, '%d', to_en_table)
end
if decimal_mark(text) == '.' then
local original = text
text = text:gsub(',', '') -- for example, interpret "1,234.5" as an enwiki value
if parms then
add_warning(parms, 0, 'cvt_enwiki_num', original, with_separator({}, text))
end
else
if numsep_remove then
text = text:gsub(numsep_remove, '')
end
if numsep_remove2 then
text = text:gsub(numsep_remove2, '')
end
if numdot ~= '.' then
text = text:gsub(numdot, '.')
end
end
return text
end
 
local function omit_separator(id)
-- Return true if there should be no separator before id (a unit symbol or name).
-- For zhwiki, there should be no separator if id uses local characters.
-- The following kludge should be a sufficient test.
if omitsep then
if id:sub(1, 2) == '-{' then -- for "-{...}-" content language variant
return true
end
if id:byte() > 127 then
local first = usub(id, 1, 1)
if first ~= 'Å' and first ~= '°' and first ~= 'µ' then
return true
end
end
end
return id:sub(1, 1) == '/' -- no separator before units like "/ha"
end
 
১৩৮ ⟶ ৬৮ নং লাইন:
local speller -- function from that module to handle spelling (set if spelling is wanted)
 
local function set_config(argsframe)
-- Set configuration options from template #invoke or defaults.
config = frame.args
maxsigfig = config.maxsigfig or 14 -- maximum number of significant figures
-- Scribunto sets the global variable 'mw'.
local data_module, text_module
-- A testing program can set the global variable 'is_test_run'.
local sandbox = config.sandbox and ('/' .. config.sandbox) or ''
local data_module, = "Module:Convert/data" ..text_module, sandboxdata_code
if is_test_run then
text_module = "Module:Convert/text" .. sandbox
local langcode = mw.language.getContentLanguage().code
extra_module = "Module:Convert/extra" .. sandbox
data_module = "convertdata-" .. langcode
spell_module = "Module:ConvertNumeric"
text_module = "converttext-" .. langcode
extra_module = "convertextra-" .. langcode
spell_module = "ConvertNumeric"
else
local sandbox = config.sandbox and ('/' .. config.sandbox) or ''
data_module = "Module:Convert/data" .. sandbox
text_module = "Module:Convert/text" .. sandbox
extra_module = "Module:Convert/extra" .. sandbox
spell_module = "Module:ConvertNumeric"
end
data_code = mw.loadData(data_module)
text_code = mw.loadData(text_module)
default_exceptions = data_code.default_exceptions
link_exceptions = data_code.link_exceptions
all_units = data_code.all_units
local translation = text_code.translation_table
১৫৫ ⟶ ৯৭ নং লাইন:
numdot = translation.numdot
numsep = translation.numsep
if numdot == ',' and numsep == '.' then
if text_code.all_messages.cvt_enwiki_num then
to_en = to_en_with_check
end
end
if translation.group then
group_method = translation.group
১৮৬ ⟶ ১২৩ নং লাইন:
to_en_table = translation.to_en
end
if translation.lang == 'en default' then
en_default = true -- for hiwiki
end
omitsep = translation.omitsep -- for zhwiki
end
numdot = config.numdot or numdot or '.' -- decimal mark before fractional digits
১৯৫ ⟶ ১২৮ নং লাইন:
-- numsep should be ',' or '.' or '' or ' ' or a Unicode character.
-- numsep_remove must work in a regex to identify separators to be removed.
ifnumsep_remove = (numsep ~== '.') thenand '%.' or numsep
numsep_remove = (numsep == '.') and '%.' or numsep
end
if numsep ~= ',' and numdot ~= ',' then
numsep_remove2 = ',' -- so numbers copied from enwiki will work
end
end
 
২৪২ ⟶ ১৭০ নং লাইন:
if type(text) == 'string' then
return text:match("^%s*(.-)%s*$")
end
end
 
local function table_len(t)
-- Return length (<100) of a numbered table to replace #t which is
-- documented to not work if t is accessed via mw.loadData().
for i = 1, 100 do
if t[i] == nil then
return i - 1
end
end
end
২৯২ ⟶ ২১০ নং লাইন:
end
-- Escape user input so it does not break the message.
-- To avoid reference tags (like {{convert|1<mathref>23xyz</mathref>|m}}) breakingor other tags
-- breaking the mouseover title, any strip marker starting with char(127) is
-- replaced with escaped '<ref>...</ref>' or '...' (text not needing i18n).
local append = ''
local pos = s:find(string.char(127), 1, true)
if pos then
if s:find('-ref-', 1, true) then
append = '...'
append = '&lt;ref&gt;...&lt;/ref&gt;'
else
append = '...'
end
s = s:sub(1, pos - 1)
end
if limit and ulen(s) > limit then
s = usub(s, 1, limit)
if append == '...' then
append = '...'
end
end
s = nowiki(s) .. (append or '')
else
s = '?'
৩১৫ ⟶ ২৩৯ নং লাইন:
local cat = wanted_category(text_code.all_categories[msg[3]]) or ''
local anchor = msg[4] or ''
local fmt = text_code.all_messages[msg.format or 'cvt_format'] or 'convert: bug'
title = title:gsub('"', '&quot;')
return format(fmt, anchor, title, text, cat)
৩২২ ⟶ ২৪৬ নং লাইন:
end
 
local function add_warning(parms, level, keymcode, text1, text2text) -- 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 config.warnings or level < 0 then
if level <= (tonumber(config.warnings) or 1) then
if parms.warnings == nil then
parms.warnings = message({ keymcode, text1, text2text })
end
end
৫৪৮ ⟶ ৪৭২ নং লাইন:
 
local unit_per_mt = {
-- Metatable to get values for a "per" unit of form "x/y".
-- 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
৫৭৬ ⟶ ৪৯৯ নং লাইন:
}
 
local function make_perlookup(unit_tableunitcode, ulookupopt_sp_us, what, utable, fails, depth)
-- 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 = { 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
local prefix
for i, v in ipairs(unit_table.per) do
if i == 1 and v == '' then
-- First unit symbol can be empty; that gives a nil first unit table.
elseif i == 1 and text_code.currency[v] then
prefix = currency_text or v
else
local success, t = ulookup(v)
if not success then return false, t end
result.per[i] = t
end
end
local multiplier = unit_table.multiplier
if not result.utype then
-- Creating an automatic per unit.
local unit1 = result.per[1]
local utype = (unit1 and unit1.utype or prefix or '') .. '/' .. result.per[2].utype
local t = data_code.per_unit_fixups[utype]
if t then
if type(t) == 'table' then
utype = t.utype or utype
result.link = result.link or t.link
multiplier = multiplier or t.multiplier
else
utype = t
end
end
result.utype = utype
end
result.scalemultiplier = multiplier or 1
result.vprefix = prefix or false -- set to non-nil to avoid calling __index
return true, setmetatable(result, unit_per_mt)
end
 
local function lookup(parms, unitcode, what, utable, fails, depth)
-- Return true, t where t is a copy of the unit's converter table,
-- or return false, t where t is an error message table.
-- Parameter opt_sp_us is true for US spelling of SI prefixes and
-- the symbol and name of the unit. If true, the result includes field
-- sp_us = true (that field may also have been in the unit definition).
-- Parameter 'what' determines whether combination units are accepted:
-- 'no_combination' : single unit only
৬৫০ ⟶ ৫৩৬ নং লাইন:
return false, { 'cvt_should_be', t.shouldbe }
end
local force_sp_us = opt_sp_us
if t.sp_us then
parms.opt_sp_usforce_sp_us = true
opt_sp_us = true
end
local target = t.target -- nil, or unitcode is an alias for this target
if target then
local success, result = lookup(parmstarget, targetopt_sp_us, what, utable, fails, depth)
if not success then return false, result end
override_from(result, t, { 'customary', 'default', 'link', 'symbol', 'symlink' })
৬৬৫ ⟶ ৫৫৩ নং লাইন:
return true, result
end
local per = t.per -- nil/false, or a numbered table for "x/y" units
if t.per then
if per then
return make_per(t, function (ucode) return lookup(parms, ucode, 'no_combination', utable, fails, depth) end)
local result = { utype = t.utype, per = {} }
result.scalemultiplier = t.multiplier or 1
override_from(result, t, { 'invert', 'iscomplex', 'default', 'link', 'symbol', 'symlink' })
result.symbol_raw = (result.symbol or false) -- to distinguish between a defined exception and a metatable calculation
local cvt = result.per
local prefix
for i, v in ipairs(per) do
if i == 1 and text_code.currency[v] then
prefix = v
else
local success, t = lookup(v, opt_sp_us, 'no_combination', utable, fails, depth)
if not success then return false, t end
cvt[i] = t
if t.sp_us then -- if the top or bottom unit forces sp=us, set the per unit to use the correct name/symbol
force_sp_us = true
end
end
end
if prefix then
result.vprefix = prefix
else
result.vprefix = false -- to avoid calling __index
end
result.sp_us = force_sp_us
return true, setmetatable(result, unit_per_mt)
end
local combo = t.combination -- nil or a table of unitcodes
৬৭৯ ⟶ ৫৯২ নং লাইন:
local cvt = result.combination
for i, v in ipairs(combo) do
local success, t = lookup(parmsv, vopt_sp_us, multiple and 'no_combination' or 'only_multiple', utable, fails, depth)
if not success then return false, t end
cvt[i] = t
৬৮৬ ⟶ ৫৯৯ নং লাইন:
end
local result = shallow_copy(t)
result.sp_us = force_sp_us
if result.prefixes then
result.si_name = ''
৭০৪ ⟶ ৬১৮ নং লাইন:
if t and t.prefixes then
local result = shallow_copy(t)
result.si_nameif = parms.opt_sp_us and si.name_us or si.namethen
result.sp_us = true
end
if result.sp_us and si.name_us then
result.si_name = si.name_us
else
result.si_name = si.name
end
result.si_prefix = si.prefix or prefix
result.scale = t.scale * 10 ^ (si.exponent * t.prefixes)
৭২২ ⟶ ৬৪৩ নং লাইন:
local engscale = text_code.eng_scales[exponent]
if engscale then
local success, result = lookup(parmsbaseunit, baseunitopt_sp_us, 'no_combination', utable, fails, depth)
if not success andthen return false, result end
if not (result.offset or result.builtin or result.engscale) then
result.defkey = unitcode -- key to lookup default exception
result.engscale = engscale
৭৫৮ ⟶ ৬৮০ নং লাইন:
local cvt = result.combination
for i, v in ipairs(combo) do
local success, t = lookup(parmsv, vopt_sp_us, 'only_multiple', utable, fails, depth)
if not success then return false, t end
if i == 1 then
৭৭৭ ⟶ ৬৯৯ নং লাইন:
end
end
if not get_range(unitcode) then -- do not require extra if looking up a range word which cannot be a unit
-- Look for x/y; split on right-most slash to get scale correct (x/y/z is x/y per z).
local top, bottom = unitcode:match('^(.-)/([^/]+)$')
if top and not unitcode:find('e%d') then
-- If valid, create an automatic per unit for an "x/y" unit code.
-- The unitcode must not include extraneous spaces.
-- 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_per({ per = {top, bottom} }, function (ucode) return lookup(parms, ucode, 'no_combination', utable, fails, depth) end)
if success then
return true, result
end
end
if not parms.opt_ignore_error and not get_range(unitcode) then
-- 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 if invoked from {{val}}
-- or if looking up a range word which cannot be a unit.
if not extra_units then
local success, extra = pcall(function () return require(extra_module).extra_units end)
৮০৫ ⟶ ৭১২ নং লাইন:
fails[unitcode] = true
local other = (utable == all_units) and extra_units or all_units
local success, result = lookup(parmsunitcode, unitcodeopt_sp_us, what, other, fails, depth)
if success then
return true, result
৮১৬ ⟶ ৭২৩ নং লাইন:
local en_code = ustring.gsub(unitcode, '%d', to_en_table)
if en_code ~= unitcode then
return lookup(parmsen_code, en_codeopt_sp_us, what, utable, fails, depth)
end
end
৮৩০ ⟶ ৭৩৭ নং লাইন:
return true
end
end
 
local function ntsh(num, debug)
-- Return html text to be used for a hidden sort key so that
-- the given number will be sorted in numeric order.
-- If debug == true, output is in a box (not hidden).
-- This implements Template:Ntsh (number table sorting, hidden).
local result, style
if not valid_number(num) then
if num < 0 then
result = '1000000000000000000'
else
result = '9000000000000000000'
end
elseif num == 0 then
result = '5000000000000000000'
else
local mag = floor(log10(abs(num)) + 1e-14)
local prefix
if num > 0 then
prefix = 7000 + mag
else
prefix = 2999 - mag
num = num + 10^(mag+1)
end
result = format('%d', prefix) .. format('%015.0f', floor(num * 10^(14-mag)))
end
if debug then
style = 'border:1px solid'
else
style = 'display:none'
end
return '<span style="' .. style .. '">' .. result .. '</span>'
end
 
৮৬০ ⟶ ৮০০ নং লাইন:
return name:sub(1, pos+1) .. name:sub(pos+2):gsub(' ', '-')
end
elseif name:sub(-1, -1) == ')' then
pos = name:find('(', 1, true)
if pos then
৯২৯ ⟶ ৮৬৯ নং লাইন:
end
 
local function digit_groupsdigit_grouper(parmsmethod, text, methodgaps)
-- Return a numbered table ofto hold groups of digits (left-to-right,which can inbe localjoined language).with
-- suitable separators (such as commas).
-- Each group is separately translated to the local language because
-- gap separators include digits which should not be translated.
-- Parameter method is a number or nil:
-- 3 for 3-digit grouping (default), or
-- 2 for 3-then-2 grouping (only for digits before decimal mark).
-- Parameter gaps is true to use <span> gaps (numsep ignored).
local len_right
return {
local len_left = text:find('.', 1, true)
n = 0,
if len_left then
add = function (self, digits)
len_right = #text - len_left
len_left self.n = len_leftself.n -+ 1
self[self.n] = from_en(digits)
else
end,
len_left = #text
join = function (self, rhs)
end
-- Concatenate in reverse order.
local twos = method == 2 and len_left > 5
if gaps then
local groups = collection()
local runresult = len_left''
for i = 1, self.n - 1 do
local n
result = '<span style="margin-left: 0.25em">' .. self[i] .. '</span>' .. result
if run < 4 or (run == 4 and parms.opt_comma5) then
end
if parms.opt_gaps then
return '<span style="white-space: nowrap">' .. self[self.n] .. result .. from_en(rhs) .. '</span>'
n = run
else
n local result = #textself[1]
for i = 2, self.n do
end
result = self[i] .. numsep .. result
elseif twos then
n = run % 2 == 0 and 1 or 2
else
n = run % 3 == 0 and 3 or run % 3
end
while run > 0 do
groups:add(n)
run = run - n
n = (twos and run > 3) and 2 or 3
end
if len_right then
if groups.n == 0 then
groups:add(0)
end
if parms.opt_gaps and len_right > 3 then
local want4 = not parms.opt_gaps3 -- true gives no gap before trailing single digit
local isfirst = true
run = len_right
while run > 0 do
n = (want4 and run == 4) and 4 or (run > 3 and 3 or run)
if isfirst then
isfirst = false
groups[groups.n] = groups[groups.n] + 1 + n
else
groups:add(n)
end
runreturn =result run.. - nfrom_en(rhs)
end
elseend,
step = 3,
groups[groups.n] = groups[groups.n] + 1 + len_right
next_position = function (self, previous)
end
-- Return position of digit just before next group.
end
-- Digits are grouped from right-to-left (least significant first).
local pos = 1
local result = previous - self.step
for i, length in ipairs(groups) do
if method == 2 then
groups[i] = from_en(text:sub(pos, pos + length - 1))
self.step = 2
pos = pos + length
end
return (result < 0) and 0 or result
return groups
end,
}
end
 
local function with_separator(parms, text) -- for forward declaration above
-- Input text is a number in en digits withand optional '.' decimal mark.
-- Return an equivalent of text, formatted for display:
-- with a custom decimal mark instead of '.', if wanted
-- with thousand separators inserted, if wanted
-- digits in local language
-- The given text is like '123' or '12312345.6789' or '123451.678923e45'.
-- (e notation can only occur when processing an input value).
-- The text has no sign (caller inserts that later, if necessary).
-- WhenSeparator usingis gaps,inserted theyonly arein insertedthe beforeinteger andpart afterof the decimal mark.significand
-- Separators(not are inserted only beforeafter the decimal mark, and not after 'e' or 'E').
if #text < 4 or parms.opt_nocomma or numsep == '' then
return from_en(text)
end
local last = text:match('()[.eE]') -- () returns position
local groups = digit_groups(parms, text, group_method)
if parms.opt_gapslast == nil then
last = #text
if groups.n <= 1 then
else
return groups[1] or ''
last = last - 1 -- index of last character before dot/e/E
end
end
local nowrap = '<span style="white-space: nowrap">'
if last < 4 or (last == 4 and parms.opt_comma5) then
local gap = '<span style="margin-left: 0.25em">'
return from_en(text)
local close = '</span>'
end
return nowrap .. groups[1] .. gap .. table.concat(groups, close .. gap, 2, groups.n) .. close .. close
local groups = digit_grouper(group_method, parms.opt_gaps)
local i = last
while i > 0 do
local position = groups:next_position(i)
groups:add(text:sub(position+1, i))
i = position
end
return table.concat(groups, numsep:join(text:sub(last+1))
end
 
-- AnInput inputvalues valuecan use values like 1.23e12, isbut displayedare usingnever scientific notation (1.23×10¹²).displayed
-- That also makes the output useusing scientific notation, except for smalllike values1.23×10¹².
-- In addition, veryVery small or very large output values use scientific notation.
-- Use format(fmtpower, significand, '10', exponent) where each argumentarg is a string.
local fmtpower = '%s<span style="margin:0 .15em 0 .25em">×</span>%s<sup>%s</sup>'
 
local function with_exponent(parms, show, exponent)
-- Return wikitext to display the implied value in scientific notation.
-- Input uses en digits; output uses digits in local language.
if #show > 1 then
return format(fmtpower, with_separator(parms, show), from_en('10'), use_minus(from_en(tostring(exponent))))
show = show:sub(1, 1) .. '.' .. show:sub(2)
end
return format(fmtpower, from_en(show), from_en('10'), use_minus(from_en(tostring(exponent))))
end
 
১,১৪৮ ⟶ ১,০৭৯ নং লাইন:
-- * Uses a custom decimal mark, if wanted.
-- * Has digits grouped where necessary, if wanted.
-- * Uses scientific notation if requested, or for very small or large values
-- (which forces resultoutput to not be spelled).
-- * Has no more than maxsigfig significant digits
-- (same as old template and {{#expr}}).
local xhi, xlo -- these control when scientific notation (exponent) is used
if parms.opt_scientific then
xhi, xlo = 4, 2 -- default for output if input uses e-notation
elseif parms.opt_scientific_always then
xhi, xlo = 0, 0 -- always use scientific notation (experimental)
else
xhi, xlo = 10, 4 -- default
end
local sign = isnegative and MINUS or ''
local maxlen = maxsigfig
১,১৭০ ⟶ ১,০৯৩ নং লাইন:
if not tfrac and not exponent then
local integer, dot, decimals = show:match('^(%d*)(%.?)(.*)')
if #integer == '0' or integer =>= ''10 then
show = integer .. decimals
exponent = #integer
elseif integer == '0' or integer == '' then
local zeros, figs = decimals:match('^(0*)([^0]?.*)')
if #figs == 0 then
১,১৭৬ ⟶ ১,১০২ নং লাইন:
show = '0.' .. zeros:sub(1, maxlen)
end
elseif #zeros >= xlo4 then
show = figs
exponent = -#zeros
১,১৮২ ⟶ ১,১০৮ নং লাইন:
show = '0.' .. zeros .. figs:sub(1, maxlen)
end
elseif #integer >= xhi then
show = integer .. decimals
exponent = #integer
else
maxlen = maxlen + #dot
১,১৯৩ ⟶ ১,১১৬ নং লাইন:
end
if exponent then
local function zeros(n)
return string.rep('0', n)
end
if #show > maxlen then
show = show:sub(1, maxlen)
end
if exponent > xhi10 or exponent <= -xlo4 or (exponent == xhi10 and show ~= '11000000000' .. zeros(xhi - 1)) then
-- Rounded value satisfies: value >= 1e9 or value < 1e-4 (1e9 = 0.1e10).
-- When xhi, xlo = 10, 4 (the default), scientific notation is used if the
-- rounded value satisfies: value >= 1e9 or value < 1e-4 (1e9 = 0.1e10),
-- except if show is '1000000000' (1e9), for example:
-- {{convert|1000000000|m|m|sigfig=10}} → 1,000,000,000 metres (1,000,000,000 m)
local significand
if #show > 1 then
significand = show:sub(1, 1) .. '.' .. show:sub(2)
else
significand = show
end
return {
clean = '.' .. show,
exponent = exponent,
sign = sign,
show = sign .. with_exponent(parms, significandshow, exponent-1),
is_scientific = true,
}
end
if exponent >= #show then
show = show .. zerosstring.rep('0', exponent - #show) -- result has no dot
elseif exponent <= 0 then
show = '0.' .. zerosstring.rep('0', -exponent) .. show
else
show = show:sub(1, exponent) .. '.' .. show:sub(exponent+1)
১,২৪৯ ⟶ ১,১৬০ নং লাইন:
local function extract_fraction(parms, text, negative)
-- If text represents a fraction, return
-- value, altvalue, show, spelled, denominator
-- where
-- value is a number (value of the fraction in argument text)
-- altvalue is an alternate interpretation of any fraction for the hands
-- unit where "1214.1+3/4" means 1214 hands 1.75 inches!
-- show is a string (formatted text for display of an input value,
-- and is spelled if wanted and possible)
-- spelled is true if show was spelled
-- denominator is value of the denominator in the fraction
-- Otherwise, return nil.
-- Input uses en digits and '.' decimal mark (input has been translated).
-- Output uses digits in local language and localcustom decimal mark, if any.
--
------------------------------------------------------------------------
-- In the following, '(3/8)' represents the wikitext required to
-- Originally this function accepted x+y/z where x, y, z were any valid
-- display a fraction with numerator 3 and denominator 8.
-- numbers, possibly with a sign. For example '1.23e+2+1.2/2.4' = 123.5,
-- In the wikitext, Unicode minus is used for a negative value.
-- and '2-3/8' = 1.625. However, such usages were found to be errors or
-- text value, show value, show
-- misunderstandings, so since August 2014 the following restrictions apply:
-- if not negative if negative
-- x (if present) is an integer or has a single digit after decimal mark
-- 3 / 8 0.375, '(3/8)' -0.375, '−(3/8)'
-- y and z are unsigned integers
-- 2 + 3 / 8 2.375, '2(3/8)' -1.625, '−2(−3/8)'
-- e-notation is not accepted
-- The overall number2 can- start3 with/ '+'8 or '-' (so 1.625, '12+32(−3/48)' and -2.375, '+12+−2(3/48)'
-- 1 + 20/8 3.5 , '1/(20/8)' 1.5 , '−1/(−20/8)'
-- and '-12-3/4' are valid).
-- 1 - 20/8 -1.5., '1(−20/8)' -3.5 , '−1(20/8)'
-- Any leading negative sign is removed by the caller, so only inputs
-- Wherever an integer appears above, numbers like 1.25 or 12.5e-3
-- like the following are accepted here (may have whitespace):
-- (which may be negative) =are falsealso false trueaccepted (there was alike leadingold '-'template).
-- Old template text =interprets '1.23e+2+12/324' as '+2123(12/324)' = '2/3'123.5!
local numstr, whole, value, altvalue
-- text = '1+2/3' '+1+2/3' '1-2/3'
local lhs, slash, denstr = text:match('^%s*([^/]-)%s*(/+)%s*(.-)%s*$')
-- text = '12.3+1/2' '+12.3+1/2' '12.3-1/2'
-- Values like '12.3+1/2' are accepted, but are intended only for use
-- with the hands unit (not worth adding code to enforce that).
------------------------------------------------------------------------
local numstr, whole
local leading_plus, prefix, numstr, slashes, denstr =
text:match('^%s*(%+?)%s*(.-)%s*(%d+)%s*(/+)%s*(%d+)%s*$')
if not leading_plus then
-- Accept a single U+2044 fraction slash because that may be pasted.
leading_plus, prefix, numstr, denstr =
text:match('^%s*(%+?)%s*(.-)%s*(%d+)%s*⁄%s*(%d+)%s*$')
slashes = '/'
end
local numerator = tonumber(numstr)
local denominator = tonumber(denstr)
if numeratordenominator == nil orthen denominator ==return nil or (negative and leading_plus ~= '') thenend
local wholestr, negfrac, rhs = lhs:match('^%s*(.-[^eE])%s*([+-])%s*(.-)%s*$')
return nil
if wholestr == nil or wholestr == '' then
end
local wholestr = nil
if prefix == '' then
wholestr = ''
whole = 0
numstr = lhs
else
-- Any prefix must be like '12+' or '12-' (whole number and fraction sign);
-- '12.3+' and '12.3-' are also accepted (single digit after decimal point)
-- because '12.3+1/2 hands' is valid (12 hands 3½ inches).
local num1, num2, frac_sign = prefix:match('^(%d+)(%.?%d?)%s*([+%-])$')
if num1 == nil then return nil end
if num2 == '' then -- num2 must be '' or like '.1' but not '.' or '.12'
wholestr = num1
else
if #num2 ~= 2 then return nil end
wholestr = num1 .. num2
end
if frac_sign ~= (negative and '-' or '+') then return nil end
whole = tonumber(wholestr)
if whole == nil then return nil end
numstr = rhs
end
negfrac = (negfrac == '-')
local value = whole + numerator / denominator
local numerator = tonumber(numstr)
if not valid_number(value) then return nil end
if numerator == nil then return nil end
local altvalue = whole + numerator / (denominator * 10)
-- Spelling of silly inputs like "-2+3/8" or "2+3/+8" (mixed or excess signs) is not supported.
local style = #slashes -- kludge: 1 or 2 slashes can be used to select style
local do_spell
if negative == negfrac or wholestr == nil then
value = whole + numerator / denominator
altvalue = whole + numerator / (denominator * 10)
do_spell = parms.opt_spell_in
if do_spell then
if not (numstr:match('^%d') and denstr:match('^%d')) then -- if either has a sign
do_spell = false
end
end
else
value = whole - numerator / denominator
altvalue = whole - numerator / (denominator * 10)
numstr = change_sign(numstr)
do_spell = false
end
if not valid_number(value) then
return nil -- overflow or similar
end
numstr = use_minus(numstr)
denstr = use_minus(denstr)
local style = #slash -- kludge: 1 or 2 slashes can be used to select style
if style > 2 then style = 2 end
local wikitext = format_fraction(parms, 'in', negative, leading_plus .. wholestr, numstr, denstr, parms.opt_spell_indo_spell, style)
return value, altvalue, wikitext, do_spell, denominator
end
 
১,৩২৬ ⟶ ১,২৩৫ নং লাইন:
-- where info is a table with the result,
-- or return false, t where t is an error message table.
-- Input can use en digits or digits in local language and can.
-- have references at the end. Accepting references is intended
-- for use in infoboxes with a field for a value passed to convert.
-- Parameter another = true if the expected value is not the first.
-- Before processing, the input text is cleaned:
১,৩৩৯ ⟶ ১,২৪৬ নং লাইন:
-- altvalue = a valid number, usually same as value but different
-- if fraction used (for hands unit)
-- singular = true if value is 1 or -1 (to use singular form of units)
-- = false if value is -1 (like old template)
-- clean = cleaned text with any separators and sign removed
-- (en digits and '.' decimal mark)
-- show = text formatted for output, possibly with ref strip markers
-- (digits in local language and custom decimal mark)
-- The resulting show:
১,৩৫১ ⟶ ১,২৫৯ নং লাইন:
-- '+' (if the input text used '+'), or is '' (if no sign in input).
text = strip(text or '')
local referenceclean = to_en(text)
local pos = text:find('\127', 1, true)
if pos then
local before = text:sub(1, pos - 1)
local remainder = text:sub(pos)
local refs = {}
while #remainder > 0 do
local ref, spaces
ref, spaces, remainder = remainder:match('^(\127UNIQ[^\127]*%-ref%-%x+%-QINU\127)(%s*)(.*)')
if ref then
table.insert(refs, ref)
else
refs = {}
break
end
end
if #refs > 0 then
text = strip(before)
reference = table.concat(refs)
end
end
local clean = to_en(text, parms)
if clean == '' then
return false, { another and 'cvt_no_num2' or 'cvt_no_num' }
১,৪০৮ ⟶ ১,২৯৫ নং লাইন:
end
if value == nil then
local spelled
if not no_fraction then
value, altvalue, show, spelled, denominator = extract_fraction(parms, clean, isnegative)
end
if value == nil then
১,৪২৩ ⟶ ১,৩১১ নং লাইন:
end
if show == nil then
singular = (value == 1 and not isnegative)
-- clean is a non-empty string with no spaces, and does not represent a fraction,
local precision = parms.input_precision
-- and value = tonumber(clean) is a number >= 0.
if precision and 0 <= precision and precision <= 8 then
-- If the input uses e-notation, show will be displayed using a power of ten, but
local fmt = '%.' .. format('%d', precision) .. 'f'
-- we use the number as given so it might not be normalized scientific notation.
show = fmt:format(value + 2e-14) -- fudge for some common cases of bad rounding
-- The input value is spelled if specified so any e-notation is ignored;
-- that allows input like 2e6 to be spelled as "two million" which works
-- because the spell module converts '2e6' to '2000000' before spelling.
local function rounded(value, default, exponent)
local precision = parms.opt_ri
if precision then
local fmt = '%.' .. format('%d', precision) .. 'f'
local result = fmt:format(tonumber(value) + 2e-14) -- fudge for some common cases of bad rounding
if not exponent then
singular = (tonumber(result) == 1)
end
return result
end
return default
end
singular = (value == 1)
local scientific
local significand, exponent = clean:match('^([%d.]+)[Ee]([+%-]?%d+)')
if significand then
show = with_exponent(parms, rounded(significand, significand, exponent), exponent)
scientific = true
else
show = with_separator(parms, rounded(value, clean))
end
show = propersign .. with_separator(parms, show)
if parms.opt_spell_in then
show = spell_number(parms, 'in', propersign .. rounded(value, clean)) or show
scientific = false
end
if scientific then
parms.opt_scientific = true
end
end
১,৪৭০ ⟶ ১,৩৩৪ নং লাইন:
singular = singular,
clean = clean,
show = show .. (reference or ''),
denominator = denominator,
}
১,৬১২ ⟶ ১,৪৭৬ নং লাইন:
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 ('abbr=off' and 'for abbr=on') or ('input' and 'output'),
-- and may specify range text for 'adj=on',
-- and may specify exception = true.
rtext = range[want_name and 'off' or 'on'] or
range[((inout == 'in') == (parms.opt_flip == true)) and 'output' or 'input']
adj_text = range['adj']
exception = range['exception']
১,৬৩৮ ⟶ ১,৫০১ নং লাইন:
end
 
local function get_composite(parms, iparm, total, 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)
-- in_unit_tabletotal = (unit table for "yd"; contains value 1 for (number of yards)
-- in_unit_table = (unit table for "yd")
-- Return true, iparm, unit where
-- iparm = index just after the composite units (7 in above example)
১,৬৫১ ⟶ ১,৫১৫ নং লাইন:
local composite_units, count = { in_unit_table }, 1
local fixups = {}
local total = in_unit_table.valinfo[1].value
local subunit = in_unit_table
while subunit.subdivs do -- subdivs is nil or a table of allowed subdivisions
local subcode = strip(parms[iparm+1])
local subdiv = subunit.subdivs[subcode] or subunit.subdivs[(all_units[subcode] or {}).target]
if not subdiv then
break
end
local success
success, subunit = lookup(parmssubcode, subcodeparms.opt_sp_us, 'no_combination')
if not success then return false, subunit end -- should never occur
success, subinfo = extract_number(parms, parms[iparm])
১,৬৮৮ ⟶ ১,৫৫১ নং লাইন:
composite_units[i].fixed_name = name
else
local success, alternate = lookup(parmsunit, unitparms.opt_sp_us, 'no_combination')
if not success then return false, alternate end -- should never occur
alternate.inout = 'in'
১,৭০৯ ⟶ ১,৫৭২ নং লাইন:
-- Also, checks are performed which may display warnings, if enabled.
-- 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
if kv_pairs.adj and kv_pairs.sing then
-- For enwiki (before translation), warn if attempt to use adj and sing
১,৭২২ ⟶ ১,৫৮৪ নং লাইন:
if en_name then
local en_value
if en_name == '$' or en_name == 'frac' or en_name == 'sigfig' then
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
১,৭৪৬ ⟶ ১,৬০৫ নং লাইন:
end
end
elseif en_name == 'stylein' or en_name == 'styleout' then
en_value = loc_value -- accept user text with no validation
else
en_value = text_code.en_option_value[en_name][loc_value]
if en_value and en_value:sub(-1) == '?' then
en_value = en_value:sub(1, -2)
add_warning(parms, -1, 'cvt_deprecated', loc_name .. '=' .. loc_value)
end
if en_value == nil then
if loc_value == '' then
add_warning(parms, 2, 'cvt_empty_option', loc_name)
else
-- loc_value can no longer be nil here (at one time, that could occur
add_warning(parms, 1, 'cvt_unknown_option', loc_name .. '=' .. loc_value)
-- with aliases like |sing=off|adj=on), but am retaining safety check.
local text = loc_value and (loc_name .. '=' .. loc_value) or loc_name
add_warning(parms, 1, 'cvt_unknown_option', text)
end
elseif en_value == '' then
১,৭৬৪ ⟶ ১,৬২০ নং লাইন:
elseif type(en_value) == 'string' and en_value:sub(1, 4) == 'opt_' then
for _, v in ipairs(split(en_value, ',')) do
parms[v] = true
local lhs, rhs = v:match('^(.-)=(.+)$')
if rhs then
parms[lhs] = tonumber(rhs) or rhs
else
parms[v] = true
end
end
en_value = nil
১,৭৭৯ ⟶ ১,৬৩০ নং লাইন:
end
end
local abbr_entered =if parms.abbradj then
if parms.adj:sub(1, 2) == 'ri' then
-- It is known that adj is 'riN' where N is a single digit, so precision is valid.
-- Only a single en digit is accepted.
parms.input_precision = tonumber(parms.adj:sub(-1))
parms.adj = nil
end
end
local cfg_abbr = config.abbr
if cfg_abbr then
১,৭৯৬ ⟶ ১,৬৫৪ নং লাইন:
end
if parms.abbr then
parms.abbr_org = parms.abbr -- original abbr that was set, before any flip
elseif parms.opt_hand_hh then
parms.abbr_org = 'on'
১,৮০২ ⟶ ১,৬৬০ নং লাইন:
else
parms.abbr = 'out' -- default is to abbreviate output only (use symbol, not name)
end
if parms.opt_spell_out and not abbr_entered then
parms.abbr = 'off' -- should show unit name when spelling the output value
end
if parms.opt_flip then
১,৮২৮ ⟶ ১,৬৮৩ নং লাইন:
end
if parms.opt_table or parms.opt_tablecen then
if abbr_enteredparms.abbr_org == nil and parms.lk == nil then
parms.opt_values = true
end
parms.table_alignlocal align = format('align="%s"', parms.opt_table and 'right' or 'center')
parms.table_joins = { align .. '|', '\n|' .. align .. '|' }
end
if parms.table_align or parms.opt_sortable_onopt_lang_en then
parms.need_table_or_sort = true
end
local disp_joins = text_code.disp_joins
local default_joins = disp_joins['b']
parms.join_between = default_joins[3] or '; '
local disp = parms.disp
if disp == nil then -- special case for the most common setting
parms.joins = default_joins
elseif disp == 'x' then
-- Later, parms.joins is set from the input parameters.
else
-- Old template does this.
local abbr = parms.abbr
if disp == 'slash' then
if abbr_entered == nil then
disp = 'slash-nbsp'
elseif abbr == 'in' or abbr == 'out' then
disp = 'slash-sp'
else
disp = 'slash-nosp'
end
elseif disp == 'sqbr' then
if abbr == 'on' then
disp = 'sqbr-nbsp'
else
disp = 'sqbr-sp'
end
end
parms.joins = disp_joins[disp] or default_joins
parms.join_between = parms.joins[3] or parms.join_between
parms.wantname = parms.joins.wantname
end
if (en_default and not parms.opt_lang_local and (parms[1] or ''):find('%d')) or parms.opt_lang_en then
from_en_table = nil
end
if en_default and from_en_table then
-- For hiwiki: localized symbol/name is defined with the US symbol/name field,
-- and is used if output uses localized numbers.
parms.opt_sp_us = true
end
return true
১,৮৯৪ ⟶ ১,৭১২ নং লাইন:
-- If the parameter is not a value, try unpacking it as a range ("1-23" for "1 to 23").
-- However, "-1-2/3" is a negative fraction (-1⅔), so it must be extracted first.
-- Do not unpack a parameter if it is like "3-1/2" which is sometimes incorrectly
-- used instead of "3+1/2" (and which should not be interpreted as "3 to ½").
-- Unpacked items are inserted into the parms table.
-- The tail recursion allows combinations like "1x2 to 3x4".
local valstr = strip(parms[i]) -- trim so any '-' as a negative sign will be at start
local success, result = extract_number(parms, valstr, i > 1)
if not success and valstr and i < 20 then -- check i to limit abuse
for _, sep in ipairs(text_code.ranges.words) do
local lhs, sep, rhs = valstr:match('^(%S+)%s+(%S+)%s+(%S.*)')
local start, stop = valstr:find(sep, 2, true) -- start at 2 to skip any negative sign for range '-'
if lhs and not (sep == '-' and rhs:match('/')) then
if sep:find('%d')start then
parms[i] = valstr:sub(stop + 1)
return success, result -- to reject {{convert|1 234 567|m}} with a decent message (en only)
table.insert(parms, i, sep)
end
table.insert(parms[, i], valstr:sub(1, start =- rhs1))
return extractor(i) -- this allows combinations like "1 x 2 to 3 x 4"
table.insert(parms, i, sep)
table.insert(parms, i, lhs)
return extractor(i)
end
if not valstr:match('%-.*/') then
for _, sep in ipairs(text_code.ranges.words) do
local start, stop = valstr:find(sep, 2, true) -- start at 2 to skip any negative sign for range '-'
if start then
parms[i] = valstr:sub(stop + 1)
table.insert(parms, i, sep)
table.insert(parms, i, valstr:sub(1, start - 1))
return extractor(i)
end
end
end
১,৯৩৬ ⟶ ১,৭৩৯ নং লাইন:
end
valinfo:add(info)
local range_itemnext = get_range(strip(parms[i]))
local range_item = get_range(next)
if not range_item then
break
১,৯৪৩ ⟶ ১,৭৪৭ নং লাইন:
range:add(range_item)
if type(range_item) == 'table' then
parms.is_range_x = range_item.is_range_x
-- For range "x", if append unit to some values, append it to all.
parms.in_range_x = parms.in_range_x or range_item.in_range_x
parms.out_range_x = parms.out_range_x or range_item.out_range_x
parms.abbr_range_x = parms.abbr_range_x or range_item.abbr_range_x
is_change = range_item.is_range_change
end
১,৯৬৩ ⟶ ১,৭৬৪ নং লাইন:
local function simple_get_values(parms)
-- If input is like "{{convert|valid_value|valid_unit|...}}",
-- return true, v, 3, in_unit, in_unit_table
-- (as for get_values(), but with a unit name and table for a valid unit;
-- 3 = index in parms of whatever follows valid_unit, if anything).
-- The valid_value is not negative and does not use a fraction, and
১,৯৭০ ⟶ ১,৭৭২ নং লাইন:
-- 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_riinput_precision or parms.opt_spell_in then return end
local clean = to_en(strip(parms[1] or ''), parms)
if #clean > 10 or not clean:match('^[0-9.]+$') then return end
local value = tonumber(clean)
১,৯৮৩ ⟶ ১,৭৮৫ নং লাইন:
}
local in_unit = strip(parms[2])
local success, in_unit_table = lookup(parmsin_unit, in_unitparms.opt_sp_us, 'no_combination')
if not success then return end
return true, { info }, 3, in_unit, in_unit_table
in_unit_table.valinfo = { info }
return true, 3, in_unit, in_unit_table
end
 
local function get_parms(argspframe)
-- If successful, return true, parms, unit where
-- parms is a table of all arguments passed to the template
২,০০৩ ⟶ ১,৮০৪ নং লাইন:
local parms = {} -- arguments passed to template, after translation
local kv_pairs = {} -- table of input key:value pairs where key is a name; needed because cannot iterate parms and add new fields to it
for k, v in pairs(pframe.args) do
if type(k) == 'number' or k == 'test' then -- parameter "test" is reserved for testing and is not translated
parms[k] = v
২,০১২ ⟶ ১,৮১৩ নং লাইন:
local success, msg = translate_parms(parms, kv_pairs)
if not success then return false, msg end
local success, valinfo, i, in_unit, in_unit_table = simple_get_values(parms)
if not success then
local valinfo
success, valinfo, i = get_values(parms)
if not success then return false, valinfo end
in_unit = strip(parms[i])
i = i + 1
success, in_unit_table = lookup(parmsin_unit, in_unitparms.opt_sp_us, 'no_combination')
if not success then
if in_unit == nil then
২,০৩১ ⟶ ১,৮৩১ নং লাইন:
utype = "length", scale = 1, bad_mcode = in_unit_table }, unit_mt)
end
in_unit_table.valinfo = valinfo
end
if parms.test == 'msg' then
২,০৪৪ ⟶ ১,৮৪৩ নং লাইন:
end
end
in_unit_table.valinfo = valinfo
in_unit_table.inout = 'in' -- this is an input unit
if not parms.range then
local success, inext, composite_unit = get_composite(parms, i, valinfo[1].value, in_unit_table)
if not success then return false, inext end
if composite_unit then
২,১৯৯ ⟶ ১,৯৯৯ নং লাইন:
end
if in_current.istemperature and out_current.istemperature then
-- Converting between common temperatures (°C, °F, °R, K); not keVT, MK.
-- Kelvin value can be almost zero, or small but negative due to precision problems.
-- Also, an input value like -300 C (below absolute zero) gives negative kelvins.
২,৩৩৪ ⟶ ২,১৩৪ নং লাইন:
end
return false, { 'cvt_bug_convert' } -- should never occur
end
 
local function user_style(parms, i)
-- Return text for a user-specified style for a table cell, or '' if none,
-- given i = 1 (input style) or 2 (output style).
local style = parms[(i == 1) and 'stylein' or 'styleout']
if style then
style = style:gsub('"', '')
if style ~= '' then
if style:sub(-1) ~= ';' then
style = style .. ';'
end
return style
end
end
return ''
end
 
local function make_table_or_sort(parms, invalue, info, in_current, scaled_top)
-- Set options to handle output for a table or a sort key, or both.
-- The text sort key is based on the value resulting from converting
-- the input to a fake base unit with scale = 1, and other properties
-- required for a conversion derived from the input unit.
-- For other modules, return the sort key in a hidden span element, and
-- the scaled value used to generate the sort key.
-- If scaled_top is set, it is the scaled value of the numerator of a per unit
-- to be combined with this unit (the denominator) to make the sort key.
-- Scaling only works with units that convert with a factor (not temperature).
local sortkey, scaled_value
if parms.opt_sortable_on then
local base = { -- a fake unit with enough fields for a valid convert
scale = 1,
invert = in_current.invert and 1,
iscomplex = in_current.iscomplex,
offset = in_current.offset and 0,
}
local outvalue, extra = convert(parms, invalue, info, in_current, base)
if extra then
outvalue = extra.outvalue
end
if in_current.istemperature then
-- Have converted to kelvin; assume numbers close to zero have a
-- rounding error and should be zero.
if abs(outvalue) < 1e-12 then
outvalue = 0
end
end
if scaled_top and outvalue ~= 0 then
outvalue = scaled_top / outvalue
end
scaled_value = outvalue
if not valid_number(outvalue) then
if outvalue < 0 then
sortkey = '1000000000000000000'
else
sortkey = '9000000000000000000'
end
elseif outvalue == 0 then
sortkey = '5000000000000000000'
else
local mag = floor(log10(abs(outvalue)) + 1e-14)
local prefix
if outvalue > 0 then
prefix = 7000 + mag
else
prefix = 2999 - mag
outvalue = outvalue + 10^(mag+1)
end
sortkey = format('%d', prefix) .. format('%015.0f', floor(outvalue * 10^(14-mag)))
end
end
local sortspan
if sortkey and (parms.opt_sortable_debug or not parms.table_align) then
sortspan = parms.opt_sortable_debug and
'<span style="border:1px solid;display:inline;" class="sortkey">' .. sortkey .. '♠</span>' or
'<span style="display:none" class="sortkey">' .. sortkey .. '♠</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
joins[i] = (i == 1 and '' or '\n|') .. style .. user_style(parms, i) .. '"' .. sort .. '|'
end
parms.table_joins = joins
end
return sortspan, scaled_value
end
 
২,৪৩০ ⟶ ২,১৪২ নং লাইন:
-- show = rounded, formatted string with the result of converting value in info,
-- using the rounding specified in parms.
-- singular = true if result (afteris roundingpositive, and ignoring(after any negative signrounding)
-- 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;
২,৪৫১ ⟶ ২,১৬৩ নং লাইন:
end
local outvalue, extra = convert(parms, invalue, info, in_current, out_current)
if parms.need_table_or_sort then
parms.need_table_or_sort = nil -- process using first input value only
make_table_or_sort(parms, invalue, info, in_current)
end
if extra then
if not outvalue then return false, extra end
২,৪৭৫ ⟶ ২,১৮৩ নং লাইন:
precision = parms.precision
if not precision then
iflocal sigfig = parms.sigfig then
if sigfig then
show, exponent = make_sigfig(outvalue, parms.sigfig)
show, exponent = make_sigfig(outvalue, sigfig)
elseif parms.opt_round then
localelseif nparms.opt_round5 =or parms.opt_roundopt_round25 then
iflocal n == 0parms.opt_round5 and 5 thenor 25
local integer, fracpartshow = math.modfformat('%.0f', floor(2(outvalue */ outvaluen) + 0.5) /* 2n)
if fracpart == 0 then
show = format('%.0f', integer)
else
show = format('%.1f', integer + fracpart)
end
else
show = format('%.0f', floor((outvalue / n) + 0.5) * n)
end
else
local inclean = info.clean
২,৫৩০ ⟶ ২,২৩০ নং লাইন:
end
local t = format_number(parms, show, exponent, isnegative)
-- Set singular using match because on some systems 0.99999999999999999 is 1.0.
if type(show) == 'string' then
t.singular = (type(show) == 'string' and (show == '1' or show:match('^1%.0*$') ~= nil) and not isnegative)
-- Set singular using match because on some systems 0.99999999999999999 is 1.0.
t.fraction_table = (type(show) == 'table') and show or nil
if exponent then
t.singular = (exponent == 1 and show:match('^10*$'))
else
t.singular = (show == '1' or show:match('^1%.0*$'))
end
else
t.fraction_table = show
t.singular = (outvalue <= 1) -- cannot have 'fraction == 1', but if it were possible it would be singular
end
t.raw_absvalue = outvalue -- absolute value before rounding
return true, setmetatable(t, {
২,৬৬৫ ⟶ ২,৩৫৭ নং লাইন:
-- 'smallsuffix' if (value < 120), or 'bigsuffix' otherwise.
-- Input must use en digits and '.' decimal mark.
local default = data_code.default_exceptions[unit_table.defkey or unit_table.symbol] or unit_table.default
if not default then
local per = unit_table.per
if per then
local function a_default(v, u)
local success, ucode = get_default(v, u)
if not success then
return '?' -- an unlikely error has occurred; will cause lookup of default to fail
end
-- Attempt to use only the first unit if a combination or output multiple.
-- This is not bulletproof but should work for most cases.
-- Where it does not work, the convert will need to specify the wanted output unit.
local t = all_units[ucode]
if t then
local combo = t.combination
if combo then
-- For a multiple like ftin, the "first" unit (ft) is last in the combination.
local i = t.multiple and table_len(combo) or 1
ucode = combo[i]
end
else
-- Try for an automatically generated combination.
local item = ucode:match('^(.-)%+') or ucode:match('^(%S+)%s')
if all_units[item] then
return item
end
end
return ucode
end
local unit1, unit2 = per[1], per[2]
local def1 = (unit1 and a_default(value, unit1) or unit_table.vprefix or '')
local def2 = a_default(1, unit2) -- 1 because per unit of denominator
return true, def1 .. '/' .. def2
end
return false, { 'cvt_no_default', unit_table.symbol }
end
২,৭৪৯ ⟶ ২,৪০৯ নং লাইন:
 
local function variable_name(clean, unit_table)
-- For slwiki (Slovenian Wikipedia), a unit name depends on the value.
-- Parameter clean is the unsigned rounded value in en digits, as a string.
-- Value Source Example for "m"
২,৭৯১ ⟶ ২,৪৫১ নং লাইন:
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.
২,৮২৯ ⟶ ২,৪৮৯ নং লাইন:
if abbr_on then
result = '/'
elseif omitsep then
result = per_word
elseif unit1 then
result = ' ' .. per_word .. ' '
২,৮৩৮ ⟶ ২,৪৯৬ নং লাইন:
if want_link and unit_table.link then
if abbr_on or not varname then
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
unit_table.sep = ''
end
return make_link(unit_table.link, result, unit_table)
end
if unit1 then
result = linked_id(parms, unit1, key_id, want_link, clean) .. result
if unit1.sep then
unit_table.sep = unit1.sep
end
elseif omitsep then
unit_table.sep = ''
end
return result .. linked_id(parms, unit2, key_id2, want_link, '1')
end
if multiplier then
-- A multiplier (like "100" in "100km") forces the unit to be plural.
multiplier = from_en(multiplier)
if not omitsepabbr_on then
multiplier = multiplier .. (abbr_on and '&nbsp;' or ' ')
endelse
multiplier = multiplier .. ' '
if not abbr_on then
if key_id == 'name1' then
key_id = 'name2'
২,৮৭৪ ⟶ ২,৫২৪ নং লাইন:
end
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
unit_table.sep = ''
end
if want_link then
local link = data_code.link_exceptions[unit_table.linkey or unit_table.symbol] or unit_table.link
if link then
local before = ''
local i = unit_table.customary
if i == 1 and parmsunit_table.opt_sp_ussp_us then
i = 2 -- show "U.S." not "US"
end
২,৯২৪ ⟶ ২,৫৭১ নং লাইন:
-- id = unit name or symbol, possibly modified
-- f = true if id is a name, or false if id is a symbol
-- using the1st valueor for2nd indexvalues '(which'), and for 'in' or 'out' (unit_table.inout).
-- Result is '' if no symbol/name is to be used.
-- In addition, set unit_table.sep = ' ' or '&nbsp;' or ''
২,৯৪১ ⟶ ২,৫৮৮ নং লাইন:
local usename = unit_table.usename
local singular = info.singular
if usename then
-- Old template does something like this.
if want_link then
-- A linked unit uses the standard singular.
else
-- Set non-standard singular.
local flipped = parms.opt_flip
if inout == 'in' then
if not adjectival and (abbr_org == 'out' or flipped) then
local value = info.value
singular = (0 < value and value < 1.0001)
end
else
if (abbr_org == 'on') or
(not flipped and (abbr_org == nil or abbr_org == 'out')) or
(flipped and abbr_org == 'in') then
singular = (info.absvalue < 1.0001 and
not info.is_scientific)
end
end
end
end
local want_name
if usename then
২,৯৪৬ ⟶ ২,৬১৫ নং লাইন:
else
if abbr_org == nil then
if disp == 'br' or disp == 'or' or disp == 'slash' then
if parms.wantname then
want_name = true
end
২,৯৮৪ ⟶ ২,৬৫৩ নং লাইন:
end
end
if unit_table.engscale or parms.is_range_x then
-- engscale: so "|1|e3kg" gives "1 thousand kilograms" (plural)
-- is_range_x: so "|0.5|x|0.9|mi" gives "0.5 by 0.9 miles" (plural)
singular = false
end
key = (adjectival or singular) and 'name1' or 'name2'
if parmsunit_table.opt_sp_ussp_us then
key = key .. '_us'
end
২,৯৯৯ ⟶ ২,৬৬৯ নং লাইন:
end
unit_table.sep = '&nbsp;'
key = parmsunit_table.opt_sp_ussp_us and 'sym_us' or 'symbol'
end
return linked_id(parms, unit_table, key, want_link, info.clean), want_name
end
 
local function decorate_value(parms, unit_table, which, number_word)
-- If needed, update unit_table so values will be shown with extra information.
-- For consistency with the old template (but different from fmtpower),
৩,০১৮ ⟶ ২,৬৮৮ নং লাইন:
end
info.decorated = true
end
if engscale then
if engscale then
local inout = unit_table.inout
local abbrinout = parmsunit_table.abbrinout
local abbr = parms.abbr
if abbr == 'on' or abbr == inout 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>'
else
elseif number_word then
local number_id
local lk = parms.lk
if lk == 'on' or lk == inout then
number_id = make_link(engscale.link, engscale[1])
else
number_id = engscale[1]
end
-- WP:NUMERAL recommends "&nbsp;" in values like "12 million".
info.show = info.show .. (parms.opt_adjectival and '-' or '&nbsp;') .. number_id
end
-- WP:NUMERAL recommends "&nbsp;" in values like "12 million".
info.show = info.show .. (parms.opt_adjectival and '-' or '&nbsp;') .. number_id
end
end
if prefix then
if prefix then
info.show = prefix .. info.show
end
end
end
৩,০৮২ ⟶ ২,৭৫২ নং লাইন:
end
return preunit .. id1
end
local disp_joins = text_code.disp_joins
local abbr = parms.abbr
local disp = parms.disp
if disp == nil then -- special case for the most common setting
parms.joins = disp_joins['b']
elseif disp ~= 'x' then
-- Old template does this.
if disp == 'slash' then
if parms.abbr_org == nil then
disp = 'slash-nbsp'
elseif abbr == 'in' or abbr == 'out' then
disp = 'slash-sp'
else
disp = 'slash-nosp'
end
elseif disp == 'sqbr' then
if abbr == 'on' then
disp = 'sqbr-nbsp'
else
disp = 'sqbr-sp'
end
end
parms.joins = disp_joins[disp] or disp_joins['b']
end
if parms.opt_also_symbol and not composite then
local join1 = parms.joins[1]
if join1 == ' (' or join1 == ' [' then
parms.joins = { ' ['join1 .. first_unit[parmsfirst_unit.opt_sp_ussp_us and 'sym_us' or 'symbol'] .. '], ' .. join1 , parms.joins[2] }
end
end
if in_current.builtin == 'mach' and first_unit.sep ~= '' then -- '' means omitsep with non-enwiki name
local prefix = id1 .. '&nbsp;'
local range = parms.range
৩,০৯৭ ⟶ ২,৭৯১ নং লাইন:
-- For simplicity and because more not needed, handle one range item only.
local prefix2 = make_id(parms, 2, first_unit) .. '&nbsp;'
result = range_text(range[1], want_name, parms, result, prefix2 .. valinfo[2].show, 'in')
end
return preunit .. result
৩,১০৯ ⟶ ২,৮০৩ নং লাইন:
sep1 = '-'
sep2 = '-'
end
if omitsep and sep == '' then
-- Testing the id of the most significant unit should be sufficient.
sep1 = ''
sep2 = ''
end
local parts = { first_unit.valinfo[1].show .. sep1 .. id1 }
৩,১২৩ ⟶ ২,৮১২ নং লাইন:
return table.concat(parts, sep2) .. mid
end
local add_unitresult, = (parms.abbr == 'mos') or
parms[parms.opt_flip and 'out_range_x' or 'in_range_x'] or
(not want_name and parms.abbr_range_x)
local range = parms.range
if range and not add_unit then
mos = (abbr == 'mos')
linked_pages[first_unit] = nil -- so the final and only id will be linked, if wanted
if not (mos or (parms.is_range_x and not want_name)) then
linked_pages[first_unit] = nil -- so the second and only id will be linked, if wanted
end
end
local id = (range == nil) and id1 or make_id(parms, range.n + 12, first_unit) or id1
local extra, was_hyphenated = hyphenated_maybe(parms, want_name, sep, id, 'in')
if mos and was_hyphenated then
mos = false -- suppress repeat of unit in a range
add_unit = false
if linked_pages[first_unit] then
linked_pages[first_unit] = nil
id = make_id(parms, 2, first_unit)
extra = hyphenated_maybe(parms, want_name, sep, id, 'in')
end
end
local result
local valinfo = first_unit.valinfo
if range then
for i = 0,if range.n do== 1 then
-- Like {{convert|1|x|2|ft}} (one range item; two values).
local number_word
-- Do what old template did.
if i == range.n then
local sep1 = first_unit.sep
add_unit = false
number_wordif =mos truethen
decorate_value(parms, in_current, 1)
end
decorate_value(parms, first_unitin_current, i+1, number_word2)
local show result = valinfo[i+1].show .. sep1 .. id1
elseif parms.is_range_x and not want_name then
if add_unit then
if abbr == 'in' or abbr == 'on' then
show = show .. first_unit.sep .. (i == 0 and id1 or make_id(parms, i+1, first_unit))
decorate_value(parms, in_current, 1)
end
end
if i == 0 then
decorate_value(parms, in_current, 2)
result = show
result = valinfo[1].show .. sep1 .. id1
else
if abbr == 'in' or abbr == 'on' then
result = range_text(range[i], want_name, parms, result, show, 'in')
decorate_value(parms, in_current, 1)
end
decorate_value(parms, in_current, 2)
result = valinfo[1].show
end
result = range_text(range[1], want_name, parms, result, valinfo[2].show)
else
-- Like {{convert|1|x|2|x|3|ft}} (two or more range items): simplify.
decorate_value(parms, in_current, 1)
result = valinfo[1].show
for i = 1, range.n do
decorate_value(parms, in_current, i+1)
result = range_text(range[i], want_name, parms, result, valinfo[i+1].show)
end
end
else
decorate_value(parms, first_unit, 1, true)
result = valinfo[1].show
end
৩,১৭৯ ⟶ ২,৮৮৭ নং লাইন:
return preunit .. id1
end
if out_current.builtin == 'mach' and out_current.sep ~= '' then -- '' means omitsep with non-enwiki name
local prefix = id1 .. '&nbsp;'
local range = parms.range
৩,১৮৬ ⟶ ২,৮৯৪ নং লাইন:
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')
end
return preunit .. result
end
local result
local add_unit = (parms[parms.opt_flip and 'in_range_x' or 'out_range_x'] or
(not want_name and parms.abbr_range_x)) and
not parms.opt_output_number_only
local range = parms.range
if range and not add_unit then
if not (parms.is_range_x and not want_name) then
linked_pages[out_current] = nil -- so the final and only id will be linked, if wanted
linked_pages[out_current] = nil -- so the second and only id will be linked, if wanted
end
end
local id = (range == nil) and id1 or make_id(parms, range.n + 12, out_current) or id1
local extra, was_hyphenated = hyphenated_maybe(parms, want_name, sep, id, 'out')
if was_hyphenated then
add_unit = false
end
local result
local valinfo = out_current.valinfo
if range then
for i = 0,if range.n do== 1 then
local number_wordsep1 = out_current.sep
iflocal iabbr == rangeparms.n thenabbr
if parms.is_range_x and not want_name then
add_unit = false
if abbr == 'out' or abbr == 'on' then
number_word = true
decorate_value(parms, out_current, 1)
end
end
decorate_value(parms, out_current, i+1, number_word)
decorate_value(parms, out_current, 2)
local show = valinfo[i+1].show
result = valinfo[1].show .. sep1 .. id1
if add_unit then
show = show .. out_current.sep .. (i == 0 and id1 or make_id(parms, i+1, out_current))
end
if i == 0 then
result = show
else
if abbr == 'out' or abbr == 'on' then
result = range_text(range[i], want_name, parms, result, show, 'out')
decorate_value(parms, out_current, 1)
end
decorate_value(parms, out_current, 2)
result = valinfo[1].show
end
result = range_text(range[1], want_name, parms, result, valinfo[2].show)
else
-- Like {{convert|1|x|2|x|3|ft}} (two or more range items): simplify.
decorate_value(parms, out_current, 1)
result = valinfo[1].show
for i = 1, range.n do
decorate_value(parms, out_current, i+1)
result = range_text(range[i], want_name, parms, result, valinfo[i+1].show)
end
end
else
decorate_value(parms, out_current, 1, true)
result = valinfo[1].show
end
৩,৩৪২ ⟶ ৩,০৫৫ নং লাইন:
else
id = out_current['symbol']
end
if i == 1 and omit_separator(id) then
-- Testing the id of the least significant unit should be sufficient.
sep1 = ''
sep2 = ''
end
if want_link then
৩,৩৮৮ ⟶ ৩,০৯৬ নং লাইন:
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')
end
end
৩,৩৯৮ ⟶ ৩,১০৬ নং লাইন:
-- or return false, t where t is an error message table.
linked_pages = {}
local success, bad_output, out_first
local bad_input_mcode = in_unit_table.bad_mcode -- false if input unit is valid
local invalue1 = in_unit_table.valinfo[1].value
local out_unit = parms.out_unit
if out_unit == nil or out_unit == '' then
if bad_input_mcode or parms.opt_input_unit_only then
bad_output = ''
else
৩,৪১৪ ⟶ ৩,১২২ নং লাইন:
end
if not bad_output and not out_unit_table then
success, out_unit_table = lookup(parmsout_unit, out_unitparms.opt_sp_us, 'any_combination')
if success then
local mismatch = check_mismatch(in_unit_table, out_unit_table)
৩,৪৩৩ ⟶ ৩,১৪১ নং লাইন:
parts[part] = process_input(parms, in_unit_table)
elseif bad_output then
parts[part]if = (bad_output =~= '') and '' or message(bad_output)then
parts[part] = message(bad_output)
end
else
local outputs = {}
৩,৪৫৯ ⟶ ৩,১৬৯ নং লাইন:
end
end
local out_first
local imax = combos and #combos or 1 -- 1 (single unit) or number of unit tables
for i = 1, imax do
৩,৪৭৯ ⟶ ৩,১৮৮ নং লাইন:
table.insert(outputs, item)
end
local sep = parms.table_joins and parms.table_joins[2] or '; '
if parms.opt_input_unit_only then
parts[part] = parms.opt_input_unit_only and '' or table.concat(outputs, sep)
elseend
end
local sep = parms.table_joins and parms.table_joins[2] or parms.join_between
if parms.opt_sortable_in or parms.opt_sortable_out then
parts[part] = table.concat(outputs, sep)
local value
if parms.opt_sortable_in then
value = invalue1
else
local info = out_first and out_first.valinfo
if info then
info = info[1]
value = info.raw_absvalue
if value and info.sign == MINUS then
value = -value
end
end
end
parts[1] = ntsh((value or 0), parms.opt_sortable_debug) .. parts[1]
end
if parms.join_before then
parts[1] = parms.join_before .. parts[1]
end
local wikitext
৩,৫১০ ⟶ ৩,২২৮ নং লাইন:
local function main_convert(frame)
-- Do convert, and if needed, do it again with higher default precision.
set_config(frame.args)
local result, out_unit_table
local success, parms, in_unit_table = get_parms(frame:getParent().args)
if success then
for i = 1, 2 do -- use counter so cannot get stuck repeating convert
৩,৫৩১ ⟶ ৩,২৪৯ নং লাইন:
end
 
return { convert = main_convert }
local function _unit(unitcode, options)
-- Helper function for Module:Val to look up a unit.
-- Parameter unitcode must be a string to identify the wanted unit.
-- Parameter options must be nil or a table with optional fields:
-- value = number (for sort key; default value is 1)
-- scaled_top = nil for a normal unit, or a number for a unit which is
-- the denominator of a per unit (for sort key)
-- si = { 'symbol', 'link' }
-- (a table with two strings) to make an SI unit
-- that will be used for the look up
-- link = true if result should be [[linked]]
-- sort = 'on' or 'debug' if result should include a sort key in a
-- span element ('debug' makes the key visible)
-- name = true for the name of the unit instead of the symbol
-- us = true for the US spelling of the unit, if any
-- Return nil if unitcode is not a non-empty string.
-- Otherwise return a table with fields:
-- text = requested symbol or name of unit, optionally linked
-- scaled_value = input value adjusted by unit scale; used for sort key
-- sortspan = span element with sort key like that provided by {{ntsh}},
-- calculated from the result of converting value
-- to a base unit with scale 1.
-- unknown = true if the unitcode was not known
unitcode = strip(unitcode)
if unitcode == nil or unitcode == '' then
return nil
end
set_config({})
linked_pages = {}
options = options or {}
local parms = {
abbr = options.name and 'off' or 'on',
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',
}
local utable
if options.si then
-- Make a dummy table of units (just one unit) for lookup to use.
-- This makes lookup recognize any SI prefix in the unitcode.
local symbol = options.si[1] or '?'
utable = { [symbol] = {
_name1 = symbol,
_name2 = symbol,
_symbol = symbol,
utype = symbol,
scale = symbol == 'g' and 0.001 or 1,
prefixes = 1,
default = symbol,
link = options.si[2],
}}
end
local success, unit_table = lookup(parms, unitcode, 'no_combination', utable)
if not success then
unit_table = setmetatable({
symbol = unitcode, name2 = unitcode,
default = "m", defkey = "m", linkey = "m",
utype = "length", scale = 1 }, unit_mt)
end
local value = tonumber(options.value) or 1
local clean = tostring(abs(value))
local info = {
value = value,
altvalue = value,
singular = (clean == '1'),
clean = clean,
show = clean,
}
unit_table.inout = 'in'
unit_table.valinfo = { info }
local sortspan, scaled_value
if options.sort then
sortspan, scaled_value = make_table_or_sort(parms, value, info, unit_table, options.scaled_top)
end
return {
text = make_id(parms, 1, unit_table),
sortspan = sortspan,
scaled_value = scaled_value,
unknown = not success and true or nil,
}
end
 
return { convert = main_convert, _unit = _unit }