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

Module:Cite

武外梗百科 爱国好学自强图新的百科全书
Zelda110留言 | 贡献2026年2月9日 (一) 21:23的版本

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

local p = {}

function p.main(frame)
    local page = frame.args.page or frame.args[1]
    local section = frame.args.section or frame.args[2]

    if not page or not section then
        return "❌ 参数不足(需要 page 和 section)"
    end

    local title = mw.title.new(page)
    if not title or not title.exists then
        return "❌ 条目不存在:" .. page
    end

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

    -- 转义段落名中的特殊字符
    local safeSection = mw.ustring.gsub(
        section,
        "([%^%$%(%)%%%.%[%]%*%+%-%?])",
        "%%%1"
    )

    -- 匹配 == 段落 ==
    local pattern = "==%s*" .. safeSection .. "%s*=="
    local startPos, endPos = mw.ustring.find(content, pattern)

    if not startPos then
        return "❌ 未找到段落:" .. section
    end

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

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

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

    sectionText = mw.text.trim(sectionText)

    return string.format(
        "'''原条目:[[%s]]'''\n\n%s",
        page,
        sectionText
    )
end

return p