模块

模块:文本

来自卡厄思梦境WIKI

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

local p = {}

local colorMap = {
    ['红']    = '#ff4d4d',
    ['蓝']    = '#7be3fd',
    ['绿']    = '#a8f428',
    ['黄']    = '#ffe44d',
    ['橙']    = '#ed9703',
    ['粉']    = '#ffb3d9',
    ['灰']    = '#a0a0a0',
    ['热情']  = '#db3762',
    ['正义']  = '#2c8fd8',
    ['秩序']  = '#21bf80',
    ['本能']  = '#eda000',
    ['虚无']  = '#9900ff',
}

local function resolveColor(color)
    if not color or color == '' then
        return nil
    end
    if colorMap[color] then
        return colorMap[color]
    end
    local hex = color
    if string.sub(hex, 1, 1) ~= '#' then
        hex = '#' .. hex
    end
    if string.match(hex, '^#[0-9a-fA-F]{3}$') or string.match(hex, '^#[0-9a-fA-F]{6}$') then
        return hex
    end
    return color
end

function p.main(frame)
    local args = frame:getParent().args

    local color    = args['颜色']   or ''
    local stroke   = args['描边']   or ''
    local underline = args['下划线'] or ''
    local italic   = args['斜体']   or ''
    local content  = args[1]        or ''

    local classes = { 'text-style' }
    local styles  = {
        '-webkit-text-stroke: 0;',
        'paint-order: normal;'
    }

    local colorVal = resolveColor(color)
    if colorVal then
        table.insert(classes, 'text-style--colored')
        table.insert(styles, 'color: ' .. colorVal .. ';')
    end

    local strokeVal = resolveColor(stroke)
    if strokeVal then
        table.insert(classes, 'text-style--stroked')
        table.insert(styles, '-webkit-text-stroke: 1px ' .. strokeVal .. ';')
        table.insert(styles, 'paint-order: stroke fill;')
        if not colorVal then
            table.insert(styles, 'color: #fff;')
        end
    end

    if underline ~= '' then
        table.insert(classes, 'text-style--underline')
    end

    if italic ~= '' then
        table.insert(classes, 'text-style--italic')
    end

    local html = '<span class="' .. table.concat(classes, ' ') .. '" style="' .. table.concat(styles, ' ') .. '">' .. content .. '</span>'
    return html
end

return p