Module:Gapnum: Difference between revisions

From Zoophilia Wiki
Jump to navigationJump to search
meta>The Mol Man
n only *needed* to be a number when the module was subtracting to get frac part; turning it into a number reduces the degree of freedom we have in number length; such as on phi
meta>The Mol Man
rewrote a bit. would like something I can require for val
Line 8: Line 8:
end
end
local args = getArgs(frame, {wrappers = 'Template:Gapnum'})
local args = getArgs(frame, {wrappers = 'Template:Gapnum'})
local gap =  args.gap or '0.25em'
local n = args[1]
if not n then
error('Parameter 1 is required')
end
local gap =  args.gap
local precision = tonumber(args.prec)


local ret_string = '<span style="white-space:nowrap">'
return p._gaps(n,gap,precision)
end


if not args[1] then
function p._gaps(n,gap,precision)
error('Parameter 1 is required')
local nstr = tostring(n)
if not gap then
gap = '.25em'
end
end
local n = args[1]
if not precision then
if not tonumber(n) then
precision = -1
error('Unable to convert "' .. args[1] .. '" to a number')
end
end
local nstr = tostring(n)
local decimalloc = nstr:find('.', 1, true)
local decimalloc = nstr:find('.', 1, true)
local int_part, frac_part
local int_part, frac_part
Line 29: Line 34:
frac_part = nstr:sub(decimalloc + 1)
frac_part = nstr:sub(decimalloc + 1)
end
end
local ret = mw.html.create('span')
local int_string = ''
:css('white-space','nowrap')
-- Loop to handle most of the groupings; from right to left, so that if a group has
local int_string = {}
-- less than 3 members, it will be the first group
-- Loop to handle most of the groupings; from right to left, so that if a group has less than 3 members, it will be the first group
while int_part:len() > 3 do
while int_part:len() > 3 do
int_string = '<span style="margin-left:'..gap..';">'..int_part:sub(-3)..'</span>'..int_string
-- Insert in first spot, since we're moving backwards
table.insert(int_string,1,int_part:sub(-3))
int_part = int_part:sub(1,-4)
int_part = int_part:sub(1,-4)
end
end
-- handle any left over numbers; add directly
-- handle any left over numbers
ret:wikitext(int_part)
int_string = int_part..int_string
-- Build intstring
for _, v in ipairs(int_string) do
ret_string = ret_string..int_string
ret:tag('span')
:css('margin-left',gap)
local precision = tonumber(args.prec) or -1
:wikitext(v)
:done()
end
 
if precision ~= 0 and frac_part then
if precision ~= 0 and frac_part then
local frac_string = '.'
ret:wikitext('.')
if precision == -1 then
if precision == -1 then
precision = frac_part:len()
precision = frac_part:len()
Line 58: Line 67:
frac_part = frac_part .. string.rep('0', offset)
frac_part = frac_part .. string.rep('0', offset)
end
end
-- The first group after the decimal shouldn't have a gap between the decimal
-- The first group after the decimal shouldn't have a gap between the decimal
if frac_part:len() >= 3 then
ret:wikitext(frac_part:sub(1,3))
frac_string = frac_string..frac_part:sub(1,3)
frac_part = frac_part:sub(4)
frac_part = frac_part:sub(4)
for v in string.gmatch(frac_part,'%d%d?%d?') do
else
ret:tag('span')
frac_string = frac_string..frac_part
:css('margin-left',gap)
frac_part = ''
:wikitext(v)
end
:done()
-- Loop to handle most of the groupings; from left to right, so that if a group has
-- less than 3 members, it will be the last group
while frac_part:len() >= 3 do
frac_string = frac_string..'<span style="margin-left:'..gap..';">'..frac_part:sub(1,3)..'</span>'
frac_part = frac_part:sub(4)
end
-- Handle any left over numbers
if frac_part:len() > 0 then
frac_string = frac_string..'<span style="margin-left:'..gap..';">'..frac_part..'</span>'
end
if frac_string:len() > 1 then
ret_string = ret_string..frac_string
end
end
end
end
-- Closing span tag
ret_string = ret_string..'</span>'


return ret_string
return ret
end
end


return p
return p

Revision as of 22:47, 6 January 2015

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

local p = {}

local getArgs

function p.main(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	local args = getArgs(frame, {wrappers = 'Template:Gapnum'})
	local n = args[1]
	if not n then
		error('Parameter 1 is required')
	end
	local gap =  args.gap
	local precision = tonumber(args.prec)

	return p._gaps(n,gap,precision)
end

function p._gaps(n,gap,precision)
	local nstr = tostring(n)
	if not gap then
		gap = '.25em'
	end
	if not precision then
		precision = -1
	end
	local decimalloc = nstr:find('.', 1, true)
	local int_part, frac_part
	if decimalloc == nil then
		int_part = nstr
	else
		int_part = nstr:sub(1, decimalloc-1)
		frac_part = nstr:sub(decimalloc + 1)
	end
	local ret = mw.html.create('span')
							:css('white-space','nowrap')
	local int_string = {}
	-- Loop to handle most of the groupings; from right to left, so that if a group has less than 3 members, it will be the first group
	while int_part:len() > 3 do
		-- Insert in first spot, since we're moving backwards
		table.insert(int_string,1,int_part:sub(-3))
		int_part = int_part:sub(1,-4)
	end
	-- handle any left over numbers; add directly
	ret:wikitext(int_part)
	-- Build intstring
	for _, v in ipairs(int_string) do
		ret:tag('span')
				:css('margin-left',gap)
				:wikitext(v)
			:done()
	end

	if precision ~= 0 and frac_part then
		ret:wikitext('.')
		if precision == -1 then
			precision = frac_part:len()
		end
		-- Reduce the length of the string if required precision is less than actual precision
		-- OR
		-- Increase it (by adding 0s) if the required precision is more than actual
		local offset = precision - frac_part:len()
		if offset < 0 then
			frac_part = frac_part:sub(1,precision)
		elseif offset > 0 then
			frac_part = frac_part .. string.rep('0', offset)
		end
		-- The first group after the decimal shouldn't have a gap between the decimal
		ret:wikitext(frac_part:sub(1,3))
		frac_part = frac_part:sub(4)
		for v in string.gmatch(frac_part,'%d%d?%d?') do
			ret:tag('span')
					:css('margin-left',gap)
					:wikitext(v)
				:done()
		end
	end

	return ret
end

return p