Difference between revisions of "Module:Person height"

From annadreambrush.com/wiki
Jump to navigation Jump to search
imported>Frietjes
m (1 revision imported)
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
-- This module will implement [[Template:Infobox person/height]]
+
-- This module implements [[Template:Infobox person/height]]
  
 
local p = {}
 
local p = {}
Line 21: Line 21:
 
end
 
end
  
function convert(frame, args)
+
local function isnumber(s)
local s = clean(args[1] or '')
+
if s then
local prefer_m = (args['prefer'] or '') == 'm'
+
s = mw.ustring.gsub(s, '%+%s*%d+%s*/%s*%d+%s*$', '')
local force_m = (args['force'] or '') == 'm'
+
return tonumber(s)
local prefer_cm = (args['prefer'] or '') == 'cm'
+
end
local force_cm = (args['force'] or '') == 'cm'
+
return nil
 +
end
 +
 
 +
local function get_convert_args(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'
 +
 +
unconverted = clean(s or '') -- basic unit cleaning
 
 
local cleaned = mw.ustring.gsub(s, '&[Nn][Bb][Ss][Pp];', ' ')
+
s = mw.ustring.gsub(unconverted, '&[Nn][Bb][Ss][Pp];', ' ')
 
 
local m = mw.ustring.find(cleaned, 'm')
+
local m = mw.ustring.find(s, 'm')
local c = mw.ustring.find(cleaned, 'cm')
+
local c = mw.ustring.find(s, 'cm')
local f = mw.ustring.find(cleaned, 'ft')
+
local f = mw.ustring.find(s, 'ft')
local i = mw.ustring.find(cleaned, 'in')
+
local i = mw.ustring.find(s, 'in')
 
 
 
if m == nil and f == nil and i == nil then
 
if m == nil and f == nil and i == nil then
return s
+
return '', unconverted
 
end
 
end
 
 
 
if c ~= nil and f == nil and i == nil then
 
if c ~= nil and f == nil and i == nil then
local n = tonumber(mw.ustring.sub(cleaned, 1, c - 1))
+
local n = mw.ustring.sub(s, 1, c - 1)
if n == nil then
+
if isnumber(n) then
return s
+
return force_m
end
+
and {n/100,'m','ftin',0,['abbr']='on'}
if force_m then
+
or {n,'cm','ftin',0,['abbr']='on'}, mw.ustring.sub(s, c+2)
return frame:expandTemplate{ title = 'convert', args = {n/100,'m','ftin',0,['abbr']='on'}} .. mw.ustring.sub(cleaned, c+2)
 
 
end
 
end
return frame:expandTemplate{ title = 'convert', args = {n,'cm','ftin',0,['abbr']='on'}} .. mw.ustring.sub(cleaned, c+2)
+
return '', unconverted
 
end
 
end
 
 
 
if m ~= nil and c == nil and f == nil and i == nil then
 
if m ~= nil and c == nil and f == nil and i == nil then
local n = tonumber(mw.ustring.sub(cleaned, 1, m - 1))
+
local n = mw.ustring.sub(s, 1, m - 1)
if n == nil then
+
if isnumber(n) then
return s
+
return force_cm  
end
+
and {n*100,'cm','ftin',0,['abbr']='on'}
if force_cm then
+
or {n,'m','ftin',0,['abbr']='on'}, mw.ustring.sub(s, m+1)
return frame:expandTemplate{ title = 'convert', args = {n*100,'cm','ftin',0,['abbr']='on'}} .. mw.ustring.sub(cleaned, m+1)
 
 
end
 
end
return frame:expandTemplate{ title = 'convert', args = {n,'m','ftin',0,['abbr']='on'}} .. mw.ustring.sub(cleaned, m+1)
+
return '', unconverted
 
end
 
end
 
 
 
if f ~= nil and i ~=nil and m == nil then
 
if f ~= nil and i ~=nil and m == nil then
local n1 = tonumber(mw.ustring.sub(cleaned, 1, f - 1))
+
local n1 = mw.ustring.sub(s, 1, f - 1)
local n2 = tonumber(mw.ustring.sub(cleaned, f+2, i - 1))
+
local n2 = mw.ustring.sub(s, f+2, i - 1)
if n1 == nil or n2 == nil then
+
if isnumber(n1) and isnumber(n2) then
return s
+
return (force_m or prefer_m)
end
+
and {n1,'ft',n2,'in', 'm',2,['abbr']='on'}
if force_m or prefer_m then
+
or {n1,'ft',n2,'in', 'cm',0,['abbr']='on'}, mw.ustring.sub(s, i+2)
return frame:expandTemplate{ title = 'convert', args = {n1,'ft',n2,'in', 'm',2,['abbr']='on'}} .. mw.ustring.sub(cleaned, i+2)
 
 
end
 
end
return frame:expandTemplate{ title = 'convert', args = {n1,'ft',n2,'in', 'cm',0,['abbr']='on'}} .. mw.ustring.sub(cleaned, i+2)
+
return '', unconverted
 
end
 
end
 
 
 
if f ~= nil and i == nil and m == nil then
 
if f ~= nil and i == nil and m == nil then
local n = tonumber(mw.ustring.sub(cleaned, 1, f - 1))
+
local n = mw.ustring.sub(s, 1, f - 1)
if n == nil then
+
if isnumber(n) then
return s
+
return (force_m or prefer_m)
end
+
and {n,'ft','m',2,['abbr']='on'}
if force_m or prefer_m then
+
or {n,'ft','cm',0,['abbr']='on'}, mw.ustring.sub(s, f+2)
return frame:expandTemplate{ title = 'convert', args = {n,'ft','m',2,['abbr']='on'}} .. mw.ustring.sub(cleaned, f+2)
 
 
end
 
end
return frame:expandTemplate{ title = 'convert', args = {n,'ft','cm',0,['abbr']='on'}} .. mw.ustring.sub(cleaned, f+2)
+
return '', unconverted
 
end
 
end
 
 
 
if i ~= nil and f == nil and m == nil then
 
if i ~= nil and f == nil and m == nil then
local n = tonumber(mw.ustring.sub(cleaned, 1, i - 1))
+
local n = mw.ustring.sub(s, 1, i - 1)
if n == nil then
+
if isnumber(n) then
return s
+
return (force_m or prefer_m)
end
+
and {n,'in','m',2,['abbr']='on'}
if force_m or prefer_m then
+
or {n,'in','cm',0,['abbr']='on'}, mw.ustring.sub(s, i+2)
return frame:expandTemplate{ title = 'convert', args = {n,'in','cm',2,['abbr']='on'}} .. mw.ustring.sub(cleaned, i+2)
 
 
end
 
end
return frame:expandTemplate{ title = 'convert', args = {n,'in','cm',0,['abbr']='on'}} .. mw.ustring.sub(cleaned, i+2)
+
return '', unconverted
 
end
 
end
 
 
return s
+
return '', unconverted
 +
end
 +
 
 +
function convert(frame, args)
 +
local targs, str = get_convert_args(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
 
end
  

Latest revision as of 17:56, 9 March 2020

Documentation for this module may be created at Module:Person height/doc

-- This module implements [[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

local function isnumber(s)
	if s then
		s = mw.ustring.gsub(s, '%+%s*%d+%s*/%s*%d+%s*$', '')
		return tonumber(s)
	end
	return nil
end

local function get_convert_args(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'
	
	unconverted = clean(s or '') -- basic unit cleaning
	
	s = mw.ustring.gsub(unconverted, '&[Nn][Bb][Ss][Pp];', ' ')
	
	local m = mw.ustring.find(s, 'm')
	local c = mw.ustring.find(s, 'cm')
	local f = mw.ustring.find(s, 'ft')
	local i = mw.ustring.find(s, 'in')
	
	if m == nil and f == nil and i == nil then
		return '', unconverted
	end
	
	if c ~= nil and f == nil and i == nil then
		local n = mw.ustring.sub(s, 1, c - 1)
		if isnumber(n) then
			return force_m
				and {n/100,'m','ftin',0,['abbr']='on'}
				or {n,'cm','ftin',0,['abbr']='on'}, mw.ustring.sub(s, c+2)
		end
		return '', unconverted
	end
	
	if m ~= nil and c == nil and f == nil and i == nil then
		local n = mw.ustring.sub(s, 1, m - 1)
		if isnumber(n) then
			return force_cm 
				and {n*100,'cm','ftin',0,['abbr']='on'}
				or {n,'m','ftin',0,['abbr']='on'}, mw.ustring.sub(s, m+1)
		end
		return '', unconverted
	end
	
	if f ~= nil and i ~=nil and m == nil then
		local n1 = mw.ustring.sub(s, 1, f - 1)
		local n2 = mw.ustring.sub(s, f+2, i - 1)
		if isnumber(n1) and isnumber(n2) then
			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(s, i+2)
		end
		return '', unconverted
	end
	
	if f ~= nil and i == nil and m == nil then
		local n = mw.ustring.sub(s, 1, f - 1)
		if isnumber(n) then
			return (force_m or prefer_m)
				and {n,'ft','m',2,['abbr']='on'}
				or {n,'ft','cm',0,['abbr']='on'}, mw.ustring.sub(s, f+2)
		end
		return '', unconverted
	end
	
	if i ~= nil and f == nil and m == nil then
		local n = mw.ustring.sub(s, 1, i - 1)
		if isnumber(n) then
			return (force_m or prefer_m)
				and {n,'in','m',2,['abbr']='on'}
				or {n,'in','cm',0,['abbr']='on'}, mw.ustring.sub(s, i+2)
		end
		return '', unconverted
	end
	
	return '', unconverted
end

function convert(frame, args)
	local targs, str = get_convert_args(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

function p.main(frame)
	return convert(frame, frame.args[1] and frame.args or frame:getParent().args)
end

return p