Module:Cite:修订间差异
武外梗百科 爱国好学自强图新的百科全书
更多操作
创建页面,内容为“local p = {} function p.main(frame) local page = frame.args.page or frame.args[1] local section = frame.args.section or frame.args[2] if not page or not section then return "❌ 参数不足(需要 page 和 section)" end local title = mw.title.new(page) if not title then return "❌ 条目不存在" end local content = title:getContent() if not content then return "❌ 无法读取条目内容"…” |
无编辑摘要 |
||
| (未显示同一用户的8个中间版本) | |||
| 第1行: | 第1行: | ||
local p = {} | local p = {} | ||
function p. | function p.include(frame) | ||
local | local pageName = mw.text.trim(frame.args[1] or frame.args.page or "") | ||
local section = | local section = mw.text.trim(frame.args[2] or frame.args.section or "") | ||
if pageName == "" or section == "" then return "" end | |||
local title = mw.title.new(pageName) | |||
if not title or not title.exists then return "" end | |||
local title = mw.title.new( | |||
if not title then | |||
local content = title:getContent() | local content = title:getContent() | ||
if not content then | if not content then return "" end | ||
-- | -- 更安全的 section 提取:用 string.find 避免 gmatch 边缘 bug | ||
local pattern = "==%s*" .. mw.ustring.gsub( | local safeSection = mw.text.encode(section) -- 或手动逃逸 | ||
local pattern = "\n(==+)%s*" .. mw.ustring.gsub(safeSection, "([%-%[%]%.%(%)%*%+%?%^%$])", "%%%1") .. "%s*%1%s*\n" | |||
local startPos = | local startPos, endPos = content:find(pattern) | ||
if not startPos then | if not startPos then | ||
pattern = "^(==+)%s*" .. mw.ustring.gsub(safeSection, "([%-%[%]%.%(%)%*%+%?%^%$])", "%%%1") .. "%s*%1%s*\n" | |||
startPos, endPos = content:find(pattern) | |||
end | end | ||
if not endPos then return "" end | |||
-- | local level = endPos - startPos + 1 -- 简化 level 计算 | ||
local after = | local after = content:sub(endPos + 1) | ||
local | -- 用 split 代替 gmatch,避免潜在无限 | ||
local lines = {} | |||
for line in (after .. "\n"):gmatch("(.-)\n") do | |||
local eqs, name = line:match("^(%=+)%s*(.-)%s*%1%s*$") | |||
if eqs and #eqs <= level then break end | |||
table.insert(lines, line) | |||
end | end | ||
local body = table.concat(lines, "\n") | |||
local hatnote = '<div style="color:#555;font-size:80%;font-style:italic;margin-bottom:0.5em;"> 主条目:[[' .. pageName .. ']]</div>' | |||
return hatnote .. "\n\n" .. body | |||
end | end | ||
return p | return p | ||