Content deleted Content added
ignore whitespace at end of a unit definition to handle "unitcode [link|symbol] " with extra space |
update from Module:Val/sandbox: this has various options for Template:Val/list |
||
Line 3:
local data_module -- name of module defining units
local delimit_groups = require('Module:Gapnum').groups▼
local function valerror(msg, nocat, iswarning)
Line 265 ⟶ 264:
local function convert_lookup(ucode, value, scaled_top, want_link, si, options)
local lookup = require('Module:Convert
return lookup(ucode, {
value = value,
Line 284 ⟶ 283:
get_builtin_unit(ucode, data.builtin_units_long_scale) or
get_builtin_unit(ucode, data.builtin_units)
local si,
if result then
use_result = true▼
if result.alias then
ucode = result.symbol
end
if result.scale then
Line 303 ⟶ 301:
ucode = result.ucode or ucode
si = { result.symbol, result.link }
end
else▼
result = {}▼
end
local convert_unit = convert_lookup(ucode, value, scaled_top, want_link, si, options)
if
else
if want_link then
result.text = '[[' .. result.link .. '|' .. result.symbol .. ']]'
Line 313 ⟶ 318:
result.text = result.symbol
end
▲ result.sortkey = convert_unit.sortspan
result.scaled_top = value
▲ else
▲ result = {
▲ text = convert_unit.text,
▲ scaled_top = convert_unit.scaled_value,
end
return result
Line 356 ⟶ 354:
end
local function list_units(mode)
-- Return wikitext to list the built-in units.
-- A unit code should not contain wikimarkup so don't bother escaping.
Line 402 ⟶ 400:
}
local function is_valid(ucode, unit)
if unit and not unit.more_ignored then
assert(type(unit.symbol) == 'string' and unit.symbol ~= '')
if unit.alias then
if unit.link or unit.scale_text or unit.si then
end
end
if unit.si then
if unit.scale_text then
return false
end
ucode = unit.ucode or ucode
local base = unit.symbol
Line 425 ⟶ 431:
end
return false
end
local lookup = require('Module:Convert')._unit
local function show_convert(ucode, unit)
-- If a built-in unit defines a scale or sets the SI flag, any unit defined in
-- convert is not used (the scale or SI prefix's scale is used for a sort key).
-- If there is no scale or SI flag, and the unit is not defined in convert,
-- the sort key may not be correct; this allows such units to be identified.
if not (unit.si or unit.scale_text) then
if mode == 'convert' then
unit.show = not lookup(unit.alias and unit.symbol or ucode).unknown
unit.show_text = 'CONVERT'
elseif mode == 'unknown' then
unit.show = lookup(unit.alias and unit.symbol or ucode).unknown
unit.show_text = 'UNKNOWN'
elseif not unit.alias then
-- Show convert's scale in square brackets ('[1]' for an unknown unit).
-- Don't show scale for an alias because it's misleading for temperature
-- and an alias is probably not useful for anything else.
local scale = lookup(ucode, {value=1, sort='on'}).scaled_value
if type(scale) == 'number' then
scale = string.format('%.5g', scale):gsub('e%+?(%-?)0*(%d+)', 'e%1%2')
else
scale = '?'
end
unit.show = true
unit.show_text = '[' .. scale .. ']'
end
end
end
for line in definitions:gmatch('([^\n]*)\n') do
Line 432 ⟶ 466:
local unit = get_builtin_unit(ucode, '\n' .. line .. '\n')
if is_valid(ucode, unit) then
show_convert(ucode, unit)
local flags, text
if unit.alias then
Line 447 ⟶ 482:
{ 'si', 'SI' },
{ 'scale_text', unit.scale_text },
{ 'show', unit.show_text },
}) do
if unit[f[1]] then
Line 474 ⟶ 510:
end
▲local delimit_groups = require('Module:Gapnum').groups
local function delimit(sign, numstr, fmt)
-- Return sign and numstr (unsigned digits or '.' only) after formatting.
Line 589 ⟶ 626:
if action then
if action == 'list' then
-- Kludge: am using the align parameter (a=xxx) for type of list.
▲ return list_units()
return list_units(options.align)
end
return valerror('invalid action "' .. action .. '".', options.nocat)
|