Module:PageSummary
武外梗百科 爱国好学自强图新的百科全书
更多操作
此模块的文档可以在Module:PageSummary/doc创建
local p = {}
function p.getSummaryAndImage(frame)
-- 获取条目内容
local pageTitle = frame.args[1] or mw.title.getCurrentTitle().text
local title = mw.title.new(pageTitle)
if not title or not title.exists then return "条目不存在" end
local content = title:getContent()
-- 1. 自动提取第一张图
local image = ""
for img in mw.ustring.gmatch(content, "%[%[%s*[Ff][Ii][Ll][Ee]%s*:([^|%]]+)") do
-- 过滤掉 SVG 图标
if not mw.ustring.match(img, "%.svg") then
image = mw.text.trim(img)
break
end
end
-- 2. 自动清理并提取首段
local summary = content
summary = mw.ustring.gsub(summary, "<ref[^>]*>.-</ref>", "") -- 删脚注
summary = mw.ustring.gsub(summary, "{{[^{}]-}}", "") -- 删一级模板
summary = mw.ustring.gsub(summary, "{{[^{}]-}}", "") -- 删嵌套模板
summary = mw.ustring.gsub(summary, "%[%[([^%]]-)|?([^%]]-)%]%]", "%2") -- 提取链接文字
-- 寻找第一个非空的段落
local _, firstPara = mw.ustring.match(summary, "\n\n%s*([^%*#\n][^\n]+)")
if not firstPara then
firstPara = mw.ustring.sub(summary, 1, 150) -- 保底截取
else
firstPara = mw.ustring.sub(firstPara, 1, 150)
end
-- 3. 输出 UI
local container = mw.html.create('div'):cssText('display:flex; gap:10px; border:1px solid #aaa; padding:10px;')
if image ~= "" then
container:tag('div'):wikitext(string.format('[[File:%s|100px]]', image))
end
container:tag('div'):tag('b'):wikitext(pageTitle):done():tag('p'):wikitext(firstPara .. "...")
return tostring(container)
end
return p