মডিউল:Convert: সংশোধিত সংস্করণের মধ্যে পার্থক্য
বিষয়বস্তু বিয়োগ হয়েছে বিষয়বস্তু যোগ হয়েছে
Yahia.barie (আলোচনা | অবদান) অ হালনাগাদ করা হয়েছে |
আফতাবুজ্জামান (আলোচনা | অবদান) আলোচনা ছাড়া এটি হালনাগাদ করবেন না |
||
১৫ নং লাইন:
-- 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
local
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 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
-- 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
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
text = text:gsub(numsep_remove, '')
end
if numdot ~= '.' then
text = text:gsub(numdot, '.')
end
if to_en_table then
text = ustring.gsub(text, '%d', to_en_table)
end
return text
end
১৩৮ ⟶ ৬৮ নং লাইন:
local speller -- function from that module to handle spelling (set if spelling is wanted)
local function set_config(
-- 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'.
-- A testing program can set the global variable 'is_test_run'.
local data_module,
if is_test_run then
local langcode = mw.language.getContentLanguage().code
data_module = "convertdata-" .. langcode
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 translation.group then
group_method = translation.group
১৮৬ ⟶ ১২৩ নং লাইন:
to_en_table = translation.to_en
end
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.
end
২৪২ ⟶ ১৭০ নং লাইন:
if type(text) == 'string' then
return text:match("^%s*(.-)%s*$")
end
end
২৯২ ⟶ ২১০ নং লাইন:
end
-- Escape user input so it does not break the message.
-- To avoid reference tags (like {{convert|1<
-- 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 = '<ref>...</ref>'
else
append = '...'
end
s = s:sub(1, pos - 1)
end
if limit and ulen(s) > limit then
s = usub(s, 1, limit)
if append == '
append = '...'
end
end
s = nowiki(s) ..
else
s = '?'
৩১৫ ⟶ ২৩৯ নং লাইন:
local cat = wanted_category(text_code.all_categories[msg[3]]) or ''
local anchor = msg[4] or ''
local fmt = text_code.all_messages[
title = title:gsub('"', '"')
return format(fmt, anchor, title, text, cat)
৩২২ ⟶ ২৪৬ নং লাইন:
end
local function add_warning(parms, level,
-- 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
if level <= (tonumber(config.warnings) or 1) then
if parms.warnings == nil then
parms.warnings = message({
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.
__index = function (self, key)
local value
৫৭৬ ⟶ ৪৯৯ নং লাইন:
}
local function
-- 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
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(
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 per then
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(
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.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(
if not success
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(
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
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(
if success then
return true, result
৮১৬ ⟶ ৭২৩ নং লাইন:
local en_code = ustring.gsub(unitcode, '%d', to_en_table)
if en_code ~= unitcode then
return lookup(
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
-- Return a
-- 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
-- 2 for 3-then-2 grouping
-- Parameter gaps is true to use <span> gaps (numsep ignored).
return {
n = 0,
add = function (self, digits)
self[self.n] = from_en(digits)
end,
join = function (self, rhs)
-- Concatenate in reverse order.
if gaps then
for i = 1, self.n - 1 do
result = '<span style="margin-left: 0.25em">' .. self[i] .. '</span>' .. result
end
return '<span style="white-space: nowrap">' .. self[self.n] .. result .. from_en(rhs) .. '</span>'
else
for i = 2, self.n do
result = self[i] .. numsep .. result
end
end
step = 3,
next_position = function (self, previous)
-- Return position of digit just before next group.
-- Digits are grouped from right-to-left (least significant first).
local result = previous - self.step
if method == 2 then
self.step = 2
return (result < 0) and 0 or result
end,
}
end
local function with_separator(parms, text)
-- Input text is a number in en digits
-- 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 '
-- (e notation can only occur when processing an input value).
-- The text has no sign (caller inserts that later, if necessary).
--
--
if
return from_en(text)
end
local last = text:match('()[.eE]') -- () returns position
if
last = #text
else
last = last - 1 -- index of last character before dot/e/E
end
if last < 4 or (last == 4 and parms.opt_comma5) then
return from_en(text)
end
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
end
--
--
--
-- Use format(fmtpower, significand, '10', exponent) where each
local fmtpower = '%s<span style="margin:0 .15em 0 .25em">×</span>%s<sup>%s</sup>'
local function with_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
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
-- (which forces
-- * Has no more than maxsigfig significant digits
-- (same as old template and {{#expr}}).
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
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 >=
show = figs
exponent = -#zeros
১,১৮২ ⟶ ১,১০৮ নং লাইন:
show = '0.' .. zeros .. figs:sub(1, maxlen)
end
else
maxlen = maxlen + #dot
১,১৯৩ ⟶ ১,১১৬ নং লাইন:
end
if exponent then
if #show > maxlen then
show = show:sub(1, maxlen)
end
if exponent >
-- Rounded value satisfies: value >= 1e9 or value < 1e-4 (1e9 = 0.1e10).
return {
clean = '.' .. show,
exponent = exponent,
sign = sign,
show = sign .. with_exponent(
is_scientific = true,
}
end
if exponent >= #show then
show = show ..
elseif exponent <= 0 then
show = '0.' ..
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 "
-- 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
--
-- In the following, '(3/8)' represents the wikitext required to
-- display a fraction with numerator 3 and denominator 8.
-- In the wikitext, Unicode minus is used for a negative value.
-- text value, show value, show
-- if not negative if negative
-- 3 / 8 0.375, '(3/8)' -0.375, '−(3/8)'
-- 2 + 3 / 8 2.375, '2(3/8)' -1.625, '−2(−3/8)'
--
-- 1 + 20/8 3.5 , '1/(20/8)' 1.5 , '−1/(−20/8)'
-- 1 - 20/8 -1.5., '1(−20/8)' -3.5 , '−1(20/8)'
-- Wherever an integer appears above, numbers like 1.25 or 12.5e-3
-- (which may be negative)
-- Old template
local numstr, whole, value, altvalue
local lhs, slash, denstr = text:match('^%s*([^/]-)%s*(/+)%s*(.-)%s*$')
local denominator = tonumber(denstr)
if
local wholestr, negfrac, rhs = lhs:match('^%s*(.-[^eE])%s*([+-])%s*(.-)%s*$')
if wholestr == nil or wholestr == '' then
whole = 0
numstr = lhs
else
whole = tonumber(wholestr)
if whole == nil then return nil end
numstr = rhs
end
negfrac = (negfrac == '-')
local numerator = tonumber(numstr)
if numerator == nil then return nil end
-- Spelling of silly inputs like "-2+3/8" or "2+3/+8" (mixed or excess signs) is not supported.
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,
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
-- 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
-- = 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
-- (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
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)
local precision = parms.input_precision
if precision and 0 <= precision and precision <= 8 then
local fmt = '%.' .. format('%d', precision) .. 'f'
show = fmt:format(value + 2e-14) -- fudge for some common cases of bad rounding
else
show =
end
show = propersign .. with_separator(parms, show)
if parms.opt_spell_in then
show = spell_number(parms, 'in', propersign ..
end
end
১,৪৭০ ⟶ ১,৩৩৪ নং লাইন:
singular = singular,
clean = clean,
show = show
denominator = denominator,
}
১,৬১২ ⟶ ১,৪৭৬ নং লাইন:
end
local function range_text(range, want_name, parms, before, after
-- 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
-- and may specify range text for 'adj=on',
-- and may specify exception = true.
rtext = range[want_name and 'off' or 'on']
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_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 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
if not subdiv then
break
end
local success
success, subunit = lookup(
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(
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.
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
if loc_value == '' then
add_warning(parms, 2, 'cvt_empty_option', loc_name)
else
local minimum
১,৭৪৬ ⟶ ১,৬০৫ নং লাইন:
end
end
else
en_value = text_code.en_option_value[en_name][loc_value]
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
-- 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
end
en_value = nil
১,৭৭৯ ⟶ ১,৬৩০ নং লাইন:
end
end
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_flip then
১,৮২৮ ⟶ ১,৬৮৩ নং লাইন:
end
if parms.opt_table or parms.opt_tablecen then
if
parms.opt_values = true
end
parms.table_joins = { align .. '|', '\n|' .. align .. '|' }
end
if parms.
from_en_table = nil
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.
-- Unpacked items are inserted into the parms table.
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 start, stop = valstr:find(sep, 2, true) -- start at 2 to skip any negative sign for range '-'
if
parms[i] = valstr:sub(stop + 1)
table.insert(parms, i, sep)
table.insert(parms
return extractor(i) -- this allows combinations like "1 x 2 to 3 x 4"
end
end
১,৯৩৬ ⟶ ১,৭৩৯ নং লাইন:
end
valinfo:add(info)
local
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
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.
local clean = to_en(strip(parms[1] or '')
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(
if not success then return end
return true, { info }, 3, in_unit, in_unit_table
end
local function get_parms(
-- 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
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(
if not success then
if in_unit == nil then
২,০৩১ ⟶ ১,৮৩১ নং লাইন:
utype = "length", scale = 1, bad_mcode = in_unit_table }, unit_mt)
end
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
২,৪৩০ ⟶ ২,১৪২ নং লাইন:
-- show = rounded, formatted string with the result of converting value in info,
-- using the rounding specified in parms.
-- singular = true if result
-- is "1", or like "1.00"
-- (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 extra then
if not outvalue then return false, extra end
২,৪৭৫ ⟶ ২,১৮৩ নং লাইন:
precision = parms.precision
if not precision then
if sigfig then
show, exponent = make_sigfig(outvalue, sigfig)
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.
t.singular = (type(show) == 'string' and (show == '1' or show:match('^1%.0*$') ~= nil) and not isnegative)
t.fraction_table = (type(show) == 'table') and show or nil
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 =
if not default then
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(
-- Return final unit id (symbol or name), optionally with a wikilink,
-- and update unit_table.sep if required.
২,৮২৯ ⟶ ২,৪৮৯ নং লাইন:
if abbr_on then
result = '/'
elseif unit1 then
result = ' ' .. per_word .. ' '
২,৮৩৮ ⟶ ২,৪৯৬ নং লাইন:
if want_link and unit_table.link then
if abbr_on or not varname then
result = (unit1 and
else
result = (unit1 and variable_name(clean, unit1) or '') .. result .. variable_name('1', unit2)
end
return make_link(unit_table.link, result, unit_table)
end
if unit1 then
result = linked_id(
end
return result .. linked_id(
end
if multiplier then
-- A multiplier (like "100" in "100km") forces the unit to be plural.
multiplier = from_en(multiplier)
if
multiplier = multiplier ..
multiplier = multiplier .. ' '
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 want_link then
local link =
if link then
local before = ''
local i = unit_table.customary
if i == 1 and
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
-- Result is '' if no symbol/name is to be used.
-- In addition, set unit_table.sep = ' ' or ' ' 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
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
key = key .. '_us'
end
২,৯৯৯ ⟶ ২,৬৬৯ নং লাইন:
end
unit_table.sep = ' '
key =
end
return linked_id(
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),
৩,০১৮ ⟶ ২,৬৮৮ নং লাইন:
end
info.decorated = true
end
if engscale then
local abbr = parms.abbr
if abbr == 'on' or abbr == inout then '<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
end
-- WP:NUMERAL recommends " " in values like "12 million".
info.show = info.show .. (parms.opt_adjectival and '-' or ' ') .. number_id
end
end
info.show = prefix .. info.show 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 = {
end
end
if in_current.builtin == 'mach
local prefix = id1 .. ' '
local range = parms.range
৩,০৯৭ ⟶ ২,৭৯১ নং লাইন:
-- 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
end
return preunit .. result
৩,১০৯ ⟶ ২,৮০৩ নং লাইন:
sep1 = '-'
sep2 = '-'
end
local parts = { first_unit.valinfo[1].show .. sep1 .. id1 }
৩,১২৩ ⟶ ২,৮১২ নং লাইন:
return table.concat(parts, sep2) .. mid
end
local
local range = parms.range
if range
mos = (abbr == 'mos')
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,
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
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 valinfo = first_unit.valinfo
if range then
-- Like {{convert|1|x|2|ft}} (one range item; two values).
-- Do what old template did.
local sep1 = first_unit.sep
decorate_value(parms, in_current, 1)
decorate_value(parms,
elseif parms.is_range_x and not want_name then
if abbr == 'in' or abbr == 'on' then
decorate_value(parms, in_current, 1)
end
decorate_value(parms, in_current, 2)
result = valinfo[1].show .. sep1 .. id1
else
if abbr == 'in' or abbr == 'on' then
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
result = valinfo[1].show
end
৩,১৭৯ ⟶ ২,৮৮৭ নং লাইন:
return preunit .. id1
end
if out_current.builtin == 'mach
local prefix = id1 .. ' '
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
end
return preunit .. result
end
local result
local range = parms.range
if range
if not (parms.is_range_x and not want_name) then
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,
local extra
local valinfo = out_current.valinfo
if range then
local
if parms.is_range_x and not want_name then
if abbr == 'out' or abbr == 'on' then
decorate_value(parms, out_current, 1)
end
decorate_value(parms, out_current, 2)
result = valinfo[1].show .. sep1 .. id1
else
if abbr == 'out' or abbr == 'on' then
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
result = valinfo[1].show
end
৩,৩৪২ ⟶ ৩,০৫৫ নং লাইন:
else
id = out_current['symbol']
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
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
bad_output = ''
else
৩,৪১৪ ⟶ ৩,১২২ নং লাইন:
end
if not bad_output and not out_unit_table then
success, out_unit_table = lookup(
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] = message(bad_output)
end
else
local outputs = {}
৩,৪৫৯ ⟶ ৩,১৬৯ নং লাইন:
end
end
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 '; '
end
if parms.opt_sortable_in or parms.opt_sortable_out then
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
local wikitext
৩,৫১০ ⟶ ৩,২২৮ নং লাইন:
local function main_convert(frame)
-- Do convert, and if needed, do it again with higher default precision.
set_config(frame
local result, out_unit_table
local success, parms, in_unit_table = get_parms(frame:getParent()
if success then
for i = 1, 2 do -- use counter so cannot get stuck repeating convert
৩,৫৩১ ⟶ ৩,২৪৯ নং লাইন:
end
return { convert = main_convert }
|