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

Module:PageSummary:修订间差异

武外梗百科 爱国好学自强图新的百科全书
无编辑摘要
无编辑摘要
第3行: 第3行:
local function stripImages(text)
local function stripImages(text)
     if not text or text == "" then return "" end
     if not text or text == "" then return "" end
   
    -- 模式全部重新写,避免复制隐藏字符
     text = string.gsub(text, "%[%[(文件|Image|图像):", "[[File:")
     text = string.gsub(text, "%[%[(文件|Image|图像):", "[[File:")
     text = string.gsub(text, "%[%[(分类|Category):", "[[Category:")
     text = string.gsub(text, "%[%[(分类|Category):", "[[Category:")
   
     local out = {}
     local out = {}
     local i = 1
     local i = 1
     local len = #text
     local len = #text
   
     while i <= len do
     while i <= len do
         if text:byte(i) == 91 and text:byte(i+1) == 91 then
         if text:byte(i) == 91 and text:byte(i+1) == 91 then
第40行: 第36行:
         if i > 1200 then break end
         if i > 1200 then break end
     end
     end
   
     return table.concat(out)
     return table.concat(out)
end
end
第46行: 第41行:
local function cleanFinal(text)
local function cleanFinal(text)
     if not text or text == "" then return "" end
     if not text or text == "" then return "" end
   
    -- 所有模式都重新手写,使用 string.gsub(模式是纯 ASCII)
     text = string.gsub(text, "{{[^{}]*}}", "")
     text = string.gsub(text, "{{[^{}]*}}", "")
     text = string.gsub(text, "<ref.-</?ref[^>]*>", "")
     text = string.gsub(text, "<ref.-</?ref[^>]*>", "")
第55行: 第48行:
     text = string.gsub(text, "%s*\n%s*", " ")
     text = string.gsub(text, "%s*\n%s*", " ")
     text = string.gsub(text, "%s+", " ")
     text = string.gsub(text, "%s+", " ")
   
     return mw.text.trim(text)
     return mw.text.trim(text)
end
end
第62行: 第54行:
     local pageName = mw.text.trim(frame.args[1] or "")
     local pageName = mw.text.trim(frame.args[1] or "")
     if pageName == "" then return "" end
     if pageName == "" then return "" end
   
 
     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()
     if not content or content == "" then return "" end
     if not content or content == "" then return "" end
   
 
    -- 简单清洗 content,避免传入 gsub 的文本有问题
     content = string.gsub(content, "[\0-\31\127]", "")   -- 清理控制字符
     content = string.gsub(content, "[\0-\31\127]", "") -- 移除控制字符
     local rawExcerpt = mw.ustring.sub(content, 1, 800) or ""
     local rawExcerpt = mw.ustring.sub(content, 1, 800) or ""
   
 
     -- 提取第一张图片(使用 find + 手动截取)
     -- 提取第一张图片
     local firstImage = nil
     local firstImage = nil
     local prefixes = {"File:", "文件:"}
     local prefixes = {"File:", "文件:"}
     for _, prefix in ipairs(prefixes) do
     for _, prefix in ipairs(prefixes) do
         local pattern = "%[%[" .. prefix
         local start = mw.ustring.find(rawExcerpt, "%[%[" .. prefix)
        local start = mw.ustring.find(rawExcerpt, pattern)
         if start then
         if start then
             local pipePos = mw.ustring.find(rawExcerpt, "|", start)
             local pipe = mw.ustring.find(rawExcerpt, "|", start)
             local endPos = mw.ustring.find(rawExcerpt, "%]%]", start)
             local close = mw.ustring.find(rawExcerpt, "%]%]", start)
             local boundary = pipePos or endPos
             local boundary = pipe or close
             if boundary then
             if boundary then
                 local candidate = mw.ustring.sub(rawExcerpt, start + #pattern + 1, boundary - 1)
                 local candidate = mw.ustring.sub(rawExcerpt, start + #prefix + 1, boundary - 1)
                 candidate = mw.text.trim(candidate)
                 candidate = mw.text.trim(candidate)
                 if candidate ~= "" and mw.ustring.find(candidate, "%.") then
                 if candidate ~= "" and mw.ustring.find(candidate, "%.") then
第93行: 第83行:
         end
         end
     end
     end
   
 
     local step1    = stripImages(rawExcerpt) or ""
     local step1    = stripImages(rawExcerpt) or ""
     local cleanText = cleanFinal(step1) or ""
     local cleanText = cleanFinal(step1) or ""
      
 
     -- 摘要截断(安全版)
     local targetLen = 120
     local targetLen = 120
     local summary = cleanText
     local summary = cleanText
   
 
     local ulen = mw.ustring.len(summary) or 0
     local ulen = mw.ustring.len(summary) or 0
     if ulen > targetLen then
     if ulen > targetLen then
         local temp = mw.ustring.sub(summary, 1, targetLen + 20)
         local temp = mw.ustring.sub(summary, 1, targetLen + 25)
       
 
         local breakPos = nil
         local breakPos = nil
         for i = mw.ustring.len(temp), 1, -1 do
         for i = mw.ustring.len(temp), 1, -1 do
             local char = mw.ustring.sub(temp, i, i)
             local ch = mw.ustring.sub(temp, i, i)
             if char:match("[ %s,。!?;:,…、]") then
            -- 安全断点判断(避免复杂字符类)
             if ch == " " or ch == "," or ch == "。" or ch == "!" or ch == "?" or ch == ";" or ch == ":" or ch == "、" or ch == "" then
                 breakPos = i
                 breakPos = i
                 break
                 break
             end
             end
         end
         end
       
 
         if breakPos and breakPos > targetLen - 30 then
         if breakPos and breakPos > targetLen - 30 then
             summary = mw.ustring.sub(temp, 1, breakPos)
             summary = mw.ustring.sub(temp, 1, breakPos)
第118行: 第110行:
             summary = mw.ustring.sub(temp, 1, targetLen)
             summary = mw.ustring.sub(temp, 1, targetLen)
         end
         end
       
 
         summary = mw.text.trim(summary) .. "…"
         summary = mw.text.trim(summary) .. "…"
     else
     else
         summary = mw.text.trim(summary)
         summary = mw.text.trim(summary)
     end
     end
      
 
     -- HTML 输出
     local container = mw.html.create('div')
     local container = mw.html.create('div')
         :css('margin-bottom', '25px')
         :css('margin-bottom', '25px')
         :css('display', 'flow-root')
         :css('display', 'flow-root')
   
 
     container:tag('div')
     container:tag('div')
         :css('font-size', '1.3em')
         :css('font-size', '1.3em')
第134行: 第127行:
         :css('border-bottom', '1px solid #eee')
         :css('border-bottom', '1px solid #eee')
         :wikitext('[[' .. pageName .. ']]')
         :wikitext('[[' .. pageName .. ']]')
   
 
     local textDiv = container:tag('div')
     local textDiv = container:tag('div')
         :css('line-height', '1.6')
         :css('line-height', '1.6')
         :css('color', '#222')
         :css('color', '#222')
         :css('font-size', '14px')
         :css('font-size', '14px')
   
 
     if firstImage and firstImage ~= "" then
     if firstImage and firstImage ~= "" then
         textDiv:wikitext('[[' .. firstImage .. '|120px|right|link=' .. pageName .. ']]')
         textDiv:wikitext('[[' .. firstImage .. '|120px|right|link=' .. pageName .. ']]')
     end
     end
   
 
     textDiv:wikitext(summary)
     textDiv:wikitext(summary)
   
 
     return tostring(container)
     return tostring(container)
end
end


return p
return p