Module:Étalicos biologicos

De Vouiquipèdia, l’enciclopèdia abada.

La documentation pour ce module peut être créée à Module:Étalicos biologicos/doc

-- gestion de la mise en italique d'un nom scientifique, en respectant les conventions de la biologie

local p = {}

-- fonction basique mettant le titre courant en italique, sauf la partie namespace et la partie homonymie si présente
-- paramètres : lang → optionnel : ajout d'un tag de langue si présent
function p.titro_en_etalicos(frame)

    local titro = mw.title.getCurrentTitle() -- l'objet titre pour la page courante
    local resu  -- le résultat qui sera retourné
    local page = titro.text  -- le nom de la page, sans le namespace
    -- paramètre optionnel : la langue
    local lang = frame.args["lang"] or frame:getParent().args["lang"]

    -- variables contenant l'ouverture et la fermeture du span (ou vide)
    local ospan = ""
    local fspan = ""
    if (lang) then
        ospan = '<span lang="' .. lang .. '">'
        fspan = '</span>'
    end
    
    -- préparation résultat : on commence par le namespace
    if (titro.nsText ~= "") then
        resu = titro.nsText .. ":"
    end
    -- on ajoute le span de la langue si demandé
    resu = resu .. ospan
    -- on ajoute l'italique
    resu = resu .. "''"
    if (mw.ustring.find(page, " (", 1, true)) then
        -- présence d'une homonymie, on ajoute la fin des italiques
        -- avant le " (" (une seule fois, au cas où il y aurait plusieurs ())
        -- on ajoute aussi la fermeture éventuelle du span
        resu = resu .. mw.ustring.gsub(page, " [(]", "''" .. fspan .. " (", 1)
    else
        -- pas d'homonymie, on ajoute la page + la fin de l'italique + fin du span
        resu = resu .. page .. "''" .. fspan
    end
    -- résultat, dans un preprocess afin d'interpréter son action
    return frame:preprocess("{{DISPLAYTITLE:" .. resu .. "|noreplace}}")
end

-- retourne le texte après avoir supprimer les espaces, retour ligne... en début et fin de texte.
-- si texte == nil, la fonction retourne nil. 
-- Si le texte est vide ou composé uniquement d'espces, la fonction retourne un texte vide ''.
function p.trim (texto)
    return texto and string.match (texto, '^%s*(.-)%s*$')
end

-- table des éléments à ne pas mettre en italique
-- note : mettre un " " avant un terme qui existe aussi sous forme longue
--  exemple : " var.", à cause de "convar.". Sans l'espace les deux vont s'appliquer
--            au texte analysé. Notez de bien répercuter ce même espace dans la partie de droite
p.exclude = {
    { " cl[.]", " ''cl.''" },
    { "convar[.]", "''convar.''" },
    { " f[.]", " ''f.''" },
    { " gen[.]", " ''gen.''" },
    { "kl[.]", "''kl.''" },
    { "nothog[.]", "''nothog.''" },
    { "nothosp[.]", "''nothosp.''" },
    { "nothovar[.]", "''nothovar.''" },
    { " ord[.]", " ''ord.''" },
    { " fam[.]", " ''fam.''" },
    { " sect[.]", " ''sect.''" },
    { " ser[.]", " ''ser.''" },
    { " sp[.]", " ''sp.''" },
    { "subg[.]", "''subg.''" },
    { "subsp[.]", "''subsp.''" },
    { " tr[.]", " ''tr.''" },
    { " var[.]", " ''var.''" },
    { "×", "''×''" },
    { "[(]", "''(''" },
    { "[)]", "'')''" }
}
-- liste d'exclusion uniquement pour mettre droit dans la partie {{taxobox}} italique (ifgenre)
p.exclude_span = {
    { " cl[.]", " <span style=\"font-style: normal\">cl.</span>" },
    { "convar[.]", "<span style=\"font-style: normal\">convar.</span>" },
    { " f[.]", " <span style=\"font-style: normal\">f.</span>" },
    { " gen[.]", " <span style=\"font-style: normal\">gen.</span>" },
    { "kl[.]", "<span style=\"font-style: normal\">kl.</span>" },
    { "nothog[.]", "<span style=\"font-style: normal\">nothog.</span>" },
    { "nothosp[.]", "<span style=\"font-style: normal\">nothosp.</span>" },
    { "nothovar[.]", "<span style=\"font-style: normal\">nothovar.</span>" },
    { " ord[.]", " <span style=\"font-style: normal\">ord.</span>" },
    { " fam[.]", " <span style=\"font-style: normal\">fam.</span>" },
    { " sect[.]", " <span style=\"font-style: normal\">sect.</span>" },
    { " ser[.]", " <span style=\"font-style: normal\">ser.</span>" },
    { " sp[.]", " <span style=\"font-style: normal\">sp.</span>" },
    { "subg[.]", "<span style=\"font-style: normal\">subg.</span>" },
    { "subsp[.]", "<span style=\"font-style: normal\">subsp.</span>" },
    { " tr[.]", " <span style=\"font-style: normal\">tr.</span>" },
    { " var[.]", " <span style=\"font-style: normal\">var.</span>" },
    { "×", "<span style=\"font-style: normal\">×</span>" },
    { "[(]", "<span style=\"font-style: normal\">(</span>" },
    { "[)]", "<span style=\"font-style: normal\">)</span>" }
}

