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

Module:PageSummary:修订间差异

武外梗百科 爱国好学自强图新的百科全书
无编辑摘要
无编辑摘要
 
(未显示18个用户的45个中间版本)
第1行: 第1行:
local p = {}
local p = {}


-- 核心递归清理函数:通过平衡括号计数,彻底移除 [[File: ... ]] 或 {{模板}} 及其嵌套内容
-- 強化版清理:徹底根除內文圖片,保留加粗和連結
local function stripBalancedTags(text, openTag, closeTag)
local function cleanContent(text)
     if not text then return "" end
     if not text then return "" end
     local result = text
 
     -- 1. 基礎清理(注釋、腳注、表格、模板)
    text = mw.ustring.gsub(text, "<!%-%-.-%-%->", "")
    text = mw.ustring.gsub(text, "<ref[^>]*>.-</ref>", "")
    text = mw.ustring.gsub(text, "<ref[^>]-/>", "")
      
      
     while true do
     -- 移除表格 {| ... |}
         local startPos = string.find(result, openTag, 1, true)
    local prev
        if not startPos then break end
    repeat
       
        prev = text
        local count = 0
         text = mw.ustring.gsub(text, "{|[^{}]*|}", "")
         local endPos = nil
    until text == prev
         local len = mw.ustring.len(result)
 
          
    -- 移除模板 {{ ... }}
         for j = startPos, len do
    local count = 0
            local char2 = mw.ustring.sub(result, j, j + 1)
    repeat
             if char2 == openTag then
         prev = text
                count = count + 1
         text = mw.ustring.gsub(text, "{{[^{}]-}}", "")
             elseif char2 == closeTag then
         count = count + 1
                count = count - 1
    until text == prev or count > 10
                if count == 0 then
 
                    endPos = j + 1
    -- 2. 使用安全占位符保護連結,剔除圖片
                    break
    repeat
                 end
         prev = text
        text = mw.ustring.gsub(text, "%[%[%s*([^%[%]]-)%s*%]%]", function(inner)
             local low = mw.ustring.lower(inner)
             if low:match("^file:") or low:match("^image:") or
              low:match("^文件:") or low:match("^圖像:") or
              low:match("^category:") or low:match("^分類:") then
                 return ""
             end
             end
         end
            return "LINKSTART" .. inner .. "LINKEND"
       
         end)
        if endPos then
    until text == prev
            result = mw.ustring.sub(result, 1, startPos - 1) .. mw.ustring.sub(result, endPos + 1)
 
        else
    -- 3. 清理剩餘雜質
            -- 容错:若未闭合则移除起始符防止死循环
    text = mw.ustring.gsub(text, "\n==+.-==+", " ")
            result = mw.ustring.sub(result, 1, startPos - 1) .. mw.ustring.sub(result, startPos + 2)
    text = mw.ustring.gsub(text, "\n%s*[*#:]+", " ")
        end
    text = mw.ustring.gsub(text, "\n+", " ")
    end
    text = mw.ustring.gsub(text, "%s+", " ")
     return result
 
    -- 4. 初步恢復連結標籤
    text = mw.ustring.gsub(text, "LINKSTART", "[[")
    text = mw.ustring.gsub(text, "LINKEND", "]]")
 
     return mw.text.trim(text)
end
end


