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

Module:PageSummary:修订间差异

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


     -- 2. 清理正文:移除所有圖片標籤、模板、注釋、引用
     -- 2. 清理正文并处理截断(逻辑保留)
     local cleanText = content
     local cleanText = content
     cleanText = string.gsub(cleanText, "<ref.-</ref>", "")
     cleanText = string.gsub(cleanText, "<ref.-</ref>", "")
     cleanText = string.gsub(cleanText, "<ref.->", "")
     cleanText = string.gsub(cleanText, "<ref.->", "")
     cleanText = string.gsub(cleanText, "<!%-%-.-%-%->", "")
     cleanText = string.gsub(cleanText, "<!%-%-.-%-%->", "")
    -- 移除所有圖片鏈接,防止摘要中出現多餘圖片
     cleanText = string.gsub(cleanText, "%[%[[Ff]ile:.-%]%]", "")
     cleanText = string.gsub(cleanText, "%[%[[Ff]ile:.-%]%]", "")
     cleanText = string.gsub(cleanText, "%[%[[Ii]mage:.-%]%]", "")
     cleanText = string.gsub(cleanText, "%[%[[Ii]mage:.-%]%]", "")
     cleanText = string.gsub(cleanText, "%[%[文件:.-%]%]", "")
     cleanText = string.gsub(cleanText, "%[%[文件:.-%]%]", "")
     cleanText = string.gsub(cleanText, "%[%[圖像:.-%]%]", "")
     cleanText = string.gsub(cleanText, "%[%[图像:.-%]%]", "")
   
    -- 移除模板
     local limit = 10
     local limit = 10
     while string.find(cleanText, "{{.-}}") and limit > 0 do
     while string.find(cleanText, "{{.-}}") and limit > 0 do
第31行: 第27行:
         limit = limit - 1
         limit = limit - 1
     end
     end
   
     cleanText = mw.text.trim(cleanText)
     cleanText = mw.text.trim(cleanText)
     cleanText = string.gsub(cleanText, "\n", " ")
     cleanText = string.gsub(cleanText, "\n", " ")


    -- 3. 智能截斷邏輯:如果 100 字處於連結中間,則延展到連結結束
     local targetLen = 100
     local targetLen = 100
     local summary = ""
     local summary = ""
     local fullLen = mw.ustring.len(cleanText)
     if mw.ustring.len(cleanText) <= targetLen then
 
    if fullLen <= targetLen then
         summary = cleanText
         summary = cleanText
     else
     else
         summary = mw.ustring.sub(cleanText, 1, targetLen)
         summary = mw.ustring.sub(cleanText, 1, targetLen)
       
        -- 檢查是否截斷在了連結 [[...]] 中間
         local restText = mw.ustring.sub(cleanText, targetLen + 1)
         local restText = mw.ustring.sub(cleanText, targetLen + 1)
       
         -- 链接延展处理
         -- 判斷 summary 中最後一個 [[ 是否有對應的 ]]
         local lastOpen = 0
         local lastOpen = 0
         local lastClose = 0
         local lastClose = 0
第65行: 第54行:
             tempPos = found
             tempPos = found
         end
         end
        -- 如果 [[ 在 ]] 之後,說明截斷點在連結內
         if lastOpen > lastClose then
         if lastOpen > lastClose then
            -- 在剩餘文本中尋找第一個出現的 ]]
             local endOfLink = string.find(restText, "%]%]")
             local endOfLink = string.find(restText, "%]%]")
             if endOfLink then
             if endOfLink then summary = summary .. string.sub(restText, 1, endOfLink + 1) end
                summary = summary .. string.sub(restText, 1, endOfLink + 1)
            end
        end
 
        -- 檢查粗體 ''' 的處理 (同理)
        local _, boldTags = string.gsub(summary, "'''", "")
        if boldTags % 2 ~= 0 then
            local endOfBold = string.find(restText, "'''")
            if endOfBold then
                summary = summary .. string.sub(restText, 1, endOfBold + 2)
            end
         end
         end
         summary = summary .. "..."
         summary = summary .. "..."
     end
     end


     -- 4. 構建輸出容器
     -- === 3. 维基百科风格渲染 ===
   
    -- 外层容器:高斯模糊边框、悬停变色、白色底
     local container = mw.html.create('div')
     local container = mw.html.create('div')
         :css({
         :css({
            ['border'] = '1px solid #a2a9b1',
            ['background-color'] = '#f8f9fa',
            ['padding'] = '15px',
             ['margin'] = '10px 0',
             ['margin'] = '10px 0',
             ['display'] = 'flow-root',
            ['background'] = '#fff',
             ['border-radius'] = '4px'
            ['border'] = '1px solid #a2a9b1', -- 标准维基百科灰框色
            ['border-radius'] = '2px',
             ['display'] = 'flex',            -- 使用 Flex 布局实现响应式
            ['flex-direction'] = 'row-reverse', -- 图片在右,文字在左
             ['overflow'] = 'hidden',
            ['box-shadow'] = '0 1px 1px rgba(0,0,0,0.05)',
            ['min-height'] = '120px'
         })
         })


    -- 图片部分
     if firstImage then
     if firstImage then
         container:tag('div')
         container:tag('div')
             :css({['float'] = 'right', ['margin-left'] = '15px'})
             :css({
             :wikitext('[[' .. firstImage .. '|120px|border|link=' .. pageName .. ']]')
                ['flex-shrink'] = '0',
                ['width'] = '140px',
                ['background-color'] = '#f8f9fa',
                ['display'] = 'flex',
                ['align-items'] = 'center',
                ['justify-content'] = 'center',
                ['border-left'] = '1px solid #a2a9b1'
            })
             :wikitext('[[' .. firstImage .. '|140x140px|center|link=' .. pageName .. ']]')
     end
     end


     container:tag('div')
     -- 文字内容部分
         :css({['font-weight'] = 'bold', ['font-size'] = '1.2em', ['margin-bottom'] = '8px'})
    local textCol = container:tag('div')
         :css({
            ['padding'] = '12px 16px',
            ['flex-grow'] = '1',
            ['display'] = 'flex',
            ['flex-direction'] = 'column',
            ['justify-content'] = 'center'
        })
 
    -- 标题(维基经典的衬线体或加粗体)
    textCol:tag('div')
        :css({
            ['font-size'] = '1.2em',
            ['font-weight'] = 'bold',
            ['margin-bottom'] = '5px',
            ['color'] = '#000'
        })
         :wikitext('[[' .. pageName .. ']]')
         :wikitext('[[' .. pageName .. ']]')


     container:tag('div')
     -- 摘要文本
         :css({['line-height'] = '1.6'})
    textCol:tag('div')
         :css({
            ['font-size'] = '0.92em',
            ['line-height'] = '1.5',
            ['color'] = '#202122' -- 维基百科标准正文黑色
        })
         :wikitext(summary)
         :wikitext(summary)



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

