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

Module:PageSummary:修订间差异

武外梗百科 爱国好学自强图新的百科全书
创建页面,内容为“local p = {} -- 辅助函数:只移除不必要的干扰项(如模板、注释、引文),保留链接和加粗 local function cleanWikitextForDisplay(text) if not text then return "" end -- 1. 移除 <ref> 及其内容(避免摘要里出现 [1][2] 或引文列表) text = string.gsub(text, "<ref.-</ref>", "") text = string.gsub(text, "<ref.->", "") -- 2. 移除 HTML 注释 text = string.gsub(text, "<!%-%-.-%-%->", "…”
 
无编辑摘要
第1行: 第1行:
local p = {}
local p = {}


-- 辅助函数:只移除不必要的干扰项(如模板、注释、引文),保留链接和加粗
function p.getSummaryAndImage(frame)
local function cleanWikitextForDisplay(text)
     local pageName = frame.args[1] or ""
     if not text then return "" end
    local title = mw.title.new(pageName)
      
      
     -- 1. 移除 <ref> 及其内容(避免摘要里出现 [1][2] 或引文列表)
    if not title or not title.exists then return "頁面不存在" end
     text = string.gsub(text, "<ref.-</ref>", "")
    local content = title:getContent()
     text = string.gsub(text, "<ref.->", "")
 
     -- 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, "%[%[圖像:.-%]%]", "")
      
      
     -- 2. 移除 HTML 注释
     -- 移除模板
    text = string.gsub(text, "<!%-%-.-%-%->", "")
 
    -- 3. 移除模板 {{...}}(大部分模板在摘要里会显示异常)
     local limit = 10
     local limit = 10
     while string.find(text, "{{.-}}") and limit > 0 do
     while string.find(cleanText, "{{.-}}") and limit > 0 do
         text = string.gsub(text, "{{[^{}]+}}", "")
         cleanText = string.gsub(cleanText, "{{[^{}]+}}", "")
         limit = limit - 1
         limit = limit - 1
     end
     end
   
    cleanText = mw.text.trim(cleanText)
    cleanText = string.gsub(cleanText, "\n", " ")


     -- 4. 移除行首的标题符号(如 == 标题 ==)
     -- 3. 智能截斷邏輯:如果 100 字處於連結中間,則延展到連結結束
     text = string.gsub(text, "\n==+.-==+", " ")
    local targetLen = 100
      
     local summary = ""
    -- 5. 将换行符转为空格,确保摘要连贯
     local fullLen = mw.ustring.len(cleanText)
    text = string.gsub(text, "\n", " ")


     return mw.text.trim(text)
     if fullLen <= targetLen then
end
        summary = cleanText
    else
        summary = mw.ustring.sub(cleanText, 1, targetLen)
       
        -- 檢查是否截斷在了連結 [[...]] 中間
        local restText = mw.ustring.sub(cleanText, targetLen + 1)
       
        -- 判斷 summary 中最後一個 [[ 是否有對應的 ]]
        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


-- 智能截断函数:在不破坏 [[ ]] 或 ''' ''' 的前提下截取字数
        -- 如果 [[ ]] 之後,說明截斷點在連結內
local function smartTruncate(text, length)
        if lastOpen > lastClose then
    local count = 0
            -- 在剩餘文本中尋找第一個出現的 ]]
    local result = ""
            local endOfLink = string.find(restText, "%]%]")
    local i = 1
            if endOfLink then
    local len = mw.ustring.len(text)
                summary = summary .. string.sub(restText, 1, endOfLink + 1)
   
            end
    -- 简单的截取,如果刚好在标签中间截断,MediaWiki 解析器通常会自动修复
        end
    -- 但为了保险,我们只截取纯文本长度,或者简单按长度切断
    if len <= length then return text end
   
    return mw.ustring.sub(text, 1, length) .. "..."
end


