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 08:44, 6 January 2014 (create a "float" variable instead of doing the processing during rendering). 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 categoryHandler = require('Module:Category handler').main

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

local function formatpt(num)
	-- Formats a font size number as a string appended with "pt".
	return tostring(num) .. 'pt'
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

--------------------------------------------------------------------------------
-- 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 div tag values.
	local float = args.float or 'left'
	local borderWidth = args['border-width'] or args['border-s']
	borderWidth = checkNumVal(borderWidth, 1)
	local borderColor = args['border-color'] or args[1] or args['border-c'] or args['id-c'] or '#999'
	local width = 240 - 2 * borderWidth -- Also used in the table tag.
	local bodyClass = args.bodyclass

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

	-- Get id values.
	local id = args.logo or args[3] or args.id
	local idWidth = args['logo-width'] or args['id-w'] or '45'
	idWidth = checkNumVal(idWidth, 45)
	local idHeight = args['logo-height'] or args['id-h']
	idHeight = checkNumVal(idHeight, 45)
	local idBackgroundColor = args['logo-background'] or args[1] or args['id-c'] or '#ddd'
	local idTextAlign = args['id-a'] or 'center'
	local idFontSize = args['logo-size'] or args[5] or args['id-s']
	idFontSize = checkNumVal(idFontSize, 14)
	local idColor = args['logo-color'] or args['id-fc'] or 'black'
	local idPadding = args['logo-padding'] or args['id-p'] or '0 1px 0 0'
	local idLineHeight = args['logo-line-height'] or args['id-lh'] or '1.25em'
	local idOtherParams = args['logo-other-param'] or args['id-op']
	local idClass = args['id-class']

	-- Get info values.
	local info = args.info or args[4] or "''info''"
	local infoTextAlign = args['info-a'] or 'left'
	local infoFontSize = args['info-size'] or args['info-s']
	infoFontSize = checkNumVal(infoFontSize, 8)
	local infoHeight = args['logo-height'] or args['id-h']
	infoHeight = checkNumVal(infoHeight, 45)
	local infoPadding = args['info-padding'] or args['info-p'] or '0 4px 0 4px'
	local infoLineHeight = args['info-line-height'] or args['info-lh'] or '1.25em'
	local infoColor = args['info-color'] or args['info-fc'] or 'black'
	local infoOtherParams = args['info-other-param'] or args['info-op']
	local infoClass = args['info-class']

	-- Render the div tag html.
	local root = htmlBuilder.create('div')
	root
		.css('float', float)
		.css('border', formatpx(borderWidth) .. ' solid ' .. borderColor)
		.css('margin', '1px')
		.css('width', formatpx(width))
		.addClass('wikipediauserbox')
		.addClass(bodyClass)

	-- Render the table tag html.
	local tableroot = root.tag('table')
	tableroot
		.css('border-collapse', 'collapse')
		.css('width', formatpx(width))
		.css('margin-bottom', '0')
		.css('background', backgroundColor)
	
	-- Render the id html.
	local tablerow = tableroot.tag('tr')
	if id then
		tablerow.tag('th')
			.css('border', '0')
			.css('width', formatpx(idWidth))
			.css('height', formatpx(idHeight))
			.css('background', idBackgroundColor)
			.css('text-align', idTextAlign)
			.css('font-size', formatpt(idFontSize))
			.css('color', idColor)
			.css('padding', idPadding)
			.css('line-height', idLineHeight)
			.css('vertical-align', 'middle')
			.cssText(idOtherParams)
			.addClass(idClass)
			.wikitext(id)
	end

	-- Render the info html.
	tablerow.tag('td')
		.css('border', '0')
		.css('text-align', infoTextAlign)
		.css('font-size', formatpt(infoFontSize))
		.css('padding', infoPadding)
		.css('height', formatpx(infoHeight))
		.css('line-height', infoLineHeight)
		.css('color', infoColor)
		.css('vertical-align', 'middle')
		.cssText(infoOtherParams)
		.addClass(infoClass)
		.wikitext(info)

	return tostring(root)
end

function p.categories(args)
	local cats = {}
	cats[#cats + 1] = args.usercategory
	cats[#cats + 1] = args.usercategory2
	cats[#cats + 1] = args.usercategory3
	if #cats > 0 then
		-- Build category handler arguments.
		local chargs = {}
		chargs.nocat = args.nocat
		if args.notcatsubpages then
			chargs.subpage = 'no'
		end
		-- User namespace.
		local user = ''
		for i, cat in ipairs(cats) do
			user = user .. makeCat(cat)
		end
		chargs.user = user
		-- Template namespace.
		local basepage = mw.title.getCurrentTitle().baseText
		local template = ''
		for i, cat in ipairs(cats) do
			template = template .. makeCat(cat, basepage)
		end
		chargs.template = template
		return categoryHandler(chargs)
	else
		return nil
	end
end

return p