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

Module:Cite:修订间差异

武外梗百科 爱国好学自强图新的百科全书
无编辑摘要
无编辑摘要
 
(未显示同一用户的5个中间版本)
第2行: 第2行:


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


    if page == "" or section == "" then
     local title = mw.title.new(pageName)
        return ""
     if not title or not title.exists then return "" end
    end
 
    -- 和你给的示例一致:直接用 title + getContent
     local title = mw.title.new(page)
     if not title or not title.exists then
        return ""
    end


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


     -- 转义标题名(防正则炸)
     -- 更安全的 section 提取:用 string.find 避免 gmatch 边缘 bug
     local safeSection = mw.ustring.gsub(
     local safeSection = mw.text.encode(section)  -- 或手动逃逸
        section,
    local pattern = "\n(==+)%s*" .. mw.ustring.gsub(safeSection, "([%-%[%]%.%(%)%*%+%?%^%$])", "%%%1") .. "%s*%1%s*\n"
        "([%^%$%(%)%%%.%[%]%*%+%-%?])",
     local startPos, endPos = content:find(pattern)
        "%%%1"
    )
 
    ----------------------------------------------------------------
    -- 1. 匹配任意级别标题,并捕获 '=' 数量
    ----------------------------------------------------------------
    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*%1%s*\n"
         pattern = "^(==+)%s*" .. mw.ustring.gsub(safeSection, "([%-%[%]%.%(%)%*%+%?%^%$])", "%%%1") .. "%s*%1%s*\n"
         startPos, endPos, eqs =
         startPos, endPos = content:find(pattern)
            mw.ustring.find(content, headerPattern)
     end
     end
    if not endPos then return "" end


     if not endPos or not eqs then
     local level = endPos - startPos + 1  -- 简化 level 计算
        return ""
     local after = content:sub(endPos + 1)
     end


     local level = mw.ustring.len(eqs)
     -- 用 split 代替 gmatch,避免潜在无限
 
     local lines = {}
    ----------------------------------------------------------------
     for line in (after .. "\n"):gmatch("(.-)\n") do
     -- 2. 从标题结束后开始,找下一个“级别 ≤ 当前级别”的标题
        local eqs, name = line:match("^(%=+)%s*(.-)%s*%1%s*$")
     ----------------------------------------------------------------
        if eqs and #eqs <= level then break end
    local after = mw.ustring.sub(content, endPos + 1)
         table.insert(lines, line)
 
    local nextHeaderPattern = "\n={1," .. level .. "}[^=].-\n"
    local nextStart = mw.ustring.find(after, nextHeaderPattern)
 
    local body
    if nextStart then
         body = mw.ustring.sub(after, 1, nextStart - 1)
    else
        body = after
     end
     end


     ----------------------------------------------------------------
     local body = table.concat(lines, "\n")
    -- 3. 维基百科式帽注 + 原样正文
     local hatnote = '<div style="color:#555;font-size:80%;font-style:italic;margin-bottom:0.5em;">&nbsp;&nbsp;&nbsp;&nbsp;主条目:[[' .. pageName .. ']]</div>'
    ----------------------------------------------------------------
     return hatnote .. "\n\n" .. body
     local hatnote =
        '<div class="hatnote">主条目:[[' .. page .. ']]</div>\n\n'
 
     return hatnote .. body
end
end


return p
return p

2026年2月17日 (二) 16:50的最新版本

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

local p = {}

function p.include(frame)
    local pageName = 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 pageName == "" or section == "" then return "" end

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

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

    -- 更安全的 section 提取:用 string.find 避免 gmatch 边缘 bug
    local safeSection = mw.text.encode(section)  -- 或手动逃逸
    local pattern = "\n(==+)%s*" .. mw.ustring.gsub(safeSection, "([%-%[%]%.%(%)%*%+%?%^%$])", "%%%1") .. "%s*%1%s*\n"
    local startPos, endPos = content:find(pattern)
    if not startPos then
        pattern = "^(==+)%s*" .. mw.ustring.gsub(safeSection, "([%-%[%]%.%(%)%*%+%?%^%$])", "%%%1") .. "%s*%1%s*\n"
        startPos, endPos = content:find(pattern)
    end
    if not endPos then return "" end

    local level = endPos - startPos + 1  -- 简化 level 计算
    local after = content:sub(endPos + 1)

    -- 用 split 代替 gmatch,避免潜在无限
    local lines = {}
    for line in (after .. "\n"):gmatch("(.-)\n") do
        local eqs, name = line:match("^(%=+)%s*(.-)%s*%1%s*$")
        if eqs and #eqs <= level then break end
        table.insert(lines, line)
    end

    local body = table.concat(lines, "\n")
    local hatnote = '<div style="color:#555;font-size:80%;font-style:italic;margin-bottom:0.5em;">&nbsp;&nbsp;&nbsp;&nbsp;主条目:[[' .. pageName .. ']]</div>'
    return hatnote .. "\n\n" .. body
end

return p