Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Please sign up or log in to edit the wiki.
-- Modified version of Module:Userbox from English Wikipedia
-- Orignally imported on 2024-06-24
-- @see https://en.wikipedia.org/w/index.php?title=Module:Userbox&oldid=1215046832
--
-- Modificaitons
-- - Use <div> instead of <table>
-- - Move default inline styles to TemplateStyles
-- - Removed category handler because it is overkill, we only need the category on User namespace
-- - Removed contrast checker because it only works on HEX colors
-- - Simplify return logic

-- This module implements {{userbox}}.

local p = {}

--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------

local function checkNum(val, default)
	-- Checks whether a value is a number greater than or equal to zero. If so,
	-- returns it as a number. If not, returns a default value.
	val = tonumber(val)
	if val and val >= 0 then
		return val
	else
		return default
	end
end

local function addSuffix(num, suffix)
	-- Turns a number into a string and adds a suffix.
	if num then
		return tostring(num) .. suffix
	else
		return nil
	end
end

local function checkNumAndAddSuffix(num, suffix)
	-- Checks a value with checkNum and adds a suffix.
	num = checkNum(num)
	return addSuffix(num, suffix)
end

local function makeCat(cat, sort)
	-- Makes a category link.
	if sort then
		return mw.ustring.format('[[Category:%s|%s]]', cat, sort)
	else
		return mw.ustring.format('[[Category:%s]]', cat)
	end
end

--------------------------------------------------------------------------------
-- Argument processing
--------------------------------------------------------------------------------

local function makeInvokeFunc(funcName)
	return function(frame)
		local origArgs = require('Module:Arguments').getArgs(frame)
		local args = {}
		for k, v in pairs(origArgs) do
			args[k] = v
		end
		return p.main(funcName, args)
	end
end

p.userbox = makeInvokeFunc('_userbox')
p['userbox-2'] = makeInvokeFunc('_userbox-2')
p['userbox-r'] = makeInvokeFunc('_userbox-r')

--------------------------------------------------------------------------------
-- Main functions
--------------------------------------------------------------------------------

function p.main(funcName, args)
	local userboxData = p[funcName](args)
	local userbox = p.render(userboxData)
	local cats = p.categories(args)
	return userbox .. (cats or '')
end

function p._userbox(args)
	-- Does argument processing for {{userbox}}.
	local data = {}

	-- Get div tag values.
	data.float = args.float
	local borderWidthNum = checkNum(args['border-width'] or args['border-s']) -- Used to calculate width.
	data.borderWidth = addSuffix(borderWidthNum, 'px')
	data.borderColor = args['border-color'] or args['border-c'] or args[1] or args['id-c']
	data.bodyClass = args.bodyclass
	data.backgroundColor = args['info-background'] or args[2] or args['info-c']

	-- Get info values.
	data.info = args.info or args[4] or "<code>{{{info}}}</code>"
	data.infoTextAlign = args['info-a']
	data.infoFontSize = checkNumAndAddSuffix(args['info-size'] or args['info-s'], 'px')
	data.infoHeight = checkNumAndAddSuffix(args['logo-height'] or args['id-h'], 'px')
	data.infoPadding = args['info-padding'] or args['info-p']
	data.infoLineHeight = args['info-line-height'] or args['info-lh']
	data.infoColor = args['info-color'] or args['info-fc']
	data.infoOtherParams = args['info-other-param'] or args['info-op']
	data.infoClass = args['info-class']

	-- Get id values.
	local id = args.logo or args[3] or args.id
	data.id = id
	data.showId = id and true or false
	data.idWidth = checkNumAndAddSuffix(args['logo-width'] or args['id-w'], 'px')
	data.idHeight = checkNumAndAddSuffix(args['logo-height'] or args['id-h'], 'px')
	data.idBackgroundColor = args['logo-background'] or args[1] or args['id-c']
	data.idTextAlign = args['id-a']
	data.idFontSize = checkNumAndAddSuffix(args['logo-size'] or args[5] or args['id-s'], 'px')
	data.idColor = args['logo-color'] or args['id-fc'] or data.infoColor
	data.idPadding = args['logo-padding'] or args['id-p']
	data.idLineHeight = args['logo-line-height'] or args['id-lh']
	data.idOtherParams = args['logo-other-param'] or args['id-op']
	data.idClass = args['id-class']

	return data
