মডিউল:দমন বিষয়শ্রেণীসমূহ

উইকিপিডিয়া, মুক্ত বিশ্বকোষ থেকে

This is a simple module to strip categories from wikitext. For example, if passed the code "foo[[Category:Some category]]", it will return only "foo".

The module properly handles categories escaped with the colon trick, categories whose names include invalid characters such as ">", and categories that are surrounded with nowiki tags. However, it does not support complex wikitext such as nested links or magic words like __TOC__. Even so, it should still remove the vast majority of categories from any given wikitext.

ব্যবহার[সম্পাদনা]

{{#invoke:Suppress categories|main|input text}}

উদাহরণ[সম্পাদনা]

Code Output
{{#invoke:দমন বিষয়শ্রেণীসমূহ|main|foo}} foo
{{#invoke:দমন বিষয়শ্রেণীসমূহ|main|foo[[বিষয়শ্রেণী:কিছু বিষয়শ্রেণী]]}} foo
{{#invoke:দমন বিষয়শ্রেণীসমূহ|main|foo[[বিষয়শ্রেণী:কিছু বিষয়শ্রেণী]]bar[[বিষয়শ্রেণী:আরেকটি বিষয়শ্রেণী]]}} foobar
{{#invoke:দমন বিষয়শ্রেণীসমূহ|main|foo{{{some_parameter|[[বিষয়শ্রেণী:Bar]]}}}}} foo
{{#invoke:দমন বিষয়শ্রেণীসমূহ|main|foo[[বিষয়শ্রেণী:Bad ca[]tegory link]]}} foo[[বিষয়শ্রেণী:Bad ca[]tegory link]]
{{#invoke:দমন বিষয়শ্রেণীসমূহ|main|foo[[:বিষয়শ্রেণী:Colon trick]]}} fooবিষয়শ্রেণী:Colon trick
{{#invoke:দমন বিষয়শ্রেণীসমূহ|main|foo[[বিষয়শ্রেণী:Piped link|bar]]}} foo
{{#invoke:দমন বিষয়শ্রেণীসমূহ|main|foo[[বিষয়শ্রেণী:Piped link|ba[]r]]}} foo
{{#invoke:দমন বিষয়শ্রেণীসমূহ|main|foo[[non-বিষয়শ্রেণী সংযোগ]]}} foonon-বিষয়শ্রেণী সংযোগ
{{#invoke:দমন বিষয়শ্রেণীসমূহ|main|foo[[ বিষয়শ্রেণী : শূণ্যস্থান সাথে কিছু বিষয়শ্রেণী ]]}} 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][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