Usus +/-

Usurpatur in “Formula:S

{{A|{{#invoke:strConversion|laAncora|{{{1}}}}}}}{{#invoke:sectio|section}}<noinclude>

-- Copy of the [[:fr:Module:section]]
-- 2018-07-09 - Module:phrasis to Module:locutio.
-- 2017-05-21 - Removed obsolete reference to Module:linguae.
-- 2016-08-17 - Inhibited creating real categories until algorithm is fixed to work properly.
-- 2016-08-04 — Modified and extended by Usor:YaganZ
-- Modified and maintained by: Usor:DenisWasRight

b = require('Module:basis')
typ = require('Module:partes orationis')
-- lang = require('Module:linguae')
sect = require('Module:sectionibus')
locu = require('Module:locutio')

local p = {}

-- Types of words allowed in international conventions (-> no language name in the categories)
local conv_allowed = {
	['scientific names'] = true,
	['numbers'] = true,
}

-- General error messages
local link_help_section = "[[Victionarium:List of sections|<span title=\"Unknown section\">*</span>]]"
local link_help_type = "[[Victionarium:Types of words|<span title=\"Unknown section\">*</span>]]"
local link_help_language = "[[VT:List of languages|<span title=\"Unknown language\">*</span>]]"
local link_help_number = "<span title=\"Incorrect number\">*</span>"

-- Regroup the categories
categories = {}
-- Regroup the error messages
errors = {}

-- Activer pour voir les catégories générées (seulement pour déboguer avec l’aperçu : désactiver en sauvegardant)
-------------------------------
local isdebug = false
-------------------------------

-- For the anchors generated
local show_anchor = false

function _add_category(name_cat, key, writing)
	local text_category = ''
	-- Debug : displays all
    if isdebug then
    	if key then
        	text_category = b.fact_category(name_cat, nil, true) .. '(' .. key .. ')'
        else
        	text_category = b.fact_category(name_cat, nil, true)
        end
    -- Actual use: create a true category
    else
    	text_category = b.fact_category_content(name_cat, key, writing)
    end
    table.insert(categories, text_category)
end

function _add_error(message)
	if message ~= nil and message ~= '' then
    	table.insert(errors, message)
    end
end

-- Confirm that number is a positive integer
function _check_num(num)
    num = tonumber(num)
    if num == nil then return false end	-- Pas un nombre
    if num ~= math.floor(num) then return false end	-- Pas un entier
    if num > 0 then
       return true
    end
    return false
end

---------------------------------------------------------------------------------------------------------------------
-- SECTION TYPES OF WORDS

-- Crée le texte qui sera affiché pour la section de type de mot donné
function _fact_title(typen, flex, loc, num)
    -- Full Name
    local name = typ.get_name_singular(typen, loc, flex)
    
    -- No name ?
    if name == nil then
    	-- Peut-être parce qu’on demande un nom de locution pour un mot qui n’en est pas ?
    	local name_loc = typ.get_name_singular(typen, true, flex)
    	if not loc and name_loc ~= nil then
    		name = name_loc
    		_add_category("Victionarium:Phrases without space", typen)
    	end
    end
    
    -- Number ?
    local numtext = ''
    if num then
        if _check_num(num) then
            numtext = ' ' .. num
        else
            _add_error(link_help_number)
        	_add_category("Victionarium:Incorrect section number", num)
        end
    end
    
    -- Final display
    if (typen) then
    	-- Type of word defined?
        if name then
            return b.ucfirst(name) .. numtext
        else
        	-- Undefined: posted (but with an asterisk + category)
            _add_error(link_help_type)
        	_add_category('Victionarium:Sections with unknown name', typen)
            return numtext
        end
	-- If type is not even given
	else
		_add_error(link_help_type)
        _add_category('Victionarium:Untitled sections')
		return 'Untitled sections'
    end
end

-- Crée l’ancre correspondant au titre de section donné
function _fact_anchor(article, lang, typen, flex, loc, num)
    -- Type abbreviation
    local abrev = typ.get_abrev(typen, loc, flex)
    
    local anchor = ''
    
    -- Final display
    if (lang and abrev) then
        anchor = lang .. '-' .. abrev
        num = num or 1
       if num then
       		if _check_num(num) then
            	anchor = anchor .. '-' .. num
            end
        end
    end
    
    return anchor
end

local sinogramme -- pour un clé de sinogramme

-- Crée la catégorie correspondant au titre de section donné
function _fact_category(code_lang, typen, flex, loc, key, gender, title)
    -- Pas de catégorie si pas espace principal
    if not b.page_of_content then return '' end
    
    -- Nom complet du type au pluriel pour la catégorie
    local name = typ.get_name_plural(typen, loc, flex)
    
    -- Spécial : genre pour les prénoms
    if name == 'given names' and gender ~= nil then
    	if gender == 'm' then
    		name = name .. ' masc.'
    	elseif gender == 'f' then
    		name = name .. ' fem.'
    	elseif gender == 'mf' then
    		name = name .. ' mix.'
    	else
    		_add_category('Victionarium:Sections with invalid gender', gender)
    	end
    end
    
    -- Language name
--    local language = lang.get_name(code_lang)
    local language = 'Latine'
    
    -- Category "type of word" and "language"
    if name and language then
    	local name_cat = ''
		-- Spécial : sections spéciales en conventions internationales
		-- Pas de nom de langue pour ça
		if code_lang == 'conv' then
			if conv_allowed[name] ~= nil then
				name_cat = b.ucfirst(name)
			else
				-- Type de mot pas autorisé pour les conventions internationales : afficher normalement, mais catégoriser ?
        		--_ajoute_categorie('Wiktionnaire:Sections non autorisées en conventions internationales', nom)
				name_cat = b.ucfirst(name) .. '  ' .. language
			end
		else
			name_cat = b.ucfirst(name) .. '  ' .. language
		end

        -- Ajoute aussi la catégorie de langue si la clef est donnée (pour modifier la catégorie créée par la section de langue)
        if key ~= nil and key ~= '' then
        	_add_category(language, key)
        end

        -- clé de tri d’un sinogramme
        if name == 'sinogrammes' then
            if not sinogramme then
                sinogramme = require('Module:Sinogramme')
            end
            key = sinogramme.string_radical_line(title)
        end

        _add_category(name_cat, key)
    else
    	-- Nom correct, mais langue incorrecte : pas de catégorie
        if name then
			_add_error(link_help_language)
        	if code_lang ~= nil and code_lang ~= '' then
        		-- Code donné : n’est pas défini dans la liste
        		_add_category('Victionarium:Sections with unknown language', name)
        	else
        		-- Pas de code langue donné du tout
        		_add_category('Victionarium:Sections with unspecified language', name)
        	end
        end
    end
end

-- TYPES DE MOT : FONCTION POUR MODÈLE (ou en lui passant le frame d’un modèle)
function p.entry(frame)
    -- Récupération des variables nécessaires à la création du titre
    local args = frame:getParent().args
    local argsnum = b.trim_parametres(args)
    local article =  mw.title.getCurrentTitle()
    local typen = argsnum[1]        -- Le type de mot (nom standard ou alias)
    local lang  = argsnum[2]        -- Code langue
    local key = args['key']		-- Clé de tri (quand le tri par défaut ne convient pas)
    
    -- Stocker les erreurs pour afficher les catégories à la fin
    local error_cat = ''
    
    -- s’agit-il d’une flexion ?
    local flex = false
    if argsnum[3] then
    	if argsnum[3]=='flexion' then
        	flex=true
        else
    		_add_category('Victionarium:Sections type with invalid parameter 3')
    	end
    end
    
    -- s’agit-il d’une locution ?
    local loc = locu.is_phrase(args, article, lang)
    
    local num = args.num
    
    -- S’il s’agit d’un alias, on crée une catégorie (pour remplacer les alias par le mot standard si on le veut)
    if typ.is_alias(typen) then
    	_add_category('Victionarium:Types of words sections using alias', typen)
    end
    
    -- Crée le texte, l’ancre, la catégorie et utilise le tout pour créer le titre de section de type de mot complet.
--    _fact_category(lang, typen, flex, loc, key, args['gender'], article.text)
    local text_title = _fact_title(typen, flex, loc, num)
    local anchor = _fact_anchor(article, lang, typen, flex, loc, num)
    -- Récupère aussi la class du type de mot (si elle existe) pour afficher une icône adaptée
    local class = sect.get_class('dictio') or ""
    local final =  '<span class="' .. class .. '" id="' .. anchor .. '">' .. text_title  .. '</span>'
    
    -- Ajoute ancre par défaut
    if num == nil or tonumber(num) == 1 then
    	local anchor_defaut = mw.ustring.gsub(anchor, '-1$', '')
    	local anchor_defaut_span = '<span id="' .. anchor_defaut .. '" ></span>'
    	final = final .. anchor_defaut_span
	end
	
    if show_anchor then final = final .. ' ' .. anchor end
    return final
end

---------------------------------------------------------------------------------------------------------------------
-- AUTRES SECTIONS (étymologie, synonymes...)

-- AUTRES SECTIONS : FONCTION POUR MODÈLE (ou en lui passant le frame d’un modèle)
-- Création d’un titre de section pour tout ce qui n’est pas type de mot, ni langue
function p.section_other(frame)
    -- Récupération des variables nécessaires à la création du titre
    local args = frame:getParent().args
    local argsnum = b.trim_parametres(args)
    local title = argsnum[1]
    if argsnum[2] then
    	_add_category('Victionarium:Sections with unnecessary parameters', argsnum[2])
    end
    
    -- S’il s’agit d’un alias, on crée une catégorie (pour remplacer les alias par le mot standard si on le veut)
    if sect.is_alias(title) then
    	-- On ignore ces deux titres, utilisés (pour l’instant) sans préférence
    	local ignore_alias = {['voir'] = true, ['trad-trier'] = true, ['variantes orthographiques'] = true}
    	if ignore_alias[title] == nil then
    		_add_category('Victionarium:Sections using alias', title)
    	end
    end
    
    -- Récupération du texte associé à ce titre (créé dans le module dédié section_article)
    local text_title = title and b.ucfirst(sect.get_name_section(title)) or "Untitled sections"
    
    -- Récupère aussi la class de la section (si elle existe) pour afficher une icône adaptée
    local class = sect.get_class(title) or ""
    
    -- Finalisation du texte affiché
    local final =  '<span class="' .. class .. '">' .. text_title  .. '</span>'
    return final
end

---------------------------------------------------------------------------------------------------------------------
-- TOUTES SECTIONS (autres que LANGUE)

-- TOUTES SECTIONS : FONCTION POUR MODÈLE
-- Renvoie un titre de section, normale ou avec de type de mot défini
function p.section(frame)
    local args = frame:getParent().args
    local title = args[1] and mw.text.trim(args[1]) or ''
    
    local text_final = ''
    
    -- Pas même un titre donné ?
    if title == nil or title == '' then
        _add_error(link_help_section)
        _add_category('Victionarium:Untitled sections')
        text_final = 'Untitled sections'
    else
	    
	    -- S’agit-il d’un titre de type de mot ?
	    if typ.is_type(title) then
	        text_final = p.entry(frame)
	        
	    -- Sinon, est-ce une section autorisée ? (pas de fonction dédiée pour l’instant)
	    elseif sect.is_title(title) then
	        text_final = p.section_other(frame)
	    
	    -- Section non-supportée : on affiche quand même la section, mais avec un lien vers l’aide (avec une *)
	    -- pour soit 1) trouver le bon nom à utiliser,
	    -- soit 2) proposer que le titre utilisé soit supporté.
		else
			-- Lien d’aide selon qu’il y a une langue donnée ou pas
    		if args[2] ~= nil and args[2] ~= '' then
        		_add_error(link_help_type)
        	else
        		_add_error(link_help_section)
        	end
        	
        	_add_category('Victionarium:Sections with unknown title', title)
	        text_final = b.ucfirst(title)
	    end
	end
    
    -- Finalise les erreurs et les catégories
    local text_errors = table.concat(errors)
    local text_categories = table.concat(categories)
    return text_final .. text_errors .. text_categories
end

return p