local module = {}
local getArgs = require('Module:Arguments').getArgs
local getCode = require('Module:GetPageCode')
local function parseArray(arg)
local arr = {}
if type(arg) == 'string' then
local iter = mw.ustring.gmatch(arg .. ',', '([^,]*),')
for v in iter do
table.insert(arr, v)
end
elseif type(arg) == 'table' then
arr = arg
end
return arr
end
function _main(args, frame)
local pages = parseArray(args['pages'] or args[1] or '', ',')
if #pages == 0 or pages[1] == '' then error('必须传入至少一个页面名!') end
local ptns = args['ptns'] or args[2]
local filter = args['filter'] and parseArray(args['filter']) or {}
if ptns == nil then error('必须传入正则表达式!') end
local split = args['split'] or args[3] or ','
if string.find(ptns, '^/.+/$') then
ptns = { (string.gsub(ptns, '^/(.+)/$', '%1')) }
else
ptns = parseArray(ptns)
end
local contents = ''
for i, v in ipairs(pages) do
local content = getCode(v)
if content == nil then
error('页面【'..v..'】或它所指向的页面未创建或无效!')
end
contents = contents..content
end
local result = {}
for i, v in ipairs(ptns) do
mw.ustring.gsub(contents, v, function(...)
local ss = { ... }
for i, v in ipairs(ss) do
result[#result + 1] = v
end
end)
end
if #filter > 0 then
for i, v in ipairs(result) do
for _, filterPair in ipairs(filter) do
local findPattern, replaceWith = unpack(parseArray(filterPair))
result[i] = mw.ustring.gsub(v, findPattern, replaceWith or '')
end
end
end
local resultStr = table.concat(result, split)
split = string.gsub(split, '([%%%(%)%.%+%-%*%?%[%]%^%$])', '%%%1')
return (string.gsub(resultStr, '^(.+)'..split..'$', '%1'))
end
function module.main(frame)
local args = getArgs(frame)
return _main(args, frame)
end
return module