Module:PageSummary:修订间差异
武外梗百科 爱国好学自强图新的百科全书
更多操作
无编辑摘要 |
无编辑摘要 标签:已被回退 |
||
| 第1行: | 第1行: | ||
local p = {} | local p = {} | ||
-- 辅助函数:安全清理标签 | |||
local function safeStrip(text) | local function safeStrip(text) | ||
if not text then return "" end | if not text then return "" end | ||
| 第7行: | 第8行: | ||
text = string.gsub(text, "<!%-%-.-%-%->", "") | text = string.gsub(text, "<!%-%-.-%-%->", "") | ||
local | -- 彻底剥离带长描述的图片链接(处理嵌套 [[ ]]) | ||
for _, | local prefixes = {"[[File:", "[[Image:", "[[文件:", "[[图像:", "[[file:", "[[image:"} | ||
text = string. | for _, pre in ipairs(prefixes) do | ||
while string.find(text, pre, 1, true) do | |||
local s = string.find(text, pre, 1, true) | |||
local count, e = 0, nil | |||
for j = s, mw.ustring.len(text) do | |||
local c2 = mw.ustring.sub(text, j, j+1) | |||
if c2 == "[[" then count = count + 1 | |||
elseif c2 == "]]" then | |||
count = count - 1 | |||
if count == 0 then e = j + 1 break end | |||
end | |||
end | |||
if e then text = mw.ustring.sub(text, 1, s - 1) .. mw.ustring.sub(text, e + 1) | |||
else text = mw.ustring.sub(text, 1, s - 1) .. mw.ustring.sub(text, s + 2) end | |||
end | |||
end | end | ||
-- 移除模板(限制5层嵌套,防止CPU跑满) | |||
for i = 1, 5 do | for i = 1, 5 do | ||
local | local nt = string.gsub(text, "{{[^{}]+}}", "") | ||
if | if nt == text then break end | ||
text = | text = nt | ||
end | end | ||
text = string.gsub(text, "\n==+.-==+", " ") | text = string.gsub(text, "\n==+.-==+", " ") -- 删标题符 | ||
text = string.gsub(text, "\n%s*[*#:]+", " ") | text = string.gsub(text, "\n%s*[*#:]+", " ") -- 删列表符 | ||
text = string.gsub(text, "&[Nn][Bb][Ss][Pp];", " ") | text = string.gsub(text, "&[Nn][Bb][Ss][Pp];", " ") | ||
text = string.gsub(text, "\n", " ") | text = string.gsub(text, "\n", " ") | ||
| 第29行: | 第45行: | ||
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 " | if not title or not title.exists then return "" end | ||
local content = title:getContent() | local content = title:getContent() | ||
local rawExcerpt = mw.ustring.sub(content, 1, 500) | local rawExcerpt = mw.ustring.sub(content, 1, 500) | ||
-- 提取第一张图 | |||
local firstImage = string.match(rawExcerpt, "%[%[([Ff]ile:.-)[|%]]") or | local firstImage = string.match(rawExcerpt, "%[%[([Ff]ile:.-)[|%]]") or | ||
string.match(rawExcerpt, "%[%[([Ii]mage:.-)[|%]]") or | string.match(rawExcerpt, "%[%[([Ii]mage:.-)[|%]]") or | ||
| 第40行: | 第57行: | ||
local cleanText = safeStrip(rawExcerpt) | local cleanText = safeStrip(rawExcerpt) | ||
-- 150字截断并处理链接残符 | |||
local targetLen = 150 | local targetLen = 150 | ||
local summary = mw.ustring.sub(cleanText, 1, targetLen) | local summary = mw.ustring.sub(cleanText, 1, targetLen) | ||
while mw.ustring.sub(summary, -1) == "[" do summary = mw.ustring.sub(summary, 1, -2) end | |||
while mw.ustring.sub(summary, -1) == "[" do | |||
local _, opens = string.gsub(summary, "%[%[", "") | local _, opens = string.gsub(summary, "%[%[", "") | ||
| 第52行: | 第67行: | ||
local rest = mw.ustring.sub(cleanText, targetLen + 1, targetLen + 100) | local rest = mw.ustring.sub(cleanText, targetLen + 1, targetLen + 100) | ||
local endLink = string.find(rest, "]]", 1, true) | local endLink = string.find(rest, "]]", 1, true) | ||
if endLink then | if endLink then summary = summary .. string.sub(rest, 1, endLink + 1) | ||
else local lastOpen = string.find(summary:reverse(), "%[%[") | |||
else | if lastOpen then summary = string.sub(summary, 1, #summary - lastOpen - 1) end | ||
end | end | ||
end | end | ||
| 第63行: | 第76行: | ||
if mw.ustring.len(cleanText) > targetLen then summary = summary .. "..." end | if mw.ustring.len(cleanText) > targetLen then summary = summary .. "..." end | ||
local container = mw.html.create('div'):css({['margin']=' | -- 渲染 | ||
local container = mw.html.create('div'):css({['margin-bottom']='25px', ['display']='flow-root'}) | |||
if firstImage then | if firstImage then | ||
container:wikitext('[[' .. firstImage .. '|150px|right|link=' .. pageName .. ']]') | container:wikitext('[[' .. firstImage .. '|150px|right|link=' .. pageName .. ']]') | ||
end | end | ||
container:tag('div') | container:tag('div') | ||
:css({['font-size']='1. | :css({['font-size']='1.3em', ['font-weight']='bold', ['margin-bottom']='6px'}) | ||
:wikitext('[[' .. pageName .. ']]') | :wikitext('[[' .. pageName .. ']]') | ||
container:tag('div') | container:tag('div') | ||
:css({['line-height']='1.6', ['color']='#202122'}) | :css({['line-height']='1.6', ['color']='#202122', ['font-size']='0.95em'}) | ||
:wikitext(summary) | :wikitext(summary) | ||