--[[
  Liste d'exclusion de noms de clades qui sont arpetan et donc ne doivent pas être en italique
--]]
p.exclude_clados = {
    "Angiospèrmès"
}

--[[
  Retourne vrai si le nom passé est un nom de clade en arpetan (ça *doit* être un rang clade)
--]]
function p.clado_arpetan(nom)
    local tst -- on regarde si présence d'accents
    tst = mw.ustring.find(nom, "[éèêëàäâçùüûïîôö]")
    if (tst ~= nil) then
        return true -- visiblement en arpetan on laisse sans mettre en italique
    end
    -- liste d'exclusion de noms traités comme arpetan
    local i = 1
    while (p.exclude_clados[i] ~= nil) do
        if (p.exclude_clados[i] == nom) then
            -- exception, en arpetan, on laisse sans italiques
            return true
        end
        i = i + 1 
    end
    return false
end

-- si 'true' indique regne tout en italique
p.regnos = {
 ["èprôva"]=false, ["alga"]=true, ["bétye"]=false, ["arch·ès"]=true,
 ["bactèria"]=true, ["champegnon"]=true, ["protisto"]=false, ["planta"]=true, ["virus"]=true,
 ["netro"]=true, ["ecarioto"]=false, ["procarioto"]=true
}
-- si true indique rang inférieur (ou égal) au genre
p.renches = {
 ["clado"]=false, ["tipo"]=false, ["groupo"]=false, ["pas-rengiê"]=false, ["sot-fôrma"]=true,
 ["fôrma"]=true, ["cultivar"]=true, ["variètât"]=true, ["sot-èspèce"]=true, ["hibrido"]=true,
 ["èspèce"]=true, ["sot-sèria"]=true, ["sèria"]=true, ["sot-sèccion"]=false, ["sèccion"]=false,
 ["sot-genro"]=true, ["genro"]=true, ["sot-tribu"]=false, ["tribu"]=false, ["supèr-tribu"]=false,
 ["enfra-tribu"]=false, ["sot-famelye"]=false, ["famelye"]=false, ["èpifamelye"]=false, ["supèr-famelye"]=false,
 ["micro-ôrdre"]=false, ["enfra-ôrdre"]=false, ["sot-ôrdre"]=false, ["ôrdre"]=false, ["supèr-ôrdre"]=false,
 ["sot-cohorta"]=false, ["cohorta"]=false, ["supèr-cohorta"]=false, ["enfra-cllâssa"]=false, ["sot-cllâssa"]=false,
 ["cllâssa"]=false, ["supèr-cllâssa"]=false, ["enfra-fôrche"]=false, ["sot-fôrche"]=false, ["fôrche"]=false,
 ["supèr-fôrche"]=false, ["sot-division"]=false, ["division"]=false, ["supèr-division"]=false, ["enfra-règno"]=false,
 ["branche"]=false, ["sot-règno"]=false, ["règno"]=false, ["supèr-règno"]=false, ["sot-domêno"]=false,
 ["domêno"]=false, ["empiro"]=false
}
-- retourne true si rang+regne a besoin de l'italique
function p.renche_regno_et(renche, regno)
    if (renche == nil or renche == "" or regno == nil or regno == "") then
        return nil
    end
    local reg = p.regnos[regno]
    if (reg == nil) then
        return nil
    end
    if (reg == true) then
        return true -- tout en italique
    end
    local rag = p.renches[renche]
    if (rag == nil) then
        return nil
    end
    if (rag == true) then
        return true
    else
        return false
    end
