Aller au contenu

Module:Prix

De Wikivoyage

 Documentation[voir] [modifier] [historique] [purger]

Voir aussi

[modifier]

Catégories générées

[modifier]

require('strict')
local round = require('Module:Math')._round
local data = mw.loadData('Module:Prix/Data')

local lang = mw.getContentLanguage()
local NBSP = mw.ustring.char(0xA0)
local NNBSP = mw.ustring.char(0x202F)

local function _isDefined(s)
	return s ~= '' and s
end

local function narrow(s)
	return s:gsub('&#x[aA]0;', ' '):gsub(' ', ' '):gsub('&nn?bsp;', ' '):gsub(NBSP, ' '):gsub('^ +',''):gsub(' +$',''):gsub(' +', NNBSP)
end

local namespaceCategorisation = {
	[0] = true, [4] = true, [10] = true, [12] = true, [14] = true, [100] = true
}
-- Liste des devises à convertir
local conversions = {
	'EUR', 'USD', 'CAD', 'CHF', 'CFA'
 	-- Devises les plus courantes, en premier l’euro et celles liées à l’euro à taux fixe garanti (sont exclues ici les anciennes devises des membres de la zone Euro) :
--	'EUR', 'CVE', 'XAF', 'XCG', 'XOF', 'XPF',
 	-- Devises des autres principaux pays francophones :
--	'CAD', 'CHF', 'CHE', 'DZD', 'MAD', 'MGA', 'TND', 'VND',
	-- Autres devises de l'Union européenne :
--	'BGN', 'CZK', 'DKK', 'HUF', 'PLN', 'SVK',
	-- Autres devises européennes ou de leurs dépendances, à taux variable :
--	'ANG', 'GBP', 'NOK',
	-- Autres devises importantes les plus souvent converties :
--	'AUD', 'BRL', 'CNY', 'HKD', 'ILS', 'INR', 'JPY', 'KRW', 'NZD', 'RUB', 'TWD', 'USD', 'ZAR',
	-- Devises spéciales les plus importantes :
--	'XDR', 'XAU', 'BTC'
}

local function _prix(prix, devise)
	local result
	if type(prix) == 'number' then
		local prixLocal = narrow(lang:formatNum(prix))
		local dataLocal = data[devise] -- teste si le code de devise existe (ou si c'est une abréviation courante correspondant à une devise non ambiguë)
		if type(dataLocal) == 'table' and dataLocal.code then
			devise = dataLocal.code -- convertit l'abréviation reconnue en son code ISO standard
			result = '<span style="white-space:nowrap">' .. prixLocal .. NNBSP ..
				'<abbr title="'.. (dataLocal.nom or devise) .. '">' .. narrow(dataLocal.abreviation or devise) .. '</abbr></span>'
			local cours = tonumber(dataLocal.cours)
			if cours then -- si le cours de change est bien présent dans 'Module:Prix/data'
				local dateLocal = dataLocal.date or ''
				local first = '<span title="Les utilisateurs connectés peuvent choisir leur monnaie de référence dans leurs Préférences via l’onglet Gadgets">'
				for _, conv in ipairs(conversions) do
					conv = data[conv]
					if conv and conv.cours and conv.code ~= devise then -- devise différente et convertible
						local ref = tonumber(conv.cours)
						local prec = conv.precision and conv.precision
									or (ref > 50) and 0 or 2 -- data ne précise pas encore la précision des sous-unités (estime la selon son cours pour 1 euro)
						local date = conv.date or ''
						if date < dateLocal then
							date = dateLocal
						end
						result = result
							-- Il y a une exception pour les classes "MonnaieRef*', le gadget actuel reconnait l’alias 'CFA' (ambigu) et non le code ISO standard 'XOF' (ou 'XAF').
							.. first .. '<span class="MonnaieRef' .. (conv.code == 'XOF' and 'CFA' or conv.code) .. '" style="display:none;white-space:nowrap"> (≈ ' 
							.. narrow(lang:formatNum(round(prix * ref / cours, prec))) .. NNBSP
							.. '<abbr title="' .. conv.nom .. ', cours estimé du ' .. date .. '">' .. narrow(conv.abreviation) .. '</abbr>) </span> '
						first = ''
					end
				end
				if first == '' then
					result = result .. '</span>'
				end
			end
		else
			result = '<span style="white-space:nowrap">' .. prixLocal .. NNBSP .. narrow(devise) ..
				'<sup style="color:#F00"><small>[devise inconnue]</small></sup></span>'
			if namespaceCategorisation[mw.title.getCurrentTitle().namespace] then
				result = result .. '[[Catégorie:Page avec unité monétaire manquante ou inconnue]]'
			end
		end
	else
		if type(prix) ~= 'string' then
			prix = tostring(prix)
		end
		if type(devise) ~= 'string' then
			devise = tostring(devise)
		end
		result = '<span style="white-space:nowrap">' .. prix .. NNBSP .. devise ..
			'<sup style="color:#F00"><small>[Utilisez le point comme séparateur de décimales]</small></sup></span>'
			if namespaceCategorisation[mw.title.getCurrentTitle().namespace] then
				result = result .. '[[Catégorie:Page avec valeur numérique incorrecte]]'
			end
	end    
	return result
end

local function _processText(text)
    if not _isDefined(text) then return '' end
    local prices = {}
    local idCounter = 1

    -- Pattern 1a: numero seguito da simbolo valuta (es. 15,34€)
    for amount, currency in mw.ustring.gmatch(text, "([%d%.,]+)%s*([%$€£¥])") do
        table.insert(prices, {amount = amount, currency = currency})
    end
--[[
    -- Pattern 1b: numero seguito da valuta ISO (es. 1.234,56 EUR)
    for amount, currency in mw.ustring.gmatch(text, "([%d%.,]+)%s*([%a][%a][%a])") do
        table.insert(prices, {amount = amount, currency = currency})
    end
]]--
--[[
    -- Pattern 2a: simbolo valuta seguito da numero (es. £60)
   for currency, amount in mw.ustring.gmatch(text, "([%$€£¥])%s*([%d%.,]+)") do
        table.insert(prices, {amount = amount, currency = currency})
    end
]]--
--[[
    -- Pattern 2a: valuta ISO seguita da numero (es. USD1,234.99)
   for currency, amount in mw.ustring.gmatch(text, "([%a][%a][%a])%s*([%d%.,]+)") do
        table.insert(prices, {amount = amount, currency = currency})
    end
]]--
    -- Chiamo _prix per ogni match
    for _, price in ipairs(prices) do
        local num = tonumber((price.amount:gsub(",", ".")))
        local entry = data[price.currency]
        local iso = (entry and entry.code) or price.currency
        local formatted = _prix(num, iso)
		text = text:gsub(price.amount .. "%s*" .. price.currency, formatted, 1)
    end

    return text
end

local p = {}
function p.processText(frame)
    local text = frame.args[1] or frame.args.text or ""
    return _processText( text )
end

function p.Prix(frame)
	return _prix(tonumber(frame.args[1]), frame.args[2])
end
return p