Module:Cite:修订间差异
武外梗百科 爱国好学自强图新的百科全书
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第9行: | 第9行: | ||
end | end | ||
-- | -- 和你给的示例一致:直接用 title + getContent | ||
local title = mw.title.new(page) | local title = mw.title.new(page) | ||
if not title or not title.exists then | if not title or not title.exists then | ||
| 第15行: | 第15行: | ||
end | end | ||
local content = title:getContent() | local content = title:getContent() | ||
if not content then | if not content then | ||
| 第21行: | 第20行: | ||
end | end | ||
-- | -- 转义标题名(防正则炸) | ||
local safeSection = mw.ustring.gsub( | local safeSection = mw.ustring.gsub( | ||
section, | section, | ||
| 第28行: | 第27行: | ||
) | ) | ||
-- | ---------------------------------------------------------------- | ||
local headerPattern = "\n= | -- 1. 匹配任意级别标题,并捕获 '=' 数量 | ||
local startPos, endPos = mw.ustring.find(content, headerPattern) | ---------------------------------------------------------------- | ||
local headerPattern = "\n(=+)%s*" .. safeSection .. "%s*%1%s*\n" | |||
local startPos, endPos, eqs = | |||
mw.ustring.find(content, headerPattern) | |||
-- | -- 兼容标题在页面最开头 | ||
if not startPos then | if not startPos then | ||
headerPattern = "^= | headerPattern = "^(=+)%s*" .. safeSection .. "%s*%1%s*\n" | ||
startPos, endPos = mw.ustring.find(content, headerPattern) | startPos, endPos, eqs = | ||
mw.ustring.find(content, headerPattern) | |||
end | end | ||
if not endPos then | if not endPos or not eqs then | ||
return "" | return "" | ||
end | end | ||
-- | local level = mw.ustring.len(eqs) | ||
---------------------------------------------------------------- | |||
-- 2. 从标题结束后开始,找下一个“级别 ≤ 当前级别”的标题 | |||
---------------------------------------------------------------- | |||
local after = mw.ustring.sub(content, endPos + 1) | local after = mw.ustring.sub(content, endPos + 1) | ||
- | local nextHeaderPattern = "\n={1," .. level .. "}[^=].-\n" | ||
local | local nextStart = mw.ustring.find(after, nextHeaderPattern) | ||
local body | local body | ||
if | if nextStart then | ||
body = mw.ustring.sub(after, 1, | body = mw.ustring.sub(after, 1, nextStart - 1) | ||
else | else | ||
body = after | body = after | ||
end | end | ||
-- | ---------------------------------------------------------------- | ||
-- 3. 维基百科式帽注 + 原样正文 | |||
"'' | ---------------------------------------------------------------- | ||
local hatnote = | |||
'<div class="hatnote">主条目:[[' .. page .. ']]</div>\n\n' | |||
return hatnote .. body | |||
end | end | ||
return p | return p | ||