Module:Code
local p = {}
function p.main(frame)
-- 获取调用该模板的父框架参数
local args = frame:getParent().args
local parts = {}
local maxIndex = 0
-- 找到最大的数字索引
for k, _ in pairs(args) do
if type(k) == "number" and k > maxIndex then
maxIndex = k
end
end
-- 核心:还原被 | 截断的所有部分
-- 如果 maxIndex 为 0,说明可能用了显式的 code= 参数
if maxIndex > 0 then
for i = 1, maxIndex do
-- args[i] 如果不存在则设为空字符串,确保 concat 能运行
parts[i] = args[i] or ""
end
content = table.concat(parts, "|")
else
content = args.code or ""
end
if content == "" then return "" end
-- 使用 extensionTag 完美避开 Wikitext 转义问题
return frame:extensionTag{
name = "syntaxhighlight",
content = content,
args = {
lang = args.lang or "cpp",
line = args.line
}
}
end
return p