Module:Convert: Difference between revisions
Jump to navigation
Jump to search
>Johnuniq (hard to remember that "%s" is a regex pattern in Lua; comment on some prefix bugs) |
>Johnuniq (major refactor of formatting input/output values; implement sortable=on) |
||
Line 9: | Line 9: | ||
- Some conversions require two outputs: {{convert|55|nmi|km mi}}. | - Some conversions require two outputs: {{convert|55|nmi|km mi}}. | ||
- Some units have two values: {{convert|3.21|m|ftin}}. | - Some units have two values: {{convert|3.21|m|ftin}}. | ||
]] | ]] | ||
Line 586: | Line 585: | ||
cfg.maxsigfig = 20 -- maximum number of significant figures | cfg.maxsigfig = 20 -- maximum number of significant figures | ||
return cfg | 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 | end | ||
local function withseparator(text) | local function withseparator(text) | ||
-- Return | -- 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). | -- Separator is inserted only in the integer part (not in fraction). | ||
-- Four-digit integer parts have a separator (like | -- Four-digit integer parts have a separator (like '1,234'). | ||
local numsep = config.numsep | local numsep = config.numsep | ||
if #numsep == 0 then | if #numsep == 0 then | ||
return text | return text | ||
end | end | ||
local function insert(text | local function insert(text, last) | ||
local result = '' | local result = '' | ||
while last >= | while last >= 1 do | ||
if last >= | if last >= 4 then | ||
result = numsep .. text:sub(last-2, last) .. result | result = numsep .. text:sub(last-2, last) .. result | ||
last = last - 3 | last = last - 3 | ||
else | else | ||
return text:sub( | return text:sub(1, last) .. result | ||
end | end | ||
end | end | ||
return result | return result | ||
end | end | ||
local last = text:find(config.numdot, 1, true) | |||
local last = text:find(config.numdot, | |||
if last == nil then | if last == nil then | ||
last = #text | last = #text | ||
Line 624: | Line 665: | ||
last = last - 1 | last = last - 1 | ||
end | end | ||
return | return insert(text, last) .. text:sub(last+1) | ||
end | end | ||
Line 690: | Line 731: | ||
end | end | ||
local function | local missing = { 'Need value', 'Need second value' } | ||
-- Return true, | local invalid = { 'Value "%s" must be a number', 'Second value "%s" must be a number' } | ||
-- | |||
if | local function extract_number(args, index, which) | ||
if | -- 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 | local numsep = config.numsep | ||
if | if numsep == '' then | ||
local | clean = text | ||
if | else | ||
return true | 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 value = tonumber(clean) | |||
if value == nil then return false, (invalid[which]):format(text) end | |||
local singular = (value == 1) | |||
if negative and (value ~= 0) then | |||
value = -value | |||
end | |||
-- TODO Someone has to change text to use exponents for very large/small. | |||
local show = propersign .. withseparator(clean) | |||
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 | end | ||
local function require_integer( | local function require_integer(text, missing, invalid) | ||
-- Return true, n where n = integer equivalent to given | -- 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). | |||
if | -- Using regex avoids irritations with input like '-0.000001'. | ||
if | if text == nil then return false, missing end | ||
return true, | if string.match(text, '^-?%d+$') == nil then | ||
return false, invalid:format(text) | |||
end | |||
return true, tonumber(text) | |||
end | end | ||
local function get_parms(pframe) | local function get_parms(pframe) | ||
-- Return true, t where t is a table with all arguments passed to the | -- Return true, t where t is a table with all arguments passed to the | ||
-- converted to named arguments. | -- 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. | |||
-- | |||
-- added here could be provided by the user of the template | |||
local range_types = { -- text to separate input, output ranges | local range_types = { -- text to separate input, output ranges | ||
['and'] = {' and ', ' and '}, | ['and'] = {' and ', ' and '}, | ||
Line 726: | Line 829: | ||
['+/-'] = {' ± ', ' ± '}, | ['+/-'] = {' ± ', ' ± '}, | ||
} | } | ||
local success, | local success, msg | ||
local args = {} | 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, | if not success then return success, msg end | ||
if success then | |||
local in_unit = args[2] | local in_unit = args[2] | ||
local i = 3 | local i = 3 | ||
local range = range_types[in_unit] | local range = range_types[in_unit] | ||
if range ~= nil then | if range ~= nil then | ||
success, msg = extract_number(args, 3, 2) | |||
success, | if not success then return success, msg end | ||
if success then | |||
in_unit = args[4] | in_unit = args[4] | ||
i = 5 | i = 5 | ||
Line 754: | Line 855: | ||
end | end | ||
local function default_roundto( | local function default_roundto(inclean, factor) | ||
-- Return a default value for round_to (an integer like 2, 0, -2). | -- Return a default value for round_to (an integer like 2, 0, -2). | ||
-- prec = (precision implied in | -- prec = (precision implied in inclean) | ||
-- = (#digits after dot, or negative of #zeroes before dot) | -- = (#digits after dot, or negative of #zeroes before dot) | ||
-- If conversion is multiplication by a factor, and | -- If conversion is multiplication by a factor, and | ||
Line 767: | Line 868: | ||
-- TODO Exception required for temperature. | -- TODO Exception required for temperature. | ||
local prec = 0 | local prec = 0 | ||
local dot = | local dot = inclean:find('.', 1, true) | ||
-- TODO SHOULD DO THIS? local dot = inclean:find(config.numdot, 1, true) | |||
if dot ~= nil then | if dot ~= nil then | ||
prec = | prec = inclean:sub(dot+1):len() | ||
if prec == 0 then | if prec == 0 then | ||
inclean = inclean:sub(1, -2) | |||
end | end | ||
end | end | ||
if prec == 0 then | if prec == 0 then | ||
prec = - | prec = -inclean:match('0*$'):len() | ||
end | end | ||
if factor ~= nil | if factor ~= nil then | ||
factor = math.abs(factor) | |||
if factor >= 0.02 then | |||
prec = prec - math.floor(math.log10(factor*5)) | |||
end | |||
end | end | ||
return prec | return prec | ||
Line 785: | Line 890: | ||
local function scaled(value, in_unit, out_unit) | local function scaled(value, in_unit, out_unit) | ||
-- Return scaled value for a simple convert. | -- Return scaled value for a simple convert. | ||
local result = (value - in_unit.offset) * (in_unit.scale / out_unit.scale) + out_unit.offset | |||
if result == 0 then return 0 end -- safety: result is 0 even for -0 (negative zero) | |||
return result | |||
end | end | ||
local function cvtround(invalue, | local function cvtround(invalue, inclean, parms) | ||
-- Return true, | -- Return true, show, singular | ||
-- using the rounding specified in parms | -- where | ||
-- show = '' if invalue == 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 | -- This code combines convert/round because some rounding requires | ||
-- knowledge of what we are converting. | -- knowledge of what we are converting. | ||
Line 799: | Line 910: | ||
-- TODO Limit values to avoid abuse (for example, can currently set | -- TODO Limit values to avoid abuse (for example, can currently set | ||
-- round_to to very large values like 999). | -- round_to to very large values like 999). | ||
local | local show, singular = '', false | ||
if invalue == nil then return true, | if invalue == nil or invalue == '' then return true, show, singular end | ||
local outvalue = scaled(invalue, parms.in_unit_table, parms.out_unit_table) | local outvalue = scaled(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 round_to = parms.round_to | ||
local sigfig = parms.sigfig | local sigfig = parms.sigfig | ||
Line 809: | Line 930: | ||
-- Ignore sigfig, disp. | -- Ignore sigfig, disp. | ||
success, round_to = require_integer(round_to, 'Need value', 'round_to "%s" must be an integer') | success, round_to = require_integer(round_to, 'Need value', 'round_to "%s" must be an integer') | ||
if not success then return | if not success then return false, round_to end | ||
elseif sigfig then | elseif sigfig then | ||
-- Ignore disp. | -- Ignore disp. | ||
success, sigfig = require_integer(sigfig, 'Need value', 'sigfig "%s" must be an integer') | success, sigfig = require_integer(sigfig, 'Need value', 'sigfig "%s" must be an integer') | ||
if not success then return | if not success then return false, sigfig end | ||
if sigfig <= 0 then | if sigfig <= 0 then | ||
local msg = 'sigfig "%s" must be positive' | local msg = 'sigfig "%s" must be positive' | ||
return false, msg:format(parms.sigfig) | return false, msg:format(parms.sigfig) | ||
end | end | ||
show = formatnumber(outvalue, sigfig) | |||
elseif disp == '5' then | elseif disp == '5' then | ||
outvalue = math.floor((outvalue / 5) + 0.5) * 5 | outvalue = math.floor((outvalue / 5) + 0.5) * 5 | ||
show = string.format('%.0f', outvalue) | |||
else | else | ||
auto = true -- using default rounding | auto = true -- using default rounding | ||
-- TODO If conversion is not multiplication by a number, need factor = nil. | -- TODO If conversion is not multiplication by a number, need factor = nil. | ||
local factor = outvalue / invalue | local factor | ||
round_to = default_roundto( | if invalue == 0 then | ||
factor = 1 -- LATER is this correct? | |||
else | |||
factor = outvalue / invalue | |||
end | |||
round_to = default_roundto(inclean, factor) | |||
end | end | ||
if round_to then | if round_to then | ||
Line 841: | Line 959: | ||
-- TODO No less than two significant figures. | -- TODO No less than two significant figures. | ||
end | end | ||
local fmt = '%.' .. string.format('%d', round_to) .. 'f' | local fmt = '%.' .. string.format('%d', round_to) .. 'f' | ||
show = string.format(fmt, outvalue) | |||
else | else | ||
-- This always keeps two sig figs. Should that be done if not auto? | -- This always keeps two sig figs. Should that be done if not auto? | ||
Line 858: | Line 973: | ||
if round_to > 0 then | if round_to > 0 then | ||
local scaled = string.format('%.0f', outvalue/(10^round_to)) | local scaled = string.format('%.0f', outvalue/(10^round_to)) | ||
show = scaled .. string.rep('0', round_to) | |||
else | else | ||
show = formatnumber(outvalue, 2) -- can't zero digits; keep 2 sig figs | |||
end | end | ||
end | end | ||
end | end | ||
return true, withseparator( | if 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 | end | ||
Line 925: | Line 1,045: | ||
return false, msg:format(parms.in_unit_table.utype, parms.out_unit_table.utype) | return false, msg:format(parms.in_unit_table.utype, parms.out_unit_table.utype) | ||
end | end | ||
local | local outshow1, outshow2, outsingular1, outsingular2 | ||
success, outshow1, outsingular1 = cvtround(parms.in_value1, parms.in_clean1, parms) | |||
success, | if not success then return success, outshow1 end | ||
if not success then return success, | success, outshow2, outsingular2 = cvtround(parms.in_value2, parms.in_clean2, parms) | ||
success, | if not success then return success, outshow2 end | ||
if not success then return success, | |||
local range = parms.range | local range = parms.range | ||
local disp = parms.disp | local disp = parms.disp | ||
local wikitext | local wikitext | ||
----------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | ||
-- TODO Clean up following | -- TODO Clean up following. | ||
local inkey, outkey, | local inshow1, inshow2 = parms.in_show1, parms.in_show2 | ||
if parms.value | local inkey, outkey, insymkey, outsymkey = 'name2', 'name2', 'symbol', 'symbol' | ||
if parms.in_singular1 then -- TODO how process second input value? | |||
inkey = 'name1' | inkey = 'name1' | ||
end | end | ||
if outsingular1 then | |||
if | |||
outkey = 'name1' | outkey = 'name1' | ||
end | end | ||
if parms.sp == 'us' then | if parms.sp == 'us' or parms.in_unit_table.sp_us then | ||
inkey = inkey .. '_us' | inkey = inkey .. '_us' | ||
insymkey = 'sym_us' | |||
end | |||
if parms.sp == 'us' or parms.out_unit_table.sp_us then | |||
outkey = outkey .. '_us' | outkey = outkey .. '_us' | ||
outsymkey = 'sym_us' | |||
end | end | ||
local in_name = parms.in_unit_table[inkey] -- will not need to calculate all of these | local in_name = parms.in_unit_table[inkey] -- will not need to calculate all of these | ||
local in_symbol = parms.in_unit_table[ | local in_symbol = parms.in_unit_table[insymkey] | ||
local out_name = parms.out_unit_table[outkey] | local out_name = parms.out_unit_table[outkey] | ||
local out_symbol = parms.out_unit_table[ | local out_symbol = parms.out_unit_table[outsymkey] | ||
local abbr = parms.abbr | local abbr = parms.abbr | ||
local in_id, out_id | local in_id, out_id | ||
Line 980: | Line 1,101: | ||
if disp == 'output only' then | if disp == 'output only' then | ||
wikitext = '%s %s' | wikitext = '%s %s' | ||
wikitext = wikitext:format( | wikitext = wikitext:format(outshow1, out_id) | ||
elseif disp == 'output number only' or disp == 'number' then | elseif disp == 'output number only' or disp == 'number' then | ||
wikitext = | wikitext = outshow1 | ||
elseif disp == 'unit' then | elseif disp == 'unit' then | ||
wikitext = in_id | wikitext = in_id | ||
Line 989: | Line 1,110: | ||
elseif disp == 'flip' then | elseif disp == 'flip' then | ||
wikitext = disp_single['b'] | wikitext = disp_single['b'] | ||
wikitext = wikitext:format( | wikitext = wikitext:format(outshow1, out_id, inshow1, in_id) | ||
else | else | ||
wikitext = disp_single[disp] or disp_single['b'] | wikitext = disp_single[disp] or disp_single['b'] | ||
wikitext = wikitext:format( | wikitext = wikitext:format(inshow1, in_id, outshow1, out_id) | ||
end | end | ||
else | else | ||
-- TODO Need in_id, out_id (and more) here. | -- TODO Need in_id, out_id (and more) here. | ||
wikitext = disp_double[disp] or disp_double['b'] | wikitext = disp_double[disp] or disp_double['b'] | ||
wikitext = wikitext:format( | 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 | end | ||
----------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
Revision as of 03:50, 19 September 2012
Documentation for this module may be created at Module:Convert/doc
--[[ Important TODO Bugs with prefixes: - 1 km2 = 1 km × 1 km = 1,000,000 m2 (it is NOT a kilo square meter). - {{convert|9|ha|acre}} gives "9 hectoares (22 acre)", but it should be "hectares". Are there other spelling exceptions when joining prefix + unit? Later TODO Too many items to list, but following are some points: - Output needs rather than space in several places. - Some conversions require two outputs: {{convert|55|nmi|km mi}}. - Some units have two values: {{convert|3.21|m|ftin}}. ]] local function clonetable(t) -- Return a shallow copy of t. result = {} for k, v in pairs(t) do result[k] = v end return result end --[[-----BEGIN DATA TABLES----- Three data tables follow: SIprefixes prefixes like 'M' (mega, 10^6) units all properties for a unit, including default output defaultunits default output exceptions ('Mg' and 'g' have different defaults) SIprefixes and defaultunits are maintained by editing this file. However, the units table is generated by a script which reads the wikitext for a wiki page that documents properties of units. Could consider putting some or all tables in another module. Check values at http://en.wikipedia.org/wiki/Conversion_of_units and http://en.wikipedia.org/wiki/Template:Convert/list_of_units ]] local SIprefixes = { ['Y'] = { exponent = 24, name = 'yotta' }, ['Z'] = { exponent = 21, name = 'zetta' }, ['E'] = { exponent = 18, name = 'exa' }, ['P'] = { exponent = 15, name = 'peta' }, ['T'] = { exponent = 12, name = 'tera' }, ['G'] = { exponent = 9, name = 'giga' }, ['M'] = { exponent = 6, name = 'mega' }, ['k'] = { exponent = 3, name = 'kilo' }, ['H'] = { exponent = 2, name = 'hecto' }, -- not an SI prefix, but allow for people typing this ['h'] = { exponent = 2, name = 'hecto' }, ['da']= { exponent = 1, name = 'deca' }, ['D'] = { exponent = 1, name = 'deca' }, -- not an SI prefix, but allow for people typing this ['d'] = { exponent = -1, name = 'deci' }, ['c'] = { exponent = -2, name = 'centi' }, ['m'] = { exponent = -3, name = 'milli' }, ['µ'] = { exponent = -6, name = 'micro' }, ['u'] = { exponent = -6, name = 'micro' }, -- not an SI prefix, but allow for people typing this ['n'] = { exponent = -9, name = 'nano' }, ['p'] = { exponent =-12, name = 'pico' }, ['f'] = { exponent =-15, name = 'femto' }, ['a'] = { exponent =-18, name = 'atto' }, ['z'] = { exponent =-21, name = 'zepto' }, ['y'] = { exponent =-24, name = 'yocto' }, } local usesubstitute = { -- If unit has an SI prefix, these fields may have "%s" where prefix belongs. 'name1', 'name1_us', 'name2', 'name2_us', 'link', } local function set_prefixes(unit, prefixname) -- Insert given prefix name into the fields which require it -- (and which should contain '%s' to be replaced with the prefix). -- Pity we have to do all this work when most results are not needed, -- but it's cleaner to do it here rather than in final processing. if unit.prefixes then for _, name in ipairs(usesubstitute) do local value = unit[name] unit[name] = value:gsub('%%s', prefixname, 1) end end end -- Do not change the data in this table because it is created by running -- a script on the wikitext from a wiki page (see note above). local units = { lookup = function (self, unit) -- Return true, t where t is the unit's converter table (or false, message). -- Given 'unit' is a symbol (like 'g'), with an optional SI prefix (as in 'kg'). -- If, for example, 'kg' is in this table, that entry is used; otherwise prefix is applied. local t = self[unit] if t ~= nil then local result = clonetable(t) set_prefixes(result, '') result.baseunit = unit result.prefix = '' return true, result end for plen = 2, 1, -1 do -- Check for longer prefix first ('dam' is decametre). local prefix = string.sub(unit, 1, plen) local si = SIprefixes[prefix] if si ~= nil then local baseunit = unit:sub(plen+1) local t = self[baseunit] if t ~= nil and t.prefixes == true then local result = clonetable(t) set_prefixes(result, si.name) result.symbol = prefix .. result.symbol result.sym_us = prefix .. result.sym_us result.baseunit = baseunit result.prefix = prefix result.scale = t.scale * 10^si.exponent return true, result end end end local msg = 'Unit %s is not known.[[Category:Convert unknown unit]]' return false, msg:format(unit) end, ["m2"] = { name1 = "square %smetre", name1_us = "square %smeter", name2 = "square %smetres", name2_us = "square %smeters", symbol = "m<sup>2</sup>", sym_us = "m<sup>2</sup>", utype = "area", scale = 1, offset = 0, prefixes = true, default = "sqyd", link = "Square %smetre", }, ["a"] = { name1 = "%sare", name1_us = "%sare", name2 = "%sares", name2_us = "%sares", symbol = "a", sym_us = "a", utype = "area", scale = 100, offset = 0, prefixes = true, default = "acre", link = "Hectare#Are", }, ["sqft"] = { name1 = "square foot", name1_us = "square foot", name2 = "square feet", name2_us = "square feet", symbol = "sq ft", sym_us = "sq ft", utype = "area", scale = 0.09290304, offset = 0, default = "m2", link = "Square foot", }, ["sqyd"] = { name1 = "square yard", name1_us = "square yard", name2 = "square yards", name2_us = "square yards", symbol = "sq yd", sym_us = "sq yd", utype = "area", scale = 0.83612736, offset = 0, default = "m2", link = "Square yard", }, ["sqin"] = { name1 = "square inch", name1_us = "square inch", name2 = "square inches", name2_us = "square inches", symbol = "sq in", sym_us = "sq in", utype = "area", scale = 0.00064516, offset = 0, default = "cm2", link = "Square inch", }, ["acre"] = { name1 = "acre", name1_us = "acre", name2 = "acres", name2_us = "acres", symbol = "acre", sym_us = "acre", utype = "area", scale = 4046.8564224, offset = 0, default = "ha", link = "Acre", }, ["m"] = { name1 = "%smetre", name1_us = "%smeter", name2 = "%smetres", name2_us = "%smeters", symbol = "m", sym_us = "m", utype = "length", scale = 1, offset = 0, prefixes = true, default = "ft", link = "%smetre", }, ["mi"] = { name1 = "mile", name1_us = "mile", name2 = "miles", name2_us = "miles", symbol = "mi", sym_us = "mi", utype = "length", scale = 1609.344, offset = 0, default = "km", link = "Mile", }, ["ft"] = { name1 = "foot", name1_us = "foot", name2 = "feet", name2_us = "feet", symbol = "ft", sym_us = "ft", utype = "length", scale = 0.3048, offset = 0, default = "m", link = "Foot (unit)", }, ["foot"] = { name1 = "foot", name1_us = "foot", name2 = "feet", name2_us = "feet", symbol = "ft", sym_us = "ft", utype = "length", scale = 0.3048, offset = 0, default = "m", link = "Foot (unit)", }, ["yd"] = { name1 = "yard", name1_us = "yard", name2 = "yards", name2_us = "yards", symbol = "yd", sym_us = "yd", utype = "length", scale = 0.3048 * 3, offset = 0, default = "m", link = "Yard", }, ["in"] = { name1 = "inch", name1_us = "inch", name2 = "inches", name2_us = "inches", symbol = "in", sym_us = "in", utype = "length", scale = 0.0254, offset = 0, default = "cm", link = "Inch", }, ["g"] = { name1 = "%sgram", name1_us = "%sgram", name2 = "%sgrams", name2_us = "%sgrams", symbol = "g", sym_us = "g", utype = "mass", scale = 0.001, offset = 0, prefixes = true, default = "oz", link = "%sgram", }, ["lb"] = { name1 = "pound", name1_us = "pound", name2 = "pounds", name2_us = "pounds", symbol = "lb", sym_us = "lb", utype = "mass", scale = 0.45359237, offset = 0, default = "kg", link = "Pound (mass)", }, ["oz"] = { name1 = "ounce", name1_us = "ounce", name2 = "ounces", name2_us = "ounces", symbol = "oz", sym_us = "oz", utype = "mass", scale = 0.45359237/16, offset = 0, default = "g", link = "Ounce", }, ["ozt"] = { name1 = "troy ounce", name1_us = "troy ounce", name2 = "troy ounces", name2_us = "troy ounces", symbol = "ozt", sym_us = "ozt", utype = "mass", scale = 0.0311034768, offset = 0, default = "g", link = "Troy ounce", }, ["K"] = { name1 = "kelvin", name1_us = "kelvin", name2 = "kelvins", name2_us = "kelvins", symbol = "K", sym_us = "K", utype = "temperature", scale = 1, offset = 0, default = "C", link = "Kelvin", }, ["C"] = { name1 = "degree Celsius", name1_us = "degree Celsius", name2 = "degrees Celsius", name2_us = "degrees Celsius", symbol = "°C", sym_us = "°C", utype = "temperature", scale = 1, offset = -273.15, default = "F", link = "Celsius", }, ["°C"] = { name1 = "degree Celsius", name1_us = "degree Celsius", name2 = "degrees Celsius", name2_us = "degrees Celsius", symbol = "°C", sym_us = "°C", utype = "temperature", scale = 1, offset = -273.15, default = "F", link = "Celsius", }, ["F"] = { name1 = "degree Fahrenheit", name1_us = "degree Fahrenheit", name2 = "degrees Fahrenheit", name2_us = "degrees Fahrenheit", symbol = "°F", sym_us = "°F", utype = "temperature", scale = 5/9, offset = 32-273.15*(9/5), default = "C", link = "Fahrenheit", }, ["°F"] = { name1 = "degree Fahrenheit", name1_us = "degree Fahrenheit", name2 = "degrees Fahrenheit", name2_us = "degrees Fahrenheit", symbol = "°F", sym_us = "°F", utype = "temperature", scale = 5/9, offset = 32-273.15*(9/5), default = "C", link = "Fahrenheit", }, ["m3"] = { name1 = "cubic %smetre", name1_us = "cubic %smeter", name2 = "cubic %smetres", name2_us = "cubic %smeters", symbol = "m<sup>3</sup>", sym_us = "m<sup>3</sup>", utype = "volume", scale = 1, offset = 0, prefixes = true, default = "cuyd", link = "Cubic %smetre", }, ["l"] = { name1 = "%slitre", name1_us = "%sliter", name2 = "%slitres", name2_us = "%sliters", symbol = "l", sym_us = "l", utype = "volume", scale = 0.001, offset = 0, prefixes = true, default = "imppt", link = "Litre", }, ["L"] = { name1 = "%slitre", name1_us = "%sliter", name2 = "%slitres", name2_us = "%sliters", symbol = "L", sym_us = "L", utype = "volume", scale = 0.001, offset = 0, prefixes = true, default = "imppt", link = "Litre", }, ["cuyd"] = { name1 = "cubic yard", name1_us = "cubic yard", name2 = "cubic yards", name2_us = "cubic yards", symbol = "cu yd", sym_us = "cu yd", utype = "volume", scale = 0.764554857984, offset = 0, default = "m3", link = "Cubic yard", }, ["USgal"] = { name1 = "US gallon", name1_us = "U.S. gallon", name2 = "US gallons", name2_us = "U.S. gallons", symbol = "US gal", sym_us = "U.S. gal", utype = "volume", scale = 0.003785411784, offset = 0, default = "L", link = "US gallon", }, ["USoz"] = { name1 = "US fluid ounce", name1_us = "U.S. fluid ounce", name2 = "US fluid ounces", name2_us = "U.S. fluid ounces", symbol = "US fl oz", sym_us = "U.S. fl oz", utype = "volume", scale = 0.003785411784/128, offset = 0, default = "cL", link = "US fluid ounce", }, ["USpt"] = { name1 = "U.S. pint", name1_us = "U.S. pint", name2 = "U.S. pints", name2_us = "U.S. pints", symbol = "US pt", sym_us = "US pt", utype = "volume", scale = 0.003785411784/8, offset = 0, default = "dL", }, ["impgal"] = { name1 = "imperial gallon", name1_us = "imperial gallon", name2 = "imperial gallons", name2_us = "imperial gallons", symbol = "imp gal", sym_us = "imp gal", utype = "volume", scale = 0.00454609, offset = 0, default = "L", link = "Imperial gallon", }, ["impoz"] = { name1 = "imperial fluid ounce", name1_us = "imperial fluid ounce", name2 = "imperial fluid ounces", name2_us = "imperial fluid ounces", symbol = "imp fl oz", sym_us = "imp fl oz", utype = "volume", scale = 0.00454609/160, offset = 0, default = "cL", link = "Imperial fluid ounce", }, ["imppt"] = { name1 = "imperial pint", name1_us = "imperial pint", name2 = "imperial pints", name2_us = "imperial pints", symbol = "imp pt", sym_us = "imp pt", utype = "volume", scale = 0.00454609/8, offset = 0, default = "dL", link = "Imperial pint", }, } local defaultunits = { lookup = function (self, unit_table) -- Return true, s where s = name of unit's default output unit (or false, message). local baseunit = unit_table.baseunit local prefix = unit_table.prefix local unit = prefix .. baseunit local default = self[unit] if default ~= nil then return true, default end t = units[baseunit] if t ~= nil then local default = t.default if default ~= nil then return true, default end end local msg = 'Unit %s has no default target conversion.[[Category:Convert unknown unit]]' return false, msg:format(unit) end, -- Prefixed units with a default different from that of the base unit. ['kg'] = 'lb', ['Mg'] = 'lb', ['Gg'] = 'lb', ['pm'] = 'in', ['nm'] = 'in', ['um'] = 'in', ['mm'] = 'in', ['cm'] = 'in', ['dm'] = 'in', ['dam'] = 'yd', ['Hm'] = 'yd', ['km'] = 'mi', ['Mm'] = 'mi', ['mL'] = 'impoz', ['cL'] = 'impoz', ['dL'] = 'impoz', ['daL'] = 'impgal', ['HL'] = 'impgal', } -------END DATA TABLES----- -- 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 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 value = tonumber(clean) if value == nil then return false, (invalid[which]):format(text) end local singular = (value == 1) if negative and (value ~= 0) then value = -value end -- TODO Someone has to change text to use exponents for very large/small. local show = propersign .. withseparator(clean) 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() 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 scaled(value, in_unit, out_unit) -- Return scaled value for a simple convert. local result = (value - in_unit.offset) * (in_unit.scale / out_unit.scale) + out_unit.offset if result == 0 then return 0 end -- safety: result is 0 even for -0 (negative zero) return result end local function cvtround(invalue, inclean, parms) -- Return true, show, singular -- where -- show = '' if invalue == 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 = scaled(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 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: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). 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) if success then parms.out_unit_table = t else return success, t end if parms.in_unit_table.utype ~= parms.out_unit_table.utype then local msg = 'Cannot convert %s to %s.[[Category:Convert dimension mismatch]]' return false, msg:format(parms.in_unit_table.utype, parms.out_unit_table.utype) 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 ----------------------------------------------------------------------------- -- TODO Clean up following. local inshow1, inshow2 = parms.in_show1, parms.in_show2 local inkey, outkey, insymkey, outsymkey = 'name2', 'name2', '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 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 if abbr == 'on' then -- all symbols in_id = in_symbol out_id = out_symbol elseif abbr == 'off' then -- all names in_id = in_name out_id = out_name elseif abbr == 'in' then -- input symbols in_id = in_symbol out_id = out_symbol elseif abbr == 'out' then -- output symbols [is this just the default?] in_id = in_name out_id = out_symbol elseif abbr == 'values' then -- show only values -- LATER elseif abbr == 'mos' then -- for ranges, abbreviate with input unit repeated -- LATER else -- default in_id = in_name out_id = out_symbol 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 = {} function p.convert(frame) config = get_config(frame) local pframe = frame:getParent() local parms, text success, parms = get_parms(pframe) if success then success, text = process(parms) end if not success then if not is_test_run then -- A testing program can set the above global variable so this script -- will not attempt to load mw, which is only needed on the wiki. local bodge = require "Module:mw" -- This fixes up mw.text.tag for us. local params = {style="color:black; background-color:orange;"} text=mw.text.tag({name="span", contents="[[Module talk:Convert|Conversion error]]: " .. text, params=params}) end end return text end return p