Module:Convert/makeunits: Difference between revisions

Content deleted Content added
Undid revision 1075162681 by Rwv37 (talk) no, see preceding comment in text
accept abbr=off for an alias so it uses unit name; add pernames (names for second unit in a per) for ukwiki
 
Line 153:
perunits = 'Automatic per units',
varnames = 'Variable names',
pernames = 'Names for second unit in a per',
},
titles = {
Line 232 ⟶ 233:
m_per_undef = 'Unit "$1" has undefined unit code "$2" in the "per".',
m_percent_s = 'Field "$1" must not contain "%s".',
m_pnm_cnt = 'Names for second unit in a per section: each row must have two columns.',
m_pnm_dup = 'Unit "$1" already has a per name.',
m_pnm_miss = 'Missing field for a per name.',
m_pnm_undef = 'Unit "$1" in per names is not defined.',
m_pfx_bad = 'Unknown prefix: "$1".',
m_pfx_name = 'Unit with Prefix set must include Name.',
Line 893 ⟶ 898:
end
 
local function update_units(units, composites, varnames, pernames)
-- Update some unit definitions with extra data defined in other sections.
-- This is done after all input data has been processed.
Line 901 ⟶ 906:
unit.subdivs = '{ ' .. table.concat(comp.subdivs, ', ') .. ' }'
end
local vn =if varnames[unit.unitcode] then
unit.varname = vnvarnames[unit.unitcode]
if vn then
end
unit.varname = vn
if pernames[unit.unitcode] then
unit.pername = pernames[unit.unitcode]
end
end
Line 1,086 ⟶ 1,093:
local good
if not empty(rhs) then
for _, item in ipairs({ 'sp', 'default', 'link', 'multiplier', 'symbol', 'symlink', 'abbr' }) do
if lhs == item then
if item == 'sp' then
Line 1,120 ⟶ 1,127:
unit[item] = rhs
good = true
elseif item == 'abbr' then
if target and rhs == 'off' then
unit.usename = 1
good = true
end
else
if target and rhs == target[item] then
Line 1,518 ⟶ 1,530:
end
data[ucode] = table.concat(names, '!')
end
end
 
local function make_pername(cfg, data)
-- Return a function which, when called, stores a table that defines a
-- per name for a unit. The table is stored in data (also a table).
return function (utype, fields)
-- Set or update an entry in the data table to record that a unit has a
-- non-standard per name if used as the second unit in a per unit (x per y).
-- The target units must be defined first.
-- Parameter utype is ignored (it is nil).
-- Throw an error if a problem occurs.
local count = #fields
if count ~= 2 then
quit('m_pnm_cnt')
end
local ucode, pername
for i = 1, count do
local v = fields[i]
if vnempty(v) then
quit('m_pnm_miss')
end
if i == 1 then -- unitcode
ucode = v
if not get_unit(v) then
quit('m_pnm_undef', v)
end
else
pername = v
end
end
if data[ucode] then
quit('m_pnm_dup', ucode)
end
data[ucode] = pername
end
end
Line 1,795 ⟶ 1,842:
'symbol',
'sp_us',
'usename',
'default',
'link',
Line 1,832 ⟶ 1,880:
'name2',
'name2_us',
'pername',
'varname',
'symbol',
Line 2,036 ⟶ 2,085:
-- Return units, defaults, links (three tables).
-- Throw an error if a problem occurs.
local composites, defaults, links, units, perunits, varnames, pernames = {}, {}, {}, {}, {}, {}, {}
local sections = {
{ 'overrides' , make_override , overrides , 0 },
Line 2,047 ⟶ 2,096:
{ 'perunits' , make_perunit , perunits , 1 },
{ 'varnames' , make_varname , varnames , 1 },
{ 'pernames' , make_pername , pernames , 1 },
}
local lines = get_page_lines(cfg.data_title)
Line 2,064 ⟶ 2,114:
check_all_defaults(cfg, units)
check_all_pers(cfg, units)
update_units(units, composites, varnames, pernames)
return units, defaults, links, perunits
end