此模块的文档可以在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. 清理正文并处理截断(逻辑保留)
    local cleanText = content
    cleanText = string.gsub(cleanText, "<ref.-</ref>", "")
    cleanText = string.gsub(cleanText, "<ref.->", "")
    cleanText = string.gsub(cleanText, "<!%-%-.-%-%->", "")
    cleanText = string.gsub(cleanText, "%[%[[Ff]ile:.-%]%]", "")
    cleanText = string.gsub(cleanText, "%[%[[Ii]mage:.-%]%]", "")
    cleanText = string.gsub(cleanText, "%[%[文件:.-%]%]", "")
    cleanText = string.gsub(cleanText, "%[%[图像:.-%]%]", "")
    local limit = 10
    while string.find(cleanText, "{{.-}}") and limit > 0 do
        cleanText = string.gsub(cleanText, "{{[^{}]+}}", "")
        limit = limit - 1
    end
    cleanText = mw.text.trim(cleanText)
    cleanText = string.gsub(cleanText, "\n", " ")

    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

    -- === 3. 维基百科风格渲染 ===
    
    -- 外层容器:高斯模糊边框、悬停变色、白色底
    local container = mw.html.create('div')
        :css({
            ['margin'] = '10px 0',
            ['background'] = '#fff',
            ['border'] = '1px solid #a2a9b1', -- 标准维基百科灰框色
            ['border-radius'] = '2px',
            ['display'] = 'flex',             -- 使用 Flex 布局实现响应式
            ['flex-direction'] = 'row-reverse', -- 图片在右,文字在左
            ['overflow'] = 'hidden',
            ['box-shadow'] = '0 1px 1px rgba(0,0,0,0.05)',
            ['min-height'] = '120px'
        })

    -- 图片部分
    if firstImage then
        container:tag('div')
            :css({
                ['flex-shrink'] = '0',
                ['width'] = '140px',
                ['background-color'] = '#f8f9fa',
                ['display'] = 'flex',
                ['align-items'] = 'center',
                ['justify-content'] = 'center',
                ['border-left'] = '1px solid #a2a9b1'
            })
            :wikitext('[[' .. firstImage .. '|140x140px|center|link=' .. pageName .. ']]')
    end

    -- 文字内容部分
    local textCol = container:tag('div')
        :css({
            ['padding'] = '12px 16px',
            ['flex-grow'] = '1',
            ['display'] = 'flex',
            ['flex-direction'] = 'column',
            ['justify-content'] = 'center'
        })

    -- 标题(维基经典的衬线体或加粗体)
    textCol:tag('div')
        :css({
            ['font-size'] = '1.2em',
            ['font-weight'] = 'bold',
            ['margin-bottom'] = '5px',
            ['color'] = '#000'
        })
        :wikitext('[[' .. pageName .. ']]')

    -- 摘要文本
    textCol:tag('div')
        :css({
            ['font-size'] = '0.92em',
            ['line-height'] = '1.5',
            ['color'] = '#202122' -- 维基百科标准正文黑色
        })
        :wikitext(summary)

    return tostring(container)
end

return p