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

Module:PageSummary

武外梗百科 爱国好学自强图新的百科全书
Zelda110留言 | 贡献2025年12月21日 (日) 17:46的版本

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

local p = {}

-- 高效清理函数:通过非贪婪匹配和有限循环移除复杂标签
local function safeStrip(text)
    if not text then return "" end
    
    -- 1. 移除引用和注释(这些最耗性能,优先处理)
    text = string.gsub(text, "<ref.-</ref>", "")
    text = string.gsub(text, "<ref.->", "")
    text = string.gsub(text, "<!%-%-.-%-%->", "")
    
    -- 2. 移除图片标签(核心优化:直接匹配包含 File/文件 等前缀的 [[...]])
    -- 使用非贪婪匹配并在发现嵌套链接时进行有限次迭代,避免死循环
    local imagePrefixes = {"[Ff]ile:", "[Ii]mage:", "文件:", "图像:"}
    for _, prefix in ipairs(imagePrefixes) do
        -- 匹配 [[Prefix: ... ]] 直到遇到对应的闭合括号,不使用递归以节省 CPU
        text = string.gsub(text, "%[%[" .. prefix .. ".-%]%]", "")
    end

    -- 3. 移除模板 {{...}}
    -- 限制最多处理 10 层嵌套,防止 CPU 跑满
    for i = 1, 10 do
        local newText = string.gsub(text, "{{[^{}]+}}", "")
        if newText == text then break end
        text = newText
    end

    -- 4. 移除列表符号和标题符号
    text = string.gsub(text, "\n==+.-==+", " ")
    text = string.gsub(text, "\n%s*[*#:]+", " ")
    text = string.gsub(text, "&[Nn][Bb][Ss][Pp];", " ")
    text = string.gsub(text, "\n", " ")
    text = string.gsub(text, "%s+", " ")

    return mw.text.trim(text)
end

function p.getSummaryAndImage(frame)
    local pageName = frame.args[1] or ""
    local title = mw.title.new(pageName)
    if not title or not title.exists then return "页面不存在" end
    
    -- 只读取前 800 个字符进行处理,减轻解析负担
    local content = title:getContent()
    local rawExcerpt = mw.ustring.sub(content, 1, 800)

    -- 1. 快速提取第一张图
    local firstImage = string.match(rawExcerpt, "%[%[([Ff]ile:.-)[|%]]") or 
                       string.match(rawExcerpt, "%[%[([Ii]mage:.-)[|%]]") or
                       string.match(rawExcerpt, "%[%[(文件:.-)[|%]]")

    -- 2. 安全清理文本
    local cleanText = safeStrip(rawExcerpt)

    -- 3. 截断 150 字
    local targetLen = 150
    local summary = mw.ustring.sub(cleanText, 1, targetLen)
    
    -- 简单的链接补全逻辑,不再使用循环搜索,改用简单的计数
    local _, opens = string.gsub(summary, "%[%[", "")
    local _, closes = string.gsub(summary, "%]%]", "")
    if opens > closes then
        local rest = mw.ustring.sub(cleanText, targetLen + 1, targetLen + 50)
        local endLink = string.find(rest, "]]", 1, true)
        if endLink then
            summary = summary .. string.sub(rest, 1, endLink + 1)
        end
    end
    
    if mw.ustring.len(cleanText) > targetLen then
        summary = summary .. "..."
    end

    -- 4. 渲染 (保持维基原生风格)
    local container = mw.html.create('div'):css({['margin']='15px 0', ['display']='flow-root'})
    if firstImage then
        container:wikitext('[[' .. firstImage .. '|150px|right|link=' .. pageName .. ']]')
    end
    container:tag('div')
        :css({['font-size']='1.4em', ['font-weight']='bold', ['margin-bottom']='8px'})
        :wikitext('[[' .. pageName .. ']]')
    container:tag('div')
        :css({['line-height']='1.6', ['color']='#202122'})
        :wikitext(summary)

    return tostring(container)
end

return p