模組:Special wikitext/Custom Module
外观
本模块主要用於require('Module:Module wikitext')._addText
(Module:Module wikitext)中,用於客製化模組命名空間。
使用方法為複製以下代碼到要客製化的模組中:
防止模板循環
如果上述設置仍無法防止模板循環,即模組或調用模組的頁面出現以下錯誤:
- 檢查到模板循環:Module:Special wikitext/Custom Module
- 檢查到模板循環:Module:Documentation
- 页面超出展开深度限制
- 警告:包含模板大小过大。 一些模板将不会包含。
可考慮改用以下代碼:
參見
local p = {}
function p.main()
local wikitext = mw.title.new("Template:Special wikitext/Custom Module"):getContent()
--關於noinclude、includeonly和onlyinclude的regexp
local empty_string = ""
local re_noinclude_head = "<%s*[Nn][Oo][Ii][Nn][Cc][Ll][Uu][Dd][Ee]%s*>"
local re_noinclude_tail = "<%s*/%s*[Nn][Oo][Ii][Nn][Cc][Ll][Uu][Dd][Ee]%s*>"
local re_noinclude = "<%s*/?%s*[Nn][Oo][Ii][Nn][Cc][Ll][Uu][Dd][Ee]%s*/?%s*>"
local re_includeonly = "<%s*/?%s*[Ii][Nn][Cc][Ll][Uu][Dd][Ee][Oo][Nn][Ll][Yy]%s*/?%s*>"
local re_onlyinclude = "<%s*/?%s*[Oo][Nn][Ll][Yy][Ii][Nn][Cc][Ll][Uu][Dd][Ee]%s*/?%s*>"
local re_removes = {{"noinclude",re_noinclude}, {"includeonly",re_includeonly}, {"onlyinclude",re_onlyinclude}}
--摘除模板樣板中的noinclude與當中的內容
local noinclude = mw.ustring.find(wikitext, re_noinclude_head)
local _i = 1
while noinclude do --如果找到<noinclude>
--尋找</noinclude>
local _, noinclude_end = mw.ustring.find(wikitext, re_noinclude_tail)
--如無</noinclude>,視為noinclude到頁面結尾
noinclude_end = noinclude_end or -1
--去除掉<noinclude>...</noinclude>與其之間的內容
wikitext = mw.ustring.sub(wikitext, 1, noinclude-1) .. ((noinclude_end < 0) and empty_string or mw.ustring.sub(wikitext, noinclude_end + 1, -1))
--尋找下一個<noinclude>
noinclude = mw.ustring.find(wikitext, re_noinclude_head)
_i = _i + 1 --避免無窮迴圈
if _i > 100 then mw.addWarning(module_warn..'<noinclude>過多') break end
end --<noinclude></noinclude>內容移除完畢
local frame = mw.getCurrentFrame()
return frame:preprocess(wikitext)
end
return p