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

Module:PageSummary:修订间差异

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


-- 核心递归清理函数:通过平衡括号计数,彻底移除 [[File: ... ]] 或 {{模板}} 及其嵌套内容
-- 高效清理函数:通过非贪婪匹配和有限循环移除复杂标签
local function stripBalancedTags(text, openTag, closeTag)
local function safeStrip(text)
     if not text then return "" end
     if not text then return "" end
    local result = text
      
      
     while true do
     -- 1. 移除引用和注释(这些最耗性能,优先处理)
        local startPos = string.find(result, openTag, 1, true)
    text = string.gsub(text, "<ref.-</ref>", "")
        if not startPos then break end
    text = string.gsub(text, "<ref.->", "")
       
    text = string.gsub(text, "<!%-%-.-%-%->", "")
        local count = 0
   
        local endPos = nil
    -- 2. 移除图片标签(核心优化:直接匹配包含 File/文件 等前缀的 [[...]])
        local len = mw.ustring.len(result)
    -- 使用非贪婪匹配并在发现嵌套链接时进行有限次迭代,避免死循环
       
    local imagePrefixes = {"[Ff]ile:", "[Ii]mage:", "文件:", "图像:"}
        for j = startPos, len do
    for _, prefix in ipairs(imagePrefixes) do
            local char2 = mw.ustring.sub(result, j, j + 1)
         -- 匹配 [[Prefix: ... ]] 直到遇到对应的闭合括号,不使用递归以节省 CPU
            if char2 == openTag then
         text = string.gsub(text, "%[%[" .. prefix .. ".-%]%]", "")
                count = count + 1
    end
            elseif char2 == closeTag then
 
                count = count - 1
    -- 3. 移除模板 {{...}}
                if count == 0 then
    -- 限制最多处理 10 层嵌套,防止 CPU 跑满
                    endPos = j + 1
    for i = 1, 10 do
                    break
        local newText = string.gsub(text, "{{[^{}]+}}", "")
                end
         if newText == text then break end
            end
        text = newText
        end
          
         if endPos then
            result = mw.ustring.sub(result, 1, startPos - 1) .. mw.ustring.sub(result, endPos + 1)
        else
            -- 容错:若未闭合则移除起始符防止死循环
            result = mw.ustring.sub(result, 1, startPos - 1) .. mw.ustring.sub(result, startPos + 2)
         end
     end
     end
     return result
 
    -- 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
end


第41行: 第40行:
     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
   
    -- 只读取前 800 个字符进行处理,减轻解析负担
     local content = title:getContent()
     local content = title:getContent()
    local rawExcerpt = mw.ustring.sub(content, 1, 800)


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


     -- 2. 处理内容:预加载 500 字符
     -- 2. 安全清理文本
     local rawText = mw.ustring.sub(content, 1, 500)
     local cleanText = safeStrip(rawExcerpt)
   
 
     -- A. 剥离图片和模板(递归处理嵌套链接和长描述)
     -- 3. 截断 150 字
     rawText = stripBalancedTags(rawText, "[[", "]]") -- 移除所有链接(含图片),后文再截断
     local targetLen = 150
    -- 注意:因为上面已经移除了所有 [[ ]],摘要里的链接会暂时丢失。
    local summary = mw.ustring.sub(cleanText, 1, targetLen)
    -- 如果要保留链接,我们需要更复杂的正则。但为了“删干净”图片描述,
    -- 我们重新从 content 获取一个不含图片但保留链接的版本。
      
      
     -- 重新逻辑:仅针对图片前缀进行剥离,保留普通链接
     -- 简单的链接补全逻辑,不再使用循环搜索,改用简单的计数
     local function stripOnlyImages(text)
     local _, opens = string.gsub(summary, "%[%[", "")
        local prefixes = {"[[File:", "[[Image:", "[[文件:", "[[图像:", "[[file:", "[[image:"}
    local _, closes = string.gsub(summary, "%]%]", "")
        for _, pre in ipairs(prefixes) do
    if opens > closes then
            while string.find(text, pre, 1, true) do
        local rest = mw.ustring.sub(cleanText, targetLen + 1, targetLen + 50)
                local s = string.find(text, pre, 1, true)
        local endLink = string.find(rest, "]]", 1, true)
                local count = 0
        if endLink then
                local e = nil
            summary = summary .. string.sub(rest, 1, endLink + 1)
                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
         end
        return text
     end
     end
      
      
    local processedText = mw.ustring.sub(content, 1, 500)
     if mw.ustring.len(cleanText) > targetLen then
    processedText = stripOnlyImages(processedText) -- 仅删图片,保留链接
    processedText = stripBalancedTags(processedText, "{{", "}}") -- 删模板
 
    -- B. 移除格式符号
    processedText = string.gsub(processedText, "\n==+.-==+", " ") -- 删标题
    processedText = string.gsub(processedText, "\n%s*[*#:]+", " ") -- 删列表符
    processedText = string.gsub(processedText, "<ref.-</ref>", "")
    processedText = string.gsub(processedText, "<ref.->", "")
    processedText = string.gsub(processedText, "<!%-%-.-%-%->", "")
    processedText = string.gsub(processedText, "&[Nn][Bb][Ss][Pp];", " ")
    processedText = string.gsub(processedText, "\n", " ")
    processedText = string.gsub(processedText, "%s+", " ")
   
    local cleanText = mw.text.trim(processedText)
 
    -- 3. 截断逻辑:150 字并处理链接补全
    local targetLen = 150
    local summary = ""
     if mw.ustring.len(cleanText) <= targetLen then
        summary = cleanText
    else
        summary = mw.ustring.sub(cleanText, 1, targetLen)
        local restText = mw.ustring.sub(cleanText, targetLen + 1)
       
        -- 链接延展
        local lastOpen = 0
        local lastClose = 0
        local tempPos = 0
        while true do
            local found = string.find(summary, "%[%[", tempPos + 1)
            if not found then break end
            lastOpen = found
            tempPos = found
        end
        tempPos = 0
        while true do
            local found = string.find(summary, "%]%]", tempPos + 1)
            if not found then break end
            lastClose = found
            tempPos = found
        end
 
        if lastOpen > lastClose then
            local endOfLink = string.find(restText, "%]%]")
            if endOfLink then
                summary = summary .. string.sub(restText, 1, endOfLink + 1)
            end
        end
         summary = summary .. "..."
         summary = summary .. "..."
     end
     end


     -- 4. 渲染
     -- 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
         container:wikitext('[[' .. firstImage .. '|150px|right|link=' .. pageName .. ']]')
         container:wikitext('[[' .. firstImage .. '|150px|right|link=' .. pageName .. ']]')
     end
     end
     container:tag('div')
     container:tag('div')
         :css({['font-size']='1.4em', ['font-weight']='bold', ['margin-bottom']='8px'})
         :css({['font-size']='1.4em', ['font-weight']='bold', ['margin-bottom']='8px'})
         :wikitext('[[' .. pageName .. ']]')
         :wikitext('[[' .. pageName .. ']]')
     container:tag('div')
     container:tag('div')
         :css({['line-height']='1.6', ['color']='#202122'})
         :css({['line-height']='1.6', ['color']='#202122'})