模組:Namespace/ForEach
外观

概要
由Module:TemplateParameters#getFormatingStringByArgument擴展而來的用法,用來大量印出一樣的命名空間資料,如Wikipedia:编辑提示#建立編輯提示
參數及使用方法
{{#invoke:Namespace/ForEach|main |template=<nowiki> * {{{nsid}}}:{{{ns}}}({{NSPN|2={{{nsid}}}}})</nowiki> |cond=<nowiki>{{#expr:{{{nsid}}}>=0}}</nowiki> |delnowiki=true |delmingw=true }}
- 0:{{{ns}}}(条目)
- 1:{{{ns}}}(讨论)
- 2:{{{ns}}}(用户)
- 3:{{{ns}}}(用户讨论)
- 4:{{{ns}}}(项目)
- 5:{{{ns}}}(项目讨论)
- 6:{{{ns}}}(文件)
- 7:{{{ns}}}(文件讨论)
- 8:{{{ns}}}(界面)
- 9:{{{ns}}}(界面讨论)
- 10:{{{ns}}}(Template)
- 11:{{{ns}}}(模板讨论)
- 12:{{{ns}}}(帮助)
- 13:{{{ns}}}(帮助讨论)
- 14:{{{ns}}}(分类)
- 15:{{{ns}}}(分类讨论)
- 100:{{{ns}}}(主题)
- 101:{{{ns}}}(主题讨论)
- 102:{{{ns}}}(维基专题)
- 103:{{{ns}}}(维基专题讨论)
- 118:{{{ns}}}(Draft)
- 119:{{{ns}}}(草稿讨论)
- 126:{{{ns}}}(MOS)
- 127:{{{ns}}}(MOS talk)
- 710:{{{ns}}}(TimedText)
- 711:{{{ns}}}(字幕讨论)
- 828:{{{ns}}}(模块)
- 829:{{{ns}}}(模块讨论)
- 2600:{{{ns}}}(结构式讨论)
- template支援參數列表:
nsid
:命名空間IDns
:命名空間名稱(相當於{{ns:{{{nsid}}}}}
)
- cond支援參數列表:
nsid
:命名空間ID
local p = {}
local getArgs = require('Module:Arguments').getArgs
local mError = require('Module:Error')
local mTemplateParameters = require('Module:TemplateParameters')
local yesno = require('Module:Yesno')
local lan = require('Module:Namespace')._lan
local data = mw.loadData('Module:Namespace/data')
local function formatStringWrap(str, args, forcePreprocess)
if not mw.ustring.match(str, '{') then
return str
end
local result = mTemplateParameters._getFormatingStringByArgument(str, args)
if mw.ustring.match(result, '{') and forcePreprocess then
result = mw.getCurrentFrame():preprocess(result)
end
return result
end
local function tryInvoke(expr, args, forcePreprocess)
if type(expr) == type(tryInvoke) then
return expr(args, forcePreprocess)
end
return formatStringWrap(tostring(expr), args, forcePreprocess)
end
function p._format(template, cond, reverse)
cond = cond or function () return true end
reverse = reverse or false
local output = ''
local function forEach(nsid)
local name = lan(data.Namespace[tostring(nsid)])
local display = name
if data.Namespace[tostring(nsid) .. '-display'] then
display = lan(data.Namespace[tostring(nsid) .. '-display'])
end
if yesno(tryInvoke(cond, {nsid = nsid}, true)) then
output = output .. tryInvoke(template, {nsid = nsid, name = name, display = display})
end
end
if reverse then
for index = #data.Namespace.list, 1, -1 do
forEach(data.Namespace.list[index])
end
else
for _, nsid in ipairs(data.Namespace.list) do
forEach(nsid)
end
end
return output
end
function p._main(args)
local template = args.template or args.format or args[1] or args['1']
local cond = args.cond or args[2] or args['2'] or 'false'
local delnowiki = yesno(args.delnowiki)
local delmsgnw = yesno(args.delmsgnw)
local reverse = yesno(args.reverse)
if not template or template == '' then
error('沒有提供渲染模板。')
end
if delnowiki then
template = mw.text.unstripNoWiki(template)
cond = mw.text.unstripNoWiki(cond)
end
if delmsgnw then
template = mw.text.decode(template)
cond = mw.text.unstripNoWiki(cond)
end
return mw.getCurrentFrame():preprocess(p._format(template, cond, reverse))
end
function p.main(frame)
local args = getArgs(frame)
local status, result = pcall(p._main, args)
return status and result or mError.error{'[[Module:Namespace/ForEach]]錯誤:' .. result}
end
return p