Module:Color scheme

-- -- Color scheme template module. -- @module     cs -- @version     0.5.0 -- @usage -- @author     Speedit -- @release    stable; tested

-- Module package. local cs = {}

-- Module variables. local title = mw.title.getCurrentTitle

-- Module message data. -- @see        Module:Colorscheme/i18n local i18n = require('Module:I18n').loadMessages('Color scheme')

-- Template view configuration. local view = { {       item  = 'color-box', class = 'pi-border-color', tag  = 'div', css  = { ['background']   = '$1', ['border-radius'] = '4px', ['border-style'] = 'solid', ['border-width'] = '2px', ['height']       = '60px', ['width']        = '60px' }   },    {        item = 'color-name', tag = 'b', text = '$2' },   {        item = 'separator', tag = 'span', css = { display = 'none' }, text = ' - ' },   {        item = 'color-hex', tag = 'code', text = '$1' },   {        item = 'separator', tag = 'span', css = { display = 'none' }, text = ' ('   },    {        item = 'color-purpose',        tag  = 'i',        text = '$3'    },    {        item = 'separator',        tag  = 'span',        css  = { display = 'none' },        text = '; '    },    {        item = 'variable-name',        tag  = 'var',        text = '$4'    },    {        item  = 'separator',        tag   = 'span',        css   = { display = 'none' },        text  = ')' } }

function cs.main(frame) if frame.args and frame.args.type then -- Loop over frame arguments. local args = {} for n, p in pairs(frame.args) do           if tonumber(n) == n then local l = math.floor(n / 8) local i = (n % 8) == 0 and 8 or (n % 8) args[l][i] = i == 3 and i18n:msg('for', {                       mw.text.trim(p)                    }) or                    mw.text.trim(p) end end -- Type and name parameters. local type = mw.ustring.gsub(           mw.text.split(frame.args.type, '/')[1],            ' ', '_'        ) local name = mw.ustring.lower(           frame.args.name or            title.rootText        ) -- Color scheme container. local scheme = mw.html.create('ul'):attr({           ['class']      = 'color-scheme color-scheme__' .. name,            ['id']         = 'color-scheme__' .. name .. '-' .. type        }):css({            ['display']    = 'inline',            ['font-size']  = '12px',            ['margin']     = '20px 0',            ['text-align'] = 'center'        }) -- Render logic for color items. local items = {} for i, p in ipairs(args) do           -- Item creation. items[i] = { wrapper = mw.html.create('li', { parent = scheme }):attr({                    ['id']    = 'color-scheme__color-'                        .. name .. '-'                        .. type .. '-' .. tostring(i),                    ['class'] = 'color-scheme__color'                }):css({                    ['display']     = 'inline-flex',                    ['flex-flow']   = 'column nowrap',                    ['align-items'] = 'center',                    ['margin']      = '20px 10px 0',                    ['width']       = '120px'                }), children = {} }           for c, v in ipairs(view) do                items[i].children[c] = mw.html.create(v.tag, {                    parent = items[i].wrapper                }) :attr({                   ['class'] = 'color-scheme__' .. v.item                }) :addClass(v.class):css(v.css) :wikitext((function(t) if t then return mw.ustring.gsub(t, '%$([%w-]+)', p)                   end end)(c.text)) end end -- Export container as string. scheme = tostring(scheme) return scheme else error('no "type" argument supplied') end end

-- Package export. return cs