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

Module:PageSummary:修订间差异

武外梗百科 爱国好学自强图新的百科全书
无编辑摘要
无编辑摘要
第8行: 第8行:
     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行: 第14行:
                       string.match(content, "%[%[(图像:.-)[|%]]")
                       string.match(content, "%[%[(图像:.-)[|%]]")


     -- 2. 彻底清理正文(去除所有图片、模板、引用)
     -- 2. 处理内容:先获取双倍长度的内容以备清理
     local cleanText = content
    -- 取前 400 个字符确保清理掉 ==标题== 和 [[图片]] 后仍有足够文字
     local rawText = mw.ustring.sub(content, 1, 400)
      
      
     -- 核心:彻底去除摘要文本中所有的图片标记
     -- A. 彻底移除所有图片链接
    -- 使用更严谨的匹配:从 [[File: 开始,匹配到第一个不被嵌套的 ]]
     rawText = string.gsub(rawText, "%[%[[Ff]ile:.-%]%]", "")
     cleanText = string.gsub(cleanText, "%[%[[Ff]ile:.-%]%]", "")
     rawText = string.gsub(rawText, "%[%[[Ii]mage:.-%]%]", "")
     cleanText = string.gsub(cleanText, "%[%[[Ii]mage:.-%]%]", "")
     rawText = string.gsub(rawText, "%[%[文件:.-%]%]", "")
     cleanText = string.gsub(cleanText, "%[%[文件:.-%]%]", "")
     rawText = string.gsub(rawText, "%[%[图像:.-%]%]", "")
     cleanText = string.gsub(cleanText, "%[%[图像:.-%]%]", "")
      
      
     -- 移除其余干扰项
     -- B. 移除 == 标题 == 格式符号(保留标题文字或直接删除,这里选择直接删除以保持摘要连贯)
     cleanText = string.gsub(cleanText, "<ref.-</ref>", "")
     rawText = string.gsub(rawText, "\n==+.-==+", " ")
    cleanText = string.gsub(cleanText, "<ref.->", "")
    cleanText = string.gsub(cleanText, "<!%-%-.-%-%->", "")
      
      
    -- C. 移除模板、注释、引用
    rawText = string.gsub(rawText, "<ref.-</ref>", "")
    rawText = string.gsub(rawText, "<ref.->", "")
    rawText = string.gsub(rawText, "<!%-%-.-%-%->", "")
     local limit = 10
     local limit = 10
     while string.find(cleanText, "{{.-}}") and limit > 0 do
     while string.find(rawText, "{{.-}}") and limit > 0 do
         cleanText = string.gsub(cleanText, "{{[^{}]+}}", "")
         rawText = string.gsub(rawText, "{{[^{}]+}}", "")
         limit = limit - 1
         limit = limit - 1
     end
     end
      
      
     cleanText = mw.text.trim(cleanText)
     -- D. 清理换行和多余空格
     cleanText = string.gsub(cleanText, "\n", " ")
     rawText = string.gsub(rawText, "\n", " ")
    local cleanText = mw.text.trim(rawText)


     -- 3. 延展截断逻辑
     -- 3. 智能截断:从清理后的文本中截取前 100 字,并处理链接延展
     local targetLen = 100
     local targetLen = 100
     local summary = ""
     local summary = ""
第47行: 第50行:
         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
