Module:Convert: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
(update from sandbox per Template talk:Convert#Module version 5)
(update from sandbox per Template talk:Convert#Module version 6)
Line 36: Line 36:
local extra_units -- nil or table of extra units from extra_module
local extra_units -- nil or table of extra units from extra_module
local ignore_extra_units -- if true, do not require the 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)
local function from_en(text)
Line 115: Line 118:
end
end


local function want_separator(id)
local function omit_separator(id)
-- Return true if id (a unit symbole or name) should be proceeded by a separator.
-- 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.
-- For zhwiki, there should be no separator if id uses local characters.
-- The following kludge should be a sufficient test.
-- The following kludge should be a sufficient test.
if omitsep then
if id:sub(1, 2) == '-{' then -- for "-{...}-" content language variant
if id:sub(1, 2) == '-{' then -- for "-{...}-" content language variant
return false
return true
end
end
if id:byte() > 127 then
if id:byte() > 127 then
local first = usub(id, 1, 1)
local first = usub(id, 1, 1)
if first ~= 'Å' and first ~= '°' and first ~= 'µ' then
if first ~= 'Å' and first ~= '°' and first ~= 'µ' then
return false
return true
end
end
end
end
end
return id:sub(1, 1) == '/' -- no separator before units like "/ha"
return true
end
end


Line 637: Line 642:
for i, v in ipairs(per) do
for i, v in ipairs(per) do
if i == 1 and text_code.currency[v] then
if i == 1 and text_code.currency[v] then
prefix = v
prefix = currency_text or v
else
else
local success, t = lookup(v, opt_sp_us, 'no_combination', utable, fails, depth)
local success, t = lookup(v, opt_sp_us, 'no_combination', utable, fails, depth)
Line 821: Line 826:
-- If debug == true, output is in a box (not hidden).
-- If debug == true, output is in a box (not hidden).
-- This implements Template:Ntsh (number table sorting, hidden).
-- This implements Template:Ntsh (number table sorting, hidden).
num = num or 0
local result, style
local result, style
if not valid_number(num) then
if not valid_number(num) then
Line 1,656: Line 1,662:
-- Also, checks are performed which may display warnings, if enabled.
-- 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.
-- 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
if kv_pairs.adj and kv_pairs.sing then
-- For enwiki (before translation), warn if attempt to use adj and sing
-- For enwiki (before translation), warn if attempt to use adj and sing
Line 1,668: Line 1,675:
if en_name then
if en_name then
local en_value
local en_value
if en_name == 'frac' or en_name == 'sigfig' then
if en_name == '$' or en_name == 'frac' or en_name == 'sigfig' then
if loc_value == '' then
if loc_value == '' then
add_warning(parms, 2, 'cvt_empty_option', loc_name)
add_warning(parms, 2, 'cvt_empty_option', loc_name)
elseif en_name == '$' then
-- Value should be a single character like "€" for the euro currency symbol, but anything is accepted.
currency_text = (loc_value == 'euro') and '€' or loc_value
else
else
local minimum
local minimum
Line 1,722: Line 1,732:
end
end
end
end
local abbr_entered = parms.abbr
local cfg_abbr = config.abbr
local cfg_abbr = config.abbr
if cfg_abbr then
if cfg_abbr then
Line 1,738: Line 1,749:
end
end
if parms.abbr then
if parms.abbr then
parms.abbr_org = parms.abbr -- original abbr that was set, before any flip
parms.abbr_org = parms.abbr -- original abbr, before any flip
elseif parms.opt_hand_hh then
elseif parms.opt_hand_hh then
parms.abbr_org = 'on'
parms.abbr_org = 'on'
Line 1,767: Line 1,778:
end
end
if parms.opt_table or parms.opt_tablecen then
if parms.opt_table or parms.opt_tablecen then
if parms.abbr_org == nil and parms.lk == nil then
if abbr_entered == nil and parms.lk == nil then
parms.opt_values = true
parms.opt_values = true
end
end
Line 1,785: Line 1,796:
local abbr = parms.abbr
local abbr = parms.abbr
if disp == 'slash' then
if disp == 'slash' then
if parms.abbr_org == nil then
if abbr_entered == nil then
disp = 'slash-nbsp'
disp = 'slash-nbsp'
elseif abbr == 'in' or abbr == 'out' then
elseif abbr == 'in' or abbr == 'out' then
Line 2,623: Line 2,634:
result = (unit1 and variable_name(clean, unit1) or '') .. result .. variable_name('1', unit2)
result = (unit1 and variable_name(clean, unit1) or '') .. result .. variable_name('1', unit2)
end
end
if omitsep and not want_separator(result) then
if omit_separator(result) then
unit_table.sep = ''
unit_table.sep = ''
end
end
Line 2,655: Line 2,666:
end
end
local id = unit_table.fixed_name or ((varname and not abbr_on) and variable_name(clean, unit_table) or unit_table[key_id])
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_separator(id) then
if omit_separator(id) then
unit_table.sep = ''
unit_table.sep = ''
end
end
Line 3,172: Line 3,183:
id = out_current['symbol']
id = out_current['symbol']
end
end
if omitsep and i == 1 and not want_separator(id) then
if i == 1 and omit_separator(id) then
-- Testing the id of the least significant unit should be sufficient.
-- Testing the id of the least significant unit should be sufficient.
sep1 = ''
sep1 = ''
Line 3,227: Line 3,238:
-- or return false, t where t is an error message table.
-- or return false, t where t is an error message table.
linked_pages = {}
linked_pages = {}
local success, bad_output, out_first
local success, bad_output
local bad_input_mcode = in_unit_table.bad_mcode -- false if input unit is valid
local bad_input_mcode = in_unit_table.bad_mcode -- false if input unit is valid
local invalue1 = in_unit_table.valinfo[1].value
local invalue1 = in_unit_table.valinfo[1].value
Line 3,254: Line 3,265:
end
end
local flipped = parms.opt_flip and not bad_input_mcode
local flipped = parms.opt_flip and not bad_input_mcode
local sortkey
if parms.opt_sortable_in then
sortkey = ntsh(invalue1, parms.opt_sortable_debug)
end
local parts = {}
local parts = {}
for part = 1, 2 do
for part = 1, 2 do
Line 3,290: Line 3,305:
end
end
end
end
local out_first
local imax = combos and #combos or 1 -- 1 (single unit) or number of unit tables
local imax = combos and #combos or 1 -- 1 (single unit) or number of unit tables
for i = 1, imax do
for i = 1, imax do
Line 3,309: Line 3,325:
table.insert(outputs, item)
table.insert(outputs, item)
end
end
if parms.opt_sortable_out then
local sep = parms.table_joins and parms.table_joins[2] or parms.join_between
local value
parts[part] = parms.opt_input_unit_only and '' or table.concat(outputs, sep)
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
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] = table.concat(outputs, sep)
end
end
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 parms.opt_sortable_in then
if i == 1 or parms.table_joins then
value = invalue1
parts[i] = sortkey .. 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
end
end
parts[1] = ntsh((value or 0), parms.opt_sortable_debug) .. parts[1]
end
end
local wikitext
local wikitext