“模块:GetParentSec”的版本间的差异 - 知沅教育出版社偶梦课程教研所

“模块:GetParentSec”的版本间的差异

 
(未显示同一用户的4个中间版本)
第2行: 第2行:
  
 
function p.getParentSection(frame)
 
function p.getParentSection(frame)
    -- 获取当前页面 标题
+
   -- 获取调用位置的行号
 +
   local callLine = frame:getParent():getTitle()
 +
  
 +
    -- 获取当前页面 内容
 
    local currentTitle = mw.title.getCurrentTitle()
 
    local currentTitle = mw.title.getCurrentTitle()
 +
   local content = currentTitle:getContent() or ""
 
    
 
    
    --  获取页面内容
+
    --  解析章节结构
    local content = currentTitle:getContent()
+
    local parentStack = {}
    if not content then return "警告 - 无法获取页面内容!" end
+
    local lineNumber = 0
 
    
 
    
    --  解析 章节 结构
+
    for line in content:gmatch("[^\r\n]+") do
   local sections = {}
+
     lineNumber = lineNumber + 1
   for level, section in content:gmatch('(=+)([^=]+)=+') do
+
    
      table.insert(sections, {
+
     --  匹配 章节 标题
       name = mw.text.trim(section),  -- 去除前后空白
+
     local level, title = line:match("^(=+)([^=]+)=+$")
 +
      if level then
 
        level = #level
 
        level = #level
     })
+
       title = mw.text.trim(title)
   end
+
      
  
+
       --  维护父 章节
   --  获取 章节 名称
+
       while #parentStack > 0 and parentStack[#parentStack].level >= level do
   local currentSection = frame:getParent().args[1] or ""
+
         table.remove(parentStack)
  
+
       end
   -- 查找章节位置
+
       table.insert(parentStack, {level = level, title = title, line = lineNumber})
   local currentIndex = 0
+
        
   for i, sec in ipairs(sections) do
+
        --  检查是否是当前调用行
     if sec.name == currentSection then
+
       if tostring(lineNumber) == callLine then
        currentIndex = i
+
         if #parentStack > 1 then  -有父 章节
        break
+
           return parentStack[#parentStack-1].title
     end
+
         else
   end
+
           return ""  --  没有父 章节
  
+
         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
 
    end
 
    end
 
    
 
    
    return parentSection
+
    return ""
 
end
 
end
  
 
return p
 
return p

2025年4月30日 (三) 21:46的最新版本

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

local p = {}

function p.getParentSection(frame)
    -- 获取调用位置的行号
    local callLine = frame:getParent():getTitle() 
    
    -- 获取当前页面内容
    local currentTitle = mw.title.getCurrentTitle()
    local content = currentTitle:getContent() or ""
    
    -- 解析章节结构
    local parentStack = {}
    local lineNumber = 0
    
    for line in content:gmatch("[^\r\n]+") do
        lineNumber = lineNumber + 1
        
        -- 匹配章节标题
        local level, title = line:match("^(=+)([^=]+)=+$")
        if level then
            level = #level
            title = mw.text.trim(title)
            
            -- 维护父章节栈
            while #parentStack > 0 and parentStack[#parentStack].level >= level do
                table.remove(parentStack)
            end
            table.insert(parentStack, {level = level, title = title, line = lineNumber})
            
            -- 检查是否是当前调用行
            if tostring(lineNumber) == callLine then
                if #parentStack > 1 then  -- 有父章节
                    return parentStack[#parentStack-1].title
                else
                    return ""  -- 没有父章节
                end
            end
        end
    end
    
    return ""
end

return p