Module:Code

来自三三百科
33DAI留言 | 贡献2026年2月12日 (四) 02:06的版本 (创建页面,内容为“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 ma…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转到导航 跳转到搜索

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