မာတိကာသို့ ခုန်သွားရန်

မဝ်ဂျူ:bases

နူ ဝိက်ရှေန်နရဳ

Documentation for this module may be created at မဝ်ဂျူ:bases/doc

local p = {}
local locale = mw.language.new('fr')
local currentTitle = mw.title.getCurrentTitle()

-- Namespaces

p.NS_MEDIA = {
  id = -2,
  canonical_name = 'Media',
  local_name = 'Media',
}
p.NS_SPECIAL = {
  id = -1,
  canonical_name = 'Special',
  local_name = 'Special',
}
p.NS_MAIN = {
  id = 0,
  canonical_name = '',
  local_name = '',
}
p.NS_TALK = {
  id = 1,
  canonical_name = 'Talk',
  local_name = 'Discussion',
}
p.NS_USER = {
  id = 2,
  canonical_name = 'User',
  local_name = 'Utilisateur',
}
p.NS_USER_TALK = {
  id = 3,
  canonical_name = 'User talk',
  local_name = 'Discussion utilisateur',
}
p.NS_PROJECT = {
  id = 4,
  canonical_name = 'Project',
  local_name = 'Wiktionnaire',
}
p.NS_PROJECT_TALK = {
  id = 5,
  canonical_name = 'Project talk',
  local_name = 'Discussion Wiktionnaire',
}
p.NS_FILE = {
  id = 6,
  canonical_name = 'File',
  local_name = 'Fichier',
}
p.NS_FILE_TALK = {
  id = 7,
  canonical_name = 'File talk',
  local_name = 'Discussion fichier',
}
p.NS_MEDIAWIKI = {
  id = 8,
  canonical_name = 'MediaWiki',
  local_name = 'MediaWiki',
}
p.NS_MEDIAWIKI_TALK = {
  id = 9,
  canonical_name = 'MediaWiki talk',
  local_name = 'Discussion MediaWiki',
}
p.NS_TEMPLATE = {
  id = 10,
  canonical_name = 'Template',
  local_name = 'Template',
}
p.NS_TEMPLATE_TALK = {
  id = 11,
  canonical_name = 'Template talk',
  local_name = 'Model discussion',
}
p.NS_HELP = {
  id = 12,
  canonical_name = 'Help',
  local_name = 'Help',
}
p.NS_HELP_TALK = {
  id = 13,
  canonical_name = 'Help talk',
  local_name = 'Discussion aide',
}
p.NS_CATEGORY = {
  id = 14,
  canonical_name = 'Category',
  local_name = 'Category',
}
p.NS_CATEGORY_TALK = {
  id = 15,
  canonical_name = 'Category talk',
  local_name = 'Category discussion',
}

p.NS_ANNEXE = {
  id = 100,
  canonical_name = 'Annexe',
  local_name = 'Annexe',
}
p.NS_DISCUSSION_ANNEXE = {
  id = 101,
  canonical_name = 'Discussion Annexe',
  local_name = 'Discussion Annexe',
}
p.NS_TRANSWIKI = {
  id = 102,
  canonical_name = 'Transwiki',
  local_name = 'Transwiki',
}
p.NS_DISCUSSION_TRANSWIKI = {
  id = 103,
  canonical_name = 'Discussion Transwiki',
  local_name = 'Discussion Transwiki',
}
p.NS_PORTAIL = {
  id = 104,
  canonical_name = 'Portail',
  local_name = 'Portail',
}
p.NS_DISCUSSION_PORTAIL = {
  id = 105,
  canonical_name = 'Discussion Portail',
  local_name = 'Discussion Portail',
}
p.NS_THESAURUS = {
  id = 106,
  canonical_name = 'Thesaurus',
  local_name = 'Thesaurus',
}
p.NS_DISCUSSION_THESAURUS = {
  id = 107,
  canonical_name = 'Discussion Thesaurus',
  local_name = 'Discussion Thesaurus',
}
p.NS_PROJET = {
  id = 108,
  canonical_name = 'Projet',
  local_name = 'Projet',
}
p.NS_DISCUSSION_PROJET = {
  id = 109,
  canonical_name = 'Discussion Projet',
  local_name = 'Discussion Projet',
}
p.NS_RECONSTRUCTION = {
  id = 110,
  canonical_name = 'ဗီုပြင်သိုင်တၟိ',
  local_name = 'ဗီုပြင်သိုင်တၟိ',
}
p.NS_DISCUSSION_RECONSTRUCTION = {
  id = 111,
  canonical_name = 'Discussion Reconstruction',
  local_name = 'Discussion Reconstruction',
}
p.NS_TUTORIEL = {
  id = 112,
  canonical_name = 'Tutoriel',
  local_name = 'Tutoriel',
}
p.NS_DISCUSSION_TUTORIEL = {
  id = 113,
  canonical_name = 'Discussion Tutoriel',
  local_name = 'Discussion Tutoriel',
}
p.NS_RIME = {
  id = 114,
  canonical_name = 'Rime',
  local_name = 'Rime',
}
p.NS_DISCUSSION_RIME = {
  id = 115,
  canonical_name = 'Discussion Rime',
  local_name = 'Discussion Rime',
}
p.NS_CONJUGAISON = {
  id = 116,
  canonical_name = 'Conjugaison',
  local_name = 'Conjugaison',
}
p.NS_DISCUSSION_CONJUGAISON = {
  id = 117,
  canonical_name = 'Discussion Conjugaison',
  local_name = 'Discussion Conjugaison',
}
p.NS_RACINE = {
  id = 118,
  canonical_name = 'Racine',
  local_name = 'Racine',
}
p.NS_DISCUSSION_RACINE = {
  id = 119,
  canonical_name = 'Discussion Racine',
  local_name = 'Discussion Racine',
}

