Module:Val: Difference between revisions

use i18n from Module:Val/sandbox added by User:Moroboshi
m (1 revision imported: Wikipedia article on Nicotine modules needed)
(use i18n from Module:Val/sandbox added by User:Moroboshi)
Line 1: Line 1:
-- For Template:Val, output a number and optional unit.
-- For Template:Val, output a number and optional unit.
-- Format options include scientific and uncertainty notations.
-- Format options include scientific and uncertainty notations.
local numdot = '.'  -- decimal mark (use ',' for Italian)
local numsep = ','  -- group separator (use ' ' for Italian)
local mtext = {
-- Message and other text that should be localized.
['mt-bad-exponent'] =      'exponent parameter (<b>e</b>)',
['mt-parameter'] =          'parameter ',
['mt-not-number'] =        'is not a valid number',
['mt-cannot-range'] =      'cannot use a range if the first parameter includes "e"',
['mt-need-range'] =        'needs a range in parameter 2',
['mt-should-range'] =      'should be a range',
['mt-cannot-with-e'] =      'cannot be used if the first parameter includes "e"',
['mt-not-range'] =          'does not accept a range',
['mt-cannot-e'] =          'cannot use e notation',
['mt-too-many-parameter'] = 'too many parameters',
['mt-need-number'] =        'need a number after the last parameter because it is a range.',
['mt-ignore-parameter4'] =  'Val parameter 4 ignored',
['mt-val-not-supported'] =  'Val parameter "%s=%s" is not supported',
['mt-invalid-scale'] =      'Unit "%s" has invalid scale "%s"',
['mt-both-u-ul'] =          'unit (<b>u</b>) and unit with link (<b>ul</b>) are both specified, only one is allowed.',
['mt-both-up-upl'] =        'unit per (<b>up</b>) and unit per with link (<b>upl</b>) are both specified, only one is allowed.',
}


