« Module:Country data » : différence entre les versions
Aucun résumé des modifications |
réorganisation du code, ajout d'une fonction p.nationality |
||
| Ligne 6 : | Ligne 6 : | ||
local p = {}; | local p = {}; | ||
local linguistic = require "Module:Linguistique" | local linguistic = require "Module:Linguistique" | ||
local | local gdomain = mw.loadData ( "Module:Drapeau/Domaine" ) | ||
local gdata = mw.loadData ("Module:Country data/liste" ) | |||
local function _getCompetition(c,aaaa) | |||
local symbs = { | |||
['cm'] = "à la Coupe du monde", | |||
['coupedumonde'] = "à la Coupe du monde", | |||
['ce'] = "au championnat d'Europe", | |||
['euro'] = "au championnat d'Europe", | |||
[' | ['chm'] = "au championnat du monde", | ||
['can'] = "à la Coupe des Confédérations", | |||
['coupedesconfederations'] = "à la Coupe des Confédérations", | |||
['en'] = "en", | |||
['jo'] = "aux Jeux olympiques", | |||
['jp'] = "aux Jeux paralympiques", | |||
} | } | ||
local str = symbs[string.lower(c or '')] or "" | |||
--edition | --edition | ||
if(aaaa ~= nil and aaaa ~= "") then | if(aaaa ~= nil and aaaa ~= "") then | ||
| Ligne 51 : | Ligne 31 : | ||
local o=gdomain.jo["_"..aaaa]; | local o=gdomain.jo["_"..aaaa]; | ||
if(o ~= nil) then | if(o ~= nil) then | ||
str = str .." "..o | |||
end | end | ||
else | else | ||
str = str .." "..aaaa | |||
end | end | ||
end | end | ||
return | return str | ||
end | end | ||
function | local function printFlag(flagfile, alt, displayformat) | ||
if (border ~= | |||
border='border|' | displayformat = displayformat or {} | ||
local size = displayformat.size or '20x15' | |||
local border = displayformat.border or '1px' or 'border|' | |||
if (border ~= '-') then | |||
border = 'border|' | |||
end | end | ||
if not alt then | if not alt then | ||
alt = '' | alt = '' | ||
end | end | ||
return '<span class="flagicon">' .. | return '<span class="flagicon">' .. | ||
'[[Fichier:' .. | '[[Fichier:' .. flagfile .. '|' .. size ..'px|' .. border .. alt ..']]' .. | ||
'</span>'; | '</span>'; | ||
end | end | ||
local function | local function bestfordate(data, period) -- data contient une table dont les clés sont des dates au format ['2010'] = ou ['2010-05-04'] = | ||
if type(data) == string then | |||
return data | |||
end | end | ||
if (not period) or (period == 'default') then | |||
if not | return data.default | ||
return | |||
end | end | ||
if type( | local year, month, day | ||
if type(period) == 'table' then | |||
year, month, day = period.year, period.month, period.day | |||
end | end | ||
if type( | if type(period) == 'string' then | ||
year, month, data = period:match("(%d+)%-(%d+)%-(%d+)") | |||
end | end | ||
local | local val = datatable["default"] | ||
local numericyear = tonumber(year)-- ne prend en compte que les années // à améliorer | |||
if numericyear then | |||
if | |||
local bestdate = -10000 | local bestdate = -10000 | ||
for i, j in pairs (datatable | for i, j in pairs (datatable) do | ||
local k = tonumber(i) | local k = tonumber(i) | ||
if k and (k < numericdate) and (k > bestdate) then | if k and (k < numericdate) and (k > bestdate) then | ||
| Ligne 118 : | Ligne 87 : | ||
end | end | ||
end | end | ||
if | return val | ||
end | |||
local function valueAtDate(data, period, topic) -- topic: type de drapeau, genre de l'adjectif, etc. | |||
if type(data) == 'string' or type(data) == 'nil' then | |||
return data | |||
end | |||
if type(data) ~= 'table' then | |||
return error('bad datatabpe ' .. type(data)) | |||
end | |||
if topic and data[topic] then | |||
return valueAtDate(data[topic]) | |||
elseif type(data.default) == 'table' then -- si default est une table, ça veut dire defaulttopic, pas defaultdata | |||
return valueAtDate(data.default) | |||
end | |||
return bestfordate(data, period) | |||
end | |||
local function getData(datatable, typedata, topic, period) -- récupère la chaîne de caractère la plus appropriée dans la datatable | |||
-- datatable: la table de sous module par exemple [[Module:Country data/grenade]] | |||
-- typedata: "flag" / "name" / "adjective" | |||
-- period: data in ISO format | |||
-- topic: for instance "navy" for naval ensign | |||
local val = datatable[typedata] | |||
if not val then -- error handling ? | |||
return nil | return nil | ||
end | end | ||
return val | |||
local val = valueAtDate(val, period, topic) | |||
if type(val) ~= 'string' then -- error handling ? | |||
return error() | |||
end | |||
return val | |||
end | end | ||
local function getLabel(data, topic, | local function getAdjective(data, gender, number) | ||
if not gender then | |||
gender = 'm' | |||
end | |||
if not number then | |||
number = 's' | |||
end | |||
if (gender ~= 'm' and gender ~= 'f') then | |||
return error('gender should be m or f is ' .. gender) | |||
end | |||
if (number ~= 's' and number ~= 'p') then | |||
return error('number should be s or p is ' .. number) | |||
end | |||
return getData(data, 'adjective', (gender .. number)) | |||
end | |||
local function getLabel(data, topic, period, form) | |||
local label | local label | ||
if (not form) or form == 'short' then | if (not form) or form == 'short' then | ||
label = getData(data, 'shortname', topic, | label = getData(data, 'shortname', topic, period) | ||
end | end | ||
if not label then | if not label then | ||
| Ligne 138 : | Ligne 156 : | ||
end | end | ||
local function getLink(data, topic, | local function getLink(data, topic, period) | ||
local link = getData(data, 'link', topic, | local link = getData(data, 'link', topic, period) | ||
if (not link) and data.item then | if (not link) and data.item then | ||
link = mw.wikibase.label(getData(data, 'item')) | link = mw.wikibase.label(getData(data, 'item')) | ||
| Ligne 178 : | Ligne 196 : | ||
function p.List(frame) | function p.List(frame) | ||
return p.list(frame) | return p.list(frame) | ||
end | |||
local function flagIcon(data, flagtype, period, displayformat) | |||
local flagimage = getData(data, 'flag', flagtype, period) | |||
if flagimage then | |||
return printFlag(flagimage, alt, displayformat) | |||
end | |||
end | end | ||
| Ligne 210 : | Ligne 236 : | ||
local link = args["lien"] | local link = args["lien"] | ||
local label = args["label"] | local label = args["label"] | ||
local | local period = args[2] or args["année"] or '' | ||
local align = args["align"] or "left" | local align = args["align"] or "left" | ||
local target = args["target"] | local target = args["target"] | ||
| Ligne 228 : | Ligne 254 : | ||
end | end | ||
-- image | -- image | ||
local | local flag = flagIcon(data, flagtype) | ||
if (label == '-') then | if (label == '-') then | ||
return | return 'flag' | ||
end | end | ||
| Ligne 265 : | Ligne 287 : | ||
local val | local val | ||
if align == 'right' then | if align == 'right' then | ||
val = text .. ' ' .. | val = text .. ' ' .. flag | ||
else | else | ||
val = | val = flag .. ' ' .. text | ||
end | end | ||
return val, true -- true indique le succès | return val, true -- true indique le succès | ||
end | |||
function p.nationality(zone, gender, number, topic, period) | |||
local success, data = pcall(getDatatable, zone) | |||
if not success then return | |||
zone | |||
end | |||
local label = getAdjective(data, gender, number) | |||
local link = getLink(data, topic, period) | |||
if link then | |||
return '[[' .. link .. '|' .. label .. ']]' | |||
else | |||
return label | |||
end | |||
end | end | ||
Version du 31 mai 2015 à 15:51
--[[
This module is intended to replace the functionality of
[[Modèle:Drapeau2/Libellé]] and related
templates. It provides several methods, including
]]
local p = {};
local linguistic = require "Module:Linguistique" local gdomain = mw.loadData ( "Module:Drapeau/Domaine" ) local gdata = mw.loadData ("Module:Country data/liste" )
local function _getCompetition(c,aaaa) local symbs = { ['cm'] = "à la Coupe du monde", ['coupedumonde'] = "à la Coupe du monde", ['ce'] = "au championnat d'Europe", ['euro'] = "au championnat d'Europe", ['chm'] = "au championnat du monde", ['can'] = "à la Coupe des Confédérations", ['coupedesconfederations'] = "à la Coupe des Confédérations", ['en'] = "en", ['jo'] = "aux Jeux olympiques", ['jp'] = "aux Jeux paralympiques", }
local str = symbs[string.lower(c or )] or ""
--edition
if(aaaa ~= nil and aaaa ~= "") then
if(c=="jo" or c=="jp" ) then
local o=gdomain.jo["_"..aaaa];
if(o ~= nil) then
str = str .." "..o
end
else
str = str .." "..aaaa
end
end
return str
end
local function printFlag(flagfile, alt, displayformat)
displayformat = displayformat or {} local size = displayformat.size or '20x15' local border = displayformat.border or '1px' or 'border|'
if (border ~= '-') then border = 'border|' end
if not alt then alt = end
return '' ..
'' .. border .. alt ..'' ..
'';
end
local function bestfordate(data, period) -- data contient une table dont les clés sont des dates au format ['2010'] = ou ['2010-05-04'] = if type(data) == string then return data end if (not period) or (period == 'default') then return data.default end
local year, month, day if type(period) == 'table' then year, month, day = period.year, period.month, period.day end if type(period) == 'string' then year, month, data = period:match("(%d+)%-(%d+)%-(%d+)") end
local val = datatable["default"] local numericyear = tonumber(year)-- ne prend en compte que les années // à améliorer if numericyear then local bestdate = -10000 for i, j in pairs (datatable) do local k = tonumber(i) if k and (k < numericdate) and (k > bestdate) then val = j bestdate = k end end end return val end
local function valueAtDate(data, period, topic) -- topic: type de drapeau, genre de l'adjectif, etc.
if type(data) == 'string' or type(data) == 'nil' then
return data
end
if type(data) ~= 'table' then
return error('bad datatabpe ' .. type(data))
end
if topic and data[topic] then
return valueAtDate(data[topic])
elseif type(data.default) == 'table' then -- si default est une table, ça veut dire defaulttopic, pas defaultdata
return valueAtDate(data.default)
end
return bestfordate(data, period)
end
local function getData(datatable, typedata, topic, period) -- récupère la chaîne de caractère la plus appropriée dans la datatable -- datatable: la table de sous module par exemple Module:Country data/grenade -- typedata: "flag" / "name" / "adjective" -- period: data in ISO format -- topic: for instance "navy" for naval ensign
local val = datatable[typedata]
if not val then -- error handling ? return nil end
local val = valueAtDate(val, period, topic)
if type(val) ~= 'string' then -- error handling ? return error() end return val end
local function getAdjective(data, gender, number) if not gender then gender = 'm' end if not number then number = 's' end if (gender ~= 'm' and gender ~= 'f') then return error('gender should be m or f is ' .. gender) end if (number ~= 's' and number ~= 'p') then return error('number should be s or p is ' .. number) end return getData(data, 'adjective', (gender .. number)) end
local function getLabel(data, topic, period, form) local label if (not form) or form == 'short' then label = getData(data, 'shortname', topic, period) end if not label then label = getData(data, 'name') end if (not label) and data.item then label = mw.wikibase.label(getData(data, 'item')) end return label end
local function getLink(data, topic, period) local link = getData(data, 'link', topic, period) if (not link) and data.item then link = mw.wikibase.label(getData(data, 'item')) end return link end
local function applyregex(str, areadata) local cio = 'code CIO (en attente)' --require('Module:Wikidata')._formatStatements({entity= areadata.item, property= 'P984'}) or '??'
local label = getData(areadata, 'name') local of = linguistic.of(label, areadata.genre)
str = mw.ustring.gsub(str, '$de$label', of) str = mw.ustring.gsub(str, '$label', label) str = mw.ustring.gsub(str, '$cio', 'cio')
if string.find(str, '$gentile') then local function get(genre) return getData(areadata, 'adjective', genre) end local gentileMS, gentileFS, gentileMP, gentileFP = get('ms'), get('fs'), get('mp'), get('fp') str = mw.ustring.gsub(str, '$gentileMS', gentileMS) str = mw.ustring.gsub(str, '$gentileFS', gentileFS) str = mw.ustring.gsub(str, '$gentileMP', gentileMP) str = mw.ustring.gsub(str, '$gentileFP', gentileFP) end return str end
local function getDatatable(zone) zone = mw.ustring.lower(zone) if gdata[zone] then return require('Module:Country data/' .. gdata[zone]) else -- si le module existe mais n'est pas dans la liste return require('Module:Country data/' .. zone) end end
function p.List(frame)
return p.list(frame)
end
local function flagIcon(data, flagtype, period, displayformat)
local flagimage = getData(data, 'flag', flagtype, period)
if flagimage then
return printFlag(flagimage, alt, displayformat)
end
end
function p.standarddisplay(zone, args) if not zone then return nil end -- nettoyage des paramètres if not args then args = {} end for i, j in pairs(args) do args[i] = mw.text.trim(j) -- remove whitespaces if args[i] == then args[i] = nil end end
-- ajout des valeurs par défaut
local size = args["size"] or '20x15' local flagtype = args["type"]
local domain if args['domain'] and gdomain then domain = args['domain'] domain = mw.ustring.lower(string.gsub(domain, " ", "")) domain = linguistic.toascii(domain) domain = gdomain.domainlist["domain"] end if args["domain"] and not domain then return error("domaine non reconnu:" .. args["domain"]) end local align = args["align"] or 'left' local link = args["lien"] local label = args["label"] local period = args[2] or args["année"] or local align = args["align"] or "left" local target = args["target"] local competition = args["compétition"] local extra =
-- chargement des données if not zone then return nil -- error('paramètre 1 (pays) manquant') end local success, data = pcall(getDatatable, zone) if not success then if args.strict then return error('lieu non reconnu') end return zone, false -- false == échec end -- image local flag = flagIcon(data, flagtype) if (label == '-') then return 'flag' end
if (not link) then link = getLink(data, flagtype) end if (not label) then label = getLabel(data, flagtype) end
if competition then competition = _getCompetition(linguistic.toascii(args["compétition"]),args["édition"]) end
if link and competition then link = link .. ' ' .. competition end
if link then text = '' .. label .. '' end if target then text = target .. ' (' .. text .. ')' end
if string.find(text, '$') then -- si les données sont extraites d'un formulaire standard comme module:Drapeau/domain text = applyregex(text, data) end
local val if align == 'right' then val = text .. ' ' .. flag else val = flag .. ' ' .. text end return val, true -- true indique le succès end
function p.nationality(zone, gender, number, topic, period) local success, data = pcall(getDatatable, zone) if not success then return zone end
local label = getAdjective(data, gender, number) local link = getLink(data, topic, period) if link then return '' .. label .. '' else return label end end
function p.drapeau(frame) -- pour appel depuis un modèle : Erreur Lua dans package.lua à la ligne 80 : module 'Module:Drapeau/Domaine' not found. local success, val = pcall(p._drapeau, frame.args)
if success then return val end
-- return p._drapeau(frame.args) return frame.args[1] end
return p