Difference between revisions of "Module:Class"

From blackwiki
Jump to navigation Jump to search
blackwiki>Nihiltres
(Updated getDefinition() to provide canonical class code as secondary output, and fixed nil indexing bug in p._class())
blackwiki>Nihiltres
(Amalgamated string functions into one "normalizeValue" function, shortened a variable name, and used single quote marks for strings throughout)
Line 8: Line 8:
  
 
-- Initialize helper function variables
 
-- Initialize helper function variables
local getRawArgs, makeInvokeFunction, trimString, trimValue, normalizeValue
+
local getDefinition, getRawArgs, makeInvokeFunction, normalizeValue
local validateCode, makeCssText
 
  
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
Line 34: Line 33:
 
-- String helper functions
 
-- String helper functions
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
 
function trimString(s)
 
return s:match('^%s*(.-)%s*$')
 
end
 
 
function trimValue(val)
 
if type(val) == 'string' then
 
return trimString(val)
 
else
 
return val
 
end
 
end
 
  
 
function normalizeValue(val)
 
function normalizeValue(val)
val = trimValue(val)
+
if type(val) == 'string' then val = val:match('^%s*(.-)%s*$'):lower() end
if val ~= '' then
+
if val == '' then val = nil end
return val
+
return val
end
 
end
 
 
 
function validateCode(code)
 
if code then
 
return trimValue(code):lower()
 
else
 
return ''
 
end
 
 
end
 
end
  
Line 67: Line 45:
  
 
function getDefinition(code)
 
function getDefinition(code)
local canonicalClassCode = validateCode(code)
+
local canonicalCode = normalizeValue(code)
local class = definitions[canonicalClassCode]
+
local class = definitions[canonicalCode]
 
while class and class.alias do
 
while class and class.alias do
canonicalClassCode = class.alias
+
canonicalCode = class.alias
 
class = definitions[class.alias]
 
class = definitions[class.alias]
 
end
 
end
Line 76: Line 54:
 
return nil, nil
 
return nil, nil
 
end
 
end
return class, canonicalClassCode
+
return class, canonicalCode
 
end
 
end
  
Line 85: Line 63:
 
function p._colour(code)
 
function p._colour(code)
 
local class = getDefinition(code)
 
local class = getDefinition(code)
return class and class.colour or "transparent"
+
return class and class.colour or 'transparent'
 
--^ no defaulting's in definition yet; "transparent"'s a placeholder
 
--^ no defaulting's in definition yet; "transparent"'s a placeholder
 
end
 
end
Line 133: Line 111:
 
local cell = mw.html.create(normalizeValue(args.heading) and 'th' or 'td')
 
local cell = mw.html.create(normalizeValue(args.heading) and 'th' or 'td')
 
local icon = p.icon(args)
 
local icon = p.icon(args)
icon = icon and icon .. " " or ''
+
icon = icon and icon .. ' ' or ''
local text = classDef and classDef.labels.short or "???"
+
local text = classDef and classDef.labels.short or '???'
 
--^ "???" is defaulting placeholder
 
--^ "???" is defaulting placeholder
 
text = classDef and classDef.category and string.format(
 
text = classDef and classDef.category and string.format(
"[[:%s|%s]]",
+
'[[:%s|%s]]',
 
classDef.category,
 
classDef.category,
 
text
 
text

Revision as of 00:42, 16 December 2016

Usage

This module will implement Template:Class, Template:Class/icon and Template:Class/colour. {{#invoke:Class|function_name}}



-- This module implements [[Template:Class]], [[Template:Class/icon]] and
-- [[Template:Class/colour]].

local mArguments, mIcon -- modules to lazily load
local definitions = mw.loadData('Module:Class/importDefinitions')

local p = {}

-- Initialize helper function variables
local getDefinition, getRawArgs, makeInvokeFunction, normalizeValue

--------------------------------------------------------------------------------
-- Argument helper functions
--------------------------------------------------------------------------------

function getRawArgs(frame, wrapper)
	mArguments = mArguments or require('Module:Arguments')
	return mArguments.getArgs(frame, {
		wrappers = wrapper,
		trim = false,
		removeBlanks = false
	})
end

function makeInvokeFunction(func, wrapper)
	return function (frame)
		local args = getRawArgs(frame, wrapper)
		return func(args)
	end
end

--------------------------------------------------------------------------------
-- String helper functions
--------------------------------------------------------------------------------

function normalizeValue(val)
	if type(val) == 'string' then val = val:match('^%s*(.-)%s*$'):lower() end
	if val == '' then val = nil end
	return val
end

--------------------------------------------------------------------------------
-- Definition helper functions
--------------------------------------------------------------------------------

function getDefinition(code)
	local canonicalCode = normalizeValue(code)
	local class = definitions[canonicalCode]
	while class and class.alias do
		canonicalCode = class.alias
		class = definitions[class.alias]
	end
	if not class then
		return nil, nil
	end
	return class, canonicalCode
end

--------------------------------------------------------------------------------
-- Color functions
--------------------------------------------------------------------------------

function p._colour(code)
	local class = getDefinition(code)
	return class and class.colour or 'transparent'
	--^ no defaulting's in definition yet; "transparent"'s a placeholder
end

function p.colour(frame)
	local args = getRawArgs(frame, 'Template:Class/colour')
	local colour = p._colour(args[1])
	-- We need nowiki tags as template output beginning with "#" triggers
	-- bug 14974.
	return frame:extensionTag('nowiki', colour)
end

--------------------------------------------------------------------------------
-- Icon functions
--------------------------------------------------------------------------------

function p._icon(args)
	local class = getDefinition(args.class or args[1])
	local icon = class and class.icon
	if icon and icon.file then
		local span = mw.html.create('span')
		span
			:cssText(args.style)
			:attr('title', class.labels.full)
			:wikitext(string.format(
				'[[File:%s|%s|16px%s]]',
				icon.file,
				class.labels.full,
				icon.requiresAttribution and '' or '|link=|alt='
				--^ recall that '' is truthy in Lua
			))
		return tostring(span)
	else
		mIcon = require('Module:Icon')
		return mIcon._main{args[1]}
	end
end

p.icon = makeInvokeFunction(p._icon, 'Template:Class/icon')

--------------------------------------------------------------------------------
-- Class functions
--------------------------------------------------------------------------------

function p._class(args)
	local classDef, classCode = getDefinition(args.class or args[1])
	local cell = mw.html.create(normalizeValue(args.heading) and 'th' or 'td')
	local icon = p.icon(args)
	icon = icon and icon .. ' ' or ''
	local text = classDef and classDef.labels.short or '???'
	--^ "???" is defaulting placeholder
	text = classDef and classDef.category and string.format(
			'[[:%s|%s]]',
			classDef.category,
			text
		) or text
	cell
		:addClass('assess')
		:addClass('assess-' .. (classCode or ''))
		:css('text-align', 'center')
		:css('white-space', 'nowrap')
		:css('font-weight', args.bold ~= 'no' and 'bold' or nil)
		:css('background', p._colour(classCode))
		:wikitext(icon, text)

		return tostring(cell)
end

p.class = makeInvokeFunction(p._class, 'Template:Class')

return p