모듈:Suppress categories

큰숲백과, 나무를 보지 말고 큰 숲을 보라.
설명문서 [보기] [편집] [역사] [새로 고침]
이 모듈 문서의 출처는 위키백과:모듈:Suppress categories입니다.
분류를 위키텍스트에서 지워주는 모듈입니다. “문자열[[분류:어떤 분류]]”이라고 입력하면 “문자열”만 반환합니다.

쌍점(:)을 이용한 분류나 > 등 잘못된 문자열을 포함한 분류에서도 작동합니다. 그러나 __TOC__와 같은 특수 명령과 같은 복잡한 위키 문법은 지원하지 않습니다.

사용법

{{#invoke:Suppress categories|main|문자열 입력}}

예시

코드 결과
{{#invoke:Suppress categories|main|foo}} foo
{{#invoke:Suppress categories|main|foo[[분류:어떤 분류]]}} foo
{{#invoke:Suppress categories|main|foo[[분류:어떤 분류]]bar[[분류:또 다른 분류]]}} foobar
{{#invoke:Suppress categories|main|foo{{{인수|[[분류:Bar]]}}}}} foo
{{#invoke:Suppress categories|main|foo[[분류:잘못[]된 분류 링크]]}} foo[[분류:잘못[]된 분류 링크]]
{{#invoke:Suppress categories|main|foo[[:분류:쌍점 이용]]}} foo분류:쌍점 이용
{{#invoke:Suppress categories|main|foo[[분류:파이프|이용]]}} foo
{{#invoke:Suppress categories|main|foo[[분류:파이프|이[]용]]}} foo
{{#invoke:Suppress categories|main|foo[[분류가 아닌 링크]]}} foo분류가 아닌 링크
{{#invoke:Suppress categories|main|foo[[ 분류 : 공백이 있는 분류 ]]}} foo

같이 보기

위 설명은 모듈:Suppress categories/설명문서의 내용을 가져와 보여주고 있습니다.
연습장이나 사용자 문서에서 틀의 사용이나 수정을 연습할 수 있습니다.
분류는 /설명문서에 넣어주세요. 이 틀의 하위문서.

-- 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.
-- 태국어(th) 위키백과에서 가져옴

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
    categoryList = {'[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]', '분류'}
    for _, category in ipairs(categoryList) do
    	content = mw.ustring.gsub(
        	content,
        	'(%[%[[%s_]*' .. category .. '[%s_]*:[%s_]*(.-)[%s_]*%]%])',
        	processCategory
    	)
    end
    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