第40行: 第55行:
     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()


     -- 1. 提取第一张图(用于单独显示)
     -- 【調整】讀取前 1000 字以確保跨過開頭的表格/模板區
     local firstImage = string.match(content, "%[%[([Ff]ile:.-)[|%]]") or  
     local rawContent = title:getContent() or ""
                      string.match(content, "%[%[([Ii]mage:.-)[|%]]") or
    local limitedContent = mw.ustring.sub(rawContent, 1, 1000)
                      string.match(content, "%[%[(文件:.-)[|%]]") or
                      string.match(content, "%[%[(图像:.-)[|%]]")


     -- 2. 处理内容:预加载 500 字符
     -- 1. 提取第一張縮圖名
     local rawText = mw.ustring.sub(content, 1, 500)
     local firstImage = mw.ustring.match(limitedContent, "%[%[%s*[Ff]ile%s*:([^|%]%s\n]+)") or
   
                      mw.ustring.match(limitedContent, "%[%[%s*文件%s*:([^|%]%s\n]+)") or
    -- A. 剥离图片和模板(递归处理嵌套链接和长描述)
                      mw.ustring.match(limitedContent, "%[%[%s*[Ii]mage%s*:([^|%]%s\n]+)")
    rawText = stripBalancedTags(rawText, "[[", "]]") -- 移除所有链接(含图片),后文再截断
    -- 注意:因为上面已经移除了所有 [[ ]],摘要里的链接会暂时丢失。
    -- 如果要保留链接,我们需要更复杂的正则。但为了“删干净”图片描述,
    -- 我们重新从 content 获取一个不含图片但保留链接的版本。
   
    -- 重新逻辑:仅针对图片前缀进行剥离,保留普通链接
    local function stripOnlyImages(text)
        local prefixes = {"[[File:", "[[Image:", "[[文件:", "[[图像:", "[[file:", "[[image:"}
        for _, pre in ipairs(prefixes) do
            while string.find(text, pre, 1, true) do
                local s = string.find(text, pre, 1, true)
                local count = 0
                local e = nil
                for j = s, mw.ustring.len(text) do
                    local c2 = mw.ustring.sub(text, j, j+1)
                    if c2 == "[[" then count = count + 1
                    elseif c2 == "]]" then
                        count = count - 1
                        if count == 0 then e = j + 1 break end
                    end
                end
                if e then text = mw.ustring.sub(text, 1, s - 1) .. mw.ustring.sub(text, e + 1)
                else text = mw.ustring.sub(text, 1, s - 1) .. mw.ustring.sub(text, s + 2) end
            end
        end
        return text
    end
   
    local processedText = mw.ustring.sub(content, 1, 500)
    processedText = stripOnlyImages(processedText) -- 仅删图片,保留链接
    processedText = stripBalancedTags(processedText, "{{", "}}") -- 删模板


     -- B. 移除格式符号
     -- 2. 執行清理(此時會得到過濾掉干擾後的純文字)
     processedText = string.gsub(processedText, "\n==+.-==+", " ") -- 删标题
     local cleanText = cleanContent(limitedContent)
    processedText = string.gsub(processedText, "\n%s*[*#:]+", " ") -- 删列表符
    processedText = string.gsub(processedText, "<ref.-</ref>", "")
    processedText = string.gsub(processedText, "<ref.->", "")
    processedText = string.gsub(processedText, "<!%-%-.-%-%->", "")
    processedText = string.gsub(processedText, "&[Nn][Bb][Ss][Pp];", " ")
    processedText = string.gsub(processedText, "\n", " ")
    processedText = string.gsub(processedText, "%s+", " ")
      
      
    local cleanText = mw.text.trim(processedText)
     -- 3. 【調整】最終截取約 80 字
 
     local targetLen = 80
     -- 3. 截断逻辑:150 字并处理链接补全
     local targetLen = 150
     local summary = ""
     local summary = ""
   
     if mw.ustring.len(cleanText) <= targetLen then
     if mw.ustring.len(cleanText) <= targetLen then
         summary = cleanText
         summary = cleanText
     else
     else
         summary = mw.ustring.sub(cleanText, 1, targetLen)
        -- 截取前 80 字並加上省略號
        local restText = mw.ustring.sub(cleanText, targetLen + 1)
         summary = mw.ustring.sub(cleanText, 1, targetLen) .. "..."
          
          
         -- 链接延展
         -- 閉合加粗標籤 '''
         local lastOpen = 0
         local _, opens = mw.ustring.gsub(summary, "'''", "")
         local lastClose = 0
         if opens % 2 ~= 0 then summary = summary .. "'''" end
         local tempPos = 0
          
         while true do
         -- 閉合或清理截斷的連結 [[...
            local found = string.find(summary, "%[%[", tempPos + 1)
        if mw.ustring.match(summary, "%[%[[^%]]*$") then
            if not found then break end
             summary = mw.ustring.gsub(summary, "%[%[[^%]]*$", "") .. "..."
             lastOpen = found
            tempPos = found
         end
         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
     end


     -- 4. 渲染
     -- 4. 渲染 HTML
     local container = mw.html.create('div'):css({['margin']='15px 0', ['display']='flow-root'})
     local res = mw.html.create('div'):css({
        ['display'] = 'flow-root',
        ['line-height'] = '1.5',
        ['margin-bottom'] = '1em' -- 也可以通过这里控制整个组件下方的间距
    })
   
    -- 标题
    res:tag('div')
      :css({['font-size'] = '1.15em', ['font-weight'] = 'bold', ['margin-bottom'] = '4px'})
      :wikitext('[[' .. pageName .. ']]')


    -- 右侧缩略图
     if firstImage then
     if firstImage then
         container:wikitext('[[' .. firstImage .. '|150px|right|link=' .. pageName .. ']]')
         res:wikitext('[[File:' .. firstImage .. '|100px|right|link=' .. pageName .. ']]')
     end
     end


     container:tag('div')
     -- 摘要正文:在末尾增加空行
        :css({['font-size']='1.4em', ['font-weight']='bold', ['margin-bottom']='8px'})
    -- 方法:在 summary 字符串后直接加上 <br /> 或 \n\n
        :wikitext('[[' .. pageName .. ']]')
     res:tag('div')
 
      :wikitext(summary .. '<br /><br />')  
     container:tag('div')
        :css({['line-height']='1.6', ['color']='#202122'})
        :wikitext(summary)


     return tostring(container)
     return tostring(res)
end
end


return p
return p