Content deleted Content added
fix gapnum crash with '1.'; load sandbox data if sandbox; convert should not apply its own scale if a built-in unit has a scale; fmt=gaps puts a gap in a 4-digit integer; avoid overflow with large e |
handle action=list to list all built-in units so can preview changes; some unit validity checks; fix SI prefix if 'mc' or 'mu' (they are not recognized by convert); use sandbox units if sandbox used |
||
Line 8:
-- Return formatted message for {{val}} errors.
if is_test_run then -- LATER remove
return 'Error:
end
local ret = mw.html.create('strong')
Line 123:
result.link = item
elseif n == 3 then
result.scale_text = item
result.scale = get_scale(item, ucode)
else
result.more_ignored = item
break
end
end
end
if result.si then
local s = result.symbol
if ucode == 'mc' .. s or ucode == 'mu' .. s then
result.ucode = 'µ' .. s -- unit code for convert should be this
end
end
Line 174 ⟶ 182:
end
if result.si then
ucode = result.ucode or ucode
si = { result.symbol, result.link }
use_result = false
Line 226 ⟶ 235:
end
return { text = text, isangle = unit.isangle, sortkey = sortkey }
end
local function list_units()
-- Return wikitext to list the built-in units.
-- A unit code should not contain wikimarkup so don't bother escaping.
local data = mw.loadData(data_module)
local definitions = data.builtin_units .. data.builtin_units_long_scale
local last_was_blank = true
local n = 0
local result = {}
local function add(line)
if line == '' then
last_was_blank = true
else
if last_was_blank and n > 0 then
n = n + 1
result[n] = ''
end
last_was_blank = false
n = n + 1
result[n] = line
end
end
local si_prefixes = {
-- These are the prefixes recognized by convert; u is accepted for micro.
y = true,
z = true,
a = true,
f = true,
p = true,
n = true,
u = true,
['µ'] = true,
m = true,
c = true,
d = true,
da = true,
h = true,
k = true,
M = true,
G = true,
T = true,
P = true,
E = true,
Z = true,
Y = true,
}
local function is_valid(ucode, unit)
-- LATER Check for valid combinations of links/flags.
if unit and not unit.more_ignored then
if unit.si then
ucode = unit.ucode or ucode
local base = unit.symbol
local plen = #ucode - #base
if ucode == base or (plen > 0 and si_prefixes[ucode:sub(1, plen)] and ucode:sub(plen + 1) == base) then
return true
end
else
return true
end
end
return false
end
for line in definitions:gmatch('([^\n]*)\n') do
local pos, _ = line:find(' ', 1, true)
if pos then
local ucode = line:sub(1, pos - 1)
local unit = get_builtin_unit(ucode, '\n' .. line .. '\n')
if is_valid(ucode, unit) then
local flags, text
if unit.alias then
text = unit.symbol
else
text = '[[' .. unit.link .. '|' .. unit.symbol .. ']]'
end
if unit.isangle then
unit.nospace = nil -- don't show redundant flag
end
for _, f in ipairs({
{ 'alias', 'ALIAS' },
{ 'isangle', 'ANGLE' },
{ 'nospace', 'NOSPACE' },
{ 'si', 'SI' },
{ 'scale_text', unit.scale_text },
}) do
if unit[f[1]] then
local t = f[2]
if t:match('^%u+$') then
t = '<small>' .. t .. '</small>'
end
if flags then
flags = flags .. ' ' .. t
else
flags = t
end
end
end
if flags then
text = text .. ' • ' .. flags
end
add(ucode .. ' = ' .. text .. '<br />')
else
add(line .. ' ◆ <b>invalid definition</b><br />')
end
else
add(line)
end
end
return table.concat(result, '\n')
end
Line 279 ⟶ 397:
local function _main(number, uncertainty, unit_spec, misc_tbl)
data_module = 'Module:Val/units' .. (misc_tbl.sandbox and '/sandbox' or '')
local action = misc_tbl.action
if action then
if action == 'list' then
return list_units()
end
return valerror('invalid action "' .. action .. '".', misc_tbl.nocat)
end
local e_10 = misc_tbl.e
local fmt = misc_tbl.fmt
Line 381 ⟶ 507:
local function main(frame)
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame, {wrappers = { 'Template:Val', 'Template:Val/sandboxlua' }})
Line 421 ⟶ 543:
}
local misc_tbl = {
action = args.action,
align = args.a,
e = numbers.e,
fmt = args.fmt,
nocat = nocat,
prefix = args.p,
sandbox = string.find(frame:getTitle(), 'sandbox', 1, true) ~= nil,
sortable = args.sortable,
suffix = args.s,
}
number.nend = args['end']
|