第73行: 第77行:
     end
     end


     -- 4. 无背景、维基样式的渲染
     -- 4. 渲染:无背景、无下划线、原生图片显示
     local container = mw.html.create('div')
     local container = mw.html.create('div')
         :css({
         :css({
             ['margin'] = '15px 0',
             ['margin'] = '15px 0',
            ['padding'] = '0',
             ['background'] = 'transparent',
             ['background'] = 'transparent', -- 去除背景
             ['display'] = 'flow-root'
             ['display'] = 'flow-root'       -- 确保包裹右侧浮动图片
         })
         })


     -- 使用维基标准 thumb 样式,会自动处理边框和间距
     -- 原生图片格式:右侧浮动,无 thumb 边框
     if firstImage then
     if firstImage then
         container:wikitext('[[' .. firstImage .. '|thumb|150px|right|link=' .. pageName .. ']]')
         container:wikitext('[[' .. firstImage .. '|150px|right|link=' .. pageName .. ']]')
     end
     end


     -- 标题:维基百科标准 h2 风格
     -- 标题:加粗,无下划线,原生字体
     container:tag('div')
     container:tag('div')
         :css({
         :css({
             ['font-size'] = '1.5em',
             ['font-size'] = '1.4em',
            ['font-family'] = '"Linux Libertine", "Georgia", "Times", serif',
             ['font-weight'] = 'bold',
             ['font-weight'] = 'normal',
             ['margin-bottom'] = '10px'
             ['margin-bottom'] = '0.25em',
            ['border-bottom'] = '1px solid #a2a9b1', -- 维基标准下划线
            ['padding-bottom'] = '0'
         })
         })
         :wikitext('[[' .. pageName .. ']]')
         :wikitext('[[' .. pageName .. ']]')

2025年12月21日 (日) 17:35的版本

此模块的文档可以在Module:PageSummary/doc创建

local p = {}

function p.getSummaryAndImage(frame)
    local pageName = frame.args[1] or ""
    local title = mw.title.new(pageName)
    
    if not title or not title.exists then return "页面不存在" end
    local content = title:getContent()

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

    -- 2. 处理内容:先获取双倍长度的内容以备清理
    -- 取前 400 个字符确保清理掉 ==标题== 和 [[图片]] 后仍有足够文字
    local rawText = mw.ustring.sub(content, 1, 400)
    
    -- A. 彻底移除所有图片链接
    rawText = string.gsub(rawText, "%[%[[Ff]ile:.-%]%]", "")
    rawText = string.gsub(rawText, "%[%[[Ii]mage:.-%]%]", "")
    rawText = string.gsub(rawText, "%[%[文件:.-%]%]", "")
    rawText = string.gsub(rawText, "%[%[图像:.-%]%]", "")
    
    -- B. 移除 == 标题 == 格式符号(保留标题文字或直接删除,这里选择直接删除以保持摘要连贯)
    rawText = string.gsub(rawText, "\n==+.-==+", " ")
    
    -- C. 移除模板、注释、引用
    rawText = string.gsub(rawText, "<ref.-</ref>", "")
    rawText = string.gsub(rawText, "<ref.->", "")
    rawText = string.gsub(rawText, "<!%-%-.-%-%->", "")
    local limit = 10
    while string.find(rawText, "{{.-}}") and limit > 0 do
        rawText = string.gsub(rawText, "{{[^{}]+}}", "")
        limit = limit - 1
    end
    
    -- D. 清理换行和多余空格
    rawText = string.gsub(rawText, "\n", " ")
    local cleanText = mw.text.trim(rawText)

    -- 3. 智能截断:从清理后的文本中截取前 100 字,并处理链接延展
    local targetLen = 100
    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 .. "..."
    end

    -- 4. 渲染:无背景、无下划线、原生图片显示
    local container = mw.html.create('div')
        :css({
            ['margin'] = '15px 0',
            ['background'] = 'transparent',
            ['display'] = 'flow-root'
        })

    -- 原生图片格式:右侧浮动,无 thumb 边框
    if firstImage then
        container:wikitext('[[' .. firstImage .. '|150px|right|link=' .. pageName .. ']]')
    end

    -- 标题:加粗,无下划线,原生字体
    container:tag('div')
        :css({
            ['font-size'] = '1.4em',
            ['font-weight'] = 'bold',
            ['margin-bottom'] = '10px'
        })
        :wikitext('[[' .. pageName .. ']]')

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

    return tostring(container)
end

return p