Module:Suppress categories: Difference between revisions

From Zoophilia Wiki
Jump to navigationJump to search
meta>Mr. Stradivarius
(create)
 
m (4 revisions imported)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
-- This is a simple module to strip categories from wikitext. It does
-- This is a simple module to strip categories from wikitext. It does
-- not support nested links, handling of html entities and percent-
-- not support nested links or magic words like __TOC__, etc. Even so,
-- encoded text, magic words like __TOC__, etc. Even so, it should
-- it should still handle most categories.
-- still handle most categories.


local p = {}
local p = {}
Line 10: Line 9:
-- is returned with no changes.
-- is returned with no changes.
local function processCategory( all, submatch )
local function processCategory( all, submatch )
     local beforePipe, afterPipe = mw.ustring.match( submatch, '^(.-)[%s_]*|[%s_]*(.-)$' )
     local beforePipe = mw.ustring.match( submatch, '^(.-)[%s_]*|[%s_]*.-$' )
     beforePipe = beforePipe or submatch
     beforePipe = beforePipe or submatch
     if mw.ustring.match( beforePipe, '[%[%]<>{}%c\n]' ) then
     if mw.ustring.match( beforePipe, '[%[%]<>{}%c\n]' ) then
Line 40: Line 39:
     if frame == mw.getCurrentFrame() then
     if frame == mw.getCurrentFrame() then
         content = frame:getParent().args[1]
         content = frame:getParent().args[1]
        isPreprocessed = true
         if frame.args[1] then
         if frame.args[1] then
             content = frame.args[1]
             content = frame.args[1]
         end
         end
        isPreprocessed = true
     else
     else
         content = frame
         content = frame

Latest revision as of 01:09, 3 September 2020

Documentation for this module may be created at Module:Suppress categories/doc

-- 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][yY][%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