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

Module:PageSummary:修订间差异

武外梗百科 爱国好学自强图新的百科全书
无编辑摘要
无编辑摘要
第1行: 第1行:
local p = {}
local p = {}
-- 核心函数:通过计数平衡括号,彻底移除 [[File: ... ]] 及其嵌套内容
local function stripComplexTags(text)
    if not text then return "" end
   
    local result = text
    local i = 1
   
    while true do
        -- 寻找可能的图片起始标签
        local startPos = nil
        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 endPos = nil
        for j = startPos, mw.ustring.len(result) do
            local char2 = mw.ustring.sub(result, j, j+1)
            if char2 == "[[" then
                count = count + 1
            elseif char2 == "]]" then
                count = count - 1
                if count == 0 then
                    endPos = j + 1
                    break
                end
            end
        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
    return result
end


function p.getSummaryAndImage(frame)
function p.getSummaryAndImage(frame)
     local pageName = frame.args[1] or ""
     local pageName = frame.args[1] or ""
     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 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
第14行: 第63行:
                       string.match(content, "%[%[(图像:.-)[|%]]")
                       string.match(content, "%[%[(图像:.-)[|%]]")


     -- 2. 处理内容:预加载 1000 字符
     -- 2. 处理内容:预加载 1500 字符(处理长条目)
     local rawText = mw.ustring.sub(content, 1, 1000)
     local rawText = mw.ustring.sub(content, 1, 1500)
   
    -- A. 使用递归逻辑剥离图片
    rawText = stripComplexTags(rawText)
      
      
     -- A. 高级清理图片:处理带有长描述和嵌套链接的图片
     -- B. 移除模板 {{...}} - 同样使用平衡计数逻辑处理嵌套
    -- 循环匹配 [[File: ... ]] 结构,尽量处理嵌套情况
     while true do
     local function stripImages(text)
        local s = string.find(rawText, "{{")
         -- 匹配 [[(File|文件|Image|图像): ... ]]
         if not s then break end
         -- 此正则通过排除法尽可能多地包含字符,直到匹配到对应的闭合括号
         local count = 0
         local patterns = {"%[%[[Ff]ile:.-%]%]", "%[%[[Ii]mage:.-%]%]", "%[%[文件:.-%]%]", "%[%[图像:.-%]%]" }
         local e = nil
         for _, pat in ipairs(patterns) do
         for j = s, mw.ustring.len(rawText) do
             while string.find(text, pat) do
             local c2 = mw.ustring.sub(rawText, j, j+1)
                text = string.gsub(text, pat, "")
            if c2 == "{{" then count = count + 1
            elseif c2 == "}}" then
                count = count - 1
                if count == 0 then e = j + 1 break end
             end
             end
         end
         end
         return text
         if e then
            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
    rawText = stripImages(rawText)
 
      
     -- C. 移除标题符号、列表符、引用
     -- B. 移除 Wiki 列表符号 (*, #, :, ;)
     rawText = string.gsub(rawText, "\n==+.-==+", " ")
     rawText = string.gsub(rawText, "\n%s*[*#:]+", " ")
     rawText = string.gsub(rawText, "\n%s*[*#:]+", " ")
   
    -- C. 移除标题符号 == 标题 ==
    rawText = string.gsub(rawText, "\n==+.-==+", " ")
   
    -- D. 移除模板 {{...}}
    local limit = 15
    while string.find(rawText, "{{.-}}") and limit > 0 do
        rawText = string.gsub(rawText, "{{[^{}]+}}", "")
        limit = limit - 1
    end
   
    -- E. 移除引用 <ref> 和 注释
     rawText = string.gsub(rawText, "<ref.-</ref>", "")
     rawText = string.gsub(rawText, "<ref.-</ref>", "")
     rawText = string.gsub(rawText, "<ref.->", "")
     rawText = string.gsub(rawText, "<ref.->", "")
     rawText = string.gsub(rawText, "<!%-%-.-%-%->", "")
     rawText = string.gsub(rawText, "<!%-%-.-%-%->", "")
   
     rawText = string.gsub(rawText, "&[Nn][Bb][Ss][Pp];", " ")
    -- F. 清理多余的空白符、换行符和特殊占位符(如 &nbsp;)
     rawText = string.gsub(rawText, "&nbsp;", " ")
     rawText = string.gsub(rawText, "\n", " ")
     rawText = string.gsub(rawText, "\n", " ")
     rawText = string.gsub(rawText, "%s+", " ")
     rawText = string.gsub(rawText, "%s+", " ")
第94行: 第139行:


     -- 4. 渲染
     -- 4. 渲染
     local container = mw.html.create('div')
     local container = mw.html.create('div'):css({['margin']='15px 0', ['display']='flow-root'})
        :css({
            ['margin'] = '15px 0',
            ['background'] = 'transparent',
            ['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({
         :css({['font-size']='1.4em', ['font-weight']='bold', ['margin-bottom']='8px'})
            ['font-size'] = '1.4em',
            ['font-weight'] = 'bold',
            ['margin-bottom'] = '8px'
        })
         :wikitext('[[' .. pageName .. ']]')
         :wikitext('[[' .. pageName .. ']]')


    -- 摘要文本
     container:tag('div')
     container:tag('div')
         :css({  
         :css({['line-height']='1.6', ['color']='#202122'})
            ['line-height'] = '1.6',
            ['color'] = '#202122'
        })
         :wikitext(summary)
         :wikitext(summary)