Difference between revisions of "Module:Person height"

From annadreambrush.com/wiki
Jump to navigation Jump to search
imported>Frietjes
(first attempt at a lua version of template:infobox person/height, will require massive debugging)
 
imported>Frietjes
Line 1: Line 1:
 
local p = {}
 
local p = {}
 +
 +
local cvt = require('Module:Convert').convert
  
 
local function clean(s)
 
local function clean(s)
Line 19: Line 21:
 
end
 
end
  
function convert(args)
+
function convert(frame, args)
 
local s = clean(args[1] or '')
 
local s = clean(args[1] or '')
 
 
Line 38: Line 40:
 
return s
 
return s
 
end
 
end
return require('Module:Convert').convert({n,'cm','ftin',0,['abbr']='on'}) .. mw.ustring.sub(cleaned, c, -1)
+
frame.args = {n,'cm','ftin',0,['abbr']='on'}
 +
return cvt(frame) .. mw.ustring.sub(cleaned, c, -1)
 
end
 
end
 
 
Line 46: Line 49:
 
return s
 
return s
 
end
 
end
return require('Module:Convert').convert({n,'m','ftin',0,['abbr']='on'}) .. mw.ustring.sub(cleaned, m, -1)
+
frame.args = {n,'m','ftin',0,['abbr']='on'}
 +
return cvt(frame) .. mw.ustring.sub(cleaned, m, -1)
 
end
 
end
 
 
Line 55: Line 59:
 
return s
 
return s
 
end
 
end
return require('Module:Convert').convert({n1,'ft',n2,'in', 'ftin',0,['abbr']='on'}) .. mw.ustring.sub(cleaned, i, -1)
+
frame.args = {n1,'ft',n2,'in', 'ftin',0,['abbr']='on'}
 +
return cvt(frame) .. mw.ustring.sub(cleaned, i, -1)
 
end
 
end
 
 
Line 63: Line 68:
 
return s
 
return s
 
end
 
end
return require('Module:Convert').convert({n,'ft','cm',0,['abbr']='on'}) .. mw.ustring.sub(cleaned, f, -1)
+
frame.args = {n,'ft','cm',0,['abbr']='on'}
 +
return cvt(frame) .. mw.ustring.sub(cleaned, f, -1)
 
end
 
end
 
 
Line 71: Line 77:
 
return s
 
return s
 
end
 
end
return require('Module:Convert').convert({n,'in','cm',0,['abbr']='on'}) .. mw.ustring.sub(cleaned, i, -1)
+
frame.args = {n,'in','cm',0,['abbr']='on'}
 +
return cvt(frame) .. mw.ustring.sub(cleaned, i, -1)
 
end
 
end
 
 
Line 78: Line 85:
  
 
function p.main(frame)
 
function p.main(frame)
return convert(frame.args[1] and frame.args or frame:getParent().args)
+
return convert(frame, frame.args[1] and frame.args or frame:getParent().args)
 
end
 
end
  
 
return p
 
return p

Revision as of 16:03, 25 September 2019

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

local p = {}

local cvt = require('Module:Convert').convert

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(frame, args)
	local s = clean(args[1] or '')
	
	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
		frame.args = {n,'cm','ftin',0,['abbr']='on'}
		return cvt(frame) .. mw.ustring.sub(cleaned, c, -1)
	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
		frame.args = {n,'m','ftin',0,['abbr']='on'}
		return cvt(frame) .. 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, i - 1))
		if n1 == nil or n2 == nil then
			return s
		end
		frame.args = {n1,'ft',n2,'in', 'ftin',0,['abbr']='on'}
		return cvt(frame) .. mw.ustring.sub(cleaned, i, -1)
	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
		frame.args = {n,'ft','cm',0,['abbr']='on'}
		return cvt(frame) .. mw.ustring.sub(cleaned, f, -1)
	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
		frame.args = {n,'in','cm',0,['abbr']='on'}
		return cvt(frame) .. mw.ustring.sub(cleaned, i, -1)
	end
	
	return s
end

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

return p