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

Module:PageSummary:修订间差异

武外梗百科 爱国好学自强图新的百科全书
无编辑摘要
标签已被回退
无编辑摘要
标签手工回退 已被回退
第1行: 第1行:
local p = {}
local p = {}


-- 核心逻辑:使用简单的索引查找替代昂贵的正则表达式
-- 辅助函数:安全清理标签
local function safeStrip(text)
    if not text then return "" end
    text = string.gsub(text, "<ref.-</ref>", "")
    text = string.gsub(text, "<ref.->", "")
    text = string.gsub(text, "<!%-%-.-%-%->", "")
   
    -- 彻底剥离带长描述的图片链接(处理嵌套 [[ ]])
    local prefixes = {"[[File:", "[[Image:", "[[文件:", "[[图像:", "[[file:", "[[image:"}
    for _, pre in ipairs(prefixes) do
        while string.find(text, pre, 1, true) do
            local s = string.find(text, pre, 1, true)
            local count, e = 0, nil
            for j = s, mw.ustring.len(text) do
                local c2 = mw.ustring.sub(text, j, j+1)
                if c2 == "[[" then count = count + 1
                elseif c2 == "]]" then
                    count = count - 1
                    if count == 0 then e = j + 1 break end
                end
            end
            if e then text = mw.ustring.sub(text, 1, s - 1) .. mw.ustring.sub(text, e + 1)
            else text = mw.ustring.sub(text, 1, s - 1) .. mw.ustring.sub(text, s + 2) end
        end
    end
 
    -- 移除模板(限制5层嵌套,防止CPU跑满)
    for i = 1, 5 do
        local nt = string.gsub(text, "{{[^{}]+}}", "")
        if nt == text then break end
        text = nt
    end
 
    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)
function p.getSummaryAndImage(frame)
     local pageName = frame.args[1] or ""
     local pageName = frame.args[1] or ""
第8行: 第48行:
      
      
     local content = title:getContent()
     local content = title:getContent()
     if not content then return "" end
     local rawExcerpt = mw.ustring.sub(content, 1, 500)


     -- 1. 预切割:只拿最前面的 400 字符,从源头掐断计算量
     -- 提取第一张图
     local raw = mw.ustring.sub(content, 1, 400)
     local firstImage = string.match(rawExcerpt, "%[%[([Ff]ile:.-)[|%]]") or
                      string.match(rawExcerpt, "%[%[([Ii]mage:.-)[|%]]") or
                      string.match(rawExcerpt, "%[%[(文件:.-)[|%]]")


    -- 2. 提取图片 (仅提取文件名,不涉及复杂解析)
     local cleanText = safeStrip(rawExcerpt)
     local firstImage = string.match(raw, "%[%[[Ff]ile:([^|%]%s]+)") or
                      string.match(raw, "%[%[文件:([^|%]%s]+)")


     -- 3. 极速清理:只做简单的字符替换,不做嵌套识别
     -- 150字截断并处理链接残符
    -- 移除引用、注释、标题符、列表符
     local targetLen = 150
     local clean = raw
     local summary = mw.ustring.sub(cleanText, 1, targetLen)
     clean = string.gsub(clean, "<ref.-</ref>", "")
     while mw.ustring.sub(summary, -1) == "[" do summary = mw.ustring.sub(summary, 1, -2) end
     clean = string.gsub(clean, "<ref.->", "")
    clean = string.gsub(clean, "<!%-%-.-%-%->", "")
    clean = string.gsub(clean, "\n==+.-==+", " ")
    clean = string.gsub(clean, "\n%s*[*#:]+", " ")
   
    -- 移除图片和模板(不递归,直接找第一个闭合括号就切断)
    -- 这种处理方式虽然可能留下残余的“}}”或“]]”,但绝对保护 CPU
    clean = string.gsub(clean, "%[%[[^%]%[]+%]%]", "")
    clean = string.gsub(clean, "{{[^{}]+}}", "")
   
    -- 清理换行和空格
    clean = string.gsub(clean, "\n", " ")
    clean = string.gsub(clean, "%s+", " ")
    clean = mw.text.trim(clean)


     -- 4. 强制截断 150 字
     local _, opens = string.gsub(summary, "%[%[", "")
     local summary = mw.ustring.sub(clean, 1, 150)
     local _, closes = string.gsub(summary, "%]%]", "")
    if opens > closes then
        local rest = mw.ustring.sub(cleanText, targetLen + 1, targetLen + 100)
        local endLink = string.find(rest, "]]", 1, true)
        if endLink then 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
      
      
    -- 最后的安全阀:清理末尾残留的括号碎片
     summary = mw.text.trim(summary)
     summary = string.gsub(summary, "[%[%]{}<>]+$", "")
     if mw.ustring.len(cleanText) > targetLen then summary = summary .. "..." end
     if mw.ustring.len(clean) > 150 then summary = summary .. "..." end


     -- 5. 渲染
     -- 渲染
     local container = mw.html.create('div'):css({['margin-bottom']='20px', ['display']='flow-root'})
     local container = mw.html.create('div'):css({['margin-bottom']='25px', ['display']='flow-root'})
     if firstImage then
     if firstImage then
         container:wikitext('[[' .. firstImage .. '|130px|right|link=' .. pageName .. ']]')
         container:wikitext('[[' .. firstImage .. '|150px|right|link=' .. pageName .. ']]')
     end
     end
     container:tag('div')
     container:tag('div')
         :css({['font-size']='1.2em', ['font-weight']='bold', ['margin-bottom']='4px'})
         :css({['font-size']='1.3em', ['font-weight']='bold', ['margin-bottom']='6px'})
         :wikitext('[[' .. pageName .. ']]')
         :wikitext('[[' .. pageName .. ']]')
     container:tag('div')
     container:tag('div')
         :css({['line-height']='1.5', ['color']='#333', ['font-size']='14px'})
         :css({['line-height']='1.6', ['color']='#202122', ['font-size']='0.95em'})
         :wikitext(summary)
         :wikitext(summary)