Module:Cite
武外梗百科 爱国好学自强图新的百科全书
更多操作
此模块的文档可以在Module:Cite/doc创建
local p = {}
function p.main(frame)
local page = frame.args.page or frame.args[1]
local section = frame.args.section or frame.args[2]
if not page or not section then
return "❌ 参数不足(需要 page 和 section)"
end
local title = mw.title.new(page)
if not title or not title.exists then
return "❌ 条目不存在:" .. page
end
local content = title:getContent()
if not content then
return "❌ 无法读取条目内容"
end
-- 转义段落名中的特殊字符
local safeSection = mw.ustring.gsub(
section,
"([%^%$%(%)%%%.%[%]%*%+%-%?])",
"%%%1"
)
-- 匹配 == 段落 ==
local pattern = "==%s*" .. safeSection .. "%s*=="
local startPos, endPos = mw.ustring.find(content, pattern)
if not startPos then
return "❌ 未找到段落:" .. section
end
-- 从标题结束后开始截取
local after = mw.ustring.sub(content, endPos + 1)
-- 找下一个二级标题
local nextHeader = mw.ustring.find(after, "\n==[^=]")
local sectionText
if nextHeader then
sectionText = mw.ustring.sub(after, 1, nextHeader - 1)
else
sectionText = after
end
sectionText = mw.text.trim(sectionText)
return string.format(
"'''原条目:[[%s]]'''\n\n%s",
page,
sectionText
)
end
return p