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

Module:PageSummary:修订间差异

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


-- 核心函数:通过计数平衡括号,彻底移除 [[File: ... ]] 及其嵌套内容
-- 核心递归清理函数:通过平衡括号计数,彻底移除 [[File: ... ]] 或 {{模板}} 及其嵌套内容
local function stripComplexTags(text)
local function stripBalancedTags(text, openTag, closeTag)
     if not text then return "" end
     if not text then return "" end
   
     local result = text
     local result = text
    local i = 1
      
      
     while true do
     while true do
        -- 寻找可能的图片起始标签
         local startPos = string.find(result, openTag, 1, true)
         local startPos = nil
        if not startPos then break end
        local patterns = {"%[%[[Ff]ile:", "%[%[[Ii]mage:", "%[%[文件:", "%[%[图像:"}
        local foundPat = nil
       
        for _, pat in ipairs(patterns) do
            local s = string.find(result, pat)
            if s and (not startPos or s < startPos) then
                startPos = s
                foundPat = pat
            end
        end
          
          
        if not startPos then break end -- 没有更多图片标签了
       
        -- 从 startPos 开始寻找匹配的闭合括号
         local count = 0
         local count = 0
         local endPos = nil
         local endPos = nil
         for j = startPos, mw.ustring.len(result) do
         local len = mw.ustring.len(result)
             local char2 = mw.ustring.sub(result, j, j+1)
       
             if char2 == "[[" then
        for j = startPos, len do
             local char2 = mw.ustring.sub(result, j, j + 1)
             if char2 == openTag then
                 count = count + 1
                 count = count + 1
             elseif char2 == "]]" then
             elseif char2 == closeTag then
                 count = count - 1
                 count = count - 1
                 if count == 0 then
                 if count == 0 then
第41行: 第28行:
          
          
         if endPos then
         if endPos then
            -- 移除整段图片代码
             result = mw.ustring.sub(result, 1, startPos - 1) .. mw.ustring.sub(result, endPos + 1)
             result = mw.ustring.sub(result, 1, startPos - 1) .. mw.ustring.sub(result, endPos + 1)
         else
         else
             -- 如果没找到闭合(语法错误),强制跳过这个起始点,防止死循环
             -- 容错:若未闭合则移除起始符防止死循环
             result = mw.ustring.sub(result, 1, startPos - 1) .. mw.ustring.sub(result, startPos + 2)
             result = mw.ustring.sub(result, 1, startPos - 1) .. mw.ustring.sub(result, startPos + 2)
         end
         end
第57行: 第43行:
     local content = title:getContent()
     local content = title:getContent()


     -- 1. 提取第一张图(在清理前提取)
     -- 1. 提取第一张图(用于单独显示)
     local firstImage = string.match(content, "%[%[([Ff]ile:.-)[|%]]") or  
     local firstImage = string.match(content, "%[%[([Ff]ile:.-)[|%]]") or  
                       string.match(content, "%[%[([Ii]mage:.-)[|%]]") or
                       string.match(content, "%[%[([Ii]mage:.-)[|%]]") or
第63行: 第49行:
                       string.match(content, "%[%[(图像:.-)[|%]]")
                       string.match(content, "%[%[(图像:.-)[|%]]")


     -- 2. 处理内容:预加载 1500 字符(处理长条目)
     -- 2. 处理内容:预加载 500 字符
     local rawText = mw.ustring.sub(content, 1, 1500)
     local rawText = mw.ustring.sub(content, 1, 500)
      
      
     -- A. 使用递归逻辑剥离图片
     -- A. 剥离图片和模板(递归处理嵌套链接和长描述)
     rawText = stripComplexTags(rawText)
     rawText = stripBalancedTags(rawText, "[[", "]]") -- 移除所有链接(含图片),后文再截断
    -- 注意:因为上面已经移除了所有 [[ ]],摘要里的链接会暂时丢失。
    -- 如果要保留链接,我们需要更复杂的正则。但为了“删干净”图片描述,
    -- 我们重新从 content 获取一个不含图片但保留链接的版本。
      
      
     -- B. 移除模板 {{...}} - 同样使用平衡计数逻辑处理嵌套
     -- 重新逻辑:仅针对图片前缀进行剥离,保留普通链接
    while true do
    local function stripOnlyImages(text)
        local s = string.find(rawText, "{{")
        local prefixes = {"[[File:", "[[Image:", "[[文件:", "[[图像:", "[[file:", "[[image:"}
        if not s then break end
        for _, pre in ipairs(prefixes) do
        local count = 0
            while string.find(text, pre, 1, true) do
        local e = nil
                local s = string.find(text, pre, 1, true)
        for j = s, mw.ustring.len(rawText) do
                local count = 0
            local c2 = mw.ustring.sub(rawText, j, j+1)
                local e = nil
            if c2 == "{{" then count = count + 1
                for j = s, mw.ustring.len(text) do
            elseif c2 == "}}" then  
                    local c2 = mw.ustring.sub(text, j, j+1)
                count = count - 1
                    if c2 == "[[" then count = count + 1
                if count == 0 then e = j + 1 break end
                    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
         end
         if e then
         return text
            rawText = mw.ustring.sub(rawText, 1, s - 1) .. mw.ustring.sub(rawText, e + 1)
        else
            rawText = mw.ustring.sub(rawText, 1, s - 1) .. mw.ustring.sub(rawText, s + 2)
        end
     end
     end
   
    local processedText = mw.ustring.sub(content, 1, 500)
    processedText = stripOnlyImages(processedText) -- 仅删图片,保留链接
    processedText = stripBalancedTags(processedText, "{{", "}}") -- 删模板


     -- C. 移除标题符号、列表符、引用
     -- B. 移除格式符号
     rawText = string.gsub(rawText, "\n==+.-==+", " ")
     processedText = string.gsub(processedText, "\n==+.-==+", " ") -- 删标题
     rawText = string.gsub(rawText, "\n%s*[*#:]+", " ")
     processedText = string.gsub(processedText, "\n%s*[*#:]+", " ") -- 删列表符
     rawText = string.gsub(rawText, "<ref.-</ref>", "")
     processedText = string.gsub(processedText, "<ref.-</ref>", "")
     rawText = string.gsub(rawText, "<ref.->", "")
     processedText = string.gsub(processedText, "<ref.->", "")
     rawText = string.gsub(rawText, "<!%-%-.-%-%->", "")
     processedText = string.gsub(processedText, "<!%-%-.-%-%->", "")
     rawText = string.gsub(rawText, "&[Nn][Bb][Ss][Pp];", " ")
     processedText = string.gsub(processedText, "&[Nn][Bb][Ss][Pp];", " ")
     rawText = string.gsub(rawText, "\n", " ")
     processedText = string.gsub(processedText, "\n", " ")
     rawText = string.gsub(rawText, "%s+", " ")
     processedText = string.gsub(processedText, "%s+", " ")
      
      
     local cleanText = mw.text.trim(rawText)
     local cleanText = mw.text.trim(processedText)


     -- 3. 截断逻辑:150
     -- 3. 截断逻辑:150 字并处理链接补全
     local targetLen = 150
     local targetLen = 150
     local summary = ""
     local summary = ""
第111行: 第106行:
         local restText = mw.ustring.sub(cleanText, targetLen + 1)
         local restText = mw.ustring.sub(cleanText, targetLen + 1)
          
          
         -- 链接延展逻辑
         -- 链接延展
         local lastOpen = 0
         local lastOpen = 0
         local lastClose = 0
         local lastClose = 0