添加1,134字节
、 2025年4月30日 (三) 21:26
local p = {}
function p.getParentSection(frame)
local title = frame:getTitle()
local content = frame:callParserFunction{
name = '#tag',
args = { 'pre', frame:expandTemplate{ title = ':' .. title } }
}
local sections = {}
for level, section in content:gmatch('(=+)([^=]+)=+') do
table.insert(sections, {
name = section:match('^%s*(.-)%s*$'),
level = #level
})
end
local currentTitle = mw.title.getCurrentTitle()
local currentSection = frame:getParent().args[1] or ""
local currentIndex = 0
for i, sec in ipairs(sections) do
if sec.name == currentSection then
currentIndex = i
break
end
end
if currentIndex == 0 then
return "警告 - 无法确定当前章节!"
end
local parentSection = "警告 - 无上级章节!"
for i = currentIndex - 1, 1, -1 do
if sections[i].level < sections[currentIndex].level then
parentSection = sections[i].name
break
end
end
return parentSection
end
return p