打开/关闭菜单
60
73
29
1.1K
武外梗百科
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

Module:Cite:修订间差异

武外梗百科 爱国好学自强图新的百科全书
无编辑摘要
无编辑摘要
第9行: 第9行:
     end
     end


     -- 获取标题对象(和你给的代码一致)
     -- 和你给的示例一致:直接用 title + getContent
     local title = mw.title.new(page)
     local title = mw.title.new(page)
     if not title or not title.exists then
     if not title or not title.exists then
第15行: 第15行:
     end
     end


    -- 获取完整 wikitext
     local content = title:getContent()
     local content = title:getContent()
     if not content then
     if not content then
第21行: 第20行:
     end
     end


     -- 转义段落名,防止正则炸
     -- 转义标题名(防正则炸)
     local safeSection = mw.ustring.gsub(
     local safeSection = mw.ustring.gsub(
         section,
         section,
第28行: 第27行:
     )
     )


     -- 只匹配二级标题:== 段落 ==
     ----------------------------------------------------------------
     local headerPattern = "\n==%s*" .. safeSection .. "%s*==%s*\n"
    -- 1. 匹配任意级别标题,并捕获 '=' 数量
     local startPos, endPos = mw.ustring.find(content, headerPattern)
    ----------------------------------------------------------------
     local headerPattern = "\n(=+)%s*" .. safeSection .. "%s*%1%s*\n"
     local startPos, endPos, eqs =
        mw.ustring.find(content, headerPattern)


     -- 兼容段落在页面开头的情况
     -- 兼容标题在页面最开头
     if not startPos then
     if not startPos then
         headerPattern = "^==%s*" .. safeSection .. "%s*==%s*\n"
         headerPattern = "^(=+)%s*" .. safeSection .. "%s*%1%s*\n"
         startPos, endPos = mw.ustring.find(content, headerPattern)
         startPos, endPos, eqs =
            mw.ustring.find(content, headerPattern)
     end
     end


     if not endPos then
     if not endPos or not eqs then
         return ""
         return ""
     end
     end


     -- 从标题结束后开始
    local level = mw.ustring.len(eqs)
 
     ----------------------------------------------------------------
    -- 2. 从标题结束后开始,找下一个“级别 ≤ 当前级别”的标题
    ----------------------------------------------------------------
     local after = mw.ustring.sub(content, endPos + 1)
     local after = mw.ustring.sub(content, endPos + 1)


     -- 找下一个二级标题
     local nextHeaderPattern = "\n={1," .. level .. "}[^=].-\n"
     local nextHeader = mw.ustring.find(after, "\n==[^=]")
     local nextStart = mw.ustring.find(after, nextHeaderPattern)


     local body
     local body
     if nextHeader then
     if nextStart then
         body = mw.ustring.sub(after, 1, nextHeader - 1)
         body = mw.ustring.sub(after, 1, nextStart - 1)
     else
     else
         body = after
         body = after
     end
     end


     -- 原样返回,不清洗、不 trim
     ----------------------------------------------------------------
     return string.format(
     -- 3. 维基百科式帽注 + 原样正文
         "'''原条目:[[%s]]'''\n\n%s",
    ----------------------------------------------------------------
        page,
    local hatnote =
        body
         '<div class="hatnote">主条目:[[' .. page .. ']]</div>\n\n'
    )
 
    return hatnote .. body
end
end


return p
return p