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

Module:Cite:修订间差异

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


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


    if not page or not section then
     local title = mw.title.new(pageName)
        return "❌ 参数不足(需要 page 和 section)"
     if not title or not title.exists then return "" end
    end
 
     local title = mw.title.new(page)
     if not title or not title.exists then
        return "❌ 条目不存在:" .. page
    end


     local content = title:getContent()
     local content = title:getContent()
     if not content then
     if not content then return "" end
        return "❌ 无法读取条目内容"
    end
 
    -- 转义段落名中的特殊字符
    local safeSection = mw.ustring.gsub(
        section,
        "([%^%$%(%)%%%.%[%]%*%+%-%?])",
        "%%%1"
    )
 
    -- 匹配 == 段落 ==
    local pattern = "==%s*" .. safeSection .. "%s*=="
    local startPos, endPos = mw.ustring.find(content, pattern)


    -- 更安全的 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
     if not startPos then
         return "❌ 未找到段落:" .. section
         pattern = "^(==+)%s*" .. mw.ustring.gsub(safeSection, "([%-%[%]%.%(%)%*%+%?%^%$])", "%%%1") .. "%s*%1%s*\n"
        startPos, endPos = content:find(pattern)
     end
     end
    if not endPos then return "" end


     -- 从标题结束后开始截取
     local level = endPos - startPos + 1  -- 简化 level 计算
     local after = mw.ustring.sub(content, endPos + 1)
     local after = content:sub(endPos + 1)
 
    -- 找下一个二级标题
    local nextHeader = mw.ustring.find(after, "\n==[^=]")


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


     sectionText = mw.text.trim(sectionText)
     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 string.format(
    return hatnote .. "\n\n" .. body
        "'''原条目:[[%s]]'''\n\n%s",
        page,
        sectionText
    )
end
end


return p
return p