模块:SectionEmbed
来自东方Project线下活动维基
![]() |
本页面原来自萌娘百科页面模块:SectionEmbed,依 CC BY-NC-SA 3.0 授权引入。 经过双方编者的修改,本页面可能已与来源有很大差异。 |
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 = '' 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 local redirectMsg = '' if redirect ~= '' then redirectMsg = '(重定向至'..redirect..')' end local ptn = '^.*==+%s+'..section..'%s+==+(.-%s+)==%s+.+%s+==%s+.+$' local origin = content content = mw.ustring.gsub(content, ptn, '%1') if content == origin then errorMsg('嵌入失败,请确认在页面【'..titleName..'】'..redirectMsg..'是否存在名为“'..section..'”的二级标题,并检查书写是否正确!') end local frame_child = frame:newChild({ title = (((redirect ~= '') and redirect) or titleName), args = params }) return frame_child:preprocess(content) end function module.main(frame) local args = getArgs(frame) return module._main(args, frame) end return module