end

p['_userbox-2'] = function(args)
	-- Does argument processing for {{userbox-2}}.
	local data = {}

	-- Get div tag values.
	data.float = args.float or 'left'
	local borderWidthNum = checkNum(args['border-s'] or args[9]) -- Used to calculate width.
	data.borderWidth = addSuffix(borderWidthNum, 'px')
	data.borderColor = args['border-c'] or args[6] or args['id1-c'] or args[1]
	data.bodyClass = args.bodyclass
	data.backgroundColor = args['info-c'] or args[2]

	-- Get info values.
	data.info = args.info or args[4] or "<code>{{{info}}}</code>"
	data.infoTextAlign = args['info-a']
	data.infoFontSize = checkNumAndAddSuffix(args['info-s'], 'px')
	data.infoColor = args['info-fc'] or args[8]
	data.infoPadding = args['info-p']
	data.infoLineHeight = args['info-lh']
	data.infoOtherParams = args['info-op']

	-- Get id values.
	data.showId = true
	data.id = args.logo or args[3] or args.id1 or 'id1'
	data.idWidth = checkNumAndAddSuffix(args['id1-w'], 'px')
	data.idHeight = checkNumAndAddSuffix(args['id-h'], 'px')
	data.idBackgroundColor = args['id1-c'] or args[1]
	data.idFontSize = checkNumAndAddSuffix(args['id1-s'], 'px')
	data.idLineHeight = args['id1-lh']
	data.idColor = args['id1-fc'] or data.infoColor
	data.idPadding = args['id1-p']
	data.idOtherParams = args['id1-op']

	-- Get id2 values.
	data.showId2 = true
	data.id2 = args.logo or args[5] or args.id2 or 'id2'
	data.id2Width = checkNumAndAddSuffix(args['id2-w'], 'px')
	data.id2Height = data.idHeight
	data.id2BackgroundColor = args['id2-c'] or args[7] or args[1]
	data.id2TextAlign = 'center'
	data.id2FontSize = checkNum(args['id2-s'], 14)
	data.id2LineHeight = args['id2-lh']
	data.id2Color = args['id2-fc'] or data.infoColor
	data.id2Padding = args['id2-p']
	data.id2OtherParams = args['id2-op']

	return data
end

p['_userbox-r'] = function(args)
	-- Does argument processing for {{userbox-r}}.
	local data = {}

	-- Get div tag values.
	data.float = args.float or 'left'
	local borderWidthNum = checkNum(args['border-width'] or args['border-s']) -- Used to calculate width.
	data.borderWidth = addSuffix(borderWidthNum, 'px')
	data.borderColor = args['border-color'] or args['border-c'] or args[1] or args['id-c']
	data.bodyClass = args.bodyclass
	data.backgroundColor = args['info-background'] or args[2] or args['info-c']

	-- Get id values.
	data.showId = false -- We only show id2 in userbox-r.

	-- Get info values.
	data.info = args.info or args[4] or "<code>{{{info}}}</code>"
	data.infoTextAlign = args['info-align'] or args['info-a'] or 'left'
	data.infoFontSize = checkNumAndAddSuffix(args['info-size'] or args['info-s'], 'px')
	data.infoPadding = args['info-padding'] or args['info-p']
	data.infoLineHeight = args['info-line-height'] or args['info-lh']
	data.infoColor = args['info-color'] or args['info-fc']
	data.infoOtherParams = args['info-other-param'] or args['info-op']

	-- Get id2 values.
	data.showId2 = true
	data.id2 = args.logo or args[3] or args.id or 'id'
	data.id2Width = checkNumAndAddSuffix(args['logo-width'] or args['id-w'], 'px')
	data.id2Height = checkNumAndAddSuffix(args['logo-height'] or args['id-h'], 'px')
	data.id2BackgroundColor = args['logo-background'] or args[1] or args['id-c']
	data.id2TextAlign = args['id-a']
	data.id2FontSize = checkNum(args['logo-size'] or args[5] or args['id-s'])
	data.id2Color = args['logo-color'] or args['id-fc'] or data.infoColor
	data.id2Padding = args['logo-padding'] or args['id-p']
	data.id2LineHeight = args['logo-line-height'] or args['id-lh']
	data.id2OtherParams = args['logo-other-param'] or args['id-op']

	return data
