模組:Loop
外观

給wikitext使用的簡易迴圈,目前只有遞增型和遞減型兩種
使用方法
[编辑]{{#invoke:Loop|increment|...arguments...}}
{{#invoke:Loop|decrement|...arguments...}}
start
:開始循環的數字。end
:結束循環的數字。range
:每次循環增加或減去的數字。content
:循環的內容。delnowiki
、delmsgnw
:如其名。usingConditionalExpressions
:去除循環內的跳脫字元,參見Module:TemplateParameters#FormatingArguments。
- 範例:
{{#invoke:Loop|increment
|start=1|end=10|range=1|delnowiki=1
|content=<nowiki>* 第{{{1}}}次,你好!</nowiki>
}}
- 或
{{#invoke:Loop|increment
|start=1|end=10|range=1|usingConditionalExpressions=1
|content=* 第\{\{\{1\}\}\}次,你好!\n
}}
- 結果:
- 第1次,你好!
- 第2次,你好!
- 第3次,你好!
- 第4次,你好!
- 第5次,你好!
- 第6次,你好!
- 第7次,你好!
- 第8次,你好!
- 第9次,你好!
- 第10次,你好!
其他可用在content的變數:
{{{last}}}
:上一個數字,若不存在則為空值。{{{current}}}
:等同{{{1}}}
。{{{next}}}
:下一個數字,若不存在則為空值。
理論上也可以傳入浮點數,但如果出現了奇怪的行為的話建議使用整數。
local p = {}
local mYesno
local mTemplateParameters
local libraryUtil = require('libraryUtil')
local function yesno(value)
if not mYesno then
mYesno = require('Module:Yesno')
end
return mYesno(value)
end
function p._loop(opt)
libraryUtil.checkTypeForNamedArg('Module:Loop#_loop', 'loopWeight', opt.loopWeight, 'number', true)
libraryUtil.checkTypeForNamedArg('Module:Loop#_loop', 'validateCondition', opt.validateCondition, 'function', true)
libraryUtil.checkTypeForNamedArg('Module:Loop#_loop', 'stopCondition', opt.stopCondition, 'function', false)
return function (frame)
local args = frame.args
local loopStart = tonumber(args['start'])
local loopEnd = tonumber(args['end'])
local loopContent = args.content
if loopStart == loopEnd or not loopContent then
return ''
end
local loopRange = tonumber(args['range']) or 1
if loopRange <= 0 then
error('range must be a positive number.')
end
if (opt.loopWeight or 1) < 0 then
if loopStart < loopEnd then
error('loop cannot end.')
end
else
if loopStart > loopEnd then
error('loop cannot end.')
end
end
loopRange = loopRange * (opt.loopWeight or 1)
if opt.validateCondition then
validateCondition(loopStart, loopEnd, loopRange)
end
if yesno(args.delnowiki) then
loopContent = mw.text.unstripNoWiki(loopContent)
end
if yesno(args.delmsgnw) then
loopContent = mw.text.decode(loopContent)
end
if yesno(args.usingConditionalExpressions) then
if not mTemplateParameters then
mTemplateParameters = require('Module:TemplateParameters')
end
loopContent = mTemplateParameters._get_escape(loopContent)
end
local i = loopStart
local check_stop = opt.stopCondition(loopStart, loopEnd, loopRange)
local numbers = {}
while true do
numbers[#numbers + 1] = i
i = i + loopRange
if check_stop(i) then
break
end
end
local output = ''
for index, number in ipairs(numbers) do
output = output .. loopContent
:gsub('{{{1}}}', tostring(number))
:gsub('{{{last}}}', index > 1 and tostring(numbers[index - 1]) or '')
:gsub('{{{current}}}', tostring(number))
:gsub('{{{next}}}', index < #numbers and tostring(numbers[index + 1]) or '')
end
return mw.getCurrentFrame():preprocess(output)
end
end
p._increment = p._loop({
loopWeight = 1,
stopCondition = function (loopStart, loopEnd, loopRange)
return function (i)
return i > loopEnd
end
end
})
function p.increment(frame)
return p._increment(frame)
end
p._decrement = p._loop({
loopWeight = -1,
stopCondition = function (loopStart, loopEnd, loopRange)
return function (i)
return i < loopEnd
end
end
})
function p.decrement(frame)
return p._decrement(frame)
end
return p