p.NS_MODULE = {
  id = 828,
  canonical_name = 'Module',
  local_name = 'Module',
}
p.NS_MODULE_TALK = {
  id = 829,
  canonical_name = 'Module talk',
  local_name = 'Discussion module',
}

p.NS_TRANSLATIONS = {
  id = 1198,
  canonical_name = 'Translations',
  local_name = 'Translations',
}
p.NS_TRANSLATIONS_TALK = {
  id = 1199,
  canonical_name = 'Translations talk',
  local_name = 'Translations talk',
}

p.NS_GADGET = {
  id = 2300,
  canonical_name = 'Gadget',
  local_name = 'Gadget',
}
p.NS_GADGET_TALK = {
  id = 2301,
  canonical_name = 'Gadget talk',
  local_name = 'Discussion gadget',
}
p.NS_GADGET_DEFINITION = {
  id = 2302,
  canonical_name = 'Gadget definition',
  local_name = 'Definition of gadget',
}
p.NS_GADGET_DEFINITION_TALK = {
  id = 2303,
  canonical_name = 'Gadget definition talk',
  local_name = 'Discussion gadget definition',
}

p.NS_TOPIC = {
  id = 2600,
  canonical_name = 'Topic',
  local_name = 'Sujet',
}

-- Fonctions

--- Indicates whether the given word triggers an elision.
--- NB: cas particuliers non pris en charge (p. ex. haricot)
--- @param word string the word to test.
--- @return boolean true if the word triggers the election, false otherwise.
function p.is_elidable(word)
  return mw.ustring.match(p.lcfirst(word), "^h?[aáàâäeéèêëiíìîïoóòôöuúùûüǘǜ]") ~= nil
end

--- Indicates whether the given word is a phrase (if it contains at least one space).
--- @param word string the word to test.
--- @return boolean true if the word contains a space, false otherwise.
function p.is_locution(word)
  if word == nil then
    return nil
  end
  return mw.ustring.find(word, ". .") ~= nil
end

--- Indicates whether the given page exists.
--- @param title string the title of the page.
--- @return boolean true if the corresponding page exists, false otherwise.
function p.page_existe(title)
  return title ~= nil and mw.title.new(title).exists
end

--- Returns the text with the first letter in uppercase (if the text is in French).
--- @param text string the text.
--- @return string the text with the first letter in uppercase.
function p.ucfirst(text)
  if text == nil then
    return nil
  end
  -- parentheses needed
  return (mw.ustring.gsub(text, "^([’ǂǃǀǁ]*.)", p.uc))
end

--- Returns the text with the first letter in lowercase (if the text is in French).
--- @param text string the text.
--- @return string the text with the first letter lowercase.
function p.lcfirst(text)
  if text == nil then
    return nil
  end
  return locale:lcfirst(text)
end

--- Returns the text with capital letters (if the text is in French).
--- @param text string the text.
--- @return string the uppercase text.
function p.uc(text)
  if text == nil then
    return nil
  end
  return locale:uc(text)
end

--- Returns the text with lowercase (if the text is in French).
--- @param text string the text.
--- @return string the lowercase text.
function p.lc(text)
  if text == nil then
    return nil
  end
  return locale:lc(text)
end

--- Returns true if the context is a content page (main, annex, thesaurus or reconstruction).
--- @return boolean
function p.page_de_contenu()
  local ns = currentTitle.namespace
  return ns == p.NS_MAIN.id or ns == p.NS_ANNEXE.id or ns == p.NS_THESAURUS.id or ns == p.NS_RECONSTRUCTION.id or ns == p.NS_RIME.id or ns == p.NS_CONJUGAISON.id or ns == p.NS_RACINE.id
end