local data_module = 'Module:Val/units'
local data_module = 'Module:Val/units'
Line 59: Line 81:
-- Input like 1e3 is regarded as invalid for all except argument 1
-- Input like 1e3 is regarded as invalid for all except argument 1
-- which accepts e notation as an alternative to the 'e' argument.
-- which accepts e notation as an alternative to the 'e' argument.
-- Input commas are removed so 1,234 is the same as 1234.
-- Input group separators are removed.
local which = index
local which = index
local function fail(msg)
local function fail(msg)
local description
local description
if which == 'e' then
if which == 'e' then
description = 'exponent parameter (<b>e</b>)'
description = mtext['mt-bad-exponent']
else
else
description = 'parameter ' .. which
description = mtext['mt-parameter'] .. which
end
end
return description .. ' ' .. (msg or 'is not a valid number') .. '.'
return description .. ' ' .. (msg or mtext['mt-not-number']) .. '.'
end
end
local result = {}
local result = {}
Line 76: Line 98:
if index == 2 then
if index == 2 then
if numbers[1] and numbers[1].exp then
if numbers[1] and numbers[1].exp then
return fail('cannot use a range if the first parameter includes "e"')
return fail(mtext['mt-cannot-range'])
end
end
numbers.has_ranges = true
numbers.has_ranges = true
else
else
if not numbers.has_ranges then
if not numbers.has_ranges then
return fail('needs a range in parameter 2')
return fail(mtext['mt-need-range'])
end
end
end
end
Line 91: Line 113:
return nil
return nil
end
end
return fail('does not accept a range')
return fail(mtext['mt-not-range'])
end
end
if numbers.has_ranges and type(index) == 'number' and (index % 2 == 0) then
if numbers.has_ranges and type(index) == 'number' and (index % 2 == 0) then
return fail('should be a range')
return fail(mtext['mt-should-range'])
end
end
if index == 'e' then
if index == 'e' then
Line 100: Line 122:
if e then
if e then
if arg then
if arg then
return fail('cannot be used if the first parameter includes "e"')
return fail(mtext['mt-cannot-with-e'])
end
end
arg = e
arg = e
Line 107: Line 129:
end
end
if arg and arg ~= '' then
if arg and arg ~= '' then
arg = arg:gsub(',', '')
arg = arg:gsub(numsep, '')
if numdot ~= '.' then
arg = arg:gsub(numdot, '.')
end
if arg:sub(1, 1) == '(' and arg:sub(-1) == ')' then
if arg:sub(1, 1) == '(' and arg:sub(-1) == ')' then
result.parens = true
result.parens = true
Line 118: Line 143:
result.exp = b
result.exp = b
else
else
return fail('cannot use e notation')
return fail(mtext['mt-cannot-e'])
end
end
end
end
Line 172: Line 197:
end
end
if index > 19 then
if index > 19 then
return 'too many parameters'
return mtext['mt-too-many-parameter']
end
end
end
end
if numbers.has_ranges and (#numbers % 2 == 0) then
if numbers.has_ranges and (#numbers % 2 == 0) then
return 'need a number after the last parameter because it is a range.'
return mtext['mt-need-number']
end
end
end
end
Line 206: Line 231:
end
end
end
end
error('Unit "' .. ucode .. '" has invalid scale "' .. text .. '"')
error(string.format(mtext['mt-invalid-scale'], ucode, text))
end
end


Line 514: Line 539:
local delimit_groups = require('Module:Gapnum').groups
local delimit_groups = require('Module:Gapnum').groups
local function delimit(sign, numstr, fmt)
local function delimit(sign, numstr, fmt)
-- Return sign and numstr (unsigned digits or '.' only) after formatting.
-- Return sign and numstr (unsigned digits or numdot only) after formatting.
-- Four-digit integers are not formatted with gaps.
-- Four-digit integers are not formatted with gaps.
fmt = (fmt or ''):lower()
fmt = (fmt or ''):lower()
Line 525: Line 550:
local result
local result
if fmt == 'commas' then
if fmt == 'commas' then
result = sign .. table.concat(ipart, ',')
result = sign .. table.concat(ipart, numsep)
if dpart then
if dpart then
result = result .. '.' .. table.concat(dpart)
result = result .. numdot .. table.concat(dpart)
end
end
else
else
Line 537: Line 562:
end
end
if dpart then
if dpart then
table.insert(groups, '.' .. (table.remove(dpart, 1) or ''))
table.insert(groups, numdot .. (table.remove(dpart, 1) or ''))
for _, v in ipairs(dpart) do
for _, v in ipairs(dpart) do
table.insert(groups, '<span style="margin-left:.25em;">' .. v .. '</span>')
table.insert(groups, '<span style="margin-left:.25em;">' .. v .. '</span>')
Line 748: Line 773:
for k, v in pairs(args) do
for k, v in pairs(args) do
if type(k) == 'string' and not whitelist[k] then
if type(k) == 'string' and not whitelist[k] then
local warning = 'Val parameter "' .. k .. '=' .. v .. '" is not supported'
local warning = string.format(mtext['mt-val-not-supported'], k, v)
return valerror(warning, nocat, true)
return valerror(warning, nocat, true)
end
end
end
end
if not has_ranges and args[4] then
if not has_ranges and args[4] then
return valerror('Val parameter 4 ignored', nocat, true)
return valerror(mtext['mt-ignore-parameter4'], nocat, true)
end
end
end
end
Line 767: Line 792:
end
end
if args.u and args.ul then
if args.u and args.ul then
return valerror('unit (<b>u</b>) and unit with link (<b>ul</b>) are both specified, only one is allowed.', nocat)
return valerror(mtext['mt-both-u-ul'], nocat)
end
end
if args.up and args.upl then
if args.up and args.upl then
return valerror('unit per (<b>up</b>) and unit per with link (<b>upl</b>) are both specified, only one is allowed.', nocat)
return valerror(mtext['mt-both-up-upl'], nocat)
end
end
local values
local values
Anonymous user