Module:PageSummary
武外梗百科 爱国好学自强图新的百科全书
更多操作
此模块的文档可以在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. 处理内容:预加载 1000 字符
local rawText = mw.ustring.sub(content, 1, 1000)
-- A. 高级清理图片:处理带有长描述和嵌套链接的图片
-- 循环匹配 [[File: ... ]] 结构,尽量处理嵌套情况
local function stripImages(text)
-- 匹配 [[(File|文件|Image|图像): ... ]]
-- 此正则通过排除法尽可能多地包含字符,直到匹配到对应的闭合括号
local patterns = {"%[%[[Ff]ile:.-%]%]", "%[%[[Ii]mage:.-%]%]", "%[%[文件:.-%]%]", "%[%[图像:.-%]%]" }
for _, pat in ipairs(patterns) do
while string.find(text, pat) do
text = string.gsub(text, pat, "")
end
end
return text
end
rawText = stripImages(rawText)
-- B. 移除 Wiki 列表符号 (*, #, :, ;)
rawText = string.gsub(rawText, "\n%s*[*#:]+", " ")
-- C. 移除标题符号 == 标题 ==
rawText = string.gsub(rawText, "\n==+.-==+", " ")
-- D. 移除模板 {{...}}
local limit = 15
while string.find(rawText, "{{.-}}") and limit > 0 do
rawText = string.gsub(rawText, "{{[^{}]+}}", "")
limit = limit - 1
end
-- E. 移除引用 <ref> 和 注释
rawText = string.gsub(rawText, "<ref.-</ref>", "")
rawText = string.gsub(rawText, "<ref.->", "")
rawText = string.gsub(rawText, "<!%-%-.-%-%->", "")
-- F. 清理多余的空白符、换行符和特殊占位符(如 )
rawText = string.gsub(rawText, " ", " ")
rawText = string.gsub(rawText, "\n", " ")
rawText = string.gsub(rawText, "%s+", " ")
local cleanText = mw.text.trim(rawText)
-- 3. 截断逻辑:150 字
local targetLen = 150
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'
})
-- 原生格式图片
if firstImage then
container:wikitext('[[' .. firstImage .. '|150px|right|link=' .. pageName .. ']]')
end
-- 标题
container:tag('div')
:css({
['font-size'] = '1.4em',
['font-weight'] = 'bold',
['margin-bottom'] = '8px'
})
:wikitext('[[' .. pageName .. ']]')
-- 摘要文本
container:tag('div')
:css({
['line-height'] = '1.6',
['color'] = '#202122'
})
:wikitext(summary)
return tostring(container)
end
return p