function p.getSummaryAndImage(frame)
        -- 檢查粗體 ''' 的處理 (同理)
    local pageName = frame.args[1] or ""
        local _, boldTags = string.gsub(summary, "'''", "")
    local title = mw.title.new(pageName)
        if boldTags % 2 ~= 0 then
   
            local endOfBold = string.find(restText, "'''")
    if not title or not title.exists then return "页面不存在" end
            if endOfBold then
                summary = summary .. string.sub(restText, 1, endOfBold + 2)
            end
        end


    local content = title:getContent()
        summary = summary .. "..."
   
    -- 1. 提取第一张图片
    local imagePattern = "%[%[(%a+:.-)[|%]]"
    local firstImage = string.match(content, imagePattern)
   
    -- 检查是否是合法的图片命名空间
    if firstImage then
        local ns = string.match(firstImage, "^([^:]+):")
        local validNs = {File=true, Image=true, ["文件"]=true, ["图像"]=true}
        if not ns or not validNs[ns] then firstImage = nil end
     end
     end


    -- 2. 处理内容(保留链接和粗体)
     -- 4. 構建輸出容器
    local processedText = cleanWikitextForDisplay(content)
   
    -- 3. 截取前 100 字
    local summary = smartTruncate(processedText, 100)
 
     -- 4. 渲染输出
     local container = mw.html.create('div')
     local container = mw.html.create('div')
         :css({
         :css({
             ['border'] = '1px solid #a2a9b1',
             ['border'] = '1px solid #a2a9b1',
             ['background-color'] = '#f8f9fa',
             ['background-color'] = '#f8f9fa',
             ['padding'] = '12px',
             ['padding'] = '15px',
             ['margin'] = '10px 0',
             ['margin'] = '10px 0',
             ['border-radius'] = '2px',
             ['display'] = 'flow-root',
             ['overflow'] = 'hidden'
             ['border-radius'] = '4px'
         })
         })


第81行: 第101行:
         container:tag('div')
         container:tag('div')
             :css({['float'] = 'right', ['margin-left'] = '15px'})
             :css({['float'] = 'right', ['margin-left'] = '15px'})
             :wikitext('[[' .. firstImage .. '|120px|border]]')
             :wikitext('[[' .. firstImage .. '|120px|border|link=' .. pageName .. ']]')
     end
     end


     container:tag('div')
     container:tag('div')
         :css({['margin-bottom'] = '5px', ['font-weight'] = 'bold', ['font-size'] = '1.2em'})
         :css({['font-weight'] = 'bold', ['font-size'] = '1.2em', ['margin-bottom'] = '8px'})
         :wikitext('[[' .. pageName .. ']]')
         :wikitext('[[' .. pageName .. ']]')


     container:tag('div')
     container:tag('div')
         :css({['line-height'] = '1.5'})
         :css({['line-height'] = '1.6'})
         :wikitext(summary)
         :wikitext(summary)



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

此模块的文档可以在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", " ")

    -- 3. 智能截斷邏輯:如果 100 字處於連結中間,則延展到連結結束
    local targetLen = 100
    local summary = ""
    local fullLen = mw.ustring.len(cleanText)

    if fullLen <= targetLen then
        summary = cleanText
    else
        summary = mw.ustring.sub(cleanText, 1, targetLen)
        
        -- 檢查是否截斷在了連結 [[...]] 中間
        local restText = mw.ustring.sub(cleanText, targetLen + 1)
        
        -- 判斷 summary 中最後一個 [[ 是否有對應的 ]]
        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

        -- 檢查粗體 ''' 的處理 (同理)
        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

        summary = summary .. "..."
    end

    -- 4. 構建輸出容器
    local container = mw.html.create('div')
        :css({
            ['border'] = '1px solid #a2a9b1',
            ['background-color'] = '#f8f9fa',
            ['padding'] = '15px',
            ['margin'] = '10px 0',
            ['display'] = 'flow-root',
            ['border-radius'] = '4px'
        })

    if firstImage then
        container:tag('div')
            :css({['float'] = 'right', ['margin-left'] = '15px'})
            :wikitext('[[' .. firstImage .. '|120px|border|link=' .. pageName .. ']]')
    end

    container:tag('div')
        :css({['font-weight'] = 'bold', ['font-size'] = '1.2em', ['margin-bottom'] = '8px'})
        :wikitext('[[' .. pageName .. ']]')

    container:tag('div')
        :css({['line-height'] = '1.6'})
        :wikitext(summary)

    return tostring(container)
end

return p