Jump to content

Module:Userbox

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 04:25, 6 January 2014 (add id). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module implements {{userbox}}.

local getArgs = require('Module:Arguments').getArgs
local htmlBuilder = require('Module:HtmlBuilder')
local catHander = require('Module:Category handler')

local p = {}

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


local function checkNumVal(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 topx(num)
	-- Formats a pixel number as a string appended with "px".
	return tostring(num) .. 'px'
end

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

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

function p._main(args)
	local userbox = p.userbox(args)
	local cats = p.categories(args)
	return userbox .. (cats or '')
end

function p.userbox(args)
	-- Get border width.
	local borderWidth = args['border-width'] or args['border-s']
	borderWidth = checkNumVal(borderWidth, 1)

	-- Get border color.
	local borderColor = args['border-color'] or args[1] or args['border-c'] or args['id-c'] or '#999'

	-- Get background color.
	local backgroundColor = args['info-background'] or args[2] or args['info-c'] or '#eee'

	-- Calculate width.
	local width = 240 - 2 * borderWidth

	-- Get the id.
	local id = args.logo or args[3] or args.id

	-- Get id width.
	local idWidth = args['logo-width'] or args['id-w'] or '45'
	idWidth = checkNumVal(idWidth, 45)

	-- Get id height.
	local idHeight = args['logo-height'] or args['id-h']
	idHeight = checkNumVal(idHeight, 45)

	-- Get id background color.
	local idBackgroundColor = args['logo-background'] or args[1] or args['id-c'] or '#ddd'

	-- Get id size.
	local idSize = args['logo-size'] or args[5] or args['id-s']
	idSize = checkNumVal(idSize, 14)

	-- Get id color.
	local idColor = args['logo-color'] or args['id-fc'] or 'black'

	-- Get id padding.
	local idPadding = args['logo-padding'] or args['id-p'] or '0 1px 0 0'

	-- Get id line height.
	local idLineHeight = args['logo-line-height'] or args['id-lh'] or '1.25em'

	-- Build the box.
	local root = htmlBuilder.create('div')
	root
		.css('float', args.float or 'left')
		.css('border', topx(borderWidth) .. ' solid ' .. borderColor)
		.css('margin', '1px')
		.css('width', topx(width))
		.addClass('wikipediauserbox')
		.addClass(args.bodyclass)

	local tableroot = root.tag('table')
	tableroot
		.css('border-collapse', 'collapse')
		.css('width', topx(width))
		.css('margin-bottom', '0')
		.css('background', backgroundColor)
	
	if id then
		tableroot.tag('tr')
			.tag('th')
				.css('border', '0')
				.css('width', topx(idWidth))
				.css('height', topx(idheight))
				.css('background', idBackgroundColor)
				.css('text-align', args['id-a'] or 'center')
				.css('font-size', tostring(idSize) .. 'pt')
				.css('color', idColor)
				.css('padding', idPadding)
				.css('line-height', idLineHeight)
				.css('vertical-align', 'middle')
				.cssText(args['logo-other-param'] or args['id-op'])
				.addClass(args['id-class'])
				.wikitext(id)
	end

	return tostring(root)
end

function p.categories(args)
end

return p