bugfix1001.2
全站通知:

模块:沙盒2

来自火星求生WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:沙盒2/doc创建

-- 定义 startsWith 辅助函数
local function startsWith(str, prefix)
    return str:sub(1, #prefix) == prefix
end

-- 假设 applyColorRule 是这样的一个函数
local function applyColorRule(text, color)
    if color == 'green' then
        return '<span style="color:#4f4;">' .. text .. '</span>'
    elseif color == 'red' then
        return '<span style="color:#f33;">' .. text .. '</span>'
    else
        return text
    end
end

-- 初始化 M 表
local M = {}

function M.main(frame)
    local input = frame.args[1] or ''
    local parts = mw.text.split(input, ',')
    local output = {}

    for _, part in ipairs(parts) do
        -- 去除首尾空格
        part = part:match("^%s*(.-)%s*$")
        
        local color = nil
        -- 检查是否有前缀 G, R, W 并相应设置颜色
        if startsWith(part, 'G') then
            color = 'green'
            part = part:sub(2)
        elseif startsWith(part, 'R') then
            color = 'red'
            part = part:sub(2)
        elseif startsWith(part, 'W') then
            color = nil
            part = part:sub(2)
        else
            -- 如果部分包含破折号,则默认为红色,否则为绿色
            if part:find('-') then
                color = 'red'
            else
                color = 'green'
            end
        end

        -- 如果部分包含数字,则应用颜色规则
        if part:match("%d") then
            local coloredPart = applyColorRule(part, color)
            table.insert(output, coloredPart)
        else
            -- 否则直接插入未修改的部分
            table.insert(output, part)
        end
    end

    -- 将所有处理过的部分用逗号连接起来,去掉可能存在的结尾逗号
    local result = table.concat(output, ', ')
    result = result:gsub(',$', '')
    return result
end

return M