模块:GetParentSec

这里有,偶像们的一切!
鹤訓讨论 | 贡献2025年4月30日 (三) 21:26的版本 (建立内容为“local p = {} function p.getParentSection(frame) local title = frame:getTitle() local content = frame:callParserFunction{ name = '#tag', a…”的新页面)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

此模块的文档可以在模块:GetParentSec/doc创建

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