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

Module:Cite:修订间差异

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


    -- 获取条目内容(沿用你给的方式)
     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
第20行: 第19行:
     end
     end


     -- 转义标题名,防正则问题
     -- 转义标题名
     local safeSection = mw.ustring.gsub(
     local safeSection = mw.ustring.gsub(section, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
        section,
        "([%^%$%(%)%%%.%[%]%*%+%-%?])",
        "%%%1"
    )


     ----------------------------------------------------------------
     -- 找目标标题
    -- 1. 找目标标题,并记录级别
    ----------------------------------------------------------------
     local startPos, endPos, eqs =
     local startPos, endPos, eqs =
         mw.ustring.find(content,
         mw.ustring.find(content, "\n(=+)%s*" .. safeSection .. "%s*%1%s*\n")
            "\n(=+)%s*" .. safeSection .. "%s*%1%s*\n"
        )


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


第49行: 第37行:
     local level = mw.ustring.len(eqs)
     local level = mw.ustring.len(eqs)


     ----------------------------------------------------------------
     -- 从标题结束后开始逐行扫描
    -- 2. 从标题结束后,顺序扫描后续标题
    --    第一个 level' <= level 的标题即为终点
    ----------------------------------------------------------------
     local after = mw.ustring.sub(content, endPos + 1)
     local after = mw.ustring.sub(content, endPos + 1)
 
     local lines = {}
     local cutPos = nil
     for line in mw.ustring.gmatch(after, "([^\n]*)\n?") do
     for pos, marks in mw.ustring.gmatch(after, "()\n(=+)") do
        -- 检查是否是标题行
         local nextLevel = mw.ustring.len(marks)
        local m_eqs, m_name = mw.ustring.match(line, "^(=+)%s*(.-)%s*%1%s*$")
        if nextLevel <= level then
         if m_eqs then
            cutPos = pos
            local nextLevel = mw.ustring.len(m_eqs)
             break
            if nextLevel <= level then
                break -- 遇到同级或更高级标题就停止
             end
         end
         end
        table.insert(lines, line)
     end
     end


     local body
     local body = table.concat(lines, "\n")
    if cutPos then
        body = mw.ustring.sub(after, 1, cutPos - 1)
    else
        body = after
    end


     ----------------------------------------------------------------
     -- 维基帽注样式
    -- 3. 维基百科帽注样式
     local hatnote = '<div class="hatnote">主条目:[[' .. page .. ']]</div>\n\n'
    ----------------------------------------------------------------
     local hatnote =
        '<div class="hatnote">主条目:[[' .. page .. ']]</div>\n\n'


     return hatnote .. body
     return hatnote .. body

2026年2月9日 (一) 22:21的版本

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

local p = {}

function p.include(frame)
    local page = mw.text.trim(frame.args[1] or frame.args.page or "")
    local section = mw.text.trim(frame.args[2] or frame.args.section or "")

    if page == "" or section == "" then
        return ""
    end

    local title = mw.title.new(page)
    if not title or not title.exists then
        return ""
    end

    local content = title:getContent()
    if not content then
        return ""
    end

    -- 转义标题名
    local safeSection = mw.ustring.gsub(section, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")

    -- 找目标标题
    local startPos, endPos, eqs =
        mw.ustring.find(content, "\n(=+)%s*" .. safeSection .. "%s*%1%s*\n")

    if not startPos then
        startPos, endPos, eqs =
            mw.ustring.find(content, "^(=+)%s*" .. safeSection .. "%s*%1%s*\n")
    end

    if not endPos or not eqs then
        return ""
    end

    local level = mw.ustring.len(eqs)

    -- 从标题结束后开始逐行扫描
    local after = mw.ustring.sub(content, endPos + 1)
    local lines = {}
    for line in mw.ustring.gmatch(after, "([^\n]*)\n?") do
        -- 检查是否是标题行
        local m_eqs, m_name = mw.ustring.match(line, "^(=+)%s*(.-)%s*%1%s*$")
        if m_eqs then
            local nextLevel = mw.ustring.len(m_eqs)
            if nextLevel <= level then
                break -- 遇到同级或更高级标题就停止
            end
        end
        table.insert(lines, line)
    end

    local body = table.concat(lines, "\n")

    -- 维基帽注样式
    local hatnote = '<div class="hatnote">主条目:[[' .. page .. ']]</div>\n\n'

    return hatnote .. body
end

return p