Module:Person height
Appearance
| This Lua module is used on approximately 278,000 pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
| This module is rated as beta. It is considered ready for widespread use, but as it is still relatively new, it should be applied with some caution to ensure results are as expected. |
| This module is currently protected from editing. See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected. |
Usage
Intended for use in infoboxes; {{#invoke:person height|main|5ft 6in}} → 5 ft 6 in (168 cm). See Template:Infobox person/height for details.
See also
-- This module will implement [[Template:Infobox person/height]]
local p = {}
local function clean(s)
s = mw.ustring.gsub(s, '([^a])meter', '%1m') -- prevents "parameter" from being changed to "param"
s = mw.ustring.gsub(s, 'metre', 'm')
s = mw.ustring.gsub(s, 'centi', 'c')
s = mw.ustring.gsub(s, 'feet', 'ft')
s = mw.ustring.gsub(s, 'foot', 'ft')
s = mw.ustring.gsub(s, 'inches', 'in')
s = mw.ustring.gsub(s, 'inch', 'in')
s = mw.ustring.gsub(s, 'ms', 'm')
s = mw.ustring.gsub(s, 'ins', 'in')
s = mw.ustring.gsub(s, '%[%[[Ii]n|in%]%]', '[[inch|in]]')
s = mw.ustring.gsub(s, '%[%[[Cc]entim|cm%]%]', '[[Centimetre|cm]]')
s = mw.ustring.gsub(s, '%[%[[Mm]%]%]s', '[[Metre|m]]')
s = mw.ustring.gsub(s, '%[%[[Cc]m%]%]s', '[[Centimetre|cm]]')
return s
end
function convert(s, prefer, enforce)
local prefer_m = (prefer or '') == 'm'
local force_m = (enforce or '') == 'm'
local prefer_cm = (prefer or '') == 'cm'
local force_cm = (enforce or '') == 'cm'
s = clean(s or '') -- basic unit cleaning
local cleaned = mw.ustring.gsub(s, '&[Nn][Bb][Ss][Pp];', ' ')
local m = mw.ustring.find(cleaned, 'm')
local c = mw.ustring.find(cleaned, 'cm')
local f = mw.ustring.find(cleaned, 'ft')
local i = mw.ustring.find(cleaned, 'in')
if m == nil and f == nil and i == nil then
return '', s
end
if c ~= nil and f == nil and i == nil then
local n = tonumber(mw.ustring.sub(cleaned, 1, c - 1))
if n == nil then return '', s end
return force_m
and {n/100,'m','ftin',0,['abbr']='on'}
or {n,'cm','ftin',0,['abbr']='on'}, mw.ustring.sub(cleaned, c+2)
end
if m ~= nil and c == nil and f == nil and i == nil then
local n = tonumber(mw.ustring.sub(cleaned, 1, m - 1))
if n == nil then return '', s end
return force_cm
and {n*100,'cm','ftin',0,['abbr']='on'}
or {n,'m','ftin',0,['abbr']='on'}, mw.ustring.sub(cleaned, m+1)
end
if f ~= nil and i ~=nil and m == nil then
local n1 = tonumber(mw.ustring.sub(cleaned, 1, f - 1))
local n2 = tonumber(mw.ustring.sub(cleaned, f+2, i - 1))
if n1 == nil or n2 == nil then return '', s end
return (force_m or prefer_m)
and {n1,'ft',n2,'in', 'm',2,['abbr']='on'}
or {n1,'ft',n2,'in', 'cm',0,['abbr']='on'}, mw.ustring.sub(cleaned, i+2)
end
if f ~= nil and i == nil and m == nil then
local n = tonumber(mw.ustring.sub(cleaned, 1, f - 1))
if n == nil then return '', s end
return (force_m or prefer_m)
and {n,'ft','m',2,['abbr']='on'}
or {n,'ft','cm',0,['abbr']='on'}, mw.ustring.sub(cleaned, f+2)
end
if i ~= nil and f == nil and m == nil then
local n = tonumber(mw.ustring.sub(cleaned, 1, i - 1))
if n == nil then return '', s end
return (force_m or prefer_m)
and {n,'in','m',2,['abbr']='on'}
or {n,'in','cm',0,['abbr']='on'}, mw.ustring.sub(cleaned, i+2)
end
return '', s
end
function p.main(frame)
local args = frame.args[1] and frame.args or frame:getParent().args
local targs, str = convert(args[1], args['prefer'] or '', args['enforce'] or '')
if type(targs) == 'table' then
return frame:expandTemplate{ title = 'convert', args = targs} .. str
else
return str
end
end
return p