Zeroclanzhang(讨论 | 贡献)2024年1月16日 (二) 18:56的版本 (创建页面,内容为“-- 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_]*…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing.
This Lua module is used on many pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them.
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.
-- 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.localp={}-- 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.localfunctionprocessCategory(all,submatch)localbeforePipe=mw.ustring.match(submatch,'^(.-)[%s_]*|[%s_]*.-$')beforePipe=beforePipeorsubmatchifmw.ustring.match(beforePipe,'[%[%]<>{}%c\n]')thenreturnallelsereturn''endend-- Preprocess the content if we aren't being called from #invoke,-- and pass it to gsub to remove valid category links.localfunctionsuppress(content,isPreprocessed)ifnotisPreprocessedthencontent=mw.getCurrentFrame():preprocess(content)endcontent=mw.ustring.gsub(content,'(%[%[[%s_]*[cC][aA][tT][eE][gG][oO][rR][yY][%s_]*:[%s_]*(.-)[%s_]*%]%])',processCategory)returncontentend-- 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.)functionp.main(frame)localcontent,isPreprocessedifframe==mw.getCurrentFrame()thencontent=frame:getParent().args[1]ifframe.args[1]thencontent=frame.args[1]endisPreprocessed=trueelsecontent=frameisPreprocessed=falseendreturnsuppress(content,isPreprocessed)endreturnp