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

Module:PageSummary:修订间差异

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


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


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


    -- 4. 移除列表符号和标题符号
     text = string.gsub(text, "\n==+.-==+", " ")
     text = string.gsub(text, "\n==+.-==+", " ")
     text = string.gsub(text, "\n%s*[*#:]+", " ")
     text = string.gsub(text, "\n%s*[*#:]+", " ")
第32行: 第23行:
     text = string.gsub(text, "\n", " ")
     text = string.gsub(text, "\n", " ")
     text = string.gsub(text, "%s+", " ")
     text = string.gsub(text, "%s+", " ")
     return mw.text.trim(text)
     return mw.text.trim(text)
end
end
第41行: 第31行:
     if not title or not title.exists then return "页面不存在" end
     if not title or not title.exists then return "页面不存在" end
      
      
    -- 只读取前 800 个字符进行处理,减轻解析负担
     local content = title:getContent()
     local content = title:getContent()
     local rawExcerpt = mw.ustring.sub(content, 1, 800)
     local rawExcerpt = mw.ustring.sub(content, 1, 500)


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


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


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


    -- 4. 渲染 (保持维基原生风格)
     local container = mw.html.create('div'):css({['margin']='15px 0', ['display']='flow-root'})
     local container = mw.html.create('div'):css({['margin']='15px 0', ['display']='flow-root'})
     if firstImage then
     if firstImage then