Module:Box

From evermeet.cx Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Box/doc

-- Module:Box
-- Version 1.5 (2018-02-28)
-- Author: Helmut K. C. Tessarek

-- Creates a colored box with title and text

local box = {
    blue    = {
        bg           = '#f5faff',
        border       = '#cedff2',
        title_bg     = '#cedff2',
        title_border = '#a3b0bf',
    },
    green   = {
        bg           = '#f5fffa',
        border       = '#cef2e0',
        title_bg     = '#cef2e0',
        title_border = '#a3bfb1',
    },
    grey    = {
        bg           = '#f9f9f9',
        border       = '#ddd',
        title_bg     = '#eee',
        title_border = '#afa3bf',
    },
    yellow  = {
        bg           = '#FFFFF5',
        border       = '#F8E47A',
        title_bg     = '#FFFFCC',
        title_border = '#a3b0bf',
    },
    red     = {
        bg           = '#fff5fa',
        border       = '#f2cedd',
        title_bg     = '#f2cedd',
        title_border = '#bfa3af',
    },
    purple  = {
        bg           = '#faf5ff',
        border       = '#ddcef2',
        title_bg     = '#ddcef2',
        title_border = '#afa3bf',
    },    
}

function renderBox(args)
    local color  = ''
    local tmpcol = args["color"]
    local width  = args["width"] or ''
    local height = args["height"] or ''
    local title  = args["title"] or ''
    local text   = args["text"] or ''

    if title == '' and text == '' and (width == '' or height == '') then
        return ''
    end

    -- if no color is specified, or if the color does not exist, color is set to grey
    if type(box[tmpcol]) ~= 'nil' then
        color = tmpcol
    else
        color = 'grey'
    end

    -- check validity of width and height
    if width ~= '' then
        if tonumber(width) then
            width = width .. "px"
        end
        local errmsg = '<strong class="error">Box: <code>width</code> must be a number followed by <code>px</code> or <code>%</code></strong>'
        if string.sub(width,-2) ~= 'px' and string.sub(width,-1) ~= '%' then
            return errmsg
        end
        if string.sub(width,-2) == 'px' and not tonumber(string.sub(width,1,-3)) then
            return errmsg
        end
        if string.sub(width,-1) == '%' and not tonumber(string.sub(width,1,-2)) then
            return errmsg
        end
    end
    if height ~= '' then
        if tonumber(height) then
            height = height .. "px"
        end
        local errmsg = '<strong class="error">Box: <code>height</code> must be a number followed by <code>px</code></strong>'
        if string.sub(height,-2) ~= 'px' then
            return errmsg
        end
        if string.sub(height,-2) == 'px' and not tonumber(string.sub(height,1,-3)) then
            return errmsg
        end
    end

    local tline   = ''
    local output  = ''
    local twidth  = ''
    local theight = ''

    if title ~= '' then
        tline = '! style="padding:2px;"| <p style="margin:0; background:' .. box[color]["title_bg"] ..'; font-size:120%; font-weight:bold; border:1px solid ' .. box[color]["title_border"] ..'; text-align:left; color:#000; padding:0.2em 0.4em;">' .. title ..'</p>\n'
    end
    
    if width ~= '' then
    	twidth = 'width:' .. width .. ';'
    end
    
    if height ~= '' then
    	theight = 'height:' .. height .. ';'
    end

    output = '{|style="' .. twidth .. ' ' .. theight .. ' border-spacing:8px; margin:0px -8px"\n' ..
             '|class="MainPageBG" style="border:1px solid ' .. box[color]["border"] .. '; background:' .. box[color]["bg"] .. '; vertical-align:top; color:#000"|\n' ..
             '{|style="width:100%; border-spacing:5px; vertical-align:top; background:' .. box[color]["bg"] .. ';"\n' ..
             tline ..
             '|-\n' ..
             '|style="color:#000"|' .. text .. '\n' ..
             '|-\n' ..
             '|}\n' ..
             '|}'

    return output
end

local function showT(frame)
    -- Called by "{{box|...}}".
    return renderBox(frame:getParent().args)
end

local function show(frame)
    -- Called by "{{#invoke:box|show|...}}".
    return renderBox(frame.args)
end

return {
    showT = showT,
    show = show,
}