模块:Utils:修订间差异
来自Mindustry中文wiki
无编辑摘要 |
无编辑摘要 |
||
| 第93行: | 第93行: | ||
local count = item:sub(pos + 1):gsub("^%s*(.-)%s*$", "%1") | local count = item:sub(pos + 1):gsub("^%s*(.-)%s*$", "%1") | ||
if count:match("^%d+$") then | if count:match("^%d+$") then | ||
local icon = picture._lookup(name) | local icon = picture._lookup(name) | ||
table.insert(parts, count .. "x" .. '[[File:' .. icon .. '.png|18px|link=' .. name .. ']]' .. '[[' .. name .. ']]') | table.insert(parts, count .. "x" .. '[[File:' .. icon .. '.png|18px|link=' .. name .. ']]' .. '[[' .. name .. ']]') | ||
else | else | ||
2026年8月20日 (四) 00:20的版本
可在模块:Utils/doc创建此模块的帮助文档
-- 引入Picture模块
local picture = require('Module:Picture')
local p = {}
-- 辅助函数:去除字符串尾部的所有空白字符(空格、换行、回车等)
local function trimTrailing(str)
if str then
return str:gsub("%s+$", "")
end
return str
end
-- 条件函数,用法:{{#invoke:utils|ifeq|条件|为真输出|为假输出}}
function p.ifeq(frame)
local cond = frame.args[1] or ''
if cond ~= '' then
return frame.args[2]
else
return frame.args[3]
end
end
-- 一个用来打表的函数,会自动适配内容数量,用法:{{#invoke:utils|renderGoals|是否显示(有值为是,空则为否)|目标1|注释1|...}}
function p.renderGoals(frame)
local args = frame.args
local cells = {}
-- if判断是否输出目标栏
if not args["是否启用目标栏"] or args["是否启用目标栏"] == "" then return "" end
local i = 1
while true do
local val1 = args[i]
if val1 == nil then break end
val1 = trimTrailing(val1)
i = i + 1
local val2 = args[i]
if val2 == nil then break end
val2 = trimTrailing(val2)
table.insert(cells,val1.." || "..val2)
i = i + 1
end
if #cells == 0 then return "no arg" end
-- 大粪
return '<div class="mdtMain">\n<div class="mdtHome">\n<div class="mdtPanel mdtHomeSection">\n<h2 class="mdtFoldHeading" style="margin:0 0 8px 0;">目标列表 <span class="mdtFold"></span></h2>\n{| class="mdtSideTable"\n! 目标 !! 注释\n|-\n| ' .. table.concat(cells, '\n|-\n| ') .. '\n|}\n</div>\n</div>'
end
-- 这是波次列表的专用函数,其中的内容只有上帝记得,尽量不要修改
function p.renderWaves(frame)
local args = frame.args
if not args["是否启用波次栏"] or args["是否启用波次栏"] == "" then return "" end
local waves = {}
for key, value in pairs(args) do
local num = key:match("^波次(%d+)$")
if num then
waves[tonumber(num)] = value
end
end
local result = '<div class="mdtLayout mdtLight mdtWrapRail">\n<div class="mdtMain">\n<div class="mdtHome">\n<div class="mdtPanel mdtHomeSection">\n<h2 class="mdtFoldHeading" style="margin:0 0 8px 0;">波次列表<span class="mdtFold"></span></h2>\n{| class="mdtSideTable"\n'
local i = 1
local chunks = {}
while true do
local waveText = waves[i]
if waveText == nil then break end
-- 手动按逗号拆分,避免编码问题
local items = {}
local startPos = 1
while startPos <= #waveText do
local commaPos = waveText:find("[,,]", startPos)
if commaPos then
local item = waveText:sub(startPos, commaPos - 1)
if item ~= "" then table.insert(items, item) end
startPos = commaPos + 1
else
local item = waveText:sub(startPos)
if item ~= "" then table.insert(items, item) end
break
end
end
local parts = {}
for _, item in ipairs(items) do
-- 清理不可见字符(如零宽空格)
item = item:gsub("%z", "")
local pos = item:find("[*×]")
if pos then
local name = item:sub(1, pos - 1):gsub("^%s*(.-)%s*$", "%1")
local count = item:sub(pos + 1):gsub("^%s*(.-)%s*$", "%1")
if count:match("^%d+$") then
local icon = picture._lookup(name)
table.insert(parts, count .. "x" .. '[[File:' .. icon .. '.png|18px|link=' .. name .. ']]' .. '[[' .. name .. ']]')
else
table.insert(parts, '<span style="color:red;">格式错误: 数量非数字 (' .. item .. ')</span>')
end
else
table.insert(parts, '<span style="color:red;">格式错误: ' .. item .. '</span>')
end
end
local partsStr = table.concat(parts, " || ")
table.insert(chunks, '! rowspan="1" | 波次' .. i .. '\n|' .. partsStr)
i = i + 1
end
local chunksStr = table.concat(chunks, '\n|-\n')
return result .. chunksStr .. '\n|}\n</div>\n</div>'
end
return p
