无编辑摘要
imported>850710247liu (创建页面,内容为“local module = {} local getArgs = require('Module:Arguments').getArgs function pack_inner_loop(loop) -- 给内层循环的do参数包裹<nowiki>标签 loop…”) |
imported>=海豚= 无编辑摘要 |
||
| 第2行: | 第2行: | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
local nowiki_blocks = { 'do', 'condition' } | |||
function pack_inner_loop(loop) | function pack_inner_loop(loop) | ||
-- 给内层循环的do参数包裹<nowiki>标签 | -- 给内层循环的do参数包裹<nowiki>标签 | ||
loop = mw.ustring.gsub(loop, '{{%s-[ | loop = mw.ustring.gsub(loop, '{{%s-[Ll]oop%s-|', '{{#invoke:loop|loop|') | ||
loop = mw.ustring.gsub(loop, '{{%s-[Ww]hile%s-|', '{{#invoke:loop|while|') | |||
loop = mw.ustring.gsub(loop, '{{%s-[Dd]o[Ww]hile%s-|', '{{#invoke:loop|dowhile|') | |||
return mw.ustring.gsub(loop, '({{#invoke:loop.*)', function(a) | return mw.ustring.gsub(loop, '({{#invoke:loop.*)', function(a) | ||
local i = 1 | local i = 1 | ||
local stack = {} | local stack = {} | ||
local | local block_begin = -1 | ||
local | local block_end = -1 | ||
while (i < mw.ustring.len(a)) do | while (i < mw.ustring.len(a)) do | ||
for _, block in ipairs(nowiki_blocks) do | |||
if block_begin == -1 and mw.ustring.find(a, '^|%s-'..block..'%s-=', i) then | |||
_, block_begin = mw.ustring.find(a, '^|%s-'..block..'%s-=', i) | |||
block_begin = block_begin + 1 | |||
i = block_begin | |||
if | |||
break | |||
end | |||
end | |||
if block_begin ~= -1 then | |||
if mw.ustring.sub(a, i, i + 2) == '{{{' then | if mw.ustring.sub(a, i, i + 2) == '{{{' then | ||
i = i + 3 | i = i + 3 | ||
| 第32行: | 第40行: | ||
table.remove(stack) | table.remove(stack) | ||
else | else | ||
block_end = i - 1 | |||
break | break | ||
end | end | ||
| 第40行: | 第48行: | ||
table.remove(stack) | table.remove(stack) | ||
else | else | ||
block_end = i - 1 | |||
break | break | ||
end | end | ||
elseif mw.ustring.sub(a, i, i) == '|' and #stack == 0 then | elseif mw.ustring.sub(a, i, i) == '|' and #stack == 0 then | ||
block_end = i - 1 | |||
break | break | ||
else | else | ||
| 第53行: | 第61行: | ||
end | end | ||
end | end | ||
if | if block_begin ~= -1 and block_end == -1 then block_end = i - 1 end | ||
if | if block_begin ~= -1 and block_end ~= -1 then | ||
-- mw.log('发现内层循环 <nowiki>'..mw.ustring.sub(a, | -- mw.log('发现内层循环 <nowiki>'..mw.ustring.sub(a, block_begin, block_end)..'</nowiki>') | ||
return mw.ustring.sub(a, 1, | return mw.ustring.sub(a, 1, block_begin - 1)..'<nowiki>'.. | ||
mw.text.trim(mw.ustring.sub(a, | mw.text.trim(mw.ustring.sub(a, block_begin, block_end))..'</nowiki>'.. | ||
pack_inner_loop(mw.ustring.sub(a, | pack_inner_loop(mw.ustring.sub(a, block_end + 1, -1)) | ||
else | else | ||
return a | return a | ||
| 第65行: | 第73行: | ||
end | end | ||
function | function _loop(args, frame) | ||
local name = '' | local name = '' | ||
local _min = 1 | local _min = 1 | ||
| 第104行: | 第112行: | ||
end | end | ||
function module.loop(frame) | |||
if (frame:getParent() or frame):getTitle() == 'Template:Loop' and | |||
frame.args['do'] == nil then | |||
args = getArgs(frame) | |||
else | |||
args = frame.args | |||
end | |||
return _loop(args, frame:getParent() or frame) | |||
end | |||
-- 向后兼容 | |||
function module.main(frame) | function module.main(frame) | ||
if (frame:getParent() or frame):getTitle() == 'Template: | return module.loop(frame) | ||
end | |||
-- 别名 | |||
module["for"] = function(frame) | |||
return module.loop(frame) | |||
end | |||
local _while = function(args, frame, dowhile) | |||
--初步处理条件语句 | |||
local condition = args['condition'] or '' | |||
condition = mw.text.unstripNoWiki(condition) | |||
condition = string.gsub(condition, '<!%-%-.-%-%->', '') | |||
--修整条件语句的格式 | |||
local trim = mw.text.split(condition, '\n') | |||
condition = '' | |||
for i, v in ipairs(trim) do | |||
condition = condition..mw.text.trim(v) | |||
end | |||
condition = mw.text.decode(condition) | |||
-- mw.log('循环体= '..condition) | |||
--初步处理循环体 | |||
local loop = args['do'] or '' | |||
loop = mw.text.unstripNoWiki(loop) | |||
loop = string.gsub(loop, '<!%-%-.-%-%->', '') | |||
--处理循环体中的次级循环语句 | |||
loop = pack_inner_loop(loop) | |||
--修整循环体中语句的格式 | |||
local trim = mw.text.split(loop, '\n') | |||
loop = '' | |||
for i, v in ipairs(trim) do | |||
loop = loop..mw.text.trim(v) | |||
end | |||
loop = mw.text.decode(loop) | |||
-- mw.log('循环体= '..loop) | |||
local result = '' | |||
if dowhile then | |||
-- mw.log('执行循环体('..name..'='..tostring(i)..') '..loop) | |||
repeat result = result..frame:preprocess(loop) until frame:preprocess(condition) == '' | |||
else | |||
-- mw.log('执行循环体('..name..'='..tostring(i)..') '..loop) | |||
while frame:preprocess(condition) ~= '' do result = result..frame:preprocess(loop) end | |||
end | |||
-- mw.log('循环结果 '..result) | |||
return result | |||
end | |||
module["while"] = function(frame) | |||
if (frame:getParent() or frame):getTitle() == 'Template:While' and | |||
frame.args['do'] == nil then | |||
args = getArgs(frame) | |||
else | |||
args = frame.args | |||
end | |||
return _while(args, frame:getParent() or frame, false) | |||
end | |||
module.dowhile = function(frame) | |||
if (frame:getParent() or frame):getTitle() == 'Template:While' and | |||
frame.args['do'] == nil then | frame.args['do'] == nil then | ||
args = getArgs(frame) | args = getArgs(frame) | ||
| 第111行: | 第194行: | ||
args = frame.args | args = frame.args | ||
end | end | ||
return | return _while(args, frame:getParent() or frame, true) | ||
end | end | ||
return module | return module | ||