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

Module:PageSummary

武外梗百科 爱国好学自强图新的百科全书
Zelda110留言 | 贡献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