Module:PageSummary:修订间差异
武外梗百科 爱国好学自强图新的百科全书
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第1行: | 第1行: | ||
local p = {} | local p = {} | ||
-- | -- 供其他 Lua 模块调用的内部函数 | ||
function p._getSummary(pageName) | |||
if not | if not pageName or pageName == "" then | ||
return "<span class='error'>请输入有效的条目名称。</span>" | |||
end | 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 "<span class='error'>条目“" .. pageName .. "”不存在。</span>" | |||
end | |||
local content = title:getContent() | local content = title:getContent() | ||
if not content | if not content then | ||
return "<span class='error'>无法获取条目内容。</span>" | |||
end | |||
-- | -- 1. 提取首张图片 (匹配常用图片命名空间) | ||
content | -- 注意:不区分大小写,使用简单的正则匹配查找最早出现的文件 | ||
local firstImage = mw.ustring.match(content, "%[%[%s*[Ff][Ii][Ll][Ee]%s*:([^|%]]+)") | |||
or mw.ustring.match(content, "%[%[%s*[Ii][Mm][Aa][Gg][Ee]%s*:([^|%]]+)") | |||
or mw.ustring.match(content, "%[%[%s*文件%s*:([^|%]]+)") | |||
or mw.ustring.match(content, "%[%[%s*檔案%s*:([^|%]]+)") | |||
or mw.ustring.match(content, "%[%[%s*图片%s*:([^|%]]+)") | |||
or mw.ustring.match(content, "%[%[%s*图像%s*:([^|%]]+)") | |||
-- | -- 2. 纯文本清理流程 | ||
local | local text = content | ||
-- 屏蔽重定向页面 | |||
if mw.ustring.match(text, "^%s*#REDIRECT") or mw.ustring.match(text, "^%s*#重定向") then | |||
return "该页面是一个重定向页面。" | |||
end | end | ||
-- 去除 HTML 注释 | |||
text = mw.ustring.gsub(text, "<!%-%-.-%-%->", "") | |||
-- 去除部分可能干扰正文的 HTML 标签及其内容 | |||
text = mw.ustring.gsub(text, "<ref[^>]*>.-</ref>", "") | |||
text = mw.ustring.gsub(text, "<ref[^>]-/>", "") | |||
if | text = mw.ustring.gsub(text, "<math[^>]*>.-</math>", "") | ||
text = mw.ustring.gsub(text, "<gallery[^>]*>.-</gallery>", "") | |||
text = mw.ustring.gsub(text, "<div[^>]*>.-</div>", "") | |||
text = mw.ustring.gsub(text, "<table[^>]*>.-</table>", "") | |||
-- 循环去除模板 {{ ... }} (添加计数器防死循环) | |||
local prev, count | |||
count = 0 | |||
repeat | |||
prev = text | |||
text = mw.ustring.gsub(text, "{{[^{}]-}}", "") | |||
count = count + 1 | |||
until text == prev or count > 50 | |||
-- 循环去除表格 {| ... |} | |||
count = 0 | |||
repeat | |||
prev = text | |||
text = mw.ustring.gsub(text, "{|.-|}", "") | |||
count = count + 1 | |||
until text == prev or count > 30 | |||
-- 去除章节标题 == 标题 == | |||
text = mw.ustring.gsub(text, "==+.-==+", "") | |||
-- 核心逻辑:由内向外剥离中括号 [[ ... ]] | |||
-- 这样可以安全处理诸如 [[File:xx.jpg|thumb|这是一个[[内链]]]] 的嵌套情况 | |||
count = 0 | |||
repeat | |||
prev = text | |||
text = mw.ustring.gsub(text, "%[%[([^%[%]]-)%]%]", function(inner) | |||
local upperInner = mw.ustring.upper(inner) | |||
-- 如果是文件、图片或分类,直接整块抹除 | |||
if mw.ustring.match(upperInner, "^%s*FILE%s*:") or | |||
mw.ustring.match(upperInner, "^%s*IMAGE%s*:") or | |||
mw.ustring.match(inner, "^%s*文件%s*:") or | |||
mw.ustring.match(inner, "^%s*檔案%s*:") or | |||
mw.ustring.match(inner, "^%s*图片%s*:") or | |||
mw.ustring.match(inner, "^%s*图像%s*:") or | |||
mw.ustring.match(upperInner, "^%s*CATEGORY%s*:") or | |||
mw.ustring.match(inner, "^%s*分类%s*:") or | |||
mw.ustring.match(inner, "^%s*分類%s*:") then | |||
return "" | |||
end | end | ||
end | -- 如果是普通内链 [[链接|显示文本]],保留最后的显示文本 | ||
local parts = mw.text.split(inner, "|") | |||
return parts[#parts] | |||
end) | |||
count = count + 1 | |||
until text == prev or count > 50 | |||
-- 去除粗体和斜体 (''' 和 '') | |||
text = mw.ustring.gsub(text, "''+", "") | |||
-- 去除残留的单层 HTML 标签 (如 <br>, <span>) | |||
text = mw.ustring.gsub(text, "<[^>]+>", "") | |||
-- 合并多余的换行和空格 | |||
text = mw.ustring.gsub(text, "%s+", " ") | |||
-- 去除开头可能残留的乱码标点(因为移除了前面的信息框,这里容易留下逗号或右括号) | |||
text = mw.ustring.gsub(text, "^[%s,%.:;%?!)]】」》”’>|]+", "") | |||
text = mw.ustring.gsub(text, "^[,。、;:?!]+", "") | |||
text = mw.text.trim(text) | |||
-- | -- 3. 截取前约 100 个字符 (由于使用的是 mw.ustring,完美支持中文字符计算) | ||
local summary = mw.ustring.sub(text, 1, 100) | |||
local summary = | if mw.ustring.len(text) > 100 then | ||
summary = summary .. "..." | |||
summary = | |||
end | end | ||
-- | -- 4. 使用 mw.html 组装最终输出的 UI | ||
local container = mw.html.create('div') | local container = mw.html.create('div') | ||
: | :cssText('display: flex; gap: 15px; align-items: flex-start; padding: 15px; border: 1px solid #eaecf0; border-radius: 6px; background: #f8f9fa; margin-bottom: 1em;') | ||
if firstImage then | if firstImage then | ||
container:tag('div') | |||
:cssText('flex: 0 0 120px; border-radius: 4px; overflow: hidden;') | |||
:wikitext("[[File:" .. firstImage .. "|120px|frameless]]") | |||
end | end | ||
container:tag('div') | |||
:cssText('flex-grow: 1; font-size: 0.95em; line-height: 1.6; color: #202122;') | |||
:wikitext(summary) | |||
return tostring(container) | return tostring(container) | ||
end | |||
-- 供 Wikitext 中 {{#invoke:}} 调用的主函数 | |||
function p.main(frame) | |||
-- 优先获取模板传递的参数,如果没有,再尝试获取直接 Invoke 传递的参数 | |||
local args = frame:getParent().args | |||
if not args[1] or args[1] == "" then | |||
args = frame.args | |||
end | |||
return p._getSummary(args[1]) | |||
end | end | ||
return p | return p | ||
2026年2月18日 (三) 12:07的版本
此模块的文档可以在Module:PageSummary/doc创建
local p = {}
-- 供其他 Lua 模块调用的内部函数
function p._getSummary(pageName)
if not pageName or pageName == "" then
return "<span class='error'>请输入有效的条目名称。</span>"
end
local title = mw.title.new(pageName)
if not title or not title.exists then
return "<span class='error'>条目“" .. pageName .. "”不存在。</span>"
end
local content = title:getContent()
if not content then
return "<span class='error'>无法获取条目内容。</span>"
end
-- 1. 提取首张图片 (匹配常用图片命名空间)
-- 注意:不区分大小写,使用简单的正则匹配查找最早出现的文件
local firstImage = mw.ustring.match(content, "%[%[%s*[Ff][Ii][Ll][Ee]%s*:([^|%]]+)")
or mw.ustring.match(content, "%[%[%s*[Ii][Mm][Aa][Gg][Ee]%s*:([^|%]]+)")
or mw.ustring.match(content, "%[%[%s*文件%s*:([^|%]]+)")
or mw.ustring.match(content, "%[%[%s*檔案%s*:([^|%]]+)")
or mw.ustring.match(content, "%[%[%s*图片%s*:([^|%]]+)")
or mw.ustring.match(content, "%[%[%s*图像%s*:([^|%]]+)")
-- 2. 纯文本清理流程
local text = content
-- 屏蔽重定向页面
if mw.ustring.match(text, "^%s*#REDIRECT") or mw.ustring.match(text, "^%s*#重定向") then
return "该页面是一个重定向页面。"
end
-- 去除 HTML 注释
text = mw.ustring.gsub(text, "<!%-%-.-%-%->", "")
-- 去除部分可能干扰正文的 HTML 标签及其内容
text = mw.ustring.gsub(text, "<ref[^>]*>.-</ref>", "")
text = mw.ustring.gsub(text, "<ref[^>]-/>", "")
text = mw.ustring.gsub(text, "<math[^>]*>.-</math>", "")
text = mw.ustring.gsub(text, "<gallery[^>]*>.-</gallery>", "")
text = mw.ustring.gsub(text, "<div[^>]*>.-</div>", "")
text = mw.ustring.gsub(text, "<table[^>]*>.-</table>", "")
-- 循环去除模板 {{ ... }} (添加计数器防死循环)
local prev, count
count = 0
repeat
prev = text
text = mw.ustring.gsub(text, "{{[^{}]-}}", "")
count = count + 1
until text == prev or count > 50
-- 循环去除表格 {| ... |}
count = 0
repeat
prev = text
text = mw.ustring.gsub(text, "{|.-|}", "")
count = count + 1
until text == prev or count > 30
-- 去除章节标题 == 标题 ==
text = mw.ustring.gsub(text, "==+.-==+", "")
-- 核心逻辑:由内向外剥离中括号 [[ ... ]]
-- 这样可以安全处理诸如 [[File:xx.jpg|thumb|这是一个[[内链]]]] 的嵌套情况
count = 0
repeat
prev = text
text = mw.ustring.gsub(text, "%[%[([^%[%]]-)%]%]", function(inner)
local upperInner = mw.ustring.upper(inner)
-- 如果是文件、图片或分类,直接整块抹除
if mw.ustring.match(upperInner, "^%s*FILE%s*:") or
mw.ustring.match(upperInner, "^%s*IMAGE%s*:") or
mw.ustring.match(inner, "^%s*文件%s*:") or
mw.ustring.match(inner, "^%s*檔案%s*:") or
mw.ustring.match(inner, "^%s*图片%s*:") or
mw.ustring.match(inner, "^%s*图像%s*:") or
mw.ustring.match(upperInner, "^%s*CATEGORY%s*:") or
mw.ustring.match(inner, "^%s*分类%s*:") or
mw.ustring.match(inner, "^%s*分類%s*:") then
return ""
end
-- 如果是普通内链 [[链接|显示文本]],保留最后的显示文本
local parts = mw.text.split(inner, "|")
return parts[#parts]
end)
count = count + 1
until text == prev or count > 50
-- 去除粗体和斜体 (''' 和 '')
text = mw.ustring.gsub(text, "''+", "")
-- 去除残留的单层 HTML 标签 (如 <br>, <span>)
text = mw.ustring.gsub(text, "<[^>]+>", "")
-- 合并多余的换行和空格
text = mw.ustring.gsub(text, "%s+", " ")
-- 去除开头可能残留的乱码标点(因为移除了前面的信息框,这里容易留下逗号或右括号)
text = mw.ustring.gsub(text, "^[%s,%.:;%?!)]】」》”’>|]+", "")
text = mw.ustring.gsub(text, "^[,。、;:?!]+", "")
text = mw.text.trim(text)
-- 3. 截取前约 100 个字符 (由于使用的是 mw.ustring,完美支持中文字符计算)
local summary = mw.ustring.sub(text, 1, 100)
if mw.ustring.len(text) > 100 then
summary = summary .. "..."
end
-- 4. 使用 mw.html 组装最终输出的 UI
local container = mw.html.create('div')
:cssText('display: flex; gap: 15px; align-items: flex-start; padding: 15px; border: 1px solid #eaecf0; border-radius: 6px; background: #f8f9fa; margin-bottom: 1em;')
if firstImage then
container:tag('div')
:cssText('flex: 0 0 120px; border-radius: 4px; overflow: hidden;')
:wikitext("[[File:" .. firstImage .. "|120px|frameless]]")
end
container:tag('div')
:cssText('flex-grow: 1; font-size: 0.95em; line-height: 1.6; color: #202122;')
:wikitext(summary)
return tostring(container)
end
-- 供 Wikitext 中 {{#invoke:}} 调用的主函数
function p.main(frame)
-- 优先获取模板传递的参数,如果没有,再尝试获取直接 Invoke 传递的参数
local args = frame:getParent().args
if not args[1] or args[1] == "" then
args = frame.args
end
return p._getSummary(args[1])
end
return p