模块

文本:修订间差异

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献
无编辑摘要
律Rhyme留言 | 贡献
无编辑摘要
 
第51行: 第51行:
         ['紫'] = 'purple',
         ['紫'] = 'purple',
         ['橙'] = '#ef7829',
         ['橙'] = '#ef7829',
         ['灰'] = 'gray'
         ['灰'] = 'gray',
        ['粉'] = '#f087af'
     }
     }
      
      

2025年10月31日 (五) 15:20的最新版本

此模块的文档可以在模块:文本/doc创建

local p = {}

function p.main(frame)
    local args = {}
    local text = ""
    
    -- 收集所有参数
    for i = 1, 10 do
        if frame.args[i] and frame.args[i] ~= "" then
            table.insert(args, frame.args[i])
        end
    end
    
    -- 最后一个参数是文本内容
    if #args > 0 then
        text = args[#args]
        table.remove(args, #args)
    else
        return ""
    end
    
    local styles = {}
    
    -- 处理每个样式参数
    for _, arg in ipairs(args) do
        local style = parseStyle(arg)
        if style then
            table.insert(styles, style)
        end
    end
    
    -- 生成HTML
    if #styles > 0 then
        return '<span style="' .. table.concat(styles, '; ') .. '">' .. text .. '</span>'
    else
        return text
    end
end

function parseStyle(param)
    param = mw.text.trim(param)
    
    -- 颜色处理
    local colors = {
        ['红'] = 'red',
        ['蓝'] = '#82e4ff',
        ['绿'] = '#b5f651',
        ['黄'] = 'yellow',
        ['白'] = 'white',
        ['黑'] = 'black',
        ['紫'] = 'purple',
        ['橙'] = '#ef7829',
        ['灰'] = 'gray',
        ['粉'] = '#f087af'
    }
    
    -- 检查中文颜色
    if colors[param] then
        return 'color: ' .. colors[param]
    end
    
    -- 检查十六进制颜色或CSS颜色名
    if param:match('^#[0-9a-fA-F]+$') or param:match('^[a-zA-Z]+$') then
        -- 验证是否为有效的CSS颜色
        local validColors = {
            'red', 'blue', 'green', 'yellow', 'white', 'black', 'purple', 
            'orange', 'gray', 'pink', 'brown', 'cyan', 'magenta', 'lime',
            'navy', 'maroon', 'olive', 'teal', 'silver', 'crimson'
        }
        
        if param:match('^#') or contains(validColors, param:lower()) then
            return 'color: ' .. param
        end
    end
    
    -- 字体大小处理
    local sizes = {
        ['特小'] = 'xx-small',
        ['小'] = 'small',
        ['大'] = 'large',
        ['特大'] = 'x-large',
        ['big'] = 'large',
        ['small'] = 'small',
        ['large'] = 'large',
        ['x-large'] = 'x-large',
        ['xx-large'] = 'xx-large',
        ['x-small'] = 'x-small',
        ['xx-small'] = 'xx-small'
    }
    
    if sizes[param] then
        return 'font-size: ' .. sizes[param]
    end
    
    -- 检查像素值或百分比
    if param:match('%d+px$') or param:match('%d+%%$') or param:match('%d*%.?%d+em$') then
        return 'font-size: ' .. param
    end
    
    -- 文本装饰处理
    if param == '下划线' or param == 'underline' then
        return 'text-decoration: underline'
    end
    
    if param == '划掉' or param == '删除线' or param == 'strikethrough' then
        return 'text-decoration: line-through'
    end
    
    return nil
end

function contains(table, element)
    for _, value in pairs(table) do
        if value == element then
            return true
        end
    end
    return false
end

return p