Aller au contenu

Module:Charte

De Wikiquestia

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

local p = {}

-- Tableau des chartes pré-définies
local chartes = {
  biographie = {
    titre     = "#6eacdc9c",
    soustitre = "#a9c8e09c",
    hr        = "#6eacdc9c",
  },
  empire = {
    titre     = "#993333b0",
    soustitre = "#831f1fc7",
    hr        = "#993333b0",
  },
  geographie = {
    titre     = "#209a57d9",
    soustitre = "#209a57a8",
    hr        = "#209a57d9",
  },
  conflit = {
    titre     = "#B0C4DE",
    soustitre = "#B0C4DE",
    hr        = "#B0C4DE",
  },

  darkgods = {
    titre     = "#000000",
    soustitre = "#000000e8",
    hr        = "#000000",
    color     = "#ffffff",
  },
  langue = {
    titre     = "#9b59b6b0",  -- violet principal
    soustitre = "#a674c8a8",  -- violet plus clair
    hr        = "#9b59b6b0",  -- même que le titre
  },
  defaut = {
    titre     = "#eaecf0",
    soustitre = "#a2a9b1",
    hr        = "#eaecf0",
  }
}

-- Textes par défaut selon la charte
local soustitres = {
  biographie = "Informations biographiques",
  empire     = "Données impériales",
  geographie = "Informations géographiques",
  darkgods   = "Entité obscure",
  defaut     = "Informations générales",
}

-- Fonction pour détecter un code hex (#xxxxxx ou #xxxxxxx)
local function isHexCode(value)
  return type(value) == "string" and mw.ustring.match(value, "^#%x%x%x%x%x%x%x?$") ~= nil
end

-- Fonction principale pour renvoyer une couleur selon charte et type
function p.couleur(frame)
  local args = frame.args
local couleurOuCharte = (args["couleur"] and args["couleur"] ~= "") and args["couleur"]
                     or (args["charte"] and args["charte"] ~= "") and args["charte"]
                     or "defaut"

  local type = args["type"] or "titre"

  -- Si l'utilisateur fournit un code hexadécimal direct
  if isHexCode(couleurOuCharte) then
    if type == "texte" then
      return "#000000" -- texte noir par défaut
    else
      return couleurOuCharte
    end
  end

  -- Si type=texte et une couleur de texte personnalisée est définie
  if type == "texte" then
    if chartes[couleurOuCharte] and chartes[couleurOuCharte].color then
      return chartes[couleurOuCharte].color
    else
      return "#000000"
    end
  end

  -- Si type=soustitre
  if type == "soustitre" then
    if chartes[couleurOuCharte] and chartes[couleurOuCharte].soustitre then
      return chartes[couleurOuCharte].soustitre
    else
      return chartes["defaut"].soustitre
    end
  end

  -- Autres types (titre, hr, etc.)
  if chartes[couleurOuCharte] and chartes[couleurOuCharte][type] then
    return chartes[couleurOuCharte][type]
  else
    return chartes["defaut"][type] or "#dddddd"
  end
end

-- Fonction pour renvoyer un texte de sous-titre par défaut
function p.soustitre(frame)
  local charte = frame.args["charte"] or "defaut"
  charte = mw.ustring.lower(charte)
  return soustitres[charte] or soustitres["defaut"]
end

return p