Module:Box: Difference between revisions

From evermeet.cx Wiki
Jump to navigation Jump to search
No edit summary
version 1.5 removes deprecated html formatting -> html5
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:Box
-- Module:Box
-- Version 1.0 (2017-10-18)
-- Version 1.5 (2018-02-28)
-- Author: Helmut K. C. Tessarek
-- Author: Helmut K. C. Tessarek


Line 29: Line 29:
         title_bg    = '#FFFFCC',
         title_bg    = '#FFFFCC',
         title_border = '#a3b0bf',
         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',
     },     
     },     
}
}
Line 39: Line 51:
     local title  = args["title"] or ''
     local title  = args["title"] or ''
     local text  = args["text"] 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 no color is specified, or if the color does not exist, color is set to grey
Line 47: Line 63:
     end
     end


     local tline  = ''
     -- check validity of width and height
     local output = ''
    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


     if title == '' and text == '' then
     local tline  = ''
        return ''
    local output  = ''
     end
    local twidth  = ''
     local theight = ''


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


     output = '{|style="border-spacing:8px;margin:0px -8px"\n' ..
     output = '{|style="' .. twidth .. ' ' .. theight .. ' border-spacing:8px; margin:0px -8px"\n' ..
             '|class="MainPageBG" style="width:' .. width .. '; height:' .. height .. '; border:1px solid ' .. box[color]["border"] .. '; background:' .. box[color]["bg"] .. '; vertical-align:top; color:#000"|\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' ..
             '{|style="width:100%; border-spacing:5px; vertical-align:top; background:' .. box[color]["bg"] .. ';"\n' ..
             tline ..
             tline ..
             '|-\n' ..
             '|-\n' ..
Line 66: Line 117:
             '|-\n' ..
             '|-\n' ..
             '|}\n' ..
             '|}\n' ..
             '|}\n'
             '|}'


     return output
     return output

Latest revision as of 15:13, 28 February 2018

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,
}