end

function p.render(data)
	-- Renders the userbox html using the content of the data.
	-- Render the div tag html.
	local root = mw.html.create('div')
	root
		:addClass('template-userbox')
		:addClass('metadata')
		:addClass(data.bodyClass)
		:css('float', data.float)
		:css('border-width', data.borderWidth)
		:css('border-color', data.borderColor)
		:css('background', data.backgroundColor)

	if data.showId then
		root:tag('div')
			:addClass('template-userbox-id')
			:addClass('template-userbox-id-1')
			:css('width', data.idWidth)
			:css('height', data.idHeight)
			:css('background', data.idBackgroundColor)
			:css('text-align', data.idTextAlign)
			:css('font-size', data.idFontSize)
			:css('color', data.idColor)
			:css('padding', data.idPadding)
			:css('line-height', data.idLineHeight)
			:cssText(data.idOtherParams)
			:addClass(data.idClass)
			:tag('span')
				:wikitext(data.id)
				:done()
	end

	-- Render the info html.
	root:tag('div')
		:addClass('template-userbox-info')
		:css('text-align', data.infoTextAlign)
		:css('font-size', data.infoFontSize)
		:css('padding', data.infoPadding)
		:css('height', data.infoHeight)
		:css('line-height', data.infoLineHeight)
		:css('color', data.infoColor)
		:cssText(data.infoOtherParams)
		:addClass(data.infoClass)
		:tag('span')
				:wikitext(data.info)
				:done()
		

	-- Render the second id html.
	if data.showId2 then
		root:tag('div')
			:addClass('template-userbox-id')
			:addClass('template-userbox-id-2')
			:css('width', data.id2Width)
			:css('height', data.id2Height)
			:css('background', data.id2BackgroundColor)
			:css('text-align', data.id2TextAlign)
			:css('font-size', data.id2FontSize .. 'pt')
			:css('color', data.id2Color)
			:css('padding', data.id2Padding)
			:css('line-height', data.id2LineHeight)
			:cssText(data.id2OtherParams)
			:tag('span')
				:wikitext(data.id2)
				:done()
	end

	local styles = mw.getCurrentFrame():extensionTag {
		name = 'templatestyles', args = { src = 'Module:Userbox/styles.css' }
	}
	local output = tostring(root) .. styles

	return output
end

function p.categories(args, page)
	-- Gets categories from [[Module:Category handler]].
	-- The page parameter makes the function act as though the module was being called from that page.
	-- It is included for testing purposes.
	local cats = {}
	cats[#cats + 1] = args.usercategory
	cats[#cats + 1] = args.usercategory2
	cats[#cats + 1] = args.usercategory3
	cats[#cats + 1] = args.usercategory4
	cats[#cats + 1] = args.usercategory5
	-- Get the title object
	local title
	if page then
		title = mw.title.new(page)
	else
		title = mw.title.getCurrentTitle()
	end

	local wikitext = ''

	if title.nsText == 'User' then
		for i, cat in ipairs(cats) do
			wikitext = wikitext .. makeCat(cat)
		end
	end

	return wikitext
end

return p