Module:Color scheme

-- -- Color scheme template module. -- @module     cs -- @version     0.6.0 -- @usage -- @author     Speedit -- @release    alpha; untested

-- Module package. local cs = {}

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

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

-- Template argument utility. -- @return     {table} Argument table. function template_args return mw.getCurrentFrame:getParent.args end

-- 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  = ')' } }

-- Color scheme renderer (main function). -- @param      {table} frame Frame invocation object. -- @usage -- @return     {string} HTML of color scheme template. function cs.main(frame) -- Fetch template arguments. local args = template_args -- Test for "type" parameter. if args and args.type then -- Item data cache. local itemd = {} -- Loop over frame arguments. for n, p in pairs(args) do           if tonumber(n) == n then -- Index values. local l = math.floor(n / 4) local i = (n % 4) == 0 and 4 or (n % 4) -- Cache looped argument. itemd[l] = itemd[l] or {} itemd[l][i] = i == 3 and i18n:msg('for', {                       mw.text.trim(p)                    }) or                    mw.text.trim(p) end end -- Parameters for "type" and "name". local typedef = mw.ustring.gsub(           mw.text.split(args.type, '/')[1],            ' ', '_'        ) local name = mw.ustring.lower(           itemd.name or            title.rootText        ) -- Color scheme container. local scheme = mw.html.create('ul'):attr({           ['class']      = 'color-scheme color-scheme__' .. name,            ['id']         = 'color-scheme__' .. name .. '-' .. typedef        }):css({            ['display']    = 'inline',            ['font-size']  = '12px',            ['margin']     = '20px 0',            ['text-align'] = 'center'        }) -- Render logic for color items. local items = {} for i, p in ipairs(itemd) do           -- Argument validation. if #p % 4 ~= 0 then error('incomplete series of arguments supplied') end -- Item creation. items[i] = { wrapper = mw.html.create('li', { parent = scheme }):attr({                    ['id']    = 'color-scheme__color-'                        .. name .. '-'                        .. typedef .. '-' .. 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)(v.text)) end end -- Export container as string. scheme = tostring(scheme) return scheme else error('no "type" argument supplied') end end

-- Package export. return cs