模块

文本:修订间差异

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献
创建页面,内容为“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 "错误:未…”
 
律Rhyme留言 | 贡献
无编辑摘要
 
(未显示同一用户的9个中间版本)
第6行: 第6行:
      
      
     -- 收集所有参数
     -- 收集所有参数
     for i = 1, 9 do
     for i = 1, 10 do
         if frame.args[i] and frame.args[i] ~= "" then
         if frame.args[i] and frame.args[i] ~= "" then
             args[i] = frame.args[i]
             table.insert(args, frame.args[i])
         end
         end
     end
     end
      
      
     -- 最后一个非空参数是文本内容
     -- 最后一个参数是文本内容
     local textIndex = 0
    if #args > 0 then
     for i = 1, 9 do
        text = args[#args]
         if args[i] then
        table.remove(args, #args)
             textIndex = i
    else
        return ""
    end
   
     local styles = {}
   
    -- 处理每个样式参数
     for _, arg in ipairs(args) do
        local style = parseStyle(arg)
         if style then
             table.insert(styles, style)
         end
         end
     end
     end
      
      
     if textIndex == 0 then
    -- 生成HTML
         return "错误:未提供文本内容"
     if #styles > 0 then
         return '<span style="' .. table.concat(styles, '; ') .. '">' .. text .. '</span>'
    else
        return text
     end
     end
end
function parseStyle(param)
    param = mw.text.trim(param)
      
      
     text = args[textIndex]
     -- 颜色处理
    local colors = {
        ['红'] = 'red',
        ['蓝'] = '#82e4ff',
        ['绿'] = '#b5f651',
        ['黄'] = 'yellow',
        ['白'] = 'white',
        ['黑'] = 'black',
        ['紫'] = 'purple',
        ['橙'] = '#ef7829',
        ['灰'] = 'gray',
        ['粉'] = '#f087af'
    }
      
      
     -- CSS样式
     -- 检查中文颜色
     local styles = {}
     if colors[param] then
    local classes = {}
        return 'color: ' .. colors[param]
     local isBlackout = false
     end
      
      
     -- 处理样式参数
     -- 检查十六进制颜色或CSS颜色名
     for i = 1, textIndex - 1 do
     if param:match('^#[0-9a-fA-F]+$') or param:match('^[a-zA-Z]+$') then
        if args[i] then
        -- 验证是否为有效的CSS颜色
            local style = args[i]
        local validColors = {
            local styleLower = string.lower(style)
            'red', 'blue', 'green', 'yellow', 'white', 'black', 'purple',  
           
            'orange', 'gray', 'pink', 'brown', 'cyan', 'magenta', 'lime',
            -- 检查是否为十六进制颜色代码
             'navy', 'maroon', 'olive', 'teal', 'silver', 'crimson'
            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)
        if param:match('^#') or contains(validColors, param:lower()) then
           
             return 'color: ' .. param
            -- 检查是否为像素大小
            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")
                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")
           
            -- 黑幕效果
            elseif styleLower == "黑幕" or styleLower == "spoiler" then
                isBlackout = true
            end
         end
         end
     end
     end
      
      
     -- 生成HTML
     -- 字体大小处理
     local styleStr = ""
     local sizes = {
     if #styles > 0 then
        ['特小'] = 'xx-small',
         styleStr = ' style="' .. table.concat(styles, "; ") .. '"'
        ['小'] = '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
     end
      
      
     if isBlackout then
     return nil
        -- 黑幕效果:默认黑色背景黑色文字,鼠标悬停显示
end
        return '<span style="background-color: #000; color: #000; padding: 1px 2px; border-radius: 2px;" title="' ..
 
              mw.text.encode(text) .. '" onmouseover="this.style.color=\'#fff\'" onmouseout="this.style.color=\'#000\'">' ..
function contains(table, element)
              mw.text.encode(text) .. '</span>'
    for _, value in pairs(table) do
    else
        if value == element then
         return '<span' .. styleStr .. '>' .. mw.text.encode(text) .. '</span>'
            return true
         end
     end
     end
    return false
end
end


return p
return p

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