Module:Convert: Difference between revisions

From Zoophilia Wiki
Jump to navigationJump to search
meta>Johnuniq
fix default expressions so they do not use loadstring (not available in Scribunto)
meta>Johnuniq
massive refactor; as much as possible, handle input and output unit in the same way; handle combinations (todo: ftin)
Line 1: Line 1:
--[[
-- Convert a value from one unit of measurement to another.
Later TODO Too many items to list, but following are some points:
-- Example: {{convert|123|lb|kg}} --> 123 pounds (56 kg)
- Some conversions require two outputs: {{convert|55|nmi|km mi}}.
- Some units have two values: {{convert|3.21|m|ftin}}.
- Some units use the plural name, not the symbol, example:
    {{convert|12|ha}} --should give--> 12 hectares (30 acres)
]]


-- Conversion data is defined in another module because it is too large
-- Conversion data is defined in another module because it is too large
Line 12: Line 7:
local convertdata = require(is_test_run and "convertdata" or "Module:Convertdata")
local convertdata = require(is_test_run and "convertdata" or "Module:Convertdata")
local units = convertdata.units
local units = convertdata.units
local defaultunits = convertdata.defaultunits
local default_exceptions = convertdata.default_exceptions
local link_exceptions = convertdata.link_exceptions
 
local MINUS = '−'  -- Unicode U+2212 MINUS SIGN (UTF-8: e2 88 92)


local function strip(text)
local function strip(text)
Line 128: Line 126:
     -- Return text with Unicode minus instead of '-', if present.
     -- Return text with Unicode minus instead of '-', if present.
     if text:sub(1, 1) == '-' then
     if text:sub(1, 1) == '-' then
         return '−' .. text:sub(2)
         return MINUS .. text:sub(2)
     end
     end
     return text
     return text
Line 218: Line 216:


local function format_number(show, exponent, isnegative)
local function format_number(show, exponent, isnegative)
     -- Return wikitext to display the value implied by the arguments:
     -- Return s, f where
     --   exponent is nil; and
    --  s = wikitext formatted to display the value implied by the arguments:
     --   show is a string of digits (no sign), with an optional dot;
     --     exponent is nil; and
     -- or:
     --     show is a string of digits (no sign), with an optional dot;
     --   exponent is a number (integer) indicating where dot should be;
     --     or:
     --   show is a string of digits (no sign and no dot; there is an
     --     exponent is a number (integer) indicating where dot should be;
     --       implied dot before show; show does not start with '0').
     --     show is a string of digits (no sign and no dot; there is an
     -- The result:
     --         implied dot before show; show does not start with '0').
    --  f = true if s uses scientific notation
     -- The formatted result:
     -- * Includes a Unicode minus if isnegative.
     -- * Includes a Unicode minus if isnegative.
     -- * Has numsep inserted where necessary.
     -- * Has numsep inserted where necessary.
Line 233: Line 233:
     local sign
     local sign
     if isnegative then
     if isnegative then
         sign = '−'  -- Unicode minus
         sign = MINUS
     else
     else
         sign = ''
         sign = ''
Line 269: Line 269:
         if exponent > 10 or exponent <= -4 or (exponent == 10 and show ~= '1000000000') then
         if exponent > 10 or exponent <= -4 or (exponent == 10 and show ~= '1000000000') then
             -- Rounded value satisfies: value >= 1e9 or value < 1e-4 (1e9 = 0.1e10).
             -- Rounded value satisfies: value >= 1e9 or value < 1e-4 (1e9 = 0.1e10).
             return sign .. with_exponent(show, exponent-1)
             return sign .. with_exponent(show, exponent-1), true
         end
         end
         if exponent >= #show then
         if exponent >= #show then
Line 339: Line 339:
     local wikitext
     local wikitext
     if wholestr then
     if wholestr then
         local sign = negative and '−' or '+'
         local sign = negative and MINUS or '+'
         if negative then
         if negative then
             wholestr = change_sign(wholestr)
             wholestr = change_sign(wholestr)
Line 345: Line 345:
         wikitext = frac2:format(use_minus(wholestr), sign, numstr, denstr)
         wikitext = frac2:format(use_minus(wholestr), sign, numstr, denstr)
     else
     else
         local sign = negative and '−' or ''
         local sign = negative and MINUS or ''
         wikitext = frac1:format(sign, numstr, denstr)
         wikitext = frac1:format(sign, numstr, denstr)
     end
     end
Line 355: Line 355:


local function extract_number(args, index, which)
local function extract_number(args, index, which)
     -- Return true if successfully extract a number from the text in args[index].
     -- Return true, info if can extract a number from the text in args[index],
    -- where info is a table with the result.
     -- Otherwise, return false, message.
     -- Otherwise, return false, message.
     -- Parameter 'which' (1 or 2) selects which input value is being processed.
     -- Parameter 'which' (1 or 2) selects which input value is being
    -- processed (to select the appropriate error message, if needed).
     -- Before processing, the input text is cleaned:
     -- Before processing, the input text is cleaned:
     -- * Any thousand separators (valid or not) are removed.
     -- * Any thousand separators (valid or not) are removed.
Line 363: Line 365:
     --  '-' (if negative) or '' (otherwise).
     --  '-' (if negative) or '' (otherwise).
     --  That replaces Unicode minus with '-'.
     --  That replaces Unicode minus with '-'.
     -- If successful, following elements in table args are updated
     -- If successful, the returned info table contains named fields:
    -- (first row applies if which == 1, second otherwise):
     --   value   = a valid number
     --    args.in_value1,  args.in_singular1,  args.in_clean1,  args.in_show1
     --   singular = true if value is 1 (to use singular form of units)
    --    args.in_value2,  args.in_singular2,  args.in_clean2,  args.in_show2
     --           = false if value is -1 (like old template)
    -- Value is a valid number.
     --   clean    = cleaned text with any separators and sign removed
     -- Singular is true if value is 1 (singular form of units will be used).
     --   show    = text formatted for output
     -- Singular is false if value is -1 (like old template).
    -- For show:
     -- Clean is cleaned text with any separators and sign removed.
     -- Show is text formatted for output:
     -- * Thousand separators are inserted.
     -- * Thousand separators are inserted.
     -- * If negative, a Unicode minus is used; otherwise the sign
     -- * If negative, a Unicode minus is used; otherwise the sign
Line 392: Line 392:
     end
     end
     local propersign, negative
     local propersign, negative
     if sign == '−' or sign == '-' then
     if sign == MINUS or sign == '-' then
         propersign = '−'  -- Unicode U+2212 MINUS SIGN (UTF-8: e2 88 92)
         propersign = MINUS
         negative = true
         negative = true
     elseif sign == '+' then
     elseif sign == '+' then
Line 414: Line 414:
     end
     end
     if show == nil then
     if show == nil then
         singular = (value == 1)
         singular = (value == 1 and not negative)
         show = propersign .. with_separator(clean)
         show = propersign .. with_separator(clean)
     end
     end
Line 420: Line 420:
         value = -value
         value = -value
     end
     end
     if which == 1 then
     return true, {
         args.in_value1 = value
         value = value,
         args.in_singular1 = singular
         singular = singular,
         args.in_clean1 = clean
         clean = clean,
         args.in_show1 = show
         show = show
    else
     }
        args.in_value2 = value
        args.in_singular2 = singular
        args.in_clean2 = clean
        args.in_show2 = show
     end
    return true
end
end


Line 446: Line 440:


local function get_parms(pframe)
local function get_parms(pframe)
     -- Return true, t where t is a table with all arguments passed to the
     -- If successful, return true, args, unit where
     -- template converted to named arguments, or return false, message.
    --  args is a table of all arguments passed to the template
     -- Except for range, which is nil or a table, the named args that are
     --       converted to named arguments, and
    -- added here could be provided by the user of the template.
    --  unit is the input unit table;
    -- otherwise, return false, message.
     -- Some of the named args that are added here could be provided by the
    -- user of the template.
     -- MediaWiki removes leading and trailing whitespace from the values of
     -- MediaWiki removes leading and trailing whitespace from the values of
     -- named arguments. However, the values of numbered arguments include any
     -- named arguments. However, the values of numbered arguments include any
