模块:SectionEmbed
此模块的文档可以在模块:SectionEmbed/doc创建
local module = {}
local getArgs = require('Module:Arguments').getArgs
function errorMsg(text)
error(text..'[[分类:含有错误嵌入片段的页面]]')
end
function module._main(args, frame)
local titleName = args['page'] or ''
if titleName == '' then errorMsg('请传入要载入内容所在的页面!') end
local section = args['id'] or ''
section = string.gsub(section, '([%%%(%)%.%+%-%*%?%[%]%^%$])', '%%%1')
local params = args
params['page'] = nil
params['id'] = nil
local title = mw.title.new(titleName)
local redirect = false
local content = title:getContent()
if content == nil then errorMsg('页面名无效,请确认传入的页面名是否正确!') end
if string.find(content, '^#REDIRECT%s+%[%[.+%]%]%s*$') or string.find(content, '^#重定向%s+%[%[.+%]%]%s*$') then
redirect = string.gsub(content, '.+%[%[(.+)%]%]%s*$', '%1')
title = mw.title.new(redirect)
content = title:getContent()
if content == nil then errorMsg('传入的页面【'..titleName..'】为一个重定向页,并且它指向的页面【'..redirect..'】无效!') end
end
content = frame:expandTemplate{ title = 'regex', args = {
'replace', content, '^(={1,4}.+?={1,4})$', mdf = 'm',
'@@sectionEmbed-target:$1'
} }
local getSectionRE = '@@sectionEmbed%-target:(==?=?=?)([^\n]-)(==?=?=?)\n'
local sections = {}
string.gsub(content, getSectionRE, function(s1, s2, s3)
sections[#sections + 1] = mw.text.trim(s2)
end)
local redirectMsg = ''
if redirect ~= false then redirectMsg = '(重定向至'..redirect..')' end
local sample = content
if section ~= '' then
content = mw.ustring.gsub(content, '^.-<!%-%-%s*embed:%s*'..section..'%s*%-%->%s*(.-)%s*<!%-%-%s*embed%-end:%s*'..section..'%s*%-%->.-$', '%1')
if sample == content then
errorMsg('嵌入失败,请确认在页面【'..titleName..'】'..redirectMsg..'是否存在id为“'..section..'”的embed嵌入标记,并检查书写是否正确!')
end
else
content = mw.ustring.gsub(content, '^.-<!%-%-%s*embed.-%-%->%s*(.-)%s*<!%-%-%s*embed%-end.-%-%->.-$', '%1')
if sample == content then
errorMsg('嵌入失败,请确认在页面【'..titleName..'】'..redirectMsg..'是否存在embed嵌入标记,并检查书写是否正确!')
end
end
content = string.gsub(content, getSectionRE, function(s1, s2, s3)
s2 = mw.text.trim(s2)
local sectionNum = -1
for i, v in ipairs(sections) do
if s2 == v then
sectionNum = i
break
end
end
local editBtn = '<span class="sectionEmbed-editBtn plainlinks" title="编辑页面【'..(redirect or titleName)..'】的小节:'..s2..'"><span>[</span>[{{fullurl:'..(redirect or titleName)..'|action=edit§ion='..sectionNum..'}} 编辑]<span>]</span></span>'
return '<div class="sectionEmbed-correct">\n'..s1..s2..editBtn..s3..'\n</div>'
end)
function parse()
if string.find(content, '%b{}') then
content = string.gsub(content, '%b{}', function(s)
if string.find(s, '^{{{') == nil then
return string.gsub(s, '^{', 'ŸŸ'):gsub('}$', '‡‡')
end
local ptn = '{{{%s*([^|]+)%s*|?%s*(.*)%s*}}}'
local param = string.gsub(s, ptn, '%1')
for k, v in pairs(params) do
if mw.text.trim(param) == tostring(k) then return v end
end
return string.gsub(s, ptn, '%2')
end)
else return end
parse()
end
parse()
content = string.gsub(content, 'ŸŸ', '{'):gsub('‡‡', '}')
return frame:preprocess(content)
end
function module.main(frame)
local args = getArgs(frame)
return module._main(args, frame)
end
return module