Module:Color scheme

--

-- Color scheme template module. -- -- @see Template:Color scheme -- @example `` -- -- @module p -- @version 1.1.0 -- @author Speedit -- @author DarthKitty

local p = {} local getArgs = require('Dev:Arguments').getArgs local i18n = require('Dev:I18n').loadMessages('Color scheme') local title = mw.title.getCurrentTitle

-- Escapes a string for use in an HTML class, replacing every whitespace and -- punctuation character with an underscore. -- -- @param {string} str --    The string to escape. -- @returns {string} --    The escaped string.

local function escapeClass(str) return mw.ustring.lower(mw.ustring.gsub(str, '[%s%p]', '_')) end

-- Color scheme renderer (main function). -- -- @example `` -- -- @param {table} frame --    Frame invocation object. -- @return {string} --    HTML of color scheme template.

function p.main(frame) local args = getArgs(frame, {       parentOnly = true,    })

if not args.type then error('no "type" argument supplied') end

local typedef = escapeClass(args.type:match('^(.-)/') or args.type) local name = escapeClass(title.rootText) local scheme = mw.html.create('ul') :addClass('color-scheme color-scheme__' .. name) :attr('id', 'color-scheme__' .. name .. '-' .. typedef) :css('display', 'inline') :css('font-size', '12px') :css('margin', '20px 0') :css('text-align', 'center')

for i = 1, math.huge, 4 do       local colorHex = args[i] local colorName = args[i + 1] local colorPurpose = args[i + 2] local variableName = args[i + 3]

if not (colorHex or colorName or colorPurpose or variableName) then break elseif not colorHex then error('missing hexadecimal color code at index' .. i)       elseif not colorName then error('missing color name at index' .. (i + 1)) elseif not colorPurpose then error('missing color purpose at index' .. (i + 2)) elseif not variableName then error('missing variable name at index' .. (i + 3)) end

scheme:tag('li') :addClass('color-scheme__color') :attr('id', 'color-scheme__color-' .. typedef .. '-' .. i)           :attr('data-color', '#' .. (colorHex or 'nil')) :css('align-items', 'center') :css('display', 'inline-flex') :css('flex-flow', 'column nowrap') :css('margin', '20px 10px 0') :css('width', '120px') :tag('div') :addClass('color-scheme__color-box') :addClass('pi-border-color') :css('background-color', '#' .. colorHex) :css('border-radius', '4px') :css('border-style', 'solid') :css('border-width', '2px') :css('height', '60px') :css('width', '60px') :done :tag('b') :addClass('color-scheme__color-name') :wikitext(colorName) :done :tag('span') :addClass('color-scheme__separator') :css('display', 'none') :wikitext(' - ') :done :tag('code') :addClass('color-scheme__color-hex') :wikitext('#' .. colorHex) :done :tag('span') :addClass('color-scheme__separator') :css('display', 'none') :wikitext(' (')               :done            :tag('i')                :addClass('color-scheme__color-purpose')                :wikitext(i18n:inUserLang:msg('for', colorPurpose))                :done            :tag('span')                :addClass('color-scheme__separator')                :css('display', 'none')                :wikitext('; ')                :done            :tag('var')                :addClass('color-scheme__variable-name')                :wikitext('$' .. variableName)               :done            :tag('span')                :addClass('color-scheme__separator')                :css('display', 'none')                :wikitext(')') :done end

return tostring(scheme) end

return p