模組:Needsubst
外观
local p = {}
local yesno = require('Module:Yesno')
local messageBox = require('Module:Message box')
local cachedAllowlist = nil
local validPrefixes = {
['template'] = true,
['t'] = true,
['模板'] = true
}
local function getArgs(frame)
if frame == mw.getCurrentFrame() then
return require('Module:Arguments').getArgs(frame, { parentFirst = true })
else
return frame or {}
end
end
local function isAuto(templateTitleText)
if not templateTitleText or templateTitleText == '' then
return false
end
if cachedAllowlist == nil then
local listTitle = mw.title.new('User:Cewbot/log/20260422/subst allowlist')
if listTitle and listTitle.exists then
cachedAllowlist = listTitle:getContent() or ''
cachedAllowlist = cachedAllowlist:gsub('<!%-%-.-%-%->', '')
else
cachedAllowlist = ''
end
end
if cachedAllowlist == '' then
return false
end
local cleanName = mw.text.trim(templateTitleText)
cleanName = cleanName:gsub('^[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee]:', '')
local escapedName = mw.ustring.gsub(cleanName, "([%(%)%.%%%+%-%*%?%[%]%^%$])", "%%%1")
local patternName = mw.ustring.gsub(escapedName, "[ _]", "[ _]")
local pattern = '%[%[%s*([^:]-)%s*:%s*' .. patternName .. '%s*[%]|]'
for prefix in mw.ustring.gmatch(cachedAllowlist, pattern) do
local pr = mw.ustring.lower(mw.text.trim(prefix))
if validPrefixes[pr] then
return true
end
end
return false
end
function p.main(frame)
local args = getArgs(frame)
local title = mw.title.getCurrentTitle()
local isCategory = (title.namespace == 14)
local isTemplate = (title.namespace == 10)
local isBasePage = (title.baseText == title.text)
local subpage = title.subpageText
-- 1. 图片处理
local imageFile = args.image
if not imageFile or imageFile == '' then
imageFile = 'Information icon.svg'
end
local image = string.format('[[File:%s|40px|alt=|link=]]', imageFile)
-- 2. 核心文本构建
local textParts = {}
local subjectType = isCategory and '分類的頁面' or '模板'
-- 标语部分
table.insert(textParts, string.format("'''本%s<span style=\"color:var(--color-error,#bf3c2c);\">必须</span>[[Help:替換引用|替換引用]]'''", subjectType))
-- 使用示例(非 Category 命名空间才显示)
if not isCategory then
local usageContent
local fullBasePage = (title.nsText ~= '' and (title.nsText .. ':') or '') .. title.baseText
if fullBasePage == 'Template:Needsubst' then
usageContent = '{{[[Help:替換引用|subst]]:\'\'\'\'\'Template name\'\'\'\'\'}}'
elseif args.actualtemplate and args.actualtemplate ~= '' then
usageContent = args.actualtemplate
else
local targetName = isTemplate and title.text or title.prefixedText
usageContent = frame:expandTemplate{ title = 'Tlxs', args = { targetName } }
end
table.insert(textParts, string.format('(使用%s)', usageContent))
end
table.insert(textParts, '。')
-- 自动替换与机器人说明
local fullPageName = (title.nsText ~= '' and (title.nsText .. ':') or '') .. title.text
if isAuto(fullPageName) then
table.insert(textParts, string.format('本%s不應有包含引用,如有包含引用將由[[WP:机器人|机器人]]執行替換引用。', subjectType))
if not yesno(args.nocat) then
if subpage ~= 'doc' and subpage ~= 'sandbox' then
table.insert(textParts, '[[Category:待自動替換的維基百科模板]]')
end
end
if args[1] and args[1] ~= '' then
table.insert(textParts, args[1])
end
end
-- 替代模板说明
if args.alt and args.alt ~= '' then
local tlAlt = frame:expandTemplate{ title = 'Tl', args = { args.alt } }
table.insert(textParts, string.format('若要包含引用,請改用 %s 模板。', tlAlt))
end
-- 3. 组装 Ombox
local box = messageBox.ombox({
type = 'notice',
image = image,
text = table.concat(textParts)
})
-- 4. 底部分类
local category = ''
if isBasePage and not yesno(args.nocat) then
category = '[[Category:应被替换引用的模板]]'
end
return box .. category
end
return p