« Module:Country data » : différence entre les versions

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
spinoff de Module:Drapeau
0x010D (discussion | contributions)
Aucun résumé des modifications
Ligne 80 : Ligne 80 :
local val, alt
local val, alt
if (not datatable[typedata]) and ((typedata == 'naval') or (typedata == 'army') or (typedata == 'air force')) then -- s'il n'y a pas de drapeau spécialisé
-- si ce typedata (nom, drapeau, etc. est une chaîne, la retourner, sinon, ça doit être une table)
typedata = 'flag'
if type(datatable[typedata]) == 'string' then
return datatable[typedata]
end
end
if not datatable[typedata] then  
if type(datatable[typedata]) ~= 'table' then  
return error('demande invalide')
return error('demande invalide')
end
end
if type(datatable[typedata]) == 'string' then
 
return datatable[typedata]
-- s'il existe une valeur spéciale pour ce topic, par exemple, un drapeau spécial pour le militaire
if topic and type(val[topic]) == 'string' then
return val[topic]
end
if topic and type(val[topic]) == 'table' then
val = val[topic]
end
end
alt = datatable[typedata].alt
alt = datatable[typedata].alt

Version du 10 mars 2015 à 07:11

--[[ This module is intended to replace the functionality of Drapeau [[Modèle:Drapeau2/Libellé]] and related templates. It provides several methods, including ]]

local p = {};

-- Chargement de la banque de données des langues avec gestion d'erreur. local gdata local linguistic = require "Module:Linguistique" local success, resultat = pcall (mw.loadData, "Module:Country data/liste" )

-- chargement des données if success then

   gdata = resultat

else

   -- Banque de données à minima en cas de bogue dans le Module:Langue/Data
   gdata={

['france']={item="Q142", flag= "Flag_of_France.svg", name ="France"} } end

local gdomain local success, resultat = pcall (mw.loadData, "Module:Drapeau/Domaine" ) if success then gdomain = resultat end


local function _getCompetition(c,aaaa) local rep=""; if(c=="CM" or c=="coupedumonde") then rep="à la Coupe du monde" elseif(c=="CE" or c=="euro") then rep="au championnat d'Europe" elseif(c=="ChM") then rep="au championnat du monde" elseif(c=="CAN" or c=="coupedesconfederations") then

       rep="à la Coupe des Confédérations"
   elseif(c=="en" ) then
       rep="en"
   elseif(c=="jo" ) then
       rep="aux Jeux olympiques"
   elseif(c=="jp" ) then
       rep="aux Jeux paralympiques"
   end
   
   --edition
   if(aaaa ~= nil and aaaa ~= "") then
       if(c=="jo"  or c=="jp" ) then
           local o=gdomain.jo["_"..aaaa];
           if(o ~= nil) then
               rep=rep.." "..o
           end            
       else
           rep=rep.." "..aaaa
       end
   end
   return rep

end

function printIcon(filescr, alt, size, border) if (border ~= "no") or (border == false) then border='border|' end if not alt then alt = end

   return '' ..
               '' .. border .. alt ..'' ..
           '';

end

local function getData(datatable, typedata, topic, year) -- récupères les données stockées sur datatable -- typedata = flag ou nom -- datedata = year -- domain = arméée etc.) local val, alt

-- si ce typedata (nom, drapeau, etc. est une chaîne, la retourner, sinon, ça doit être une table) if type(datatable[typedata]) == 'string' then return datatable[typedata] end if type(datatable[typedata]) ~= 'table' then return error('demande invalide') end

-- s'il existe une valeur spéciale pour ce topic, par exemple, un drapeau spécial pour le militaire if topic and type(val[topic]) == 'string' then return val[topic] end if topic and type(val[topic]) == 'table' then val = val[topic] end alt = datatable[typedata].alt

local numericdate if year then numericdate = tonumber(year) end if numericdate then local bestdate = -10000 for i, j in pairs (datatable[typedata]) do local k = tonumber(i) if k and (k < numericdate) and (k > bestdate) then val = j bestdate = k end end end if not val then val = datatable[typedata][topic] or datatable[typedata]['default'] end return val, alt end

local function printLabel(data, topic) local label if not name then return getData(data, 'name') end end

local function printLink(areadata, topic) if areadata.link then return areadata.link[topic] or areadata.link['default'] end return mw.wikibase.sitelink(data.item) 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 standardText(areadata, topicdata) -- format standards stockées dans Module:Drapeau/domain local link = topicdata.link local label = topicdata.label return link, label 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

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 year = 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 flagimage, alt = getData(data, 'flag', domain, year) local icon -- le fichier mis en forme if flagimage then icon = printIcon(flagimage, alt, size, border) end if (label == '-') then return icon end

if domain then link, label = standardText(data, domain) end

if (not link) then link = printLink(data, flagtype) end if (not label) then label = printLabel(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 .. ' ' ..icon else val = icon .. ' ' .. text end return val, true -- true indique le succès end

function p.drapeau(frame) -- pour appel depuis un modèle : nom du pays 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