https://en.wikipedia.org/w/index.php?action=history&feed=atom&title=Module%3AUserboxWide
Module:UserboxWide - Revision history
2025-06-01T17:47:58Z
Revision history for this page on the wiki
MediaWiki 1.45.0-wmf.3
https://en.wikipedia.org/w/index.php?title=Module:UserboxWide&diff=1254630052&oldid=prev
D4n2016: increased by 40px, WIP
2024-10-31T22:46:38Z
<p>increased by 40px, WIP</p>
<p><b>New page</b></p><div>-- This module implements {{userbox}}.<br />
<br />
local categoryHandler = require('Module:Category handler').main<br />
<br />
local p = {}<br />
<br />
--------------------------------------------------------------------------------<br />
-- Helper functions<br />
--------------------------------------------------------------------------------<br />
<br />
local function checkNum(val, default)<br />
-- Checks whether a value is a number greater than or equal to zero. If so,<br />
-- returns it as a number. If not, returns a default value.<br />
val = tonumber(val)<br />
if val and val >= 0 then<br />
return val<br />
else<br />
return default<br />
end<br />
end<br />
<br />
local function addSuffix(num, suffix)<br />
-- Turns a number into a string and adds a suffix.<br />
if num then<br />
return tostring(num) .. suffix<br />
else<br />
return nil<br />
end<br />
end<br />
<br />
local function checkNumAndAddSuffix(num, default, suffix)<br />
-- Checks a value with checkNum and adds a suffix.<br />
num = checkNum(num, default)<br />
return addSuffix(num, suffix)<br />
end<br />
<br />
local function makeCat(cat, sort)<br />
-- Makes a category link.<br />
if sort then<br />
return mw.ustring.format('[[Category:%s|%s]]', cat, sort)<br />
else<br />
return mw.ustring.format('[[Category:%s]]', cat)<br />
end<br />
end<br />
<br />
--------------------------------------------------------------------------------<br />
-- Argument processing<br />
--------------------------------------------------------------------------------<br />
<br />
local function makeInvokeFunc(funcName)<br />
return function (frame)<br />
local origArgs = require('Module:Arguments').getArgs(frame)<br />
local args = {}<br />
for k, v in pairs(origArgs) do<br />
args[k] = v<br />
end<br />
return p.main(funcName, args)<br />
end<br />
end<br />
<br />
p.userbox = makeInvokeFunc('_userbox')<br />
p['userbox-2'] = makeInvokeFunc('_userbox-2')<br />
p['userbox-r'] = makeInvokeFunc('_userbox-r')<br />
<br />
--------------------------------------------------------------------------------<br />
-- Main functions<br />
--------------------------------------------------------------------------------<br />
<br />
function p.main(funcName, args)<br />
local userboxData = p[funcName](args)<br />
local userbox = p.render(userboxData)<br />
local cats = p.categories(args)<br />
return userbox .. (cats or '')<br />
end<br />
<br />
function p._userbox(args)<br />
-- Does argument processing for {{userbox}}.<br />
local data = {}<br />
<br />
-- Get div tag values.<br />
data.float = args.float or 'left'<br />
local borderWidthNum = checkNum(args['border-width'] or args['border-s'], 1) -- Used to calculate width.<br />
data.borderWidth = addSuffix(borderWidthNum, 'px')<br />
data.borderColor = args['border-color'] or args['border-c'] or args[1] or args['id-c'] or '#999'<br />
data.width = addSuffix(280 - 2 * borderWidthNum, 'px') -- Also used in the table tag.<br />
data.bodyClass = args.bodyclass<br />
<br />
-- Get table tag values.<br />
data.backgroundColor = args['info-background'] or args[2] or args['info-c'] or '#eee'<br />
<br />
-- Get info values.<br />
data.info = args.info or args[4] or "<code>{{{info}}}</code>"<br />
data.infoTextAlign = args['info-a'] or 'left'<br />
data.infoFontSize = checkNumAndAddSuffix(args['info-size'] or args['info-s'], 8, 'pt')<br />
data.infoHeight = checkNumAndAddSuffix(args['logo-height'] or args['id-h'], 45, 'px')<br />
data.infoPadding = args['info-padding'] or args['info-p'] or '0 4px 0 4px'<br />
data.infoLineHeight = args['info-line-height'] or args['info-lh'] or '1.25em'<br />
data.infoColor = args['info-color'] or args['info-fc'] or 'black'<br />
data.infoOtherParams = args['info-other-param'] or args['info-op']<br />
data.infoClass = args['info-class']<br />
<br />
-- Get id values.<br />
local id = args.logo or args[3] or args.id<br />
data.id = id<br />
data.showId = id and true or false<br />
data.idWidth = checkNumAndAddSuffix(args['logo-width'] or args['id-w'], 45, 'px')<br />
data.idHeight = checkNumAndAddSuffix(args['logo-height'] or args['id-h'], 45, 'px')<br />
data.idBackgroundColor = args['logo-background'] or args[1] or args['id-c'] or '#ddd'<br />
data.idTextAlign = args['id-a'] or 'center'<br />
data.idFontSize = checkNum(args['logo-size'] or args[5] or args['id-s'], 14)<br />
data.idColor = args['logo-color'] or args['id-fc'] or data.infoColor<br />
data.idPadding = args['logo-padding'] or args['id-p'] or '0 1px 0 0'<br />
data.idLineHeight = args['logo-line-height'] or args['id-lh'] or '1.25em'<br />
data.idOtherParams = args['logo-other-param'] or args['id-op']<br />
data.idClass = args['id-class']<br />
<br />
return data<br />
end<br />
<br />
p['_userbox-2'] = function (args)<br />
-- Does argument processing for {{userbox-2}}.<br />
local data = {}<br />
<br />
-- Get div tag values.<br />
data.float = args.float or 'left'<br />
local borderWidthNum = checkNum(args['border-s'] or args[9], 1) -- Used to calculate width.<br />
data.borderWidth = addSuffix(borderWidthNum, 'px')<br />
data.borderColor = args['border-c'] or args[6] or args['id1-c'] or args[1] or '#999999'<br />
data.width = addSuffix(280 - 2 * borderWidthNum, 'px') -- Also used in the table tag.<br />
data.bodyClass = args.bodyclass<br />
<br />
-- Get table tag values.<br />
data.backgroundColor = args['info-c'] or args[2] or '#eeeeee'<br />
<br />
-- Get info values.<br />
data.info = args.info or args[4] or "<code>{{{info}}}</code>"<br />
data.infoTextAlign = args['info-a'] or 'left'<br />
data.infoFontSize = checkNumAndAddSuffix(args['info-s'], 8, 'pt')<br />
data.infoColor = args['info-fc'] or args[8] or 'black'<br />
data.infoPadding = args['info-p'] or '0 4px 0 4px'<br />
data.infoLineHeight = args['info-lh'] or '1.25em'<br />
data.infoOtherParams = args['info-op']<br />
<br />
-- Get id values.<br />
data.showId = true<br />
data.id = args.logo or args[3] or args.id1 or 'id1'<br />
data.idWidth = checkNumAndAddSuffix(args['id1-w'], 45, 'px')<br />
data.idHeight = checkNumAndAddSuffix(args['id-h'], 45, 'px')<br />
data.idBackgroundColor = args['id1-c'] or args[1] or '#dddddd'<br />
data.idTextAlign = 'center'<br />
data.idFontSize = checkNum(args['id1-s'], 14)<br />
data.idLineHeight = args['id1-lh'] or '1.25em'<br />
data.idColor = args['id1-fc'] or data.infoColor<br />
data.idPadding = args['id1-p'] or '0 1px 0 0'<br />
data.idOtherParams = args['id1-op']<br />
<br />
-- Get id2 values.<br />
data.showId2 = true<br />
data.id2 = args.logo or args[5] or args.id2 or 'id2'<br />
data.id2Width = checkNumAndAddSuffix(args['id2-w'], 45, 'px')<br />
data.id2Height = data.idHeight<br />
data.id2BackgroundColor = args['id2-c'] or args[7] or args[1] or '#dddddd'<br />
data.id2TextAlign = 'center'<br />
data.id2FontSize = checkNum(args['id2-s'], 14)<br />
data.id2LineHeight = args['id2-lh'] or '1.25em'<br />
data.id2Color = args['id2-fc'] or data.infoColor<br />
data.id2Padding = args['id2-p'] or '0 0 0 1px'<br />
data.id2OtherParams = args['id2-op']<br />
<br />
return data<br />
end<br />
<br />
p['_userbox-r'] = function (args)<br />
-- Does argument processing for {{userbox-r}}.<br />
local data = {}<br />
<br />
-- Get div tag values.<br />
data.float = args.float or 'left'<br />
local borderWidthNum = checkNum(args['border-width'] or args['border-s'], 1) -- Used to calculate width.<br />
data.borderWidth = addSuffix(borderWidthNum, 'px')<br />
data.borderColor = args['border-color'] or args['border-c'] or args[1] or args['id-c'] or '#999'<br />
data.width = addSuffix(280 - 2 * borderWidthNum, 'px') -- Also used in the table tag.<br />
data.bodyClass = args.bodyclass<br />
<br />
-- Get table tag values.<br />
data.backgroundColor = args['info-background'] or args[2] or args['info-c'] or '#eee'<br />
<br />
-- Get id values.<br />
data.showId = false -- We only show id2 in userbox-r.<br />
<br />
-- Get info values.<br />
data.info = args.info or args[4] or "<code>{{{info}}}</code>"<br />
data.infoTextAlign = args['info-align'] or args['info-a'] or 'left'<br />
data.infoFontSize = checkNumAndAddSuffix(args['info-size'] or args['info-s'], 8, 'pt')<br />
data.infoPadding = args['info-padding'] or args['info-p'] or '0 4px 0 4px'<br />
data.infoLineHeight = args['info-line-height'] or args['info-lh'] or '1.25em'<br />
data.infoColor = args['info-color'] or args['info-fc'] or 'black'<br />
data.infoOtherParams = args['info-other-param'] or args['info-op']<br />
<br />
-- Get id2 values.<br />
data.showId2 = true<br />
data.id2 = args.logo or args[3] or args.id or 'id'<br />
data.id2Width = checkNumAndAddSuffix(args['logo-width'] or args['id-w'], 45, 'px')<br />
data.id2Height = checkNumAndAddSuffix(args['logo-height'] or args['id-h'], 45, 'px')<br />
data.id2BackgroundColor = args['logo-background'] or args[1] or args['id-c'] or '#ddd'<br />
data.id2TextAlign = args['id-a'] or 'center'<br />
data.id2FontSize = checkNum(args['logo-size'] or args[5] or args['id-s'], 14)<br />
data.id2Color = args['logo-color'] or args['id-fc'] or data.infoColor<br />
data.id2Padding = args['logo-padding'] or args['id-p'] or '0 0 0 1px'<br />
data.id2LineHeight = args['logo-line-height'] or args['id-lh'] or '1.25em'<br />
data.id2OtherParams = args['logo-other-param'] or args['id-op']<br />
<br />
return data<br />
end<br />
<br />
function p.render(data)<br />
-- Renders the userbox html using the content of the data table. <br />
-- Render the div tag html.<br />
local root = mw.html.create('div')<br />
root<br />
:css('float', data.float)<br />
:css('border', (data.borderWidth or '') .. ' solid ' .. (data.borderColor or ''))<br />
:css('margin', '1px')<br />
:css('width', data.width)<br />
:addClass('wikipediauserbox')<br />
:addClass(data.bodyClass)<br />
<br />
-- Render the table tag html.<br />
local tableroot = root:tag('table')<br />
tableroot<br />
:attr('role', 'presentation')<br />
:css('border-collapse', 'collapse')<br />
:css('width', data.width)<br />
:css('margin-bottom', '0')<br />
:css('margin-top', '0')<br />
:css('background', data.backgroundColor)<br />
:css('color', 'inherit')<br />
<br />
-- Render the id html.<br />
local tablerow = tableroot:tag('tr')<br />
if data.showId then<br />
tablerow:tag('td')<br />
:css('border', '0')<br />
:css('width', data.idWidth)<br />
:css('height', data.idHeight)<br />
:css('background', data.idBackgroundColor)<br />
:css('text-align', data.idTextAlign)<br />
:css('font-size', data.idFontSize .. 'pt')<br />
:css('font-weight', 'bold')<br />
:css('color', data.idColor)<br />
:css('padding', data.idPadding)<br />
:css('line-height', data.idLineHeight)<br />
:css('vertical-align', 'middle')<br />
:cssText(data.idOtherParams)<br />
:addClass(data.idClass)<br />
:wikitext(data.id)<br />
end<br />
<br />
-- Render the info html.<br />
tablerow:tag('td')<br />
:css('border', '0')<br />
:css('text-align', data.infoTextAlign)<br />
:css('font-size', data.infoFontSize)<br />
:css('padding', data.infoPadding)<br />
:css('height', data.infoHeight)<br />
:css('line-height', data.infoLineHeight)<br />
:css('color', data.infoColor)<br />
:css('vertical-align', 'middle')<br />
:cssText(data.infoOtherParams)<br />
:addClass(data.infoClass)<br />
:wikitext(data.info)<br />
<br />
-- Render the second id html.<br />
if data.showId2 then<br />
tablerow:tag('td')<br />
:css('border', '0')<br />
:css('width', data.id2Width)<br />
:css('height', data.id2Height)<br />
:css('background', data.id2BackgroundColor)<br />
:css('text-align', data.id2TextAlign)<br />
:css('font-size', data.id2FontSize .. 'pt')<br />
:css('font-weight', 'bold')<br />
:css('color', data.id2Color)<br />
:css('padding', data.id2Padding)<br />
:css('line-height', data.id2LineHeight)<br />
:css('vertical-align', 'middle')<br />
:cssText(data.id2OtherParams)<br />
:wikitext(data.id2)<br />
end<br />
<br />
local title = mw.title.getCurrentTitle()<br />
if (title.namespace == 2) and not title.text:match("/") then<br />
return tostring(root) -- regular user page<br />
elseif title.namespace == 14 then<br />
return tostring(root) -- category<br />
elseif title.isTalkPage then<br />
return tostring(root) -- talk page<br />
end<br />
<br />
local legible = true<br />
local contrast = require('Module:Color contrast')._ratio<br />
<br />
local function has_text(wikitext)<br />
wikitext = wikitext:gsub("]]", "|]]")<br />
wikitext = wikitext:gsub("%[%[%s*[Mm][Ee][Dd][Ii][Aa]%s*:[^|]-(|.-)]]", "")<br />
wikitext = wikitext:gsub("%[%[%s*[Ii][Mm][Aa][Gg][Ee]%s*:[^|]-(|.-)]]", "")<br />
wikitext = wikitext:gsub("%[%[%s*[Ff][Ii][Ll][Ee]%s*:[^|]-(|.-)]]", "")<br />
return mw.text.trim(wikitext) ~= ""<br />
end<br />
<br />
if contrast { data.infoColor, data.backgroundColor, error = 0 } < 4.5 then<br />
legible = false<br />
end<br />
<br />
-- For bold text >= 14pt, requirement is only 3.<br />
local idContrastThreshold = 4.5<br />
local id2ContrastThreshold = 4.5<br />
if (data.idFontSize or 0) >= 14 then<br />
idContrastThreshold = 3<br />
end<br />
if (data.id2FontSize or 0) >= 14 then<br />
id2ContrastThreshold = 3<br />
end<br />
<br />
if data.showId and contrast { data.idColor, data.idBackgroundColor, error = 0 } < idContrastThreshold then<br />
if has_text(data.id or "") then<br />
legible = false<br />
end<br />
end<br />
<br />
if data.showId2 and contrast { data.id2Color, data.id2BackgroundColor, error = 0 } < id2ContrastThreshold then<br />
if has_text(data.id2 or "") then<br />
legible = false<br />
end<br />
end<br />
<br />
if not legible then<br />
root:wikitext('[[Category:Userboxes with insufficient color contrast]]')<br />
end<br />
<br />
return tostring(root)<br />
end<br />
<br />
function p.categories(args, page)<br />
-- Gets categories from [[Module:Category handler]].<br />
-- The page parameter makes the function act as though the module was being called from that page.<br />
-- It is included for testing purposes.<br />
local cats = {}<br />
cats[#cats + 1] = args.usercategory<br />
cats[#cats + 1] = args.usercategory2<br />
cats[#cats + 1] = args.usercategory3<br />
cats[#cats + 1] = args.usercategory4<br />
cats[#cats + 1] = args.usercategory5<br />
-- Get the title object<br />
local title<br />
if page then<br />
title = mw.title.new(page)<br />
else<br />
title = mw.title.getCurrentTitle()<br />
end<br />
-- Build category handler arguments.<br />
local chargs = {}<br />
chargs.page = page<br />
chargs.nocat = args.nocat<br />
chargs.main = '[[Category:Pages with templates in the wrong namespace]]'<br />
if args.notcatsubpages then<br />
chargs.subpage = 'no'<br />
end<br />
-- User namespace.<br />
local user = ''<br />
for i, cat in ipairs(cats) do<br />
user = user .. makeCat(cat)<br />
end<br />
chargs.user = user<br />
-- Template namespace.<br />
local basepage = title.baseText<br />
local template = ''<br />
for i, cat in ipairs(cats) do<br />
template = template .. makeCat(cat, ' ' .. basepage)<br />
end<br />
chargs.template = template<br />
return categoryHandler(chargs)<br />
end<br />
<br />
return p</div>
D4n2016