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

Module:PageSummary:修订间差异

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


-- 核心剥离逻辑:状态机识别,防止图片碎片
-- 高性能清理函数
local function stripImages(text)
local function smartClean(text)
     if not text then return "" end
     if not text then return "" end
     local out = {}
 
     local i = 1
    -- 1. 预处理:去掉 HTML 注释、脚注、数学公式等标签块
     local len = mw.ustring.len(text)
    text = mw.ustring.gsub(text, "<!%-%-.-%-%->", "")
     while i <= len do
    text = mw.ustring.gsub(text, "<ref[^>]*>.-</ref>", "")
         local foundImage = false
    text = mw.ustring.gsub(text, "<ref[^>]-/>", "")
         local chunk = mw.ustring.sub(text, i, i + 10)
     text = mw.ustring.gsub(text, "<math[^>]*>.-</math>", "")
        if chunk:match("^%[%[[Ff]ile:") or chunk:match("^%[%[[Ii]mage:") or  
 
           chunk:match("^%[%[[Cc]ategory:") or  
    -- 2. 去除表格 (MediaWiki 表格以 {| 开始,|} 结束)
          chunk:match("^%[%[文件:") or chunk:match("^%[%[图像:") then
    -- 使用非贪婪匹配,重复执行以处理多个表格
            local depth = 0
     text = mw.ustring.gsub(text, "{|[^{}]*|}", "") -- 先处理最内层不含嵌套的
            local j = i
     text = mw.ustring.gsub(text, "{|.-|}", "")     -- 扩展清理
            while j <= len do
 
                local char2 = mw.ustring.sub(text, j, j + 1)
    -- 3. 递归剥离模板 {{...}}
                if char2 == "[[" then depth = depth + 1 j = j + 2
    -- 性能优化:限制循环次数,防止死循环
                elseif char2 == "]]" then depth = depth - 1 j = j + 2
     local prev, count = "", 0
                    if depth <= 0 then i = j foundImage = true break end
    repeat
                else j = j + 1 end
         prev = text
            end
         text = mw.ustring.gsub(text, "{{[^{}]-}}", "")
        count = count + 1
    until text == prev or count > 10
 
    -- 4. 剥离图片、分类等 [[File:xxx]]
    -- 逻辑:匹配所有中括号,如果是媒体文件则删掉,如果是普通链接则保留文字
    text = mw.ustring.gsub(text, "%[%[([^%[%]]-)%]%]", function(inner)
        local l_inner = mw.ustring.lower(inner)
        if l_inner:match("^file:") or l_inner:match("^image:") or  
           l_inner:match("^category:") or l_inner:match("^文件:") or  
          l_inner:match("^图像:") or l_inner:match("^分类:") then
            return "" -- 丢弃附件
         end
         end
         if not foundImage then
         -- 保留链接文字:[[A|B]] -> B, [[A]] -> A
            table.insert(out, mw.ustring.sub(text, i, i))
        local parts = mw.text.split(inner, "|")
            i = i + 1
        return parts[#parts]
        end
    end)
        if i > 600 then break end
 
     end
    -- 5. 清理剩余的格式字符
     return table.concat(out)
    text = mw.ustring.gsub(text, "''+", "")         -- 去掉粗斜体
end
    text = mw.ustring.gsub(text, "==+.-==+", "")   -- 去掉标题
    text = mw.ustring.gsub(text, "\n%s*[*#:]+", " ") -- 列表转空格
     text = mw.ustring.gsub(text, "\n+", " ")        -- 换行转空格
     text = mw.ustring.gsub(text, "%s+", " ")       -- 压缩多余空格


local function cleanFinal(text)
    text = string.gsub(text, "{{[^{}]+}}", "")
    text = string.gsub(text, "{{[^{}]+}}", "")
    text = string.gsub(text, "<ref.-</ref>", "")
    text = string.gsub(text, "<ref.->", "")
    text = string.gsub(text, "<!%-%-.-%-%->", "")
    text = string.gsub(text, "\n==+.-==+", " ")
    text = string.gsub(text, "\n%s*[*#:]+", " ")
    text = string.gsub(text, "\n", " ")
    text = string.gsub(text, "%s+", " ")
     return mw.text.trim(text)
     return mw.text.trim(text)
end
end
第49行: 第53行:
     local title = mw.title.new(pageName)
     local title = mw.title.new(pageName)
     if not title or not title.exists then return "" end
     if not title or not title.exists then return "" end
   
    local content = title:getContent()
    local rawExcerpt = mw.ustring.sub(content, 1, 400)


     -- 提取第一张图名
     -- 性能优化:只读取前 2000 个字符进行解析,而不是全篇,防止超大页面卡死
     local firstImage = string.match(rawExcerpt, "%[%[([Ff]ile:[^|%]%s]+)") or  
     local content = mw.ustring.sub(title:getContent() or "", 1, 2000)
                      string.match(rawExcerpt, "%[%[(文件:[^|%]%s]+)")


     local step1 = stripImages(rawExcerpt)
    -- 1. 提取第一张图(直接在原文找,不干扰摘要)
    local cleanText = cleanFinal(step1)
     local firstImage = mw.ustring.match(content, "%[%[%s*[Ff]ile%s*:([^|%]%s]+)") or
                      mw.ustring.match(content, "%[%[%s*文件%s*:([^|%]%s]+)") or
                      mw.ustring.match(content, "%[%[%s*[Ii]mage%s*:([^|%]%s]+)")


     local targetLen = 120
    -- 2. 提取摘要
    local cleanText = smartClean(content)
     local targetLen = 140
     local summary = mw.ustring.sub(cleanText, 1, targetLen)
     local summary = mw.ustring.sub(cleanText, 1, targetLen)
   
 
     -- 清理末尾断头链接
     if mw.ustring.len(cleanText) > targetLen then
    local _, opens = string.gsub(summary, "%[%[", "")
         summary = summary .. "..."
    local _, closes = string.gsub(summary, "%]%]", "")
    if opens > closes then
         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)
     -- 3. 渲染 UI
    if mw.ustring.len(cleanText) > targetLen then summary = summary .. "..." end
    local container = mw.html.create('div'):css({
        ['margin-bottom'] = '20px',
        ['padding'] = '15px',
        ['border'] = '1px solid #e2e2e2',
        ['border-radius'] = '8px',
        ['background-color'] = '#fff',
        ['box-shadow'] = '0 2px 5px rgba(0,0,0,0.05)',
        ['display'] = 'flow-root'
    })


     -- --- 渲染部分修改 ---
     -- 标题
    local container = mw.html.create('div'):css({['margin-bottom']='25px', ['display']='flow-root'})
   
    -- 1. 先渲染标题
     container:tag('div')
     container:tag('div')
         :css({['font-size']='1.3em', ['font-weight']='bold', ['margin-bottom']='6px', ['border-bottom']='1px solid #eee'})
         :css({['font-size'] = '1.2em', ['font-weight'] = 'bold', ['margin-bottom'] = '8px', ['color'] = '#000'})
         :wikitext('[[' .. pageName .. ']]')
         :wikitext('[[' .. pageName .. ']]')
   
 
     -- 2. 创建摘要正文容器
     -- 正文容器
     local textDiv = container:tag('div')
     local body = container:tag('div'):css({['font-size'] = '14px', ['line-height'] = '1.6', ['color'] = '#444'})
        :css({['line-height']='1.6', ['color']='#222', ['font-size']='14px'})
 
   
     -- 图片右浮动
     -- 3. 将图片放在正文的最前面,实现文字环绕
     if firstImage then
     if firstImage then
         textDiv:wikitext('[[' .. firstImage .. '|120px|right|link=' .. pageName .. ']]')
         body:wikitext('[[File:' .. firstImage .. '|100px|right|link=' .. pageName .. ']]')
     end
     end
   
 
    -- 4. 紧接着放入文字
     body:wikitext(summary)
     textDiv:wikitext(summary)


     return tostring(container)
     return tostring(container)