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

Module:Cite:修订间差异

武外梗百科 爱国好学自强图新的百科全书
无编辑摘要
无编辑摘要
第1行: 第1行:
local p = {}
local p = {}


function p.main(frame)
function p.include(frame)
     local page = frame.args.page or frame.args[1]
     local page = 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 not page or not section then
     if page == "" or section == "" then
         return "❌ 参数不足(需要 page 和 section)"
         return ""
     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
         return "❌ 条目不存在:" .. page
         return ""
     end
     end


    -- 获取完整 wikitext
     local content = title:getContent()
     local content = title:getContent()
     if not content then
     if not content then
         return "❌ 无法读取条目内容"
         return ""
     end
     end


     -- 转义段落名中的特殊字符
     -- 转义段落名,防止正则炸
     local safeSection = mw.ustring.gsub(
     local safeSection = mw.ustring.gsub(
         section,
         section,
第26行: 第28行:
     )
     )


     -- 匹配 == 段落 ==
     -- 只匹配二级标题:== 段落 ==
     local pattern = "==%s*" .. safeSection .. "%s*=="
     local headerPattern = "\n==%s*" .. safeSection .. "%s*==%s*\n"
     local startPos, endPos = mw.ustring.find(content, pattern)
     local startPos, endPos = mw.ustring.find(content, headerPattern)


    -- 兼容段落在页面开头的情况
     if not startPos then
     if not startPos then
         return "❌ 未找到段落:" .. section
         headerPattern = "^==%s*" .. safeSection .. "%s*==%s*\n"
        startPos, endPos = mw.ustring.find(content, headerPattern)
     end
     end


     -- 从标题结束后开始截取
    if not endPos then
        return ""
    end
 
     -- 从标题结束后开始
     local after = mw.ustring.sub(content, endPos + 1)
     local after = mw.ustring.sub(content, endPos + 1)


第40行: 第48行:
     local nextHeader = mw.ustring.find(after, "\n==[^=]")
     local nextHeader = mw.ustring.find(after, "\n==[^=]")


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


     sectionText = mw.text.trim(sectionText)
     -- 原样返回,不清洗、不 trim
 
     return string.format(
     return string.format(
         "'''原条目:[[%s]]'''\n\n%s",
         "'''原条目:[[%s]]'''\n\n%s",
         page,
         page,
         sectionText
         body
     )
     )
end
end


return p
return p

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

此模块的文档可以在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

    -- 获取完整 wikitext
    local content = title:getContent()
    if not content then
        return ""
    end

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

    -- 只匹配二级标题:== 段落 ==
    local headerPattern = "\n==%s*" .. safeSection .. "%s*==%s*\n"
    local startPos, endPos = mw.ustring.find(content, headerPattern)

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

    if not endPos then
        return ""
    end

    -- 从标题结束后开始
    local after = mw.ustring.sub(content, endPos + 1)

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

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

    -- 原样返回,不清洗、不 trim
    return string.format(
        "'''原条目:[[%s]]'''\n\n%s",
        page,
        body
    )
end

return p