Module:Saferoman

Documentation icon Module documentation[create]
local p={}
	local function getchar(str, i)
		return str:sub(i, i);
	end
function p.saferoman(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args;
	if (args[1] == nil)
	then
		return '';
	end
	local numstr = args[1];
	local i = 1;
	local num;
	local vin;
	local ret = '';
	while (i <= string.len(numstr))
	do
		local j = i;
		if (string.byte(getchar(numstr, i)) >= string.byte('0') and string.byte(getchar(numstr, i)) <= string.byte('9'))
		then
			while (j <= string.len(numstr) and string.byte(getchar(numstr, j)) >= string.byte('0') and string.byte(getchar(numstr, j)) <= string.byte('9'))
			do
				j = j + 1;
			end
			num = string.sub(numstr, i, j);
			num = num + 1;
			num = num - 1; --force num to be a number
			if (num > 3999)
			then
				ret = ret .. '<span style="text-decoration:overline;">'
				vin = math.floor(num / 1000);
				num = (num - (vin * 1000));
				while (vin > 3000)
				do
					ret = ret .. 'M';
					vin = vin - 1000;
				end
				ret = ret .. frame:expandTemplate{title='roman', args={vin}};
				ret = ret .. "</span>";
			end
			ret = ret .. frame:expandTemplate{title='roman', args={num}};
			i = j;
		else
			ret = ret .. getchar(numstr, i);
			i = i + 1;
		end
	end
	return ret;
end
return p;