Line 463: Line 460:
         ['+/-']  = {'&nbsp;±&nbsp;', '&nbsp;±&nbsp;'},
         ['+/-']  = {'&nbsp;±&nbsp;', '&nbsp;±&nbsp;'},
     }
     }
     local success, msg
     local success, info1, info2
     local args = {}  -- arguments passed to template
     local args = {}  -- arguments passed to template
     for k, v in pframe:argumentPairs() do
     for k, v in pframe:argumentPairs() do
         args[k] = v
         args[k] = v
     end
     end
     success, msg = extract_number(args, 1, 1)
     success, info1 = extract_number(args, 1, 1)
     if not success then return success, msg end
     if not success then return false, info1 end
     local in_unit, precision
     local in_unit, precision
     local next = strip(args[2])
     local next = strip(args[2])
Line 478: Line 475:
     else
     else
         args.range = range
         args.range = range
         success, msg = extract_number(args, 3, 2)
         success, info2 = extract_number(args, 3, 2)
         if not success then return success, msg end
         if not success then return false, info2 end
         in_unit = strip(args[4])
         in_unit = strip(args[4])
         i = 5
         i = 5
     end
     end
     if in_unit == nil then return false, 'Need input unit' end
    local success, in_unit_table = units:lookup(in_unit, args.sp)
     args.in_unit = in_unit
     if not success then return false, in_unit_table end
     in_unit_table.valinfo = { info1, info2 }  -- info2 is nil if no range
    in_unit_table.inout = 'in'  -- this is an input unit
     next = strip(args[i])
     next = strip(args[i])
     i = i + 1
     i = i + 1
Line 496: Line 495:
     else
     else
         precision = next
         precision = next
    end
    if args.adj == nil and args.sing ~= nil then
        args.adj = args.sing  -- sing (singular) is apparently an old equivalent of adj
     end
     end
     if args.adj == 'mid' then
     if args.adj == 'mid' then
Line 519: Line 521:
         end
         end
     end
     end
     if args.disp == 'x' then
     local disp = args.disp
    if disp == 'x' then
         args.joins = { args[i] or '', args[i+1] or '' }
         args.joins = { args[i] or '', args[i+1] or '' }
    elseif disp == 's' or disp == '/' then
        args.disp = 'slash'
     end
     end
     args.precision = args.precision or precision  -- allow named parameter
     args.precision = args.precision or precision  -- allow named parameter
     return true, args
    local abbr = args.abbr
    if abbr == nil then
        -- Default is to abbreviate output (use symbol), or input if flipped.
        args.abbr = (disp == 'flip') and 'in' or 'out'
    else
        args.abbr_org = abbr  -- original abbr (as entered by user)
        if disp == 'flip' then  -- 'in' = LHS, 'out' = RHS
            if abbr == 'in' then
                abbr = 'out'
            elseif abbr == 'out' then
                abbr = 'in'
            end
        end
        args.abbr = abbr
    end
     return true, args, in_unit_table
end
end


local function default_precision(inclean, invalue, outvalue, parms)
local function default_precision(inclean, invalue, outvalue, in_current, out_current)
     -- Return a default value for precision (an integer like 2, 0, -2).
     -- Return a default value for precision (an integer like 2, 0, -2).
     -- Code follows procedures used in old template.
     -- Code follows procedures used in old template.
Line 534: Line 554:
     local log10 = math.log10
     local log10 = math.log10
     local prec, minprec, adjust
     local prec, minprec, adjust
    local in_unit_table = parms.in_unit_table
     local utype = out_current.utype
    local out_unit_table = parms.out_unit_table
     local utype = out_unit_table.utype
     local fudge = 1e-14  -- {{Order of magnitude}} adds this, so we do too
     local fudge = 1e-14  -- {{Order of magnitude}} adds this, so we do too
     -- Find fractional digits, handling cases like inclean = '12.345e6'.
     -- Find fractional digits, handling cases like inclean = '12.345e6'.
     local integer, dot, fraction = inclean:match('^(%d*)([' .. config.numdot .. ']?)(%d*)')
     local integer, dot, fraction = inclean:match('^(%d*)([' .. config.numdot .. ']?)(%d*)')
     if utype == 'temperature' then
     if utype == 'temperature' then
         -- LATER: Give an error message if (invalue < in_unit_table.offset): below absolute zero?
         -- LATER: Give an error message if (invalue < in_current.offset): below absolute zero?
         adjust = 0
         adjust = 0
         local kelvin = (invalue - in_unit_table.offset) * in_unit_table.scale
         local kelvin = (invalue - in_current.offset) * in_current.scale
         if kelvin <= 0 then  -- can get zero, or small but negative value due to precision problems
         if kelvin <= 0 then  -- can get zero, or small but negative value due to precision problems
             minprec = 2
             minprec = 2
Line 555: Line 573:
             return 0
             return 0
         end
         end
         if out_unit_table.symbol == 'ft' and dot == '' then
         if out_current.symbol == 'ft' and dot == '' then
             -- More precision when output ft with integer input value.
             -- More precision when output ft with integer input value.
             adjust = -log10(in_unit_table.scale)
             adjust = -log10(in_current.scale)
         else
         else
             adjust = log10(math.abs(invalue / outvalue))
             adjust = log10(math.abs(invalue / outvalue))
Line 577: Line 595:
end
end


local function convert(value, in_unit, out_unit)
local function convert(value, in_current, out_current)
     local inscale = in_unit.scale
     local inscale = in_current.scale
     local outscale = out_unit.scale
     local outscale = out_current.scale
     if in_unit.invert ~= nil then
     if in_current.invert ~= nil then
         if in_unit.invert * out_unit.invert < 0 then
         if in_current.invert * out_current.invert < 0 then
             return 1 / (value * inscale * outscale)
             return 1 / (value * inscale * outscale)
         end
         end
         return value * (inscale / outscale)
         return value * (inscale / outscale)
     elseif in_unit.offset ~= nil then
     elseif in_current.offset ~= nil then
         return (value - in_unit.offset) * (inscale / outscale) + out_unit.offset
         return (value - in_current.offset) * (inscale / outscale) + out_current.offset
     else
     else
         return value * (inscale / outscale)
         return value * (inscale / outscale)
Line 592: Line 610:
end
end


local function cvtround(invalue, inclean, parms)
local function cvtround(parms, info, in_current, out_current)
     -- Return true, show, singular
     -- Return true, t where t is a table with the conversion results; fields:
    -- where
     --  show = rounded, formatted string from converting value in info,
    --  show = '' if invalue is nil or ''. Otherwise:
     --  show = rounded, formatted string from converting invalue,
     --      using the rounding specified in parms.
     --      using the rounding specified in parms.
     --  singular = true if result is positive, and (after rounding)
     --  singular = true if result is positive, and (after rounding)
     --      is "1", or like "1.00".
     --      is "1", or like "1.00";
    -- or return true, nil if no value specified;
     -- or return false, message if problem.
     -- or return false, message if problem.
     -- This code combines convert/round because some rounding requires
     -- This code combines convert/round because some rounding requires
Line 605: Line 622:
     -- TODO Limit values to avoid abuse (for example, can currently set
     -- TODO Limit values to avoid abuse (for example, can currently set
     -- precision to very large values like 999).
     -- precision to very large values like 999).
     local show, exponent, singular = '', nil, false
     local invalue, inclean, show, exponent, singular
    if info ~= nil then
        invalue, inclean = info.value, info.clean
    end
     if invalue == nil or invalue == '' then
     if invalue == nil or invalue == '' then
         return true, show, singular
         return true, nil
     end
     end
     local outvalue = convert(invalue, parms.in_unit_table, parms.out_unit_table)
     local outvalue = convert(invalue, in_current, out_current)
     local isnegative
     local isnegative
     if outvalue < 0 then
     if outvalue < 0 then
