« Module:Romain » : différence entre les versions

Contenu supprimé Contenu ajouté
m A modifié le niveau de protection de « Module:Romain » : Modèle très utilisé ([Modifier=Autoriser uniquement les utilisateurs autopatrolled] (infini) [Renommer=Autoriser uniquement les utilisateurs autopatrolled] (infini))
Ajout fonction fromRoman, et les fonctions permettant d'utiliser le module depuis un modèle
Ligne 63 :
end
 
function p.fromRoman( str, default )
str = str:upper()
if not str:find( '^[MDCLXVI]+$' ) then
if default then
return default
else
error( 'fromRoman expect a string containing only M, D, C, L, X, V or I letters' )
end
end
local smallRomans = { I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000 }
local result = 0
local old, n = 0
for i = str:len(), 1, -1 do
n = smallRomans[ str:sub( i, i ) ]
if n < old then
result = result - n
else
result = result + n
old = n
end
end
return result
end
 
-- compatibilité avec [[en:Module:Roman]]
function p._main(args)
return p.toRoman( args[1] )
end
 
function p.main( frame )
return p.nombreEnRomain( frame )
end
 
function p.nombreEnRomain( frame )
local num
if frame == mw.getCurrentFrame() then
num = frame.args[1] or frame:getParent().args[1]
else
num = frame
end
return p.toRoman( mw.text.trim( num ) )
end
 
function p.nombreDepuisRomain( frame )
local num
if frame == mw.getCurrentFrame() then
num = frame.args[1] or frame:getParent().args[1]
else
num = frame
end
return p.fromRoman( mw.text.trim( num ) )
end
return p