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. 提取第一张图(用于单独显示) | ||
-- 匹配 [[File:名称...]] | |||
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 | ||
| 第13行: | 第15行: | ||
string.match(content, "%[%[(图像:.-)[|%]]") | string.match(content, "%[%[(图像:.-)[|%]]") | ||
-- 2. | -- 2. 彻底清理正文 | ||
local cleanText = content | local cleanText = content | ||
-- A. 移除所有图片链接,防止摘要中出现多余图片或图片残留的链接文本 | |||
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, "%[%[图像:.-%]%]", "") | ||
-- B. 移除模板、注释、引用 | |||
cleanText = string.gsub(cleanText, "<ref.-</ref>", "") | |||
cleanText = string.gsub(cleanText, "<ref.->", "") | |||
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 | ||
| 第27行: | 第32行: | ||
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. 智能截断:遇链接则延展 | |||
local targetLen = 100 | local targetLen = 100 | ||
local summary = "" | local summary = "" | ||
local fullLen = mw.ustring.len(cleanText) | |||
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) | ||
-- | |||
-- 链接延展逻辑 | |||
local lastOpen = 0 | local lastOpen = 0 | ||
local lastClose = 0 | local lastClose = 0 | ||
| 第54行: | 第64行: | ||
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 summary = summary .. string.sub(restText, 1, endOfLink + 1) end | if endOfLink then | ||
summary = summary .. string.sub(restText, 1, endOfLink + 1) | |||
end | |||
end | end | ||
summary = summary .. "..." | summary = summary .. "..." | ||
end | end | ||
-- | -- 4. 维基百科原生风格渲染 | ||
-- 使用 display: flow-root 确保容器包裹住浮动的缩略图 | |||
-- | |||
local container = mw.html.create('div') | local container = mw.html.create('div') | ||
:css({ | :css({ | ||
['margin'] = '10px 0', | ['margin'] = '10px 0', | ||
[' | ['padding'] = '10px', | ||
['border'] = '1px solid #a2a9b1', | ['border'] = '1px solid #a2a9b1', | ||
[' | ['background-color'] = '#f8f9fa', | ||
['display'] = ' | ['display'] = 'flow-root' | ||
}) | }) | ||
-- | -- 直接渲染维基百科标准的缩略图 (右侧浮动) | ||
if firstImage then | if firstImage then | ||
container | container:wikitext('[[' .. firstImage .. '|thumb|150px|right|link=' .. pageName .. ']]') | ||
end | end | ||
-- | -- 标题 | ||
container:tag('div') | |||
:css({ | :css({ | ||
['font-size'] = '1.2em', | ['font-size'] = '1.2em', | ||
['font-weight'] = 'bold', | ['font-weight'] = 'bold', | ||
['margin-bottom'] = '5px', | ['margin-bottom'] = '5px', | ||
[' | ['border-bottom'] = '1px solid #a2a9b1', -- 维基标题下划线 | ||
['padding-bottom'] = '2px' | |||
}) | }) | ||
:wikitext('[[' .. pageName .. ']]') | :wikitext('[[' .. pageName .. ']]') | ||
-- | -- 摘要 | ||
container:tag('div') | |||
:css({ | :css({ ['line-height'] = '1.6' }) | ||
:wikitext(summary) | :wikitext(summary) | ||
2025年12月21日 (日) 17:29的版本
此模块的文档可以在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. 提取第一张图(用于单独显示)
-- 匹配 [[File:名称...]]
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
-- A. 移除所有图片链接,防止摘要中出现多余图片或图片残留的链接文本
cleanText = string.gsub(cleanText, "%[%[[Ff]ile:.-%]%]", "")
cleanText = string.gsub(cleanText, "%[%[[Ii]mage:.-%]%]", "")
cleanText = string.gsub(cleanText, "%[%[文件:.-%]%]", "")
cleanText = string.gsub(cleanText, "%[%[图像:.-%]%]", "")
-- B. 移除模板、注释、引用
cleanText = string.gsub(cleanText, "<ref.-</ref>", "")
cleanText = string.gsub(cleanText, "<ref.->", "")
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. 智能截断:遇链接则延展
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)
-- 链接延展逻辑
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. 维基百科原生风格渲染
-- 使用 display: flow-root 确保容器包裹住浮动的缩略图
local container = mw.html.create('div')
:css({
['margin'] = '10px 0',
['padding'] = '10px',
['border'] = '1px solid #a2a9b1',
['background-color'] = '#f8f9fa',
['display'] = 'flow-root'
})
-- 直接渲染维基百科标准的缩略图 (右侧浮动)
if firstImage then
container:wikitext('[[' .. firstImage .. '|thumb|150px|right|link=' .. pageName .. ']]')
end
-- 标题
container:tag('div')
:css({
['font-size'] = '1.2em',
['font-weight'] = 'bold',
['margin-bottom'] = '5px',
['border-bottom'] = '1px solid #a2a9b1', -- 维基标题下划线
['padding-bottom'] = '2px'
})
:wikitext('[[' .. pageName .. ']]')
-- 摘要
container:tag('div')
:css({ ['line-height'] = '1.6' })
:wikitext(summary)
return tostring(container)
end
return p