end

-- met un nom scientifique en italique, en respectant les conventions.
-- cette fonction met en italique sans condition.
-- cette fonction présume que le nom passé est *uniquement* un nom scientifique (pas de partie homonyme)
-- la partie rang est utilisée pour détecter les "clades" en arpetan
function p.etalico_biologico(nom, renche, regno)
    if (nom == nil or nom == "") then
        return ""
    end
    
    -- si pas besoin de l'italique on laisse
    local it = p.renche_regno_et(renche, regno)
    if (it == nil or it == false) then
        return '<span style="font-style: normal">' .. nom .. '</span>'
    end
    
    -- si rang=clade et qu'on détecte que c'est de l'arpetan on laisse sans modifier
    if (renche == "clado") then
        if (p.clado_arpetan(nom)) then
            return '<span style="font-style: normal">' .. nom .. '</span>'
        end
    end
    
    -- on remplace dans le nom toutes les occurrences à protéger
    local i = 1
    while(p.exclude[i] ~= nil) do
        nom = mw.ustring.gsub(nom, p.exclude[i][1], p.exclude[i][2])
        i = i + 1    
    end
    
    -- on retourne la partie traitée
    -- en insérant les italiques au début et à la fin
    -- attention : si on a traité un élément au tout début (ou fin) il faut le virer et ne
    -- pas remettre de '' au début (ou fin)
    local com = "''"
    local fin = "''"
    if (mw.ustring.find(nom, "^''")) then
        nom = mw.ustring.sub(nom, 3) -- on supprime les 2 1er
        com = "" -- pas au début
    end
    if (mw.ustring.find(nom, "''$")) then
        nom = mw.ustring.sub(nom, 1, -3) -- on supprime les 2 derniers
        fin = "" -- pas à la fin
    end
    
    return '<span lang="la">' .. com .. nom .. fin .. '</span>'
end


-- traite un nom scientifique pour la mise en italique
function p.ns(frame)
    -- on récupère le rang (si cultivar, fonction dédiée)
    local renche = mw.ustring.lower(mw.text.trim(frame.args["renche"] or ""))
    -- on récupère le règne
    local regno = mw.ustring.lower(mw.text.trim(frame.args["règno"] or ""))
    -- juste un wrapper
    if (renche == "cultivar") then
        return ( p.etalico_cultivar(p.trim(frame.args[1] or frame:getParent().args[1] or "")) )
    else
        return ( p.etalico_biologico(p.trim(frame.args[1] or frame:getParent().args[1] or ""), renche, regno) )
    end
end


