Module:Convert: Difference between revisions

(update from sandbox per Template talk:Convert#Module version 5)
(update from sandbox per Template talk:Convert#Module version 6)
Line 36:
local extra_units -- nil or table of extra units from extra_module
local ignore_extra_units -- if true, do not require the 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)
Line 115 ⟶ 118:
end
 
local function want_separatoromit_separator(id)
-- Return true if there should be no separator before id (a unit symbolesymbol or name) should be proceeded by a separator.
-- For zhwiki, there should be no separator if id uses local characters.
-- The following kludge should be a sufficient test.
if infoomitsep then
if id:sub(1, 2) == '-{' then -- for "-{...}-" content language variant
return falsetrue
end
end
if id:byte() > 127 then
local first = usub(id, 1, 1)
if first ~= 'Å' and first ~= '°' and first ~= 'µ' then
return falsetrue
end
end
end
return id:sub(1, 1) == '/' -- no separator before units like "/ha"
return true
end
 
Line 637 ⟶ 642:
for i, v in ipairs(per) do
if i == 1 and text_code.currency[v] then
prefix = currency_text or v
else
local success, t = lookup(v, opt_sp_us, 'no_combination', utable, fails, depth)
Line 821 ⟶ 826:
-- If debug == true, output is in a box (not hidden).
-- This implements Template:Ntsh (number table sorting, hidden).
num = num or 0
local result, style
if not valid_number(num) then
Line 1,656 ⟶ 1,662:
-- 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
Line 1,668 ⟶ 1,675:
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
Line 1,722 ⟶ 1,732:
end
end
local abbr_entered = parms.abbr
local cfg_abbr = config.abbr
if cfg_abbr then
Line 1,738 ⟶ 1,749:
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'
Line 1,767 ⟶ 1,778:
end
if parms.opt_table or parms.opt_tablecen then
if parms.abbr_orgabbr_entered == nil and parms.lk == nil then
parms.opt_values = true
end
Line 1,785 ⟶ 1,796:
local abbr = parms.abbr
if disp == 'slash' then
if parms.abbr_orgabbr_entered == nil then
disp = 'slash-nbsp'
elseif abbr == 'in' or abbr == 'out' then
Line 2,623 ⟶ 2,634:
result = (unit1 and variable_name(clean, unit1) or '') .. result .. variable_name('1', unit2)
end
if omitsep and not want_separatoromit_separator(result) then
unit_table.sep = ''
end
Line 2,655 ⟶ 2,666:
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 omitsep and not want_separatoromit_separator(id) then
unit_table.sep = ''
end
Line 3,172 ⟶ 3,183:
id = out_current['symbol']
end
if omitsep and i == 1 and not want_separatoromit_separator(id) then
-- Testing the id of the least significant unit should be sufficient.
sep1 = ''
Line 3,227 ⟶ 3,238:
-- 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
Line 3,254 ⟶ 3,265:
end
local flipped = parms.opt_flip and not bad_input_mcode
local sortkey
if parms.opt_sortable_in or parms.opt_sortable_out then
parts[1]sortkey = ntsh((value or 0)invalue1, parms.opt_sortable_debug) .. parts[1]
end
local parts = {}
for part = 1, 2 do
Line 3,290 ⟶ 3,305:
end
end
local valueout_first
local imax = combos and #combos or 1 -- 1 (single unit) or number of unit tables
for i = 1, imax do
Line 3,309 ⟶ 3,325:
table.insert(outputs, item)
end
if parms.opt_sortable_out then
local sep = parms.table_joins and parms.table_joins[2] or parms.join_between
valuelocal = -value
parts[part] = parms.opt_input_unit_only and '' or table.concat(outputs, sep)
local info = out_first and out_first.valinfo
if info = info[1]then
info = info[1]
value = info.raw_absvalue
if value and info.sign == MINUS then
value = -value
end
end
sortkey = ntsh(value, parms.opt_sortable_debug)
end
if parms.opt_input_unit_only then
parts[part] = ''
else
local sep
if parms.table_joins then
sep = parms.table_joins[2] .. (sortkey or '')
else
sep = parms.join_between
end
parts[part] = parms.opt_input_unit_only and '' or table.concat(outputs, sep)
end
end
end
if sortkey then
if parms.opt_sortable_in or parms.opt_sortable_out then
for i, v in ipairs(parts) do
local value
if i == 1 or parms.opt_sortable_intable_joins then
value parts[i] = invalue1sortkey .. v
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
Anonymous user