打开/关闭搜索
搜索
打开/关闭菜单
60
73
29
1.1K
武外梗百科
导航
首页
最近更改
随机页面
站务
其他
友链
特殊页面
打开/关闭外观设置菜单
通知
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。
user-interface-preferences
个人工具
讨论
贡献
创建账号
登录
编辑“︁
Module:PageSummary
”︁
武外梗百科 爱国好学自强图新的百科全书
查看
阅读
编辑
查看历史
associated-pages
模块
讨论
更多操作
Zelda110
(
留言
|
贡献
)
2025年12月21日 (日) 17:43的版本
(
差异
)
←上一版本
|
最后版本
(
差异
) |
下一版本→
(
差异
)
警告:您正在编辑该页面的旧版本。
如果您发布该更改,该版本后的所有更改都会丢失。
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
local p = {} -- 核心递归清理函数:通过平衡括号计数,彻底移除 [[File: ... ]] 或 {{模板}} 及其嵌套内容 local function stripBalancedTags(text, openTag, closeTag) if not text then return "" end local result = text while true do local startPos = string.find(result, openTag, 1, true) if not startPos then break end local count = 0 local endPos = nil local len = mw.ustring.len(result) for j = startPos, len do local char2 = mw.ustring.sub(result, j, j + 1) if char2 == openTag then count = count + 1 elseif char2 == closeTag then count = count - 1 if count == 0 then endPos = j + 1 break end end end if endPos then result = mw.ustring.sub(result, 1, startPos - 1) .. mw.ustring.sub(result, endPos + 1) else -- 容错:若未闭合则移除起始符防止死循环 result = mw.ustring.sub(result, 1, startPos - 1) .. mw.ustring.sub(result, startPos + 2) end end return result end function p.getSummaryAndImage(frame) local pageName = frame.args[1] or "" local title = mw.title.new(pageName) if not title or not title.exists then return "页面不存在" end local content = title:getContent() -- 1. 提取第一张图(用于单独显示) local firstImage = string.match(content, "%[%[([Ff]ile:.-)[|%]]") or string.match(content, "%[%[([Ii]mage:.-)[|%]]") or string.match(content, "%[%[(文件:.-)[|%]]") or string.match(content, "%[%[(图像:.-)[|%]]") -- 2. 处理内容:预加载 500 字符 local rawText = mw.ustring.sub(content, 1, 500) -- A. 剥离图片和模板(递归处理嵌套链接和长描述) rawText = stripBalancedTags(rawText, "[[", "]]") -- 移除所有链接(含图片),后文再截断 -- 注意:因为上面已经移除了所有 [[ ]],摘要里的链接会暂时丢失。 -- 如果要保留链接,我们需要更复杂的正则。但为了“删干净”图片描述, -- 我们重新从 content 获取一个不含图片但保留链接的版本。 -- 重新逻辑:仅针对图片前缀进行剥离,保留普通链接 local function stripOnlyImages(text) local prefixes = {"[[File:", "[[Image:", "[[文件:", "[[图像:", "[[file:", "[[image:"} for _, pre in ipairs(prefixes) do while string.find(text, pre, 1, true) do local s = string.find(text, pre, 1, true) local count = 0 local e = 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 return text end local processedText = mw.ustring.sub(content, 1, 500) processedText = stripOnlyImages(processedText) -- 仅删图片,保留链接 processedText = stripBalancedTags(processedText, "{{", "}}") -- 删模板 -- B. 移除格式符号 processedText = string.gsub(processedText, "\n==+.-==+", " ") -- 删标题 processedText = string.gsub(processedText, "\n%s*[*#:]+", " ") -- 删列表符 processedText = string.gsub(processedText, "<ref.-</ref>", "") processedText = string.gsub(processedText, "<ref.->", "") processedText = string.gsub(processedText, "<!%-%-.-%-%->", "") processedText = string.gsub(processedText, "&[Nn][Bb][Ss][Pp];", " ") processedText = string.gsub(processedText, "\n", " ") processedText = string.gsub(processedText, "%s+", " ") local cleanText = mw.text.trim(processedText) -- 3. 截断逻辑:150 字并处理链接补全 local targetLen = 150 local summary = "" if mw.ustring.len(cleanText) <= targetLen then summary = cleanText else summary = mw.ustring.sub(cleanText, 1, targetLen) local restText = mw.ustring.sub(cleanText, targetLen + 1) -- 链接延展 local lastOpen = 0 local lastClose = 0 local tempPos = 0 while true do local found = string.find(summary, "%[%[", tempPos + 1) if not found then break end lastOpen = found tempPos = found end tempPos = 0 while true do local found = string.find(summary, "%]%]", tempPos + 1) if not found then break end lastClose = found tempPos = found end if lastOpen > lastClose then local endOfLink = string.find(restText, "%]%]") if endOfLink then summary = summary .. string.sub(restText, 1, endOfLink + 1) end end summary = summary .. "..." end -- 4. 渲染 local container = mw.html.create('div'):css({['margin']='15px 0', ['display']='flow-root'}) if firstImage then container:wikitext('[[' .. firstImage .. '|150px|right|link=' .. pageName .. ']]') end container:tag('div') :css({['font-size']='1.4em', ['font-weight']='bold', ['margin-bottom']='8px'}) :wikitext('[[' .. pageName .. ']]') container:tag('div') :css({['line-height']='1.6', ['color']='#202122'}) :wikitext(summary) return tostring(container) end return p
摘要:
请注意,所有对武外梗百科的贡献均可能会被其他贡献者编辑、修改或删除。如果您不希望您的文字作品被随意编辑,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源(详情请见
武外梗百科:著作权
)。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)
预览使用本模板的页面
该页面使用的模板:
Module:PageSummary/doc
(
编辑
)
编辑“︁
Module:PageSummary
”︁
武外梗百科 爱国好学自强图新的百科全书