Line 636: Line 656:
         show = string.format('%.0f', outvalue)
         show = string.format('%.0f', outvalue)
     else
     else
         precision = default_precision(inclean, invalue, outvalue, parms)
         precision = default_precision(inclean, invalue, outvalue, in_current, out_current)
     end
     end
     if precision then
     if precision then
Line 661: Line 681:
         end
         end
     end
     end
    -- TODO Does following work when exponent ~= nil?
    --      What if show = '1000' and exponent = 1 (value = .1000*10^1 = 1)?
    --      What if show = '1000' and exponent = 2 (value = .1000*10^2 = 10)?
     if (show == '1' or show:match('^1%.0*$') ~= nil) and not isnegative then
     if (show == '1' or show:match('^1%.0*$') ~= nil) and not isnegative then
         -- Use match because on some systems 0.99999999999999999 is 1.0.
         -- Use match because on some systems 0.99999999999999999 is 1.0.
         singular = true
         singular = true
     end
     end
     return true, format_number(show, exponent, isnegative), singular
     local formatted, is_scientific = format_number(show, exponent, isnegative)
end
     return true, {
 
         show = formatted,
local function linked(in_id, out_id, parms)
         is_scientific = is_scientific,
    -- Return in_id, out_id after modifying none, one, or both by replacing
         singular = singular,
     -- the text with a wikilink, if requested in template.
         clean = show,  -- unformatted show (can be used to calculate rounded value)
    local function substitute(link)
        exponent = exponent,
        -- 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
end


-- TODO Think about when to use ' ' and when to use '&nbsp;'.
-- TODO Think about when to use ' ' and when to use '&nbsp;'.
-- Old template always uses nbsp before a unit symbol, but seems inconsistent
-- Old template always uses nbsp before a unit symbol, but seems inconsistent
-- before a unit name. Something suggested value 1000 was a threshold
-- before a unit name. Template:Convert/LoffAonSoff says
-- (use nbsp for smaller values), but no conclusive results.
--  "Numbers less than 1,000 is not wrapped nor are unit symbols"
-- so 1000 is a threshold (use nbsp for smaller values), but no conclusive results.
-- Possibly a concern is wrapping when using {{convert}} in a table
-- Possibly a concern is wrapping when using {{convert}} in a table
-- (don't want to force a column to be unnecessarily wide by using nbsp).
-- (don't want to force a column to be unnecessarily wide by using nbsp).
local disp_joins = {
local disp_joins = {
     ['or']   = { ' or ', ''  },
     ['or']         = { ' or '   , ''  },
     ['sqbr']  = { ' [' , ']' },
     ['sqbr-sp']    = { ' ['      , ']' },
     ['comma'] = { ', '  , ''  },
    ['sqbr-nbsp']  = { '&nbsp;[' , ']' },
     ['slash'] = { ' / ' , ''  },
     ['comma']     = { ', '      , '},
     ['s']     = { ' / ' , ''  },
    ['slash-sp']  = { ' / '    , ''  },
     ['b']     = { ' (' , ')' },
     ['slash-nbsp'] = { '&nbsp;/ ', ''  },
     ['slash-nosp'] = { '/'       , ''  },
     ['b']         = { ' ('     , ')' },
}
}


Line 765: Line 773:
     -- evaluates 'v < 120' as a boolean with result
     -- evaluates 'v < 120' as a boolean with result
     -- 'smallsuffix' if (value < 120), or 'bigsuffix' otherwise.
     -- 'smallsuffix' if (value < 120), or 'bigsuffix' otherwise.
     local success, default = defaultunits:lookup(unit_table)
     local default = default_exceptions[unit_table.symbol] or unit_table.default
     if success then
    if default == nil then
        if default:find('|', 1, true) == nil then
        local msg = 'Unit "%s" has no default output unit.[[Category:Convert unknown unit]]'
        return false, msg:format(unit_table.symbol)
     end
    if default:find('|', 1, true) == nil then
        return true, default
    end
    local t = {}
    default = default .. '|'  -- to get last item
    for item in default:gmatch('%s*(.-)%s*|') do
        table.insert(t, item)  -- split on '|', removing leading/trailing whitespace
    end
    if #t == 3 or #t == 4 then
        local success, result = pcall(evaluate_condition, value, t[1])
        if success then
            default = result and t[2] or t[3]
            if #t == 4 then
                default = default .. t[4]
            end
             return true, default
             return true, default
         end
         end
        local t = {}
        default = default .. '|'  -- to get last item
        for item in default:gmatch('%s*(.-)%s*|') do
            table.insert(t, item)  -- split on '|', removing leading/trailing whitespace
        end
        if #t == 3 or #t == 4 then
            local success, result = pcall(evaluate_condition, value, t[1])
            if success then
                default = result and t[2] or t[3]
                if #t == 4 then
                    default = default .. t[4]
                end
                return true, default
            end
        end
        local msg = 'Unit "%s" has an invalid default.[[Category:Convert unknown unit]]'
        default = msg:format(unit_table.prefix .. unit_table.baseunit)
     end
     end
    local msg = 'Unit "%s" has an invalid default.[[Category:Convert unknown unit]]'
    default = msg:format(unit_table.symbol)
     return false, default
     return false, default
end
end


local function process(parms)
local function make_id(parms, which, unit_table)
     -- Return true, s where s = final wikitext result (or false, message).
     -- Return id (unit symbol or name, possibly modified)
     -- TODO Clean up: avoid repeatedly accessing the same field in parms,
     -- for value 1 or 2 (which), and 'in' or 'out' (unit_table.inout).
    -- and avoid the clumsy calculation of items that are not needed.
     local abbr = parms.abbr
     local success, t
     if abbr == 'values' then
    success, t = units:lookup(parms.in_unit, parms.sp)
         return ''
    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 = get_default(parms.in_value1, parms.in_unit_table)
        if success then parms.out_unit = t else return success, t end
     end
     end
     success, t = units:lookup(parms.out_unit, parms.sp, true)
     local inout = unit_table.inout
     if success then parms.out_unit_table = t else return success, t end
    local valinfo = unit_table.valinfo
     local in_utype = parms.in_unit_table.utype
    local abbr_org = parms.abbr_org
     if in_utype ~= parms.out_unit_table.utype then
     local adj = parms.adj
        local msg = 'Cannot convert "%s" to "%s".[[Category:Convert dimension mismatch]]'
     local disp = parms.disp
        return false, msg:format(in_utype, parms.out_unit_table.utype)
    local singular = valinfo[which].singular or (inout == 'in' and adj == 'on')
     if unit_table.usename then
        -- Old template does something like this.
        if inout == 'in' then
            if adj ~= 'on' and (abbr_org == 'out' or disp == 'flip') then
                local value = valinfo[which].value
                singular = (0 < value and value < 1.0001)
            end
        else
            if (abbr_org == 'on') or
              (disp == nil and (abbr_org == nil or abbr_org == 'out')) or
              (disp == 'flip' and abbr_org == 'in') then
                local clean, exponent = valinfo[which].clean, valinfo[which].exponent
                local value = tonumber(clean)  -- absolute value (any negative sign has been ignored)
                if exponent ~= nil then
                    value = value * 10^exponent
                end
                singular = (value < 1.0001 and not valinfo[which].is_scientific)
            end
        end
     end
     end
     if parms.out_unit_table.combination then
    local key_name = 'name2'
         local msg = 'Combinations not implemented, "%s"'
    local key_symbol = 'symbol'
        return false, msg:format(parms.out_unit)
     if singular then
         key_name = 'name1'
     end
     end
    local outshow1, outshow2, outsingular1, outsingular2
     if parms.sp == 'us' or unit_table.sp_us then
    success, outshow1, outsingular1 = cvtround(parms.in_value1, parms.in_clean1, parms)
         key_name = key_name .. '_us'
    if not success then return success, outshow1 end
         key_symbol = 'sym_us'
    success, outshow2, outsingular2 = cvtround(parms.in_value2, parms.in_clean2, parms)
     if not success then return success, outshow2 end
    local inshow1, inshow2 = parms.in_show1, parms.in_show2
    local inkey, outkey = 'name2', 'name2'
    local insymkey, outsymkey = 'symbol', 'symbol'
    if parms.adj == 'on' then -- TODO how process second input value?
         inkey = 'name1'
    elseif parms.in_singular1 then  -- TODO how process second input value?
         inkey = 'name1'
     end
     end
     if outsingular1 then
    local id = unit_table[key_name]
         outkey = 'name1'
     if not unit_table.usename then
         if ( abbr == 'on' or abbr == inout or (abbr == 'mos' and inout == 'out') or
            (abbr_org == nil and unit_table.utype == 'temperature')) and not
          (abbr_org == nil and (disp == 'or' or disp == 'slash')) then
            id = unit_table[key_symbol]
        end
     end
     end
     if parms.sp == 'us' or parms.in_unit_table.sp_us then
     local lk = parms.lk
        inkey = inkey .. '_us'
    if lk == 'on' or lk == inout then
         insymkey = 'sym_us'
        local link = link_exceptions[unit_table.symbol] or unit_table.link
        if link ~= nil then
            id = '[[' .. link .. '|' .. id .. ']]'
         end
     end
     end
     if parms.sp == 'us' or parms.out_unit_table.sp_us then
     return id
         outkey = outkey .. '_us'
end
         outsymkey = 'sym_us'
 
     end
local function process_input(parms, in_current)
    if parms.in_unit_table.usename then
    -- Processing required once per conversion.
         insymkey = inkey
    -- Return block of text to represent input (value/unit).
    end
    local id1 = make_id(parms, 1, in_current)
    if parms.out_unit_table.usename then
    local extra = ''
        outsymkey = outkey
    local result
     end
    local adj = parms.adj
    local in_name = parms.in_unit_table[inkey]      -- will not need to calculate all of these
    local disp = parms.disp
    local in_symbol = parms.in_unit_table[insymkey]
    if disp == 'output only' or
    local out_name = parms.out_unit_table[outkey]
      disp == 'output number only' or disp == 'number' or
    local out_symbol = parms.out_unit_table[outsymkey]
      disp == 'u2' or disp == 'unit2' then
    local abbr = parms.abbr
         result = ''
    local in_id, out_id = in_symbol, out_symbol
         parms.joins = { '', '' }
    local istemperature = (in_utype == 'temperature')
     elseif disp == 'unit' then
    if abbr == 'on' then           -- all symbols
        if adj == 'on' then
        -- Both symbols.
            result = hyphenated(id1)
    elseif abbr == 'off' then      -- all names
         else
         in_id = in_name
            result = id1
         out_id = out_name
        end
    elseif abbr == 'in' then       -- input symbols
        parms.joins = { '', '' }
         -- Both symbols.
     else
    elseif abbr == 'out' then      -- output symbols [is this just the default?]
        local abbr = parms.abbr
        if not istemperature then
        local mos = (abbr == 'mos')
             in_id = in_name
        local range = parms.range
        local id = (range == nil) and id1 or make_id(parms, 2, in_current)
        if id ~= '' then
            if adj == 'on' then
                mos = false  -- if hyphenated, suppress repeat of unit in a range
                extra = '-' .. hyphenated(id) .. parms.mid
            else
                extra = '&nbsp;' .. id
            end
         end
         local valinfo = in_current.valinfo
        if range == nil then
            result = valinfo[1].show
         else
            if mos then
                result = valinfo[1].show .. '&nbsp;' .. id1 .. range[1] .. valinfo[2].show
            else
                result = valinfo[1].show .. range[1] .. valinfo[2].show
             end
         end
         end
    elseif abbr == 'values' then   -- show only values
        if disp == nil then -- special case the most common setting
         -- TODO Probably more needed (no preceding space for a start).
            parms.joins = disp_joins['b']
        in_id = ''
         elseif disp ~= 'x' then
        out_id = ''
            -- Old template does this.
    elseif abbr == 'mos' then       -- for ranges, abbreviate with input unit repeated
            if disp == 'slash' then
        -- LATER
                if parms.abbr_org == nil then
    else                           -- default
                    disp = 'slash-nbsp'
        if not istemperature then
                elseif abbr == 'in' or abbr == 'out' then
             in_id = in_name
                    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
         end
     end
     end
     in_id, out_id = linked(in_id, out_id, parms)
     return result .. extra
     ---BEGIN TO-DO------------Put following in cvtround---------------------
end
     local range = parms.range
 
    local in_block, in_extra, out_block
local function process_one_output(parms, out_current)
    if parms.adj == 'on' then
     -- Processing required for each output unit.
        in_extra = '-' .. hyphenated(in_id) .. parms.mid
    -- Return block of text to represent output (value/unit).
     else
     local id1 = make_id(parms, 1, out_current)
        in_extra = '&nbsp;' .. in_id
     local extra = ''
     end
     local result
    if range == nil then
        in_block = inshow1
        out_block = outshow1
    else
        in_block = inshow1 .. range[1] .. inshow2
        out_block = outshow1 .. range[2] .. outshow2
    end
     local disp = parms.disp
     local disp = parms.disp
     if disp == 'output only' then
     if disp == 'u2' or disp == 'unit2' then -- 'unit2' is not in old template
        in_block = ''
        out_block = out_block .. ' ' .. out_id
        parms.joins = { '', '' }
    elseif disp == 'output number only' or disp == 'number' then
        in_block = ''
        out_block = out_block
        parms.joins = { '', '' }
    elseif disp == 'unit' then
         if parms.adj == 'on' then
         if parms.adj == 'on' then
             in_block = hyphenated(in_id)
             result = hyphenated(id1)
         else
         else
             in_block = in_id
             result = id1
         end
         end
        out_block = ''
        parms.joins = { '', '' }
    elseif disp == 'unit2' then  -- is 'unit2' in old template?
        in_block = ''
        out_block = out_id
        parms.joins = { '', '' }
    elseif disp == 'flip' then
        in_block = out_block .. '&nbsp;' .. out_id
        out_block = in_block .. in_extra
        parms.joins = disp_joins['b']
    elseif disp == 'x' then
        in_block = in_block .. in_extra
        out_block = out_block .. '&nbsp;' .. out_id
     else
     else
         in_block = in_block .. in_extra
         local range = parms.range
         out_block = out_block .. '&nbsp;' .. out_id
         if not (disp == 'output number only' or disp == 'number') then
         parms.joins = disp_joins[disp] or disp_joins['b']
            local id = (range == nil) and id1 or make_id(parms, 2, out_current)
            if id ~= '' then
                extra = '&nbsp;' .. id
            end
        end
        local valinfo = out_current.valinfo
         if range == nil then
            result = valinfo[1].show
        else
            result = valinfo[1].show .. range[2] .. valinfo[2].show
        end
    end
    return result .. extra
end
 
local function process(parms, in_unit_table)
    -- Return true, s where s = final wikitext result (or false, message).
    local info = in_unit_table.valinfo
    local invalue1 = info[1].value
    if parms.out_unit == nil then          -- LATER need to catch empty string also?
        local success, t = get_default(invalue1, in_unit_table)
        if success then parms.out_unit = t else return false, t end
     end
     end
     parms.in_block = in_block
     local out_unit_table
     parms.out_block = out_block
    local success, t = units:lookup(parms.out_unit, parms.sp, true)
     ---END TO-DO------------------------------------------------------------
    if success then out_unit_table = t else return false, t end
     local wikitext = parms.in_block .. parms.joins[1] .. parms.out_block .. parms.joins[2]
     if in_unit_table.utype ~= out_unit_table.utype then
        local msg = 'Cannot convert "%s" to "%s".[[Category:Convert dimension mismatch]]'
        return false, msg:format(in_unit_table.utype, out_unit_table.utype)
    end
    local outputs = {}
     local combos = out_unit_table.combination  -- nil or table of unit tables
    local imax = combos and #combos or 1  -- 1 (single unit) or number of unit tables
    for i = 1, imax do
        local success, info1, info2
        local out_current = combos and combos[i] or out_unit_table
        out_current.inout = 'out'
        success, info1 = cvtround(parms, info[1], in_unit_table, out_current)
        if not success then return false, info1 end
        success, info2 = cvtround(parms, info[2], in_unit_table, out_current)
        if not success then return false, info2 end
        out_current.valinfo = { info1, info2 }
        table.insert(outputs, process_one_output(parms, out_current))
    end
    local disp = parms.disp
     local in_block = process_input(parms, in_unit_table)
    local out_block = (disp == 'unit') and '' or table.concat(outputs, '; ')
    if disp == 'flip' then
        in_block, out_block = out_block, in_block
    end
    local wikitext = in_block .. parms.joins[1] .. out_block .. parms.joins[2]
     if parms.sortable == 'on' then
     if parms.sortable == 'on' then
         wikitext = ntsh(parms.in_value1, parms.debug) .. wikitext
         wikitext = ntsh(invalue1, parms.debug) .. wikitext
     end
     end
     return true, wikitext
     return true, wikitext
Line 932: Line 1,003:


local p = {}
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.
-- 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
local bodge = require(is_test_run and "mw" or "Module:mw")  -- fix up mw.text.tag
Line 939: Line 1,009:
     config = get_config(frame)
     config = get_config(frame)
     local pframe = frame:getParent()
     local pframe = frame:getParent()
     local success, parms, text
     local text
     success, parms = get_parms(pframe)
     local success, parms, in_unit_table = get_parms(pframe)
     if success then
     if success then
         success, text = process(parms)
         success, text = process(parms, in_unit_table)
     else
     else
         text = parms
         text = parms

Revision as of 09:15, 28 January 2013

Documentation for this module may be created at Module:Convert/doc

-- Convert a value from one unit of measurement to another.
-- Example: {{convert|123|lb|kg}} --> 123 pounds (56 kg)

-- 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 default_exceptions = convertdata.default_exceptions
local link_exceptions = convertdata.link_exceptions

local MINUS = '−'  -- Unicode U+2212 MINUS SIGN (UTF-8: e2 88 92)

local function strip(text)
    -- If text is a string, return its content with no leading/trailing
    -- whitespace. Otherwise return nil.
    if type(text) == 'string' then
        return text:match("^%s*(.-)%s*$")
    end
end

-- Configuration options to keep magic values in one location.
local config = {}

local function get_config(frame)
    -- Return table of configuration options.
    local cfg = {
        -- Defaults that can be overridden by template.
        numdot = '.',       -- decimal mark before fractional digits
        numsep = ',',       -- thousands separator for numbers (',', '.', or nil)
        maxsigfig = 14,     -- maximum number of significant figures
    }
    for k, v in frame:argumentPairs() do
        cfg[k] = v          -- arguments from template's {{#invoke:}}
    end
    if cfg.maxsigfig > 20 then
        cfg.maxsigfig = 20  -- limit abuse
    end
    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 hyphenated(name)
    -- Return a hyphenated form of given name (for adjectival usage).
    -- This uses a simple and efficient procedure that works for most cases.
    -- Some units (if used) would require more, and can later think about
    -- adding a method to handle exceptions.
    -- The procedure is to replace each space with a hyphen, but
    -- not a space after ')' [for "(pre-1954&nbsp;US) nautical mile"], and
    -- not spaces immediately before '(' or in '(...)' [for cases like
    -- "British thermal unit (ISO)" and "Calorie (International Steam Table)"].
    local pos
    if name:sub(1, 1) == '(' then
        pos = name:find(')', 1, true)
        if pos ~= nil then
            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 ~= nil then
            return name:sub(1, pos-2):gsub(' ', '-') .. name:sub(pos-1)
        end
    end
    return name:gsub(' ', '-')
end

local function change_sign(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 use_minus(text)
    -- Return text with Unicode minus instead of '-', if present.
    if text:sub(1, 1) == '-' then
        return MINUS .. text:sub(2)
    end
    return text
end

local function with_separator(text)
    -- Return text with thousand separators inserted.
    -- The given text is like '123' or '12345.6789' or '1.23e45'
    -- (e notation can only occur when processing an input value).
    -- The text has no sign (caller inserts that later, if necessary).
    -- Separator is inserted only in the integer part of the significand
    -- (not after numdot, and not after 'e' or 'E').
    -- Four-digit integer parts have a separator (like '1,234').
    local numsep = config.numsep
    if numsep == '' then
        return text
    end
    local last = text:match('()[' .. config.numdot .. 'eE]')  -- () returns position
    if last == nil then
        last = #text
    else
        last = last - 1  -- index of last character before dot/e/E
    end
    if last >= 4 then
        local groups = {}
        local first = last % 3
        if first > 0 then
            table.insert(groups, text:sub(1, first))
        end
        first = first + 1
        while first < last do
            table.insert(groups, text:sub(first, first+2))
            first = first + 3
        end
        return table.concat(groups, numsep) .. text:sub(last+1)
    end
    return text
end

-- Input values can use values like 1.23e12, but are never displayed
-- using exponent notation like 1.23×10¹².
-- Very small or very large output values use exponent notation.
-- Use fmtpower:format(significand, exponent) where each arg is a string.
local fmtpower = '%s<span style="margin-left:0.2em">×<span style="margin-left:0.1em">10</span></span><sup>%s</sup>'

local function with_exponent(show, exponent)
    -- Return wikitext to display the implied value in exponent notation.
    if #show > 1 then
        show = show:sub(1, 1) .. config.numdot .. show:sub(2)
    end
    return fmtpower:format(show, use_minus(tostring(exponent)))
end

local function make_sigfig(value, sigfig)
    -- Return show, exponent that are equivalent to the result of
    -- converting the number 'value' (where value >= 0) to a string,
    -- rounded to 'sigfig' significant figures.
    -- The returned items are:
    --   show: a string of digits; no sign and no dot;
    --         there is an implied dot before show.
    --   exponent: a number (an integer) to shift the implied dot.
    -- Resulting value = tonumber('.' .. show) * 10^exponent.
    -- Examples:
    --   make_sigfig(23.456, 3) returns '235', 2 (.235 * 10^2).
    --   make_sigfig(0.0023456, 3) returns '235', -2 (.235 * 10^-2).
    --   make_sigfig(0, 3) returns '000', 1 (.000 * 10^1).
    if sigfig <= 0 then
        sigfig = 1
    elseif sigfig > config.maxsigfig then
        sigfig = config.maxsigfig
    end
    if value == 0 then
        return string.rep('0', sigfig), 1
    end
    local exp, frac = math.modf(math.log10(value))
    if frac >= 0 then
        frac = frac - 1
        exp = exp + 1
    end
    local digits = tostring(math.floor((10^(frac + sigfig)) + 0.5))
    if #digits > sigfig then
        -- Overflow (for sigfig=3: like 0.9999 rounding to "1000"; need "100").
        digits = digits:sub(1, sigfig)
        exp = exp + 1
    end
    assert(#digits == sigfig, 'Bug: rounded number has wrong length')
    return digits, exp
end

local function format_number(show, exponent, isnegative)
    -- Return s, f where
    --   s = wikitext formatted to display the value implied by the arguments:
    --     exponent is nil; and
    --     show is a string of digits (no sign), with an optional dot;
    --     or:
    --     exponent is a number (integer) indicating where dot should be;
    --     show is a string of digits (no sign and no dot; there is an
    --          implied dot before show; show does not start with '0').
    --   f = true if s uses scientific notation
    -- The formatted result:
    -- * Includes a Unicode minus if isnegative.
    -- * Has numsep inserted where necessary.
    -- * Uses exponent notation for very small or large values.
    -- * Has no more than config.maxsigfig significant digits
    --   (same as old template and {{#expr}}).
    local sign
    if isnegative then
        sign = MINUS
    else
        sign = ''
    end
    local numdot = config.numdot
    local maxlen = config.maxsigfig
    if exponent == nil then
        local integer, dot, fraction = show:match('^(%d*)([' .. numdot .. ']?)(.*)')
        if #integer >= 10 then
            show = integer .. fraction
            exponent = #integer
        elseif integer == '0' or integer == '' then
            local zeros, figs = fraction:match('^(0*)([^0]?.*)')
            if #figs == 0 then
                if #zeros > maxlen then
                    show = '0' .. numdot .. zeros:sub(1, maxlen)
                end
            elseif #zeros >= 4 then
                show = figs
                exponent = -#zeros
            elseif #figs > maxlen then
                show = '0' .. numdot .. zeros .. figs:sub(1, maxlen)
            end
        else
            maxlen = maxlen + #dot
            if #show > maxlen then
                show = show:sub(1, maxlen)
            end
        end
    end
    if exponent ~= nil then
        if #show > maxlen then
            show = show:sub(1, maxlen)
        end
        if exponent > 10 or exponent <= -4 or (exponent == 10 and show ~= '1000000000') then
            -- Rounded value satisfies: value >= 1e9 or value < 1e-4 (1e9 = 0.1e10).
            return sign .. with_exponent(show, exponent-1), true
        end
        if exponent >= #show then
            show = show .. string.rep('0', exponent - #show)  -- result has no dot
        elseif exponent <= 0 then
            show = '0' .. numdot .. string.rep('0', -exponent) .. show
        else
            show = show:sub(1, exponent) .. numdot .. show:sub(exponent+1)
        end
    end
    if isnegative and show:match('^0.?0*$') then
        sign = ''  -- don't show minus if result is negative but rounds to zero
    end
    return sign .. with_separator(show)
end

-- Fraction output format (like old template).
-- frac1: sign, numerator, denominator
-- frac2: wholenumber, sign, numerator, denominator
local frac1 = '<span style="white-space:nowrap">%s<sup>%s</sup>&frasl;<sub>%s</sub></span>'
local frac2 = '<span class="frac nowrap">%s<s style="display:none">%s</s><sup>%s</sup>&frasl;<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).
    -- Template interprets '1.23e+2+12/24' as '123(12/24)' = 123.5!
    local lhs, negfrac, rhs, numstr, numerator, denstr, denominator, wholestr, whole, 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 = change_sign(numstr)
    end
    if tostring(value):find('#', 1, true) then
        return nil  -- overflow or similar
    end
    numstr = use_minus(numstr)
    denstr = use_minus(denstr)
    local wikitext
    if wholestr then
        local sign = negative and MINUS or '+'
        if negative then
            wholestr = change_sign(wholestr)
        end
        wikitext = frac2:format(use_minus(wholestr), sign, numstr, denstr)
    else
        local sign = negative and MINUS 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, info if can extract a number from the text in args[index],
    -- where info is a table with the result.
    -- Otherwise, return false, message.
    -- Parameter 'which' (1 or 2) selects which input value is being
    -- processed (to select the appropriate error message, if needed).
    -- 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, the returned info table contains named fields:
    --   value    = a valid number
    --   singular = true if value is 1 (to use singular form of units)
    --            = false if value is -1 (like old template)
    --   clean    = cleaned text with any separators and sign removed
    --   show     = text formatted for output
    -- For show:
    -- * Thousand separators are inserted.
    -- * If negative, a Unicode minus is used; otherwise the sign
    --   is '+' (if the input text used '+'), or is ''.
    -- TODO Think about fact that the input value might be like 1.23e+123.
    -- Will the exponent break anything?
    local text = strip(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 == MINUS or sign == '-' then
        propersign = MINUS
        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 and not negative)
        show = propersign .. with_separator(clean)
    end
    if negative and (value ~= 0) then
        value = -value
    end
    return true, {
        value = value,
        singular = singular,
        clean = clean,
        show = show
    }
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)
    -- If successful, return true, args, unit where
    --   args is a table of all arguments passed to the template
    --        converted to named arguments, and
    --   unit is the input unit table;
    -- otherwise, return false, message.
    -- Some of the named args that are added here could be provided by the
    -- user of the template.
    -- MediaWiki removes leading and trailing whitespace from the values of
    -- named arguments. However, the values of numbered arguments include any
    -- whitespace entered in the template, and whitespace is used by some
    -- parameters (example: the numbered parameters associated with "disp=x").
    local range_types = {  -- text to separate input, output ranges
        ['and']   = {' and ', '&nbsp;and '},
        ['by']    = {' by ', '&nbsp;by '},
        ['to']    = {' to ', '&nbsp;to '},
        ['-']     = {'–', '–'},
        ['to(-)'] = {'&nbsp;to ', '–'},
        ['x']     = {' by ', '&nbsp;×&nbsp;'},
        ['+/-']   = {'&nbsp;±&nbsp;', '&nbsp;±&nbsp;'},
    }
    local success, info1, info2
    local args = {}  -- arguments passed to template
    for k, v in pframe:argumentPairs() do
        args[k] = v
    end
    success, info1 = extract_number(args, 1, 1)
    if not success then return false, info1 end
    local in_unit, precision
    local next = strip(args[2])
    local i = 3
    local range = range_types[next]
    if range == nil then
        in_unit = next
    else
        args.range = range
        success, info2 = extract_number(args, 3, 2)
        if not success then return false, info2 end
        in_unit = strip(args[4])
        i = 5
    end
    local success, in_unit_table = units:lookup(in_unit, args.sp)
    if not success then return false, in_unit_table end
    in_unit_table.valinfo = { info1, info2 }  -- info2 is nil if no range
    in_unit_table.inout = 'in'  -- this is an input unit
    next = strip(args[i])
    i = i + 1
    if tonumber(next) == nil then
        args.out_unit = next
        next = strip(args[i])
        if tonumber(next) ~= nil then
            i = i + 1
            precision = next
        end
    else
        precision = next
    end
    if args.adj == nil and args.sing ~= nil then
        args.adj = args.sing  -- sing (singular) is apparently an old equivalent of adj
    end
    if args.adj == 'mid' then
        args.adj = 'on'
        next = args[i]
        i = i + 1
        if next == nil then
            args.mid = ''
        else  -- mid-text words
            if next:sub(1, 1) == '-' then
                args.mid = next
            else
                args.mid = ' ' .. next
            end
        end
    elseif args.adj == 'on' then
        args.mid = ''
    end
    if precision == nil then
        if tonumber(args[i]) ~= nil then
            precision = strip(args[i])
            i = i + 1
        end
    end
    local disp = args.disp
    if disp == 'x' then
        args.joins = { args[i] or '', args[i+1] or '' }
    elseif disp == 's' or disp == '/' then
        args.disp = 'slash'
    end
    args.precision = args.precision or precision  -- allow named parameter
    local abbr = args.abbr
    if abbr == nil then
        -- Default is to abbreviate output (use symbol), or input if flipped.
        args.abbr = (disp == 'flip') and 'in' or 'out'
    else
        args.abbr_org = abbr  -- original abbr (as entered by user)
        if disp == 'flip' then  -- 'in' = LHS, 'out' = RHS
            if abbr == 'in' then
                abbr = 'out'
            elseif abbr == 'out' then
                abbr = 'in'
            end
        end
        args.abbr = abbr
    end
    return true, args, in_unit_table
end

local function default_precision(inclean, invalue, outvalue, in_current, out_current)
    -- Return a default value for precision (an integer like 2, 0, -2).
    -- Code follows procedures used in old template.
    -- Am putting exceptions to standard calculations here, as they are
    -- discovered. Can later decide if something cleaner should be done.
    -- LATER: The hand unit of length might need special processing.
    local log10 = math.log10
    local prec, minprec, adjust
    local utype = out_current.utype
    local fudge = 1e-14  -- {{Order of magnitude}} adds this, so we do too
    -- Find fractional digits, handling cases like inclean = '12.345e6'.
    local integer, dot, fraction = inclean:match('^(%d*)([' .. config.numdot .. ']?)(%d*)')
    if utype == 'temperature' then
        -- LATER: Give an error message if (invalue < in_current.offset): below absolute zero?
        adjust = 0
        local kelvin = (invalue - in_current.offset) * in_current.scale
        if kelvin <= 0 then  -- can get zero, or small but negative value due to precision problems
            minprec = 2
        else
            minprec = 2 - math.floor(log10(kelvin) + fudge)  -- 3 sigfigs in kelvin
        end
    else
        if invalue == 0 or outvalue <= 0 then
            -- We are never called with a negative outvalue, but it might be zero.
            -- This is special-cased to avoid calculation exceptions.
            return 0
        end
        if out_current.symbol == 'ft' and dot == '' then
            -- More precision when output ft with integer input value.
            adjust = -log10(in_current.scale)
        else
            adjust = log10(math.abs(invalue / outvalue))
        end
        adjust = adjust + log10(2)
        -- Ensure that the output has at least two significant figures.
        minprec = 1 - math.floor(log10(outvalue) + fudge)
    end
    if dot == '' then
        prec = -integer:match('0*$'):len()  -- '12300' gives -2, but so does '12300e-5'
    else
        if fraction == '' and utype ~= 'temperature' then
            prec = 1  -- "123." has same precision as "123.0", like old template
        else
            prec = #fraction
        end
    end
    return math.max(math.floor(prec + adjust), minprec)
end

local function convert(value, in_current, out_current)
    local inscale = in_current.scale
    local outscale = out_current.scale
    if in_current.invert ~= nil then
        if in_current.invert * out_current.invert < 0 then
            return 1 / (value * inscale * outscale)
        end
        return value * (inscale / outscale)
    elseif in_current.offset ~= nil then
        return (value - in_current.offset) * (inscale / outscale) + out_current.offset
    else
        return value * (inscale / outscale)
    end
end

local function cvtround(parms, info, in_current, out_current)
    -- Return true, t where t is a table with the conversion results; fields:
    --   show = rounded, formatted string from converting value in info,
    --      using the rounding specified in parms.
    --   singular = true if result is positive, and (after rounding)
    --      is "1", or like "1.00";
    -- or return true, nil if no value specified;
    -- or return false, message if problem.
    -- This code combines convert/round because some rounding requires
    -- knowledge of what we are converting.
    -- TODO Limit values to avoid abuse (for example, can currently set
    -- precision to very large values like 999).
    local invalue, inclean, show, exponent, singular
    if info ~= nil then
        invalue, inclean = info.value, info.clean
    end
    if invalue == nil or invalue == '' then
        return true, nil
    end
    local outvalue = convert(invalue, in_current, out_current)
    local isnegative
    if outvalue < 0 then
        isnegative = true
        outvalue = -outvalue
    end
    local success
    local precision = parms.precision
    local sigfig = parms.sigfig
    local disp = parms.disp
    if precision then
        -- Ignore sigfig, disp.
        success, precision = require_integer(precision, 'Need value', 'precision "%s" must be an integer')
        if not success then return false, precision 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, exponent = make_sigfig(outvalue, sigfig)
    elseif disp == '5' then
        outvalue = math.floor((outvalue / 5) + 0.5) * 5
        show = string.format('%.0f', outvalue)
    else
        precision = default_precision(inclean, invalue, outvalue, in_current, out_current)
    end
    if precision then
        if precision >= 0 then
            if precision <= 8 then
                -- Add a fudge to handle common cases of bad rounding due to inability
                -- to precisely represent some values. This makes the following work:
                -- {{convert|-100.1|C|K}} and {{convert|5555000|um|m|2}}.
                -- Old template uses #expr round, which invokes PHP round().
                -- LATER: Investigate how PHP round() works.
                outvalue = outvalue + 2e-14
            end
            local fmt = '%.' .. string.format('%d', precision) .. 'f'
            show = string.format(fmt, outvalue)
        else
            precision = -precision  -- #digits to zero (in addition to digits after dot)
            local shift = 10 ^ precision
            if shift > outvalue then
                show = '0'  -- like old template, user can zero all digits
            else
                show = tostring(math.floor(outvalue/shift + 0.5))
                exponent = #show + precision
            end
        end
    end
    -- TODO Does following work when exponent ~= nil?
    --      What if show = '1000' and exponent = 1 (value = .1000*10^1 = 1)?
    --      What if show = '1000' and exponent = 2 (value = .1000*10^2 = 10)?
    if (show == '1' or show:match('^1%.0*$') ~= nil) and not isnegative then
        -- Use match because on some systems 0.99999999999999999 is 1.0.
        singular = true
    end
    local formatted, is_scientific = format_number(show, exponent, isnegative)
    return true, {
        show = formatted,
        is_scientific = is_scientific,
        singular = singular,
        clean = show,  -- unformatted show (can be used to calculate rounded value)
        exponent = exponent,
    }
end

-- TODO Think about when to use ' ' and when to use '&nbsp;'.
-- Old template always uses nbsp before a unit symbol, but seems inconsistent
-- before a unit name. Template:Convert/LoffAonSoff says
--   "Numbers less than 1,000 is not wrapped nor are unit symbols"
-- so 1000 is a threshold (use nbsp for smaller values), but no conclusive results.
-- Possibly a concern is wrapping when using {{convert}} in a table
-- (don't want to force a column to be unnecessarily wide by using nbsp).
local disp_joins = {
    ['or']         = { ' or '    , ''  },
    ['sqbr-sp']    = { ' ['      , ']' },
    ['sqbr-nbsp']  = { '&nbsp;[' , ']' },
    ['comma']      = { ', '      , ''  },
    ['slash-sp']   = { ' / '     , ''  },
    ['slash-nbsp'] = { '&nbsp;/ ', ''  },
    ['slash-nosp'] = { '/'       , ''  },
    ['b']          = { ' ('      , ')' },
}

local function evaluate_condition(value, condition)
    -- Return true or false from applying a conditional expression to value,
    -- or throw an error if invalid.
    -- A very limited set of expressions is supported:
    --    v < 9
    --    v * 9 < 9
    -- where
    --    'v' is replaced with value
    --    9 is any number (as defined by Lua tonumber)
    --    '<' can also be '<=' or '>' or '>='
    -- In addition, the following form is supported:
    --    LHS and RHS
    -- where
    --    LHS, RHS = any of above expressions.
    local function compare(value, text)
        local arithop, factor, compop, limit = text:match('^%s*v%s*([*]?)(.-)([<>]=?)(.*)$')
        if arithop == nil then
            error('Invalid default expression', 0)
        elseif arithop == '*' then
            factor = tonumber(factor)
            if factor == nil then
                error('Invalid default expression', 0)
            end
            value = value * factor
        end
        limit = tonumber(limit)
        if limit == nil then
            error('Invalid default expression', 0)
        end
        if compop == '<' then
            return value < limit
        elseif compop == '<=' then
            return value <= limit
        elseif compop == '>' then
            return value > limit
        elseif compop == '>=' then
            return value >= limit
        end
        error('Invalid default expression', 0)  -- should not occur
    end
    local lhs, rhs = condition:match('^(.-%W)and(%W.*)')
    if lhs == nil then
        return compare(value, condition)
    end
    return compare(value, lhs) and compare(value, rhs)
end

local function get_default(value, unit_table)
    -- Return true, s where s = name of unit's default output unit,
    -- or, return false, message.
    -- Some units have a default that depends on the input value
    -- (the first value if a range of values is used).
    -- If '|' is in the default, the first pipe-delimited field is an
    -- expression that uses 'v' to represent the input value.
    -- Example: 'v < 120 | small | big | suffix' (suffix is optional)
    -- evaluates 'v < 120' as a boolean with result
    -- 'smallsuffix' if (value < 120), or 'bigsuffix' otherwise.
    local default = default_exceptions[unit_table.symbol] or unit_table.default
    if default == nil then
        local msg = 'Unit "%s" has no default output unit.[[Category:Convert unknown unit]]'
        return false, msg:format(unit_table.symbol)
    end
    if default:find('|', 1, true) == nil then
        return true, default
    end
    local t = {}
    default = default .. '|'  -- to get last item
    for item in default:gmatch('%s*(.-)%s*|') do
        table.insert(t, item)  -- split on '|', removing leading/trailing whitespace
    end
    if #t == 3 or #t == 4 then
        local success, result = pcall(evaluate_condition, value, t[1])
        if success then
            default = result and t[2] or t[3]
            if #t == 4 then
                default = default .. t[4]
            end
            return true, default
        end
    end
    local msg = 'Unit "%s" has an invalid default.[[Category:Convert unknown unit]]'
    default = msg:format(unit_table.symbol)
    return false, default
end

local function make_id(parms, which, unit_table)
    -- Return id (unit symbol or name, possibly modified)
    -- for value 1 or 2 (which), and 'in' or 'out' (unit_table.inout).
    local abbr = parms.abbr
    if abbr == 'values' then
        return ''
    end
    local inout = unit_table.inout
    local valinfo = unit_table.valinfo
    local abbr_org = parms.abbr_org
    local adj = parms.adj
    local disp = parms.disp
    local singular = valinfo[which].singular or (inout == 'in' and adj == 'on')
    if unit_table.usename then
        -- Old template does something like this.
        if inout == 'in' then
            if adj ~= 'on' and (abbr_org == 'out' or disp == 'flip') then
                local value = valinfo[which].value
                singular = (0 < value and value < 1.0001)
            end
        else
            if (abbr_org == 'on') or
               (disp == nil and (abbr_org == nil or abbr_org == 'out')) or
               (disp == 'flip' and abbr_org == 'in') then
                local clean, exponent = valinfo[which].clean, valinfo[which].exponent
                local value = tonumber(clean)  -- absolute value (any negative sign has been ignored)
                if exponent ~= nil then
                    value = value * 10^exponent
                end
                singular = (value < 1.0001 and not valinfo[which].is_scientific)
            end
        end
    end
    local key_name = 'name2'
    local key_symbol = 'symbol'
    if singular then
        key_name = 'name1'
    end
    if parms.sp == 'us' or unit_table.sp_us then
        key_name = key_name .. '_us'
        key_symbol = 'sym_us'
    end
    local id = unit_table[key_name]
    if not unit_table.usename then
        if ( abbr == 'on' or abbr == inout or (abbr == 'mos' and inout == 'out') or
             (abbr_org == nil and unit_table.utype == 'temperature')) and not
           (abbr_org == nil and (disp == 'or' or disp == 'slash')) then
            id = unit_table[key_symbol]
        end
    end
    local lk = parms.lk
    if lk == 'on' or lk == inout then
        local link = link_exceptions[unit_table.symbol] or unit_table.link
        if link ~= nil then
            id = '[[' .. link .. '|' .. id .. ']]'
        end
    end
    return id
end

local function process_input(parms, in_current)
    -- Processing required once per conversion.
    -- Return block of text to represent input (value/unit).
    local id1 = make_id(parms, 1, in_current)
    local extra = ''
    local result
    local adj = parms.adj
    local disp = parms.disp
    if disp == 'output only' or
       disp == 'output number only' or disp == 'number' or
       disp == 'u2' or disp == 'unit2' then
        result = ''
        parms.joins = { '', '' }
    elseif disp == 'unit' then
        if adj == 'on' then
            result = hyphenated(id1)
        else
            result = id1
        end
        parms.joins = { '', '' }
    else
        local abbr = parms.abbr
        local mos = (abbr == 'mos')
        local range = parms.range
        local id = (range == nil) and id1 or make_id(parms, 2, in_current)
        if id ~= '' then
            if adj == 'on' then
                mos = false  -- if hyphenated, suppress repeat of unit in a range
                extra = '-' .. hyphenated(id) .. parms.mid
            else
                extra = '&nbsp;' .. id
            end
        end
        local valinfo = in_current.valinfo
        if range == nil then
            result = valinfo[1].show
        else
            if mos then
                result = valinfo[1].show .. '&nbsp;' .. id1 .. range[1] .. valinfo[2].show
            else
                result = valinfo[1].show .. range[1] .. valinfo[2].show
            end
        end
        if disp == nil then  -- special case 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
    end
    return result .. extra
end

local function process_one_output(parms, out_current)
    -- Processing required for each output unit.
    -- Return block of text to represent output (value/unit).
    local id1 = make_id(parms, 1, out_current)
    local extra = ''
    local result
    local disp = parms.disp
    if disp == 'u2' or disp == 'unit2' then  -- 'unit2' is not in old template
        if parms.adj == 'on' then
            result = hyphenated(id1)
        else
            result = id1
        end
    else
        local range = parms.range
        if not (disp == 'output number only' or disp == 'number') then
            local id = (range == nil) and id1 or make_id(parms, 2, out_current)
            if id ~= '' then
                extra = '&nbsp;' .. id
            end
        end
        local valinfo = out_current.valinfo
        if range == nil then
            result = valinfo[1].show
        else
            result = valinfo[1].show .. range[2] .. valinfo[2].show
        end
    end
    return result .. extra
end

local function process(parms, in_unit_table)
    -- Return true, s where s = final wikitext result (or false, message).
    local info = in_unit_table.valinfo
    local invalue1 = info[1].value
    if parms.out_unit == nil then           -- LATER need to catch empty string also?
        local success, t = get_default(invalue1, in_unit_table)
        if success then parms.out_unit = t else return false, t end
    end
    local out_unit_table
    local success, t = units:lookup(parms.out_unit, parms.sp, true)
    if success then out_unit_table = t else return false, t end
    if in_unit_table.utype ~= out_unit_table.utype then
        local msg = 'Cannot convert "%s" to "%s".[[Category:Convert dimension mismatch]]'
        return false, msg:format(in_unit_table.utype, out_unit_table.utype)
    end
    local outputs = {}
    local combos = out_unit_table.combination  -- nil or table of unit tables
    local imax = combos and #combos or 1  -- 1 (single unit) or number of unit tables
    for i = 1, imax do
        local success, info1, info2
        local out_current = combos and combos[i] or out_unit_table
        out_current.inout = 'out'
        success, info1 = cvtround(parms, info[1], in_unit_table, out_current)
        if not success then return false, info1 end
        success, info2 = cvtround(parms, info[2], in_unit_table, out_current)
        if not success then return false, info2 end
        out_current.valinfo = { info1, info2 }
        table.insert(outputs, process_one_output(parms, out_current))
    end
    local disp = parms.disp
    local in_block = process_input(parms, in_unit_table)
    local out_block = (disp == 'unit') and '' or table.concat(outputs, '; ')
    if disp == 'flip' then
        in_block, out_block = out_block, in_block
    end
    local wikitext = in_block .. parms.joins[1] .. out_block .. parms.joins[2]
    if parms.sortable == 'on' then
        wikitext = ntsh(invalue1, parms.debug) .. wikitext
    end
    return true, wikitext
end

local p = {}
-- 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 text
    local success, parms, in_unit_table = get_parms(pframe)
    if success then
        success, text = process(parms, in_unit_table)
    else
        text = 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