Module:Convert
From Zoophilia Wiki
Documentation for this module may be created at Module:Convert/doc
--[[
Later TODO Too many items to list, but following are some points:
- Some conversions require two outputs: {{convert|55|nmi|km mi}}.
- Some units have two values: {{convert|3.21|m|ftin}}.
- At least one unit has a plural symbol (our output is "(30 acre)"):
{{convert|12|ha}} --> 12 hectares (30 acres)
]]
-- Conversion data is defined in another module because it is too large
-- to be conveniently included here.
-- A testing program can set the global variable 'is_test_run'.
local convertdata = require(is_test_run and "convertdata" or "Module:Convertdata")
local units = convertdata.units
local defaultunits = convertdata.defaultunits
-- Configuration options to keep magic values in one location.
local config = {}
local function get_config(frame)
-- Return table of configuration options.
-- Unclear if this is currently needed, but it may help if adapting
-- code for a different wiki.
local cfg = {}
-- Following settings are defaults that can be overridden by template.
cfg.numdot = '.' -- decimal mark before fractional digits
cfg.numsep = ',' -- thousands separator for numbers (',', '.', or nil)
for k, v in frame:argumentPairs() do
cfg[k] = v -- arguments from template's {{#invoke:}}
end
-- Following settings are mandatory (to limit abuse).
cfg.maxsigfig = 20 -- maximum number of significant figures
return cfg
end
local function ntsh_complement(text)
-- Return text (string of digits) after subtracting each digit from 9.
local result = ''
local first, last = 1, #text
while first <= last do
local lenblock = last + 1 - first
if lenblock > 12 then
lenblock = 12
end
local block = tonumber(text:sub(first, first + lenblock - 1))
local nines = tonumber(string.rep('9', lenblock))
local fmt = '%0' .. tostring(lenblock) .. '.0f'
result = result .. fmt:format(nines - block)
first = first + lenblock
end
return result
end
local function ntsh(n, 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 == 'yes', output is in a box (not hidden).
-- This implements Template:Ntsh (number table sorting, hidden).
local result, i, f, style
if n >= 0 then
if n > 1e16 then
result = '~'
else
i, f = math.modf(n)
f = math.floor(1e6 * f)
result = string.format('&1%016.0f%06d', i, f)
end
else
n = -n
if n > 1e16 then
result = '!'
else
i, f = math.modf(n)
f = math.floor(1e6 * f)
result = string.format('%016.0f%06d', i, f)
result = '&0' .. ntsh_complement(result)
end
end
if debug == 'yes' then
style = 'border:1px solid'
else
style = 'display:none'
end
return '<span style="' .. style .. '">' .. result .. '</span>'
end
local function withseparator(text)
-- Return text with thousand separators inserted.
-- The given text is '0' or is a positive number like '12345.6789'.
-- Separator is inserted only in the integer part (not in fraction).
-- Four-digit integer parts have a separator (like '1,234').
local numsep = config.numsep
if #numsep == 0 then
return text
end
local function insert(text, last)
local result = ''
while last >= 1 do
if last >= 4 then
result = numsep .. text:sub(last-2, last) .. result
last = last - 3
else
return text:sub(1, last) .. result
end
end
return result
end
local last = text:find(config.numdot, 1, true)
if last == nil then
last = #text
else
last = last - 1
end
return insert(text, last) .. text:sub(last+1)
end
local function formatnumber(value, sigfig)
-- Return result of converting number 'value' to a string,
-- rounded to 'sigfig' significant figures.
local format = string.format
local rep = string.rep
local sign = ''
local numdot = config.numdot
local function zeropad(text, dot)
local count = sigfig - #text
if count <= 0 then
return text
end
return text .. dot .. rep('0', count)
end
if sigfig <= 0 then
sigfig = 1
elseif sigfig > config.maxsigfig then
sigfig = config.maxsigfig
end
if value == 0 then
return zeropad('0', numdot)
end
if value < 0 then
sign = '-' -- need proper Unicode minus
value = -value
end
local digits
local exp, frac = math.modf(math.log10(value))
if frac == 0 then
-- Value 1 gives frac = 0, and 0.1 gives frac = -0 (negative zero).
-- Both results give true in 'if frac == 0'.
digits = zeropad('1', '')
exp = exp + 1 -- adjust so dot is before digits
else
local prec = sigfig
if value > 1 then
prec = prec - 1 -- will be one sig fig before dot
end
digits = format(format('%%.%df', prec), 10^frac)
if value < 1 then
-- Is MediaWiki run in a locale where following might be '0,'?
assert(digits:sub(1, 2) == '0.', 'Bug: rounded number not 0.xxx')
digits = digits:sub(3)
else
if prec == 0 then
assert(digits:find(numdot, 1, true) == nil, 'Bug: unexpected dot')
else
assert(digits:sub(2, 2) == numdot, 'Bug: rounded number not x.xxx')
digits = digits:sub(1, 1) .. digits:sub(3)
end
exp = exp + 1 -- adjust so dot is before digits
end
end
if exp >= #digits then
digits = digits .. rep('0', exp - #digits) -- result has no dot
elseif exp <= 0 then
digits = '0' .. numdot .. rep('0', -exp) .. digits
else
digits = digits:sub(1, exp) .. numdot .. digits:sub(exp+1)
end
return sign .. digits
end
local function changesign(text)
-- Change sign of text for correct appearance because it is negated.
if text:sub(1, 1) == '-' then
return text:sub(2)
end
return '-' .. text
end
local function useminus(text)
-- Return text with Unicode minus instead of '-', if present.
if text:sub(1, 1) == '-' then
return '−' .. text:sub(2)
end
return text
end
-- Fraction output format is the same as used by the old Template:Convert.
-- frac1: sign, numerator, denominator
-- frac2: wholenumber, sign, numerator, denominator
local frac1 = '<span style="white-space:nowrap">%s<sup>%s</sup>⁄<sub>%s</sub></span>'
local frac2 = '<span class="frac nowrap">%s<s style="display:none">%s</s><sup>%s</sup>⁄<sub>%s</sub></span>'
local function extract_fraction(text, negative)
-- If text represents a fraction, return value, show where
-- value is a number and show is a string.
-- Otherwise, return nil.
--
-- 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)'
-- 2 - 3 / 8 1.625, '2(−3/8)' -2.375, '−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) are also accepted (like old Template:Convert).
-- Template interprets '1.23e+2+12/24' as '123(12/24)' = 123.5!
local lhs, negfrac, rhs, numstr, numerator, denstr, denominator, wholestr, value
lhs, denstr = text:match('^%s*([^/]-)%s*/%s*(.-)%s*$')
denominator = tonumber(denstr)
if denominator == nil then return nil end
wholestr, negfrac, rhs = lhs:match('^%s*(.-[^eE])%s*([+-])%s*(.-)%s*$')
if wholestr == nil or wholestr == '' then
wholestr = nil
whole = 0
numstr = lhs
else
whole = tonumber(wholestr)
if whole == nil then return nil end
numstr = rhs
end
negfrac = (negfrac == '-')
numerator = tonumber(numstr)
if numerator == nil then return nil end
if negative == negfrac or wholestr == nil then
value = whole + numerator / denominator
else
value = whole - numerator / denominator
numstr = changesign(numstr)
end
if tostring(value):find('#', 1, true) then
return nil -- overflow or similar
end
numstr = useminus(numstr)
denstr = useminus(denstr)
local wikitext
if wholestr then
local sign = negative and '−' or '+'
if negative then
wholestr = changesign(wholestr)
end
wikitext = frac2:format(useminus(wholestr), sign, numstr, denstr)
else
local sign = negative and '−' or ''
wikitext = frac1:format(sign, numstr, denstr)
end
return value, wikitext
end
local missing = { 'Need value', 'Need second value' }
local invalid = { 'Value "%s" must be a number', 'Second value "%s" must be a number' }
local function extract_number(args, index, which)
-- Return true if successfully extract a number from the text in args[index].
-- Otherwise, return false, message.
-- Parameter 'which' (1 or 2) selects which input value is being processed.
-- Before processing, the input text is cleaned:
-- * Any thousand separators (valid or not) are removed.
-- * Any sign (and optional following whitespace) is replaced with
-- '-' (if negative) or '' (otherwise).
-- That replaces Unicode minus with '-'.
-- If successful, following elements in table args are updated
-- (first row applies if which == 1, second otherwise):
-- args.in_value1, args.in_singular1, args.in_clean1, args.in_show1
-- args.in_value2, args.in_singular2, args.in_clean2, args.in_show2
-- Value is a valid number.
-- Singular is true if value is 1 (singular form of units will be used).
-- Singular is false if value is -1 (that's how old Template:Convert worked).
-- Clean is cleaned text with any separators and sign removed.
-- Show is text formatted for output:
-- * Thousand separators are inserted.
-- * If negative, a Unicode minus is used; otherwise the sign
-- is '+' (if the input text used '+'), or is ''.
local text = args[index]
if text == nil or text == '' then return false, missing[which] end
local clean, sign
local numsep = config.numsep
if numsep == '' then
clean = text
else
clean = text:gsub('[' .. numsep .. ']', '') -- use '[.]' if numsep is '.'
end
-- Remove any sign character (assuming a number starts with '.' or a digit).
sign, clean = clean:match('^%s*([^ .%d]*)%s*(.*)')
if sign == nil or clean == nil then
return false, missing[which] -- should never occur
end
local propersign, negative
if sign == '−' or sign == '-' then
propersign = '−' -- Unicode U+2212 MINUS SIGN (UTF-8: e2 88 92)
negative = true
elseif sign == '+' then
propersign = '+'
negative = false
elseif sign == '' then
propersign = ''
negative = false
else
return false, (invalid[which]):format(text)
end
local show, singular
local value = tonumber(clean)
if value == nil then
value, show = extract_fraction(clean, negative)
if value == nil then
return false, (invalid[which]):format(text)
end
singular = false -- any fraction (even with value 1) is regarded as plural
end
if show == nil then
singular = (value == 1)
-- TODO Someone has to change text to use exponents for very large/small.
show = propersign .. withseparator(clean)
end
if negative and (value ~= 0) then
value = -value
end
if which == 1 then
args.in_value1 = value
args.in_singular1 = singular
args.in_clean1 = clean
args.in_show1 = show
else
args.in_value2 = value
args.in_singular2 = singular
args.in_clean2 = clean
args.in_show2 = show
end
return true
end
local function require_integer(text, missing, invalid)
-- Return true, n where n = integer equivalent to given text (or false, message).
-- Input should be the text for a simple integer (no separators, no Unicode minus).
-- Using regex avoids irritations with input like '-0.000001'.
if text == nil then return false, missing end
if string.match(text, '^-?%d+$') == nil then
return false, invalid:format(text)
end
return true, tonumber(text)
end
local function get_parms(pframe)
-- Return true, t where t is a table with all arguments passed to the
-- template converted to named arguments.
-- Except for range, which is nil or a table, the named args that are
-- added here could be provided by the user of the template.
local range_types = { -- text to separate input, output ranges
['and'] = {' and ', ' and '},
['by'] = {' by ', ' by '},
['to'] = {' to ', ' to '},
['-'] = {'–', '–'},
['to(-)'] = {' to ', '–'},
['x'] = {' by ', ' × '},
['+/-'] = {' ± ', ' ± '},
}
local success, msg
local args = {} -- arguments passed to template
for k, v in pframe:argumentPairs() do
args[k] = v
end
success, msg = extract_number(args, 1, 1)
if not success then return success, msg end
local in_unit = args[2]
local i = 3
local range = range_types[in_unit]
if range ~= nil then
success, msg = extract_number(args, 3, 2)
if not success then return success, msg end
in_unit = args[4]
i = 5
end
local out_unit = args[i]
local round_to = args[i+1]
if in_unit == nil then return false, 'Need input unit' end
args.in_unit = in_unit
args.out_unit = out_unit
args.range = range
args.round_to = args.round_to or round_to -- allow named parameter
return true, args
end
local function default_roundto(inclean, factor)
-- Return a default value for round_to (an integer like 2, 0, -2).
-- prec = (precision implied in inclean)
-- = (#digits after dot, or negative of #zeroes before dot)
-- If conversion is multiplication by a factor, and
-- if factor >= 0.02, compensate prec by adding N where:
-- N factor is in range
-- 1 .02 : .2 = .1/5 : .1*2
-- 0 .2 : 2 = 1/5 : 1*2
-- -1 2 : 20 = 10/5 : 10*2
-- -2 20 : 200 = 100/5 : 100*2 etc.
-- TODO Exception required for temperature.
local prec = 0
local dot = inclean:find('.', 1, true)
-- TODO SHOULD DO THIS? local dot = inclean:find(config.numdot, 1, true)
if dot ~= nil then
prec = inclean:sub(dot+1):len()
if prec == 0 then
inclean = inclean:sub(1, -2)
end
end
if prec == 0 then
prec = inclean:match('0*$'):len()
if prec ~= 0 then
prec = -prec
end
end
if factor ~= nil then
factor = math.abs(factor)
if factor >= 0.02 then
prec = prec - math.floor(math.log10(factor*5))
end
end
return prec
end
local function convert(value, in_unit, out_unit)
local inscale = in_unit.scale
local outscale = out_unit.scale
if in_unit.invert ~= nil then
if in_unit.invert * out_unit.invert < 0 then
return 1 / (value * inscale * outscale)
end
return value * (inscale / outscale)
elseif in_unit.offset ~= nil then
return (value - in_unit.offset) * (inscale / outscale) + out_unit.offset
else
return value * (inscale / outscale)
end
end
local function cvtround(invalue, inclean, parms)
-- Return true, show, singular
-- where
-- show = '' if invalue is nil or ''. Otherwise:
-- show = rounded, formatted string from converting invalue,
-- using the rounding specified in parms.
-- singular = true if result is positive, and (after rounding)
-- is "1", or like "1.00".
-- or return false, message if problem.
-- This code combines convert/round because some rounding requires
-- knowledge of what we are converting.
-- TODO Lots of checking required. Will need tweaks for special cases
-- handled by old Template:Convert.
-- TODO Limit values to avoid abuse (for example, can currently set
-- round_to to very large values like 999).
local show, singular = '', false
if invalue == nil or invalue == '' then return true, show, singular end
local outvalue = convert(invalue, parms.in_unit_table, parms.out_unit_table)
local propersign
if outvalue < 0 then
propersign = '−' -- Unicode U+2212 MINUS SIGN
outvalue = -outvalue
else
if outvalue == 0 then
outvalue = 0 -- ensure no silly -0 issues
end
propersign = ''
end
local round_to = parms.round_to
local sigfig = parms.sigfig
local disp = parms.disp
local auto = false
local success
if round_to then
-- Ignore sigfig, disp.
success, round_to = require_integer(round_to, 'Need value', 'round_to "%s" must be an integer')
if not success then return false, round_to end
elseif sigfig then
-- Ignore disp.
success, sigfig = require_integer(sigfig, 'Need value', 'sigfig "%s" must be an integer')
if not success then return false, sigfig end
if sigfig <= 0 then
local msg = 'sigfig "%s" must be positive'
return false, msg:format(parms.sigfig)
end
show = formatnumber(outvalue, sigfig)
elseif disp == '5' then
outvalue = math.floor((outvalue / 5) + 0.5) * 5
show = string.format('%.0f', outvalue)
else
auto = true -- using default rounding
-- TODO If conversion is not multiplication by a number, need factor = nil.
local factor
if invalue == 0 then
factor = 1 -- LATER is this correct?
else
factor = outvalue / invalue
end
round_to = default_roundto(inclean, factor)
end
if round_to then
if round_to >= 0 then
if auto then
-- TODO No less than two significant figures.
end
local fmt = '%.' .. string.format('%d', round_to) .. 'f'
show = string.format(fmt, outvalue)
else
-- This always keeps two sig figs. Should that be done if not auto?
round_to = -round_to -- #digits that want to zero
local maxzeroes = 0 -- maximum #digits that should be zeroed
if outvalue > 100 then
maxzeroes = math.modf(math.log10(outvalue)) - 1
end
if round_to > maxzeroes then
round_to = maxzeroes
end
if round_to > 0 then
local scaled = string.format('%.0f', outvalue/(10^round_to))
show = scaled .. string.rep('0', round_to)
else
show = formatnumber(outvalue, 2) -- can't zero digits; keep 2 sig figs
end
end
end
if (show == '1' or show:match('^1%.0*$') ~= nil) and propersign == '' then
-- Use match because on some systems 0.99999999999999999 is 1.0.
singular = true
end
-- TODO Someone has to change text to use exponents for very large/small.
return true, propersign .. withseparator(show), singular
end
local function linked(in_id, out_id, parms)
-- Return in_id, out_id after modifying none, one, or both by replacing
-- the text with a wikilink, if requested in template.
local function substitute(link)
-- TODO Work out how to insert a reasonable prefix in %s.
return link:gsub('%%s', 'xxx', 1)
end
local lk = parms.lk
if lk == 'in' or lk == 'on' then
local link = parms.in_unit_table.link
if link ~= nil then
link = substitute(link)
in_id = '[[' .. link .. '|' .. in_id .. ']]'
end
end
if lk == 'out' or lk == 'on' then
local link = parms.out_unit_table.link
if link ~= nil then
link = substitute(link)
out_id = '[[' .. link .. '|' .. out_id .. ']]'
end
end
return in_id, out_id
end
local disp_single = {
['or'] = '%s %s or %s %s',
['sqbr'] = '%s %s [%s %s]',
['comma'] = '%s %s, %s %s',
['slash'] = '%s %s / %s %s',
['s'] = '%s %s / %s %s',
['b'] = '%s %s (%s %s)',
}
local disp_double = {
['or'] = '%s%s%s %s or %s%s%s %s',
['sqbr'] = '%s%s%s %s [%s%s%s %s]',
['comma'] = '%s%s%s %s, %s%s%s %s',
['slash'] = '%s%s%s %s / %s%s%s %s',
['s'] = '%s%s%s %s / %s%s%s %s',
['b'] = '%s%s%s %s (%s%s%s %s)',
}
local function process(parms)
-- Return true, s where s = final wikitext result (or false, message).
-- TODO Clean up: avoid repeatedly accessing the same field in parms,
-- and avoid the clumsy calculation of items that are not needed.
local success, t
success, t = units:lookup(parms.in_unit)
if success then parms.in_unit_table = t else return success, t end
if parms.out_unit == nil then -- need to catch empty string also?
success, t = defaultunits:lookup(parms.in_unit_table)
if success then parms.out_unit = t else return success, t end
end
success, t = units:lookup(parms.out_unit, true)
if success then parms.out_unit_table = t else return success, t end
local in_utype = parms.in_unit_table.utype
if in_utype ~= parms.out_unit_table.utype then
local msg = 'Cannot convert %s to %s.[[Category:Convert dimension mismatch]]'
return false, msg:format(in_utype, parms.out_unit_table.utype)
end
if parms.out_unit_table.combination then
local msg = 'Combinations not implemented, %s'
return false, msg:format(parms.out_unit)
end
local outshow1, outshow2, outsingular1, outsingular2
success, outshow1, outsingular1 = cvtround(parms.in_value1, parms.in_clean1, parms)
if not success then return success, outshow1 end
success, outshow2, outsingular2 = cvtround(parms.in_value2, parms.in_clean2, parms)
if not success then return success, outshow2 end
local range = parms.range
local disp = parms.disp
local wikitext
local inshow1, inshow2 = parms.in_show1, parms.in_show2
local inkey, outkey = 'name2', 'name2'
local insymkey, outsymkey = 'symbol', 'symbol'
if parms.in_singular1 then -- TODO how process second input value?
inkey = 'name1'
end
if outsingular1 then
outkey = 'name1'
end
if parms.sp == 'us' or parms.in_unit_table.sp_us then
inkey = inkey .. '_us'
insymkey = 'sym_us'
end
if parms.sp == 'us' or parms.out_unit_table.sp_us then
outkey = outkey .. '_us'
outsymkey = 'sym_us'
end
if parms.in_unit_table.usename then
insymkey = inkey
end
if parms.out_unit_table.usename then
outsymkey = outkey
end
local in_name = parms.in_unit_table[inkey] -- will not need to calculate all of these
local in_symbol = parms.in_unit_table[insymkey]
local out_name = parms.out_unit_table[outkey]
local out_symbol = parms.out_unit_table[outsymkey]
local abbr = parms.abbr
local in_id, out_id = in_symbol, out_symbol
local istemperature = (in_utype == 'temperature')
if abbr == 'on' then -- all symbols
-- Both symbols.
elseif abbr == 'off' then -- all names
in_id = in_name
out_id = out_name
elseif abbr == 'in' then -- input symbols
-- Both symbols.
elseif abbr == 'out' then -- output symbols [is this just the default?]
if not istemperature then
in_id = in_name
end
elseif abbr == 'values' then -- show only values
-- TODO Probably more needed (no preceding space for a start).
in_id = ''
out_id = ''
elseif abbr == 'mos' then -- for ranges, abbreviate with input unit repeated
-- LATER
else -- default
if not istemperature then
in_id = in_name
end
end
in_id, out_id = linked(in_id, out_id, parms)
if range == nil then
if disp == 'output only' then
wikitext = '%s %s'
wikitext = wikitext:format(outshow1, out_id)
elseif disp == 'output number only' or disp == 'number' then
wikitext = outshow1
elseif disp == 'unit' then
wikitext = in_id
elseif disp == 'unit2' then
wikitext = out_id
elseif disp == 'flip' then
wikitext = disp_single['b']
wikitext = wikitext:format(outshow1, out_id, inshow1, in_id)
else
wikitext = disp_single[disp] or disp_single['b']
wikitext = wikitext:format(inshow1, in_id, outshow1, out_id)
end
else
-- TODO Need in_id, out_id (and more) here.
wikitext = disp_double[disp] or disp_double['b']
wikitext = wikitext:format(inshow1, range[1], inshow2, in_id, outshow1, range[2], outshow2, out_id)
end
if parms.sortable == 'on' then
wikitext = ntsh(parms.in_value1, parms.debug) .. wikitext
end
return true, wikitext
end
local p = {}
-- A testing program can set the global variable 'is_test_run'.
-- The following sets global variable 'mw' to simulate what Scribunto will do.
local bodge = require(is_test_run and "mw" or "Module:mw") -- fix up mw.text.tag
function p.convert(frame)
config = get_config(frame)
local pframe = frame:getParent()
local success, parms, text
success, parms = get_parms(pframe)
if success then
success, text = process(parms)
end
if not success then
local params = {style="color:black; background-color:orange;"}
text = mw.text.tag({name="span", contents="[[Module talk:Convert|Conversion error]]: " .. text, params=params})
end
return text
end
return p