Module:PageSummary:修订间差异
武外梗百科 爱国好学自强图新的百科全书
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第1行: | 第1行: | ||
local p = {} | local p = {} | ||
function p.getSummaryAndImage(frame) | function p.getSummaryAndImage(frame) | ||
-- | -- 获取条目内容 | ||
local pageTitle = frame.args[1] or mw.title.getCurrentTitle().text | |||
local pageTitle = | local title = mw.title.new(pageTitle) | ||
if not title or not title.exists then return "条目不存在" end | |||
local | local content = title:getContent() | ||
if not | -- 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 | end | ||
-- | -- 2. 自动清理并提取首段 | ||
local | local summary = content | ||
summary = mw.ustring.gsub(summary, "<ref[^>]*>.-</ref>", "") -- 删脚注 | |||
-- | summary = mw.ustring.gsub(summary, "{{[^{}]-}}", "") -- 删一级模板 | ||
summary = mw.ustring.gsub(summary, "{{[^{}]-}}", "") -- 删嵌套模板 | |||
local | summary = mw.ustring.gsub(summary, "%[%[([^%]]-)|?([^%]]-)%]%]", "%2") -- 提取链接文字 | ||
if | |||
-- 寻找第一个非空的段落 | |||
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 | end | ||
-- 3. | -- 3. 输出 UI | ||
local container = mw.html.create('div') | local container = mw.html.create('div'):cssText('display:flex; gap:10px; border:1px solid #aaa; padding:10px;') | ||
if image ~= "" then | if image ~= "" then | ||
container:tag('div'):wikitext(string.format('[[File:%s| | container:tag('div'):wikitext(string.format('[[File:%s|100px]]', image)) | ||
end | end | ||
container:tag('div'):tag('b'):wikitext(pageTitle):done():tag('p'):wikitext(firstPara .. "...") | |||
return tostring(container) | return tostring(container) | ||
end | end | ||
return p | return p | ||
2026年2月18日 (三) 13:06的版本
此模块的文档可以在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