Module:Suppress categories

From Nordic Larp Wiki
Revision as of 19:47, 10 October 2017 by en>MusikAnimal (Protected "Module:Suppress categories": High-risk Lua module; 1,000+ transclusions ([Edit=Require autoconfirmed or confirmed access] (indefinite)))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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
        return ''

-- 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 )
    content = mw.ustring.gsub(
    return content

-- 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]
        isPreprocessed = true
        content = frame
        isPreprocessed = false
    return suppress( content, isPreprocessed )

return p