else
currentRomanDecimal, multiplier = currentRomanDecimal * multiplier, 1
if currentRomanDecimal < prevRomanDecimal then
result = result - currentRomanDecimal
else
result = result + currentRomanDecimal
prevRomanDecimal = currentRomanDecimal
end
end
end
return result
end  
--[==[
This function converts a string containing a Roman numeral to an integer.
It works for values between 0 and 4999999999.
The input string may contain HTML tags using (not recommended).
]==]--
local function convertRomanHTMLToArabic(roman)
local result = convertRomanToArabic(roman)
if result == nil then
result = tonumber(roman)
end
return result
[==[ DISABLED FOR NOW, NOT REALLY NEEDED AND NOT CORRECTLY TESTED
local result = 0
local overline_start_len = mw.ustring.len(overline_start)
if mw.ustring.sub(roman, 1, overline_start_len) == overline_start then
local end_tag_start, end_tag_end = mw.ustring.find(roman, overline_end, overline_start_len, true)
if end_tag_start ~= nil then
local roman_high = mw.ustring.sub(roman, overline_start_len + 1, end_tag_start - 1)
local roman_low = mw.ustring.sub(roman, end_tag_end + 1, mw.ustring.len(roman)) or ''
if (mw.ustring.find(roman_high, "^[mdclxvi]+$") ~= nil) and (mw.ustring.find(roman_low, "^[mdclxvi]*$") ~= nil) then
result = convertRomanToArabic(roman_high) * 1000 + convertRomanToArabic(roman_low)
end
end
end
return result
]==]
end  
--[==[
Helper function to handle error messages.
]==]--
local function outputError(message)
return table.concat({
'<strong>Roman Module Error: ', message,
'</strong>'
})
end  
--[================[
Public functions
--]================]  
--[==[
isRoman  
Tests if the trimmed input is a valid Roman numeral. Returns true if so, false if not.
For the purposes of this function, the empty string (after trimming whitespaces) is not a Roman numeral.  
Parameters
s: string to test if it is a valid Roman numeral  
Error Handling:
If the input is not a valid Roman numeral this function returns false.
]==]--
function p.isRoman(s)
return type(s) == 'string' and convertRomanToArabic(mw.text.trim(s)) ~= nil
end  
--[==[
toArabic  
This function converts a Roman numeral into an Arabic numeral.
It works for values between 0 and 4999999999.
'N' is converted to 0 and the empty string is converted to nil.  
Parameters
roman: string containing value to convert into an Arabic numeral  
Error Handling:
If the input is not a valid Roman numeral this function returns nil.
]==]--
function p.toArabic(roman)
if type(roman) == 'string' then
roman = mw.text.trim(roman)
local result = convertRomanToArabic(roman)
if result == nil then
result = tonumber(roman)
end
return result
elseif type(roman) == 'number' then
return roman
else
return nil
end
end  
--[==[
_Numeral  
This function returns a string containing the input value formatted as a Roman numeral.
It works for values between 0 and 4999999999.  
Parameters
value: integer or string containing value to convert into a Roman numeral  
Error Handling:
If the input does not look like it contains a number or the number is outside of the
supported range an error message is returned.
]==]--
function p._Numeral(value)
if value == nil then
return outputError('missing value')
end
if type(value) == 'string' then
value = tonumber(value)
elseif type(value) ~= 'number' then
return outputError('unsupported value')
end
return convertArabicToRoman(value)
end  
--[==[
Numeral  
This function for MediaWiki converts an Arabic numeral into a Roman numeral.
It works for values between 0 and 4999999999 (includes the whole range of unsigned 32-bit integers).
Arabic numeral zero is output as 'N' (for Latin negation adverbs "nec" or "non").  
Usage:  
- uses the caller's parameters  
Parameters
1: Value to convert into a Roman numeral. Must be at least 0 and less than 5,000,000.  
Error Handling:
If the input does not look like it contains a number or the number is outside of the
supported range an error message is returned.
]==]--
function p.Numeral
-- if no argument provided than check parent template/module args
local args = .args
if args[1] == nil then
args = :getParent.args
end
return p._Numeral(args[1])
end  
return p