Módulo:Suppress categories

Documentação do módulo[ver] [editar] [histórico] [purgar]

Este é um módulo simples que ajuda a retirar categorias do wikitexto. Por exemplo, se passar o código "foo[[Categoria:Alguma categoria]]", vai devolver apenas "foo". Este modelo pode ser utilizado também para remover categorias de predefinições; se passar o código "{{minha predefinição}}", vai devolver a predefinição menos as categorias.

A predefinição trata de maneira correcta as categorias marcadas com dois pontos, categorias cujos nomes incluem caracteres inválido como ">", e categorias que tem tags nowiki em volta. Mesmo assim, isso não funciona com texto complexo wiki como "palavras mágicas" como __TOC__. Apesar disso, deve funcionar na maioria de categorias para um texto wiki.

Uso editar

{{#invoke:Suppress categories|texto introduzido}}

Exemplos editar

Código Saída
{{#invoke:Suppress categories|main|foo}} foo
{{#invoke:Suppress categories|main|foo[[Categoria:Alguma categoria]]}} foo
{{#invoke:Suppress categories|main|foo[[Categoria:Alguma categoria]]bar[[Categoria:Outra categoria]]}} foobar
{{#invoke:Suppress categories|main|foo{{{some_parameter|[[Categoria:Bar]]}}}}} foo
{{#invoke:Suppress categories|main|foo[[Categoria:Link de ca[]tegoria mal formada]]}} foo[[Categoria:Link de ca[]tegoria mal formada]]
{{#invoke:Suppress categories|main|foo[[:Categoria:Truque dois pontos]]}} fooCategoria:Truque dois pontos
{{#invoke:Suppress categories|main|foo[[Categoria:Piped link|bar]]}} foo
{{#invoke:Suppress categories|main|foo[[Categoria:Piped link|ba[]r]]}} foo
{{#invoke:Suppress categories|main|foo[[link não categoria]]}} foolink não-categoria
{{#invoke:Suppress categories|main|foo[[ Categoria : Alguma categoria com espços ]]}} foo

-- This is a simple module to strip categories from wikitext. It does
-- not support nested links or magic words like __TOC__, etc. Even so,
-- it should still handle most categories.

local p = {}

-- Detects if a category link is valid or not. If it is valid,
-- the function returns the blank string. If not, the input
-- is returned with no changes.
local function processCategory( all, submatch )
    local beforePipe = mw.ustring.match( submatch, '^(.-)[%s_]*|[%s_]*.-$' )
    beforePipe = beforePipe or submatch
    if mw.ustring.match( beforePipe, '[%[%]<>{}%c\n]' ) then
        return all
    else
        return ''
    end
end

-- Preprocess the content if we aren't being called from #invoke,
-- and pass it to gsub to remove valid category links.
local function suppress( content, isPreprocessed )
    if not isPreprocessed then
        content = mw.getCurrentFrame():preprocess( content )
    end
    content = mw.ustring.gsub(
        content,
        '(%[%[[%s_]*[cC][aA][tT][eE][gG][oO][rR][iIyY][aA]?[%s_]*:[%s_]*(.-)[%s_]*%]%])',
        processCategory
    )
    content = mw.ustring.gsub(
        content,
        '(%[%[[%s_]*[cC][aA][tT][eE][gG][oO][rR][iIyY][aA]?[%s_]*:[%s_]*(.-)[%s_]*%]%])',
        processCategory
    )
    return content
end

-- Get the content to suppress categories from, and find
-- whether the content has already been preprocessed. (If the
-- module is called from #invoke, it has been preprocessed already.)
function p.main( frame )
    local content, isPreprocessed
    if frame == mw.getCurrentFrame() then
        content = frame:getParent().args[1]
        if frame.args[1] then
            content = frame.args[1]
        end
        isPreprocessed = true
    else
        content = frame
        isPreprocessed = false
    end
    return suppress( content, isPreprocessed )
end

return p