Content deleted Content added
update from Module:Val/sandbox per discussion at Template talk:Val#Pointless 10^0 |
change 0.3em spacing to 0.225em (same total space as old code) per Template talk:Val#Proposal to reduce spacing around ± |
||
(10 intermediate revisions by 5 users not shown) | |||
Line 2:
-- Format options include scientific and uncertainty notations.
local
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 convert_module = 'Module:Convert'
local function valerror(msg, nocat, iswarning)
-- Return formatted message text for an error or warning.
-- Can append "#FormattingError" to URL of a page with a problem to find it.
local anchor = '<span id="FormattingError"
local body, category
if nocat or mw.title.getCurrentTitle():inNamespaces(1, 2, 3, 5) then
Line 15 ⟶ 38:
category = '[[Category:Pages with incorrect formatting templates use]]'
end
iswarning = false -- problems are infrequent so try showing large error so editor will notice
if iswarning then
body = '<sup class="noprint Inline-Template" style="white-space:nowrap;">' ..
Line 57 ⟶ 81:
-- Input like 1e3 is regarded as invalid for all except argument 1
-- which accepts e notation as an alternative to the 'e' argument.
-- Input
local which = index
local function fail(msg)
local description
if which == 'e' then
description = mtext['mt-bad-exponent
else
description = mtext['mt-parameter
end
return description .. ' ' .. (msg or mtext['
end
local result = {}
Line 74 ⟶ 98:
if index == 2 then
if numbers[1] and numbers[1].exp then
return fail(mtext['mt-cannot
end
numbers.has_ranges = true
else
if not numbers.has_ranges then
return fail(mtext['
end
end
Line 89 ⟶ 113:
return nil
end
return fail(mtext['
end
if numbers.has_ranges and type(index) == 'number' and (index % 2 == 0) then
return fail(mtext['mt-should
end
if index == 'e' then
Line 98 ⟶ 122:
if e then
if arg then
return fail(mtext['mt-cannot
end
arg = e
Line 105 ⟶ 129:
end
if arg and arg ~= '' then
arg = arg:gsub(
if numdot ~= '.' then
arg = arg:gsub(numdot, '.')
end
if arg:sub(1, 1) == '(' and arg:sub(-1) == ')' then
result.parens = true
Line 116 ⟶ 143:
result.exp = b
else
return fail(mtext['mt-cannot
end
end
Line 170 ⟶ 197:
end
if index > 19 then
return mtext['mt-too
end
end
if numbers.has_ranges and (#numbers % 2 == 0) then
return mtext['mt-need
end
end
Line 204 ⟶ 231:
end
end
error(
end
Line 264 ⟶ 291:
local function convert_lookup(ucode, value, scaled_top, want_link, si, options)
local lookup = require(
return lookup(ucode, {
value = value,
Line 432 ⟶ 459:
return false
end
local lookup = require(
local function show_convert(ucode, unit)
-- If a built-in unit defines a scale or sets the SI flag, any unit defined in
Line 512 ⟶ 539:
local delimit_groups = require('Module:Gapnum').groups
local function delimit(sign, numstr, fmt)
-- Return sign and numstr (unsigned digits or
-- Four-digit integers are not formatted with gaps.
fmt = (fmt or ''):lower()
Line 523 ⟶ 550:
local result
if fmt == 'commas' then
result = sign .. table.concat(ipart,
if dpart then
result = result ..
end
else
Line 532 ⟶ 559:
groups[1] = table.remove(ipart, 1)
for _, v in ipairs(ipart) do
table.insert(groups, '<span style="margin-left:.25em;">' .. v .. '</span>')
end
if dpart then
table.insert(groups,
for _, v in ipairs(dpart) do
table.insert(groups, '<span style="margin-left:.25em;">' .. v .. '</span>')
end
end
Line 555 ⟶ 582:
end
return '<span style="display:inline-block;margin-bottom:-0.3em;vertical-align:-0.4em;line-height:1.2em;font-size:85%;text-align:' ..
align .. ';">' .. sup .. '<br />' .. sub .. '</span>'
end
Line 602 ⟶ 629:
else
text = (angle or '') ..
'<span style="margin-left:0.
delimit('', uncU, fmt)
need_parens = true
Line 622 ⟶ 649:
local function _main(values, unit_spec, options)
data_module = data_module .. '/sandbox'
convert_module = convert_module .. '/sandbox'
end
local action = options.action
if action then
Line 699 ⟶ 729:
e_text = '10<sup>' .. delimit(e_10.sign, e_10.clean, fmt) .. '</sup>'
if number.clean then
e_text = '<span style="margin-left:0.25em;margin-right:0.15em;">×</span>' .. e_text
end
else
Line 743 ⟶ 773:
for k, v in pairs(args) do
if type(k) == 'string' and not whitelist[k] then
local warning = string.format(mtext['
return valerror(warning, nocat, true)
end
end
if not has_ranges and args[4] then
return valerror(mtext['
end
end
Line 754 ⟶ 784:
local function main(frame)
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame, {wrappers = { 'Template:Val
local nocat = args.nocategory
local numbers = {} -- table of number tables, perhaps with range text
Line 762 ⟶ 792:
end
if args.u and args.ul then
return valerror(mtext['
end
if args.up and args.upl then
return valerror(mtext['
end
local values
|