-- traite la mise en italiques d'un nom de cultivar. La forme est "XXXXX 'YYY'" (apostrophe simple ou typographique)
-- l'italique n'est que sur la première partie. Retourne nil si cette forme n'est pas détectée
function p.etalico_cultivar(nom)
    if (nom == nil or nom == "" or type(nom) ~= "string") then
        return nil
    end

    -- on verifie que le nom se termine par ' ou ’
    local der = mw.ustring.sub(nom, -1)
    if (der ~= "'" and der ~= "’") then
        return nil -- pas bon
    end
    -- on cherche la partie YYY
    local pd1, pf1 = mw.ustring.find(nom, "['].*[']$")
    local pd2, pf2 = mw.ustring.find(nom, "[‘].*[’]$")
    if (pd1 == nil and pd2 == nil) then
        return nil -- pas trouvé
    end
    local comencement = pd1 or pd2
    local fin = pf1 or pf2
    
    -- on récupère le début (à mettre en italiques)
    local part1 = mw.ustring.sub(nom, 1, comencement-1)
    if (part1 == nil or part1 == "") then
        return nil
    end
    local etpart1 = p.etalico_biologico(part1, "èspèce", "bétye")
    local resto = mw.ustring.sub(nom, comencement)
    return etpart1 .. resto
end

-- traite un nom de cultivar pour la mise en italique
function p.nc(frame)
    -- juste un wrapper
    return ( p.etalico_cultivar(p.trim(frame.args[1] or frame:getParent().args[1] or "")) )
end



-- applique la forme italique pour le titre de l'article (DISPLAYTITLE) à partir du
-- nom scientifique reçu en paramètre. Gère la partie homonymie. Détecte un NV
function p.titro(frame)
    local ttr
    local cat = ""

    -- le nom scientifique
    local ns = p.trim(frame.args[1] or frame:getParent().args[1] or "")
    if (ns == nil or ns == "") then
        -- pas de nom scientifique, on ne peut pas travailler
        return ""
    end
    
    -- on récupère règne et rang si présents
    local regno = frame.args["règno"] or frame:getParent().args["règno"]
    local renche = frame.args["renche"] or frame:getParent().args["renche"]
    regno = mw.text.trim(regno or "")
    renche = mw.text.trim(renche or "")

    -- test temporaire : si le nom scientifique contient de la mise en forme
    -- on range l'article dans une catégorie pour les détecter
    --- selon les combinaisons règne+rang on fait des choses différentes
    local a_eprovar = "[[{'†éèêëàâäùüûçîïôö]"
    if (regno == "virus") then
        a_eprovar = nil -- on trouve de tout dans les virus
    end
    if (renche == "clado") then
        a_eprovar = "[[{'†]" -- pour les clades les noms peuvent être en arpetan
    end
    if (regno == "bétye" and renche == "hibrido") then
        a_eprovar = "[[{'†]" -- pour les hybrides animaux les noms peuvent être en arpetan
    end
    if (renche == "cultivar") then
        a_eprovar = "[[{†]" -- les cultivars contiennent des ' ou ‘’
    end
    
    -- seulement si on a un test à faire
    if (a_eprovar ~= nil) then
        local tst = mw.ustring.find(ns, a_eprovar)
        if (tst) then
            cat = "[[Catègorie:Articllo avouéc taxobox a corregiér|NS]]" 
        end
    end

    -- spécial : si paramètre "titre" → on utilise à la place
    local ft = frame.args["titro"] or frame:getParent().args["titro"]
    if (ft) then
        -- titre forcé : on l'utilise à la place du titre réel
        -- et on ne vérifie pas le namespace
        ttr = ft
    else
        -- le titre
        local titro = mw.title.getCurrentTitle()
        if (titro.namespace ~= 0) then
            -- seulement les articles !
            return ""
        end
        ttr = titro.text
    end
    
    if (ttr == ns) then
        -- titre exactement NS → direct
        if (ft) then
            if (renche == "cultivar") then
                return frame:preprocess("<nowiki>" .. cat .. "{{DISPLAYTITLE:" .. p.etalico_cultivar(ns) .. "|noreplace}}</nowiki>")
            else
                return frame:preprocess("<nowiki>" .. cat .. "{{DISPLAYTITLE:" .. p.etalico_biologico(ns, renche, regno) .. "|noreplace}}</nowiki>")
            end
        else
            if (renche == "cultivar") then
                return cat .. frame:preprocess("{{DISPLAYTITLE:" .. p.etalico_cultivar(ns) .. "|noreplace}}")
            else
                return cat .. frame:preprocess("{{DISPLAYTITLE:" .. p.etalico_biologico(ns, renche, regno) .. "|noreplace}}")
            end
        end
    end
    
    -- si le titre est plus court que le NS ça ne peut être NS+homonymie
    local lng = mw.ustring.len(ns)
    if (mw.ustring.len(ttr) <= lng) then
        if (ft) then
            return frame:preprocess("<nowiki>" .. cat .. "</nowiki>")
        else
            return cat -- on ne fait rien
        end
    end
    
    -- on récupère les 'lng' premiers caractères du titre : ça doit être égal au NS
    -- sinon ça veut dire que ce n'est pas NS+homonymie
    local p1 = mw.ustring.sub(ttr, 1, lng)
    if (p1 ~= ns) then
        return cat -- on ne fait rien
    end
    
    -- la partie homonymie seule
    local hom = mw.ustring.sub(ttr, lng+1)
    
    -- on valide que la partie homonymie contient des ()
    local tst = mw.ustring.find(ttr, "[(]")
    if (tst == nil) then
        -- pas de parenthèse, ce n'est pas NS + homonymie
        return cat -- on ne touche rien
    end
    
    -- on retourne la mise en forme du NS italique + la partie homonymie
    if (ft) then
        if (renche == "cultivar") then
            return frame:preprocess("<nowiki>" .. cat .. "{{DISPLAYTITLE:" .. p.etalico_cultivar(ns) .. hom .. "|noreplace}}</nowiki>")
        else
            return frame:preprocess("<nowiki>" .. cat .. "{{DISPLAYTITLE:" .. p.etalico_biologico(ns, renche, regno) .. hom .. "|noreplace}}</nowiki>")
        end
    else
        if (renche == "cultivar") then
            return cat .. frame:preprocess("{{DISPLAYTITLE:" .. p.etalico_cultivar(ns) .. hom .. "|noreplace}}")
        else
            return cat .. frame:preprocess("{{DISPLAYTITLE:" .. p.etalico_biologico(ns, renche, regno) .. hom .. "|noreplace}}")
        end
    end
