模块: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