Module:Convert: Difference between revisions

accept user-defined combination like "LT+ST"; fix adj=on|lk=on bug so do not put hyphens in link target
(handle exceptions as a property of a unit; allows localization for other wikis)
(accept user-defined combination like "LT+ST"; fix adj=on|lk=on bug so do not put hyphens in link target)
Line 553:
end
end
end
-- Accept user-defined combo like "MT+LT" (convert output to MT and LT).
local combo = collection()
for item in string.gmatch(unitcode .. '+', '%s*(.-)%s*%+') do
if positem ~= '' then
combo:add(item)
end
end
if combo.n > 1 then
if what == 'no_combination' or what == 'only_multiple' then
return false, { 'cvt_bad_unit', unitcode }
end
local posutype
local result = { combination = {} }
local cvt = result.combination
for i, v in ipairs(combo) do
local success, t = lookup(v, opt_sp_us, 'no_combination', utable, fails, depth)
if not success then return false, t end
if utype then
if utype ~= t.utype then
return false, { 'cvt_mismatch', utype, t.utype }
end
else
utype = t.utype
end
cvt[i] = t
end
result.utype = utype
return true, result
end
if not extra_units then
Line 618 ⟶ 647:
end
 
local function hyphenated(name, parts)
-- Return a hyphenated form of given name (for adjectival usage).
-- The name may be linked and the target of the link must not be changed.
-- Hypothetical examples:
-- [[long ton|ton]] → [[long ton|ton]] (no change)
-- [[tonne|long ton]] → [[tonne|long-ton]]
-- [[metric ton|long ton]] → [[metric ton|long-ton]]
-- [[long ton]] → [[long ton|long-ton]]
-- Input can also have multiple links in a single name like:
-- [[United States customary units|U.S.]] [[US gallon|gallon]]
-- [[mile]]s per [[United States customary units|U.S.]] [[quart]]
-- [[long ton]]s per [[short ton]]
-- Assume that links cannot be nested (never like "[[abc[[def]]ghi]]".
-- This uses a simple and efficient procedure that works for most cases.
-- Some units (if used) would require more, and can later think about
Line 627 ⟶ 667:
-- not spaces immediately before '(' or in '(...)' [for cases like
-- "British thermal unit (ISO)" and "Calorie (International Steam Table)"].
pos =if name:find('( ', 1, true) then
local pos
if name:sub(1, 1) == '('if parts then
pos = name:find(')', 1, true)local pos
if posname:sub(1, 1) == '(' then
return name:sub(1, pos+1) ..= name:subfind(pos+2'):gsub(', '1, '-'true)
if pos then
return name:sub(1, pos-+1) .. name:sub(pos+2):gsub(' ', '-') .. name:sub(pos-1)
end
elseif name:sub(-1, -1) == ')' then
pos = name:find('(', 1, true)
if pos then
return name:sub(1, pos-2):gsub(' ', '-') .. name:sub(pos-1)
end
end
return name:gsub(' ', '-')
end
parts = collection()
for before, item, after in name:gmatch('([^[]*)(%[%[[^[]*%]%])([^[]*)') do
if item:find(' ', 1, true) then
local prefix
local plen = item:find('|', 1, true)
if plen then
prefix = item:sub(1, plen)
item = item:sub(plen + 1, -3)
else
prefix = item:sub(1, -3) .. '|'
item = item:sub(3, -3)
end
item = prefix .. hyphenated(item, parts) .. ']]'
end
parts:add(before:gsub(' ', '-') .. item .. after:gsub(' ', '-'))
end
elseif name:sub(-1, -1) if parts.n == ')'0 then
-- No link like "[[...]]" was found in the original name.
pos = name:find('(', 1, true)
parts:add(hyphenated(name, parts))
if pos then
return name:sub(1, pos-2):gsub(' ', '-') .. name:sub(pos-1)
end
return table.concat(parts)
end
return name:gsub(' ', '-')
end
 
Line 681 ⟶ 747:
-- Return a table to hold groups of digits which can be joined with
-- suitable separators (such as commas).
-- Each group is separately translated to the local language because the
-- gap separators may include '.' charactersdigits which should not be translated.
-- Parameter method is a number or nil:
-- 3 for 3-digit grouping, or
Line 715 ⟶ 781:
local result = previous - self.step
if method == 2 then
self.step = 2 -- may need more (3, 2, 2, 3, 2, 2, ...) in some languages
end
return (result < 0) and 0 or result
Anonymous user