end


-- met un nom scientifique en italique, en respectant les conventions. Utilisable uniquement :
--  - dans le modèle {{taxobox}} car il utilise une syntaxe HTML et non wiki pour tenir compte du style CSS associé
--  - sur des taxons qui sont à coup sur en italiques (inf. au genre) car ne sait pas quelles sont les conditions autour
--  - ne gère pas la partie homonymie (pas supposé se rencontrer dans les noms de taxon)
function p.etalico_taxon(nom, renche)
    if (nom == nil or nom == "") then
        return ""
    end
    
    -- si c'est un clade et détecté français on enlève l'italique
    if (renche == "clado") then
        if (p.clado_arpetan(nom)) then
            return '<span style="font-style: normal">' .. nom .. '</span>'
        end
    end
    
    -- on remplace dans le nom toutes les occurrences à protéger
    local i = 1
    while(p.exclude[i] ~= nil) do
        nom = mw.ustring.gsub(nom, p.exclude_span[i][1], p.exclude_span[i][2])
        i = i + 1
    end
    
    return nom
end

-- wrapper
function p.nt(frame)
    local nom = mw.text.trim(frame.args[1] or frame:getParent().args[1] or "")
    if (nom == "") then
        return ""
    end
    local nom2 = mw.text.trim(frame.args[2] or frame:getParent().args[2] or "")
    local rang = mw.text.trim(frame.args[3] or frame:getParent().args[3] or "")
    
    local resu
    if (nom2 == "") then
        resu = p.etalico_taxon(nom, renche)
    else
        resu = p.etalico_taxon(nom2, renche)
    end
    
    if (nom == resu) then
        return " [[" .. resu .. "]]"
    else
        return frame:preprocess(" [[" .. nom .. "|" .. resu .. "]]")
    end
end



-- module
return p