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 | ||
text = string.gsub(text, "<ref.-</ref>", "") | text = string.gsub(text, "<ref.-</ref>", "") | ||
text = string.gsub(text, "<ref.->", "") | text = string.gsub(text, "<ref.->", "") | ||
text = string.gsub(text, "<!%-%-.-%-%->", "") | text = string.gsub(text, "<!%-%-.-%-%->", "") | ||
local imagePrefixes = {"[Ff]ile:", "[Ii]mage:", "文件:", "图像:"} | local imagePrefixes = {"[Ff]ile:", "[Ii]mage:", "文件:", "图像:"} | ||
for _, prefix in ipairs(imagePrefixes) do | for _, prefix in ipairs(imagePrefixes) do | ||
text = string.gsub(text, "%[%[" .. prefix .. ".-%]%]", "") | text = string.gsub(text, "%[%[" .. prefix .. ".-%]%]", "") | ||
end | end | ||
for i = 1, 5 do | |||
for i = 1, | |||
local newText = string.gsub(text, "{{[^{}]+}}", "") | local newText = string.gsub(text, "{{[^{}]+}}", "") | ||
if newText == text then break end | if newText == text then break end | ||
| 第26行: | 第18行: | ||
end | end | ||
text = string.gsub(text, "\n==+.-==+", " ") | text = string.gsub(text, "\n==+.-==+", " ") | ||
text = string.gsub(text, "\n%s*[*#:]+", " ") | text = string.gsub(text, "\n%s*[*#:]+", " ") | ||
| 第32行: | 第23行: | ||
text = string.gsub(text, "\n", " ") | text = string.gsub(text, "\n", " ") | ||
text = string.gsub(text, "%s+", " ") | text = string.gsub(text, "%s+", " ") | ||
return mw.text.trim(text) | return mw.text.trim(text) | ||
end | end | ||
| 第41行: | 第31行: | ||
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() | ||
local rawExcerpt = mw.ustring.sub(content, 1, | 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 | ||
string.match(rawExcerpt, "%[%[(文件:.-)[|%]]") | string.match(rawExcerpt, "%[%[(文件:.-)[|%]]") | ||
local cleanText = safeStrip(rawExcerpt) | local cleanText = safeStrip(rawExcerpt) | ||
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 | |||
local _, opens = string.gsub(summary, "%[%[", "") | local _, opens = string.gsub(summary, "%[%[", "") | ||
local _, closes = string.gsub(summary, "%]%]", "") | local _, closes = string.gsub(summary, "%]%]", "") | ||
if opens > closes then | if opens > closes then | ||
local rest = mw.ustring.sub(cleanText, targetLen + 1, targetLen + | 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) | summary = summary .. string.sub(rest, 1, endLink + 1) | ||
else | |||
local lastOpen = string.find(summary:reverse(), "%[%[") | |||
if lastOpen then summary = string.sub(summary, 1, #summary - lastOpen - 1) end | |||
end | end | ||
end | end | ||
if mw.ustring.len(cleanText) > targetLen then | summary = mw.text.trim(summary) | ||
if mw.ustring.len(cleanText) > targetLen then summary = summary .. "..." end | |||
local container = mw.html.create('div'):css({['margin']='15px 0', ['display']='flow-root'}) | local container = mw.html.create('div'):css({['margin']='15px 0', ['display']='flow-root'}) | ||
if firstImage then | if firstImage then | ||