模块

模块:文本

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献2025年9月23日 (二) 09:49的版本

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

local p = {}

function p.main(frame)
    local args = {}
    local text = ""
    
    -- 收集所有参数
    for i = 1, 9 do
        if frame.args[i] and frame.args[i] ~= "" then
            args[i] = frame.args[i]
        end
    end
    
    -- 最后一个非空参数是文本内容
    local textIndex = 0
    for i = 1, 9 do
        if args[i] then
            textIndex = i
        end
    end
    
    if textIndex == 0 then
        return "错误:未提供文本内容"
    end
    
    text = args[textIndex]
    
    -- CSS样式
    local styles = {}
    local classes = {}
    
    -- 处理样式参数
    for i = 1, textIndex - 1 do
        if args[i] then
            local style = args[i]
            local styleLower = string.lower(style)
            
            -- 检查是否为十六进制颜色代码
            if string.match(style, "^#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]$") or 
               string.match(style, "^#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]$") then
                table.insert(styles, "color: " .. style)
            
            -- 检查是否为像素大小
            elseif string.match(style, "^%d+px$") then
                table.insert(styles, "font-size: " .. style)
            
            -- 检查背景颜色设置
            elseif string.match(style, "^背景颜色=(.+)$") then
                local bgColor = string.match(style, "^背景颜色=(.+)$")
                local bgColorLower = string.lower(bgColor)
                
                -- 如果是十六进制颜色
                if string.match(bgColor, "^#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]$") or 
                   string.match(bgColor, "^#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]$") then
                    table.insert(styles, "background-color: " .. bgColor)
                -- 如果是颜色名称
                elseif bgColorLower == "红" or bgColorLower == "red" then
                    table.insert(styles, "background-color: #FF0000")
                elseif bgColorLower == "蓝" or bgColorLower == "blue" then
                    table.insert(styles, "background-color: #0000FF")
                elseif bgColorLower == "绿" or bgColorLower == "green" then
                    table.insert(styles, "background-color: #008000")
                elseif bgColorLower == "黄" or bgColorLower == "yellow" then
                    table.insert(styles, "background-color: #FFD700")
                elseif bgColorLower == "紫" or bgColorLower == "purple" then
                    table.insert(styles, "background-color: #800080")
                elseif bgColorLower == "橙" or bgColorLower == "orange" then
                    table.insert(styles, "background-color: #FFA500")
                elseif bgColorLower == "黑" or bgColorLower == "black" then
                    table.insert(styles, "background-color: #000000")
                elseif bgColorLower == "白" or bgColorLower == "white" then
                    table.insert(styles, "background-color: #FFFFFF")
                elseif bgColorLower == "灰" or bgColorLower == "gray" or bgColorLower == "grey" then
                    table.insert(styles, "background-color: #808080")
                elseif bgColorLower == "粉" or bgColorLower == "pink" then
                    table.insert(styles, "background-color: #FFC0CB")
                elseif bgColorLower == "青" or bgColorLower == "cyan" then
                    table.insert(styles, "background-color: #00FFFF")
                else
                    -- 如果不是预设颜色,直接使用原始值
                    table.insert(styles, "background-color: " .. bgColor)
                end
                
                -- 添加圆角和内边距
                table.insert(styles, "padding: 2px 6px")
                table.insert(styles, "border-radius: 4px")
            
            -- 预设颜色
            elseif styleLower == "红" or styleLower == "red" then
                table.insert(styles, "color: #FF0000")
            elseif styleLower == "蓝" or styleLower == "blue" then
                table.insert(styles, "color: #0000FF")
            elseif styleLower == "绿" or styleLower == "green" then
                table.insert(styles, "color: #008000")
            elseif styleLower == "黄" or styleLower == "yellow" then
                table.insert(styles, "color: #FFD700")
            elseif styleLower == "紫" or styleLower == "purple" then
                table.insert(styles, "color: #800080")
            elseif styleLower == "橙" or styleLower == "orange" then
                table.insert(styles, "color: #FFA500")
            elseif styleLower == "黑" or styleLower == "black" then
                table.insert(styles, "color: #000000")
            elseif styleLower == "白" or styleLower == "white" then
                table.insert(styles, "color: #FFFFFF")
            elseif styleLower == "灰" or styleLower == "gray" or styleLower == "grey" then
                table.insert(styles, "color: #808080")
            elseif styleLower == "粉" or styleLower == "pink" then
                table.insert(styles, "color: #FFC0CB")
            elseif styleLower == "青" or styleLower == "cyan" then
                table.insert(styles, "color: #00FFFF")
            
            -- 文本装饰
            elseif styleLower == "下划线" then
                table.insert(styles, "text-decoration: underline")
            elseif styleLower == "加粗" or styleLower == "粗体" then
                table.insert(styles, "font-weight: bold")
            elseif styleLower == "划掉" or styleLower == "删除线" then
                table.insert(styles, "text-decoration: line-through")
            
            -- 预设字体大小
            elseif styleLower == "大" then
                table.insert(styles, "font-size: 1.2em")
            elseif styleLower == "小" then
                table.insert(styles, "font-size: 0.8em")
            elseif styleLower == "特大" then
                table.insert(styles, "font-size: 1.5em")
            elseif styleLower == "特小" then
                table.insert(styles, "font-size: 0.6em")
            end
        end
    end
    
    -- 生成HTML
    local styleStr = ""
    if #styles > 0 then
        styleStr = ' style="' .. table.concat(styles, "; ") .. '"'
    end
    
    return '<span' .. styleStr .. '>' .. mw.text.encode(text) .. '</span>'
end

return p