Module:Userbox
![]() | This Lua module is used on approximately 330,000 pages, or roughly 1% of all pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
![]() | This module depends on the following other modules: |
This module does the processing for three userbox templates, {{userbox}}, {{userbox-2}} and {{userbox-r}}.
Template | Description | Examples |
---|---|---|
{{userbox}} | Makes userboxes with an id on the left-hand side, or with no id. | Lua error in package.lua at line 80: module 'Module:HtmlBuilder' not found.
Lua error in package.lua at line 80: module 'Module:HtmlBuilder' not found. |
{{userbox-2}} | Makes userboxes with an id on both the left- and right-hand sides. | Lua error in package.lua at line 80: module 'Module:HtmlBuilder' not found. |
{{userbox-r}} | Makes userboxes with an id on the right-hand side. | Lua error in package.lua at line 80: module 'Module:HtmlBuilder' not found. |
To use any of these templates from a wiki page, please see the individual template pages for documentation. To generate userboxes directly from Lua, read on.
Generating userboxes from Lua
To generate a userbox directly from Lua, first load the module.
local userbox = require('Module:Userbox')
You can then run any of the three templates with the code:
userbox.main(functionName, args)
For {{userbox}} use the function name "_userbox
"; for {{userbox-2}} use the function name "_userbox-2
"; and for {{userbox-r}} use the function name "_userbox-r
". The args
parameter is a table of arguments to pass to the different userbox functions. To see a list of valid arguments, please consult the individual template pages.
Tracking categories
-- 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 makeFormatFunction(suffix)
return function (num)
if num then
return tostring(num) .. suffix
else
return nil
end
end
end
local formatpx = makeFormatFunction('px')
local formatpt = makeFormatFunction('pt')
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 args = getArgs(frame)
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 userbox = p[funcName](args)
local cats = p.categories(args)
return userbox .. (cats or '')
end
function p._userbox(args)
local data = {}
-- Get div tag values.
data.float = args.float or 'left'
local borderWidth = args['border-width'] or args['border-s']
data.borderWidth = checkNumVal(borderWidth, 1)
data.borderColor = args['border-color'] or args[1] or args['border-c'] or args['id-c'] or '#999'
data.width = 240 - 2 * data.borderWidth -- Also used in the table tag.
data.bodyClass = args.bodyclass
-- Get table tag values.
data.backgroundColor = args['info-background'] or args[2] or args['info-c'] or '#eee'
-- Get id values.
data.id = args.logo or args[3] or args.id
local idWidth = args['logo-width'] or args['id-w'] or '45'
data.idWidth = checkNumVal(idWidth, 45)
local idHeight = args['logo-height'] or args['id-h']
data.idHeight = checkNumVal(idHeight, 45)
data.idBackgroundColor = args['logo-background'] or args[1] or args['id-c'] or '#ddd'
data.idTextAlign = args['id-a'] or 'center'
local idFontSize = args['logo-size'] or args[5] or args['id-s']
data.idFontSize = checkNumVal(idFontSize, 14)
data.idColor = args['logo-color'] or args['id-fc'] or 'black'
data.idPadding = args['logo-padding'] or args['id-p'] or '0 1px 0 0'
data.idLineHeight = args['logo-line-height'] or args['id-lh'] or '1.25em'
data.idOtherParams = args['logo-other-param'] or args['id-op']
data.idClass = args['id-class']
-- Get info values.
data.info = args.info or args[4] or "''info''"
data.infoTextAlign = args['info-a'] or 'left'
local infoFontSize = args['info-size'] or args['info-s']
data.infoFontSize = checkNumVal(infoFontSize, 8)
local infoHeight = args['logo-height'] or args['id-h']
data.infoHeight = checkNumVal(infoHeight, 45)
data.infoPadding = args['info-padding'] or args['info-p'] or '0 4px 0 4px'
data.infoLineHeight = args['info-line-height'] or args['info-lh'] or '1.25em'
data.infoColor = args['info-color'] or args['info-fc'] or 'black'
data.infoOtherParams = args['info-other-param'] or args['info-op']
data.infoClass = args['info-class']
return p.render(data)
end
p['_userbox-2'] = function (args)
end
p['_userbox-r'] = function (args)
end
function p.render(data)
-- Render the div tag html.
local root = htmlBuilder.create('div')
root
.css('float', data.float)
.css('border', (formatpx(data.borderWidth) or '') .. ' solid ' .. (data.borderColor or ''))
.css('margin', '1px')
.css('width', formatpx(data.width))
.addClass('wikipediauserbox')
.addClass(data.bodyClass)
-- Render the table tag html.
local tableroot = root.tag('table')
tableroot
.css('border-collapse', 'collapse')
.css('width', formatpx(data.width))
.css('margin-bottom', '0')
.css('background', data.backgroundColor)
-- Render the id html.
local tablerow = tableroot.tag('tr')
if id then
tablerow.tag('th')
.css('border', '0')
.css('width', formatpx(data.idWidth))
.css('height', formatpx(data.idHeight))
.css('background', data.idBackgroundColor)
.css('text-align', data.idTextAlign)
.css('font-size', formatpt(data.idFontSize))
.css('color', data.idColor)
.css('padding', data.idPadding)
.css('line-height', data.idLineHeight)
.css('vertical-align', 'middle')
.cssText(data.idOtherParams)
.addClass(data.idClass)
.wikitext(data.id)
end
-- Render the info html.
tablerow.tag('td')
.css('border', '0')
.css('text-align', data.infoTextAlign)
.css('font-size', formatpt(data.infoFontSize))
.css('padding', data.infoPadding)
.css('height', formatpx(data.infoHeight))
.css('line-height', data.infoLineHeight)
.css('color', data.infoColor)
.css('vertical-align', 'middle')
.cssText(data.infoOtherParams)
.addClass(data.infoClass)
.wikitext(data.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