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*embed:?%s*'..(((section ~= '') and section) or '.-')..
'%s*%-%->%s*(.-)%s*<!%-%-%s*embed%-end:?%s*'..(((section ~= '') and section) or '.-')..
'%s*%-%->.-$'
local sample = content
content = mw.ustring.gsub(content, ptn, function(s1, s2)
s1 = string.gsub(s1, '([^\n]*)\n', function(s3)
if string.find(s3, '^(==?=?=?[^\n]-==?=?=?)$') then
return s3..'\n'
else
return ''
end
end)
local frame_child = frame:newChild({ title = (((redirect ~= '') and redirect) or titleName), args = params })
return string.gsub(frame_child:preprocess(s1..'\n@@@sectionEmbed-target@@@sectionEmbed-target@@@\n'..s2),
'^.-\n@@@sectionEmbed%-target@@@sectionEmbed%-target@@@\n(.-)$', '%1')
end)
if sample == content then
if section ~= '' then
errorMsg('嵌入失敗,請確認在頁面【'..titleName..'】'..redirectMsg..'是否存在id為“'..section..'”的embed嵌入標記,並檢查書寫是否正確!')
else
errorMsg('嵌入失敗,請確認在頁面【'..titleName..'】'..redirectMsg..'是否存在embed嵌入標記,並檢查書寫是否正確!')
end
end
return content
end
function module.main(frame)
local args = getArgs(frame)
return module._main(args, frame)
end
return module