Module:Box

From evermeet.cx Wiki
Revision as of 13:56, 20 October 2017 by Tessus (talk | contribs)
Jump to navigation Jump to search

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

-- Module:Box
-- Version 1.3 (2017-10-20)
-- 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

    local tline  = ''
    local output = ''

    if title ~= '' then
        tline = '! <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

    output = '{|style="width:' .. width .. '; height:' .. height .. '; 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' ..
             '{|width="100%" cellpadding="2" cellspacing="5" style="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,
}