--- Returns true if we are in a page in the main space.
--- @return boolean
function p.page_principale()
  return currentTitle:inNamespace(p.NS_MAIN.id)
end

--- Generates a category.
--- @param name string the name of the category.
--- @param sortingKey string the sorting key.
--- @param asLink boolean Indicates whether the function should return a link (:Category:…) or not.
--- @return string the category or an empty string if the name is nil.
function p.fait_categorie(name, sortingKey, asLink)
  local cat = asLink and ':Category:' or 'Category:'

  if name ~= nil then
    if sortingKey ~= nil and sortingKey ~= '' then
      return '[[' .. cat .. name .. '|' .. sortingKey .. ']]'
    else
      return '[[' .. cat .. name .. ']]'
    end
  else
    return ''
  end
end

--- Generates a category only if the context is in the main space,
--- annex, thesaurus or reconstruction.
--- @param name string the name of the category.
--- @param sortingKey string the sorting key.
--- @param asLink boolean Indicates whether the function should return a link (:Category:…) or not.
--- @return string the category or an empty string if the name is nil.
function p.fait_categorie_contenu(name, sortingKey, asLink)
  if p.page_de_contenu() then
    return p.fait_categorie(name, sortingKey, asLink) or ''
  else
    return ''
  end
end

--- Generates a category only if the context is in the main space.
--- @param name string the name of the category.
--- @param sortingKey string the sorting key.
--- @param asLink boolean Indique si la fonction doit retourner un lien (:Category:…) ou non.
--- @return string the category or an empty string if the name is nil.
function p.fait_categorie_principale(name, sortingKey, asLink)
  if p.page_principale() then
    return p.fait_categorie(name, sortingKey, asLink) or ''
  else
    return ''
  end
end

--- Creates the header of a sortable wiki table.
--- @param columnTitles table the titles of the columns.
--- @return string the header of the table.
function p.tableau_entete(columnTitles)
  return '{| class="wikitable sortable"\n|-\n!' .. table.concat(columnTitles, ' !! ')
end

--- Creates a wiki table row.
--- @param elements table the list of elements of the row.
--- @return string the row of the array.
function p.tableau_ligne(elements)
  return '|-\n|' .. table.concat(elements, ' || ')
end

--- Creates the end of a wiki table.
--- @return string the end of the array.
function p.tableau_fin()
  return '|}\n'
end

--- Superscript text.
--- @param text string the text.
--- @return string the text in a tag <sup>.
function p.exposant(text)
  return mw.ustring.format('<sup style="font-size:83%%;line-height:1">%s</sup>', text)
end

--- Tag a text written in a foreign language.
--- @param text string the text.
--- @param code string the language code ([[Module:langues/data]]).
--- @return string the tagged text.
function p.balise_langue(text, code)
  return mw.ustring.format('<span lang="%s" xml:lang="%s" class="lang-%s">%s</span>', code, code, code, text)
end

--- Removes spaces on either side of any parameters provided.
--- @param args table the table of parameters.
--- @return table a new table containing the parameters without spaces.
--- @deprecated Use [[Module:setting]] instead.
function p.trim_parametres(args)
  if args == nil then
    return nil
  end

  local trim_args = {}
  for k, v in pairs(args) do
    trim_args[k] = mw.text.trim(v)
  end

  return trim_args
end

--- Create a link.
--- @param word string the word to link to.
--- @param langCode string the language code.
--- @param anchor string L’ancre, typiquement l’ID de la section dans la page.
--- @param text string the text to display (optional).
--- @param addLangAnchor boolean If true, the language code is added to the link,
---                              only if anchor is not specified.
--- @return string Le lien.
function p.lien_modele(word, langCode, anchor, text, addLangAnchor)
  if text and text == '' then
    text = nil
  end

  if word == currentTitle.prefixedText then
    if addLangAnchor then
      return p.balise_langue(mw.ustring.format('[[%s#%s|%s]]', word, langCode, text or word), langCode)
    else
      return p.balise_langue(mw.ustring.format('[[%s|%s]]', word, text or word), langCode)
    end
  else
    if anchor and anchor ~= '' then
      anchor = langCode .. '-' .. anchor
    else
      anchor = langCode
    end

    return p.balise_langue(mw.ustring.format('[[%s#%s|%s]]', word, anchor, text or word), langCode)
  end
end

--- Find the position of the last occurrence of the second string in the first.
--- @param subject string the string to search for.
--- @param s string the string to search for.
--- @return number the position of the last occurrence of "s" in "subject" or 0 if none was found.
function p.derniereOccurrence(subject, s)
  local dernier = 0

  while subject:sub(dernier + 1, subject:len()):find(s) ~= nil do
    dernier = dernier + subject:sub(dernier + 1, subject:len()):find(s)
  end

  return dernier
end

return p