Модуль:Height: различия между версиями
Материал из проекта «Повелитель Тайн Вики»
Дополнительные действия
Admin (обсуждение | вклад) render polish |
imported>Import Импорт с англ. вики (CC BY-SA) |
||
| Строка 71: | Строка 71: | ||
return p | return p | ||
Версия от 02:24, 1 ноября 2026
-- [ru] Height formatting for {{Height}}: metric first, imperial in brackets, Russian units.
-- {{Height|m=1.73}} -> 1,73 м (5 фт 8 дюйм.) {{Height|m=1.8 to 1.9}} -> 1,8–1,9 м (5 фт 11 дюйм. – 6 фт 3 дюйм.)
local p = {}
local function num(s)
if s == nil then return nil end
s = mw.text.trim(tostring(s)):gsub(',', '.')
return tonumber(s)
end
local function fmt(x, digits)
local s
if digits then s = string.format('%.' .. digits .. 'f', x) else s = tostring(x) end
if s:find('%.') then s = s:gsub('0+$', ''):gsub('%.$', '') end
return (s:gsub('%.', ','))
end
local function ftin(meters)
local inches = meters / 0.0254
local ft = math.floor(inches / 12)
local inch = math.floor(inches - ft * 12 + 0.5)
if inch == 12 then ft = ft + 1; inch = 0 end
if inch == 0 then return ft .. ' фт' end
return ft .. ' фт ' .. inch .. ' дюйм.'
end
local function parseRange(s)
if s == nil or mw.text.trim(s) == '' then return nil end
local a, b = s:match('^%s*([%d%.,]+)%s*to%s*([%d%.,]+)%s*$')
if not a then a, b = s:match('^%s*([%d%.,]+)%s*[%-–—]%s*([%d%.,]+)%s*$') end
if a then return { num(a), num(b) } end
local v = num(s)
if v then return { v } end
return nil
end
function p.height(frame)
local args = frame:getParent().args
local function g(...)
for _, k in ipairs({...}) do
local v = args[k]
if v and mw.text.trim(v) ~= '' then return v end
end
end
local m = g('meter', 'meters', 'metre', 'metres', 'm')
local cm = g('centimeter', 'centimeters', 'centimetre', 'centimetres', 'cm')
local ft = g('foot', 'feet', 'ft')
local inch = g('inch', 'inches', 'in')
local vals, unit
if m then
vals = parseRange(m); unit = 'm'
elseif cm then
vals = parseRange(cm); unit = 'cm'
elseif ft or inch then
local mtr = ((num(ft) or 0) * 12 + (num(inch) or 0)) * 0.0254
local imp = (ft and (num(ft) .. ' фт') or '') .. ((inch and num(inch) and num(inch) ~= 0) and (' ' .. num(inch) .. ' дюйм.') or '')
return fmt(mtr, 2) .. ' м (' .. mw.text.trim(imp) .. ')'
else
return ''
end
if vals == nil then return m or cm end
local metric, imp = {}, {}
for i, v in ipairs(vals) do
local meters = unit == 'cm' and v / 100 or v
metric[i] = unit == 'cm' and fmt(v) or fmt(v)
imp[i] = ftin(meters)
end
local u = unit == 'cm' and ' см' or ' м'
return table.concat(metric, '–') .. u .. ' (' .. table.concat(imp, ' – ') .. ')'
end
return p