Aller au contenu

Module:Formatage du titre

De Wreck
Version datée du 6 février 2020 à 22:38 par Module:Formatage_du_titre>Orlodrim

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

local p = {}

function p.newPagename(frame)
    local pagename = mw.title.getCurrentTitle().text

    local args = frame.args
    local lang
    if args[1] ~= '' then
        lang = args[1]
    end

    local position = pagename:find(' %(.+%)$')

    local base
    local disambig
    if position then
        base = pagename:sub(1, position - 1)
        disambig = pagename:sub(position)
    else
        base = pagename
        disambig = ''
    end

    if lang then
        base = '<span lang="' .. lang .. '">' .. base .. '</span>'
    end

    return "''" .. base .. "''" .. disambig
end

function p.makeItalicTitle(frame)
    local title
    if frame.args.title then
        title = mw.title.new(frame.args.title)
    else
        title = mw.title.getCurrentTitle()
    end

    -- Découpe le titre en trois parties :
    -- 1. prefix = espace de noms et éventuellement page mère (si c'est une sous-page)
    -- 2. mainPart = partie à mettre en italique (page ou sous-page, sans partie entre parenthèses)
    -- 3. disambig = partie entre parenthèses
    local prefix = ''
    if title.nsText ~= '' then
        prefix = title.nsText .. ':'
    end
    if title.isSubpage then
        prefix = prefix .. title.baseText .. '/'
    end

    local mainPart = title.subpageText

    local disambig = ''
    local disambigStart = mainPart:find(' %(.+%)$')
    if disambigStart then
        disambig = mainPart:sub(disambigStart)
        mainPart = mainPart:sub(1, disambigStart - 1)
    end

    -- Recollement des trois parties en insérant une balise de langue si besoin.
    local lang = frame.args[1]
    if lang and lang ~= '' then
        mainPart = '<span lang="' .. lang .. '">' .. mainPart .. '</span>'
    end
    return prefix .. "''" .. mainPart .. "''" .. disambig
end

return p