La documentation pour ce module peut être créée à Module:Convèrsion/doc

local math_mod = require "Module:Math"
local formatnum = require "Module:Format".do_formatnum
local params = require "Module:Convèrsion/Balyês"
local defaultlang = 'frp'

local p = {}

local i18n = {
	invalidunit = 'Pâge avouéc un’unitât de mesera pas recognua'
}

local function numString(val, rounding, displayformat) -- transfôrme un nombro en chêna
	if rounding then
		val = math_mod._round( val, rounding )
	end
	val = formatnum({tostring(val)})
	if displayformat and displayformat.suffix then
		val = val .. suffix
	end
	return val
end

local function convert(value, sourceunitdata, targetunitdata) -- convèrtir na valor numerica en lo sin pariér dedens un’ôtr’unitât de mesera
	if not value then
		return nil
	end
	if type(value) ~= 'number' then
		return error("bad datatype: " .. type(value))
	end
	if (not sourceunitdata) or (not targetunitdata) then
		return value
	end
	return value * sourceunitdata[2] / targetunitdata[2]
end

function p.displayvalue(val, sourceunit, displayformat, errhandling) -- fât vêre na valor formatâye)
	
	-- prèparacion des paramètros
	local numval = tonumber(val)
	if not numval then -- se les balyês sont bizârres, lèssont la fonccion apelenta sè dèbrolyér
		return val
	end
	
	if not displayformat or type(displayformat) ~= 'table' then
		displayformat = {}
	end
	local showunit, showlink, rounding, targetunit = displayformat.showunit, displayformat.showlink, displayformat.rounding, displayformat.targetunit
	
	-- rècupèracion de les balyês regardent les unitâts
	if sourceunit and not targetunit then
		targetunit = sourceunit
	end
	local sourceunitdata, targetunitdata = sourceunit, targetunit
	if type(sourceunitdata) ~= 'table' then
		sourceunitdata = params.units[sourceunit] or params.units[params.redirects[sourceunit]]
	end
	if type(targetunitdata) ~= 'table' then	
		targetunitdata = params.units[targetunit] or params.units[params.redirects[targetunit]]
	end
	
	if sourceunitdata and targetunitdata and (targetunitdata[1] ~= sourceunitdata[1]) then
		return error("measurement unit mismatch: " .. l[1] .. ' and ' .. targetunitdata[1] )
	end
	
	-- partia numerica
	if (sourceunit and targetunit) and (sourceunit ~= targetunit) then
		numval = convert(numval, sourceunitdata, targetunitdata)
	end
	local numstr = numString(numval, rounding)


	-- visualisacion de l’unitât
	local unitstr, link
	if not targetunitdata then -- por èvitar les cofieries
		targetunitdata = {}
	end
	if showunit == 'long' then -- format long = montrar l’unitât tot entiér
		if (numval or 0) > 1 then
			unitstr = targetunitdata[7]
		else
			unitstr = targetunitdata[6]
		end
	elseif showunit then
		unitstr = targetunitdata[3]
	end
	if sourceunit and showunit and (not unitstr) then
		numstr = numstr .. '[[Category:' .. i18n.invalidunit .. ']]'
	end
	-- showlink
	if type( displayformat.showlink == 'string') then --lims pèrsonalisâs
		link = displayformat.showlink
	elseif displayformat.showlink == true then -- lims de vers l’articllo consacrâ
		link =  targetunitdata[5]
	end
	if unitstr and link then
		unitstr = '[[' .. link .. '|' .. unitstr .. ']]'
	end

	return numstr .. ' ' .. (unitstr or '')
end

function p.display(frame)
	local args = frame.args
	local value, origunit, targetunit = args[1], args[2], args[3]
	local rounding = args.rounding
	local showlink, showunit = 	args.showlink, args.showunit
	if showunit == 'true' then
		showunit = true
	end
	if showlink == 'true' then
		showlink = true
	end
	displayformat = {showunit = showunit, showlink = showlink, rounding = rounding, targetunit = targetunit}
	return p.displayvalue(value, origunit, displayformat)
end
return p