Module:CollectCodeData
跳转到导航
跳转到搜索
local module = {}
local getArgs = require('Module:Arguments').getArgs
local getCode = require('Module:GetPageCode')
local array = require('Module:Var-array')
function _main(args, frame)
local pages = mw.text.split(args['pages'] or args[1] or '', ',')
if #pages == 1 and pages[1] == '' then error('必须传入至少一个页面名!') end
local ptns = args['ptns'] or args[2]
local filter = array.get(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 = array.get(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 type(filter[1]) == 'string' then
for i, v in ipairs(result) do
result[i] = mw.ustring.gsub(v, filter[1], filter[2] or '')
end
else
for i, ptn in ipairs(filter) do
for ind, val in ipairs(result) do
result[ind] = mw.ustring.gsub(val, ptn[1], ptn[2] or '')
end
end
end
local resultStr = ''
for i, v in ipairs(result) do
resultStr = resultStr..v..split
end
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