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(&#039;Module:Category handler&#039;).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 &gt;= 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(&#039;[[Category:%s|%s]]&#039;, cat, sort)<br /> else<br /> return mw.ustring.format(&#039;[[Category:%s]]&#039;, 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(&#039;Module:Arguments&#039;).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(&#039;_userbox&#039;)<br /> p[&#039;userbox-2&#039;] = makeInvokeFunc(&#039;_userbox-2&#039;)<br /> p[&#039;userbox-r&#039;] = makeInvokeFunc(&#039;_userbox-r&#039;)<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 &#039;&#039;)<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 &#039;left&#039;<br /> local borderWidthNum = checkNum(args[&#039;border-width&#039;] or args[&#039;border-s&#039;], 1) -- Used to calculate width.<br /> data.borderWidth = addSuffix(borderWidthNum, &#039;px&#039;)<br /> data.borderColor = args[&#039;border-color&#039;] or args[&#039;border-c&#039;] or args[1] or args[&#039;id-c&#039;] or &#039;#999&#039;<br /> data.width = addSuffix(280 - 2 * borderWidthNum, &#039;px&#039;) -- Also used in the table tag.<br /> data.bodyClass = args.bodyclass<br /> <br /> -- Get table tag values.<br /> data.backgroundColor = args[&#039;info-background&#039;] or args[2] or args[&#039;info-c&#039;] or &#039;#eee&#039;<br /> <br /> -- Get info values.<br /> data.info = args.info or args[4] or &quot;&lt;code&gt;{{{info}}}&lt;/code&gt;&quot;<br /> data.infoTextAlign = args[&#039;info-a&#039;] or &#039;left&#039;<br /> data.infoFontSize = checkNumAndAddSuffix(args[&#039;info-size&#039;] or args[&#039;info-s&#039;], 8, &#039;pt&#039;)<br /> data.infoHeight = checkNumAndAddSuffix(args[&#039;logo-height&#039;] or args[&#039;id-h&#039;], 45, &#039;px&#039;)<br /> data.infoPadding = args[&#039;info-padding&#039;] or args[&#039;info-p&#039;] or &#039;0 4px 0 4px&#039;<br /> data.infoLineHeight = args[&#039;info-line-height&#039;] or args[&#039;info-lh&#039;] or &#039;1.25em&#039;<br /> data.infoColor = args[&#039;info-color&#039;] or args[&#039;info-fc&#039;] or &#039;black&#039;<br /> data.infoOtherParams = args[&#039;info-other-param&#039;] or args[&#039;info-op&#039;]<br /> data.infoClass = args[&#039;info-class&#039;]<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[&#039;logo-width&#039;] or args[&#039;id-w&#039;], 45, &#039;px&#039;)<br /> data.idHeight = checkNumAndAddSuffix(args[&#039;logo-height&#039;] or args[&#039;id-h&#039;], 45, &#039;px&#039;)<br /> data.idBackgroundColor = args[&#039;logo-background&#039;] or args[1] or args[&#039;id-c&#039;] or &#039;#ddd&#039;<br /> data.idTextAlign = args[&#039;id-a&#039;] or &#039;center&#039;<br /> data.idFontSize = checkNum(args[&#039;logo-size&#039;] or args[5] or args[&#039;id-s&#039;], 14)<br /> data.idColor = args[&#039;logo-color&#039;] or args[&#039;id-fc&#039;] or data.infoColor<br /> data.idPadding = args[&#039;logo-padding&#039;] or args[&#039;id-p&#039;] or &#039;0 1px 0 0&#039;<br /> data.idLineHeight = args[&#039;logo-line-height&#039;] or args[&#039;id-lh&#039;] or &#039;1.25em&#039;<br /> data.idOtherParams = args[&#039;logo-other-param&#039;] or args[&#039;id-op&#039;]<br /> data.idClass = args[&#039;id-class&#039;]<br /> <br /> return data<br /> end<br /> <br /> p[&#039;_userbox-2&#039;] = function (args)<br /> -- Does argument processing for {{userbox-2}}.<br /> local data = {}<br /> <br /> -- Get div tag values.<br /> data.float = args.float or &#039;left&#039;<br /> local borderWidthNum = checkNum(args[&#039;border-s&#039;] or args[9], 1) -- Used to calculate width.<br /> data.borderWidth = addSuffix(borderWidthNum, &#039;px&#039;)<br /> data.borderColor = args[&#039;border-c&#039;] or args[6] or args[&#039;id1-c&#039;] or args[1] or &#039;#999999&#039;<br /> data.width = addSuffix(280 - 2 * borderWidthNum, &#039;px&#039;) -- Also used in the table tag.<br /> data.bodyClass = args.bodyclass<br /> <br /> -- Get table tag values.<br /> data.backgroundColor = args[&#039;info-c&#039;] or args[2] or &#039;#eeeeee&#039;<br /> <br /> -- Get info values.<br /> data.info = args.info or args[4] or &quot;&lt;code&gt;{{{info}}}&lt;/code&gt;&quot;<br /> data.infoTextAlign = args[&#039;info-a&#039;] or &#039;left&#039;<br /> data.infoFontSize = checkNumAndAddSuffix(args[&#039;info-s&#039;], 8, &#039;pt&#039;)<br /> data.infoColor = args[&#039;info-fc&#039;] or args[8] or &#039;black&#039;<br /> data.infoPadding = args[&#039;info-p&#039;] or &#039;0 4px 0 4px&#039;<br /> data.infoLineHeight = args[&#039;info-lh&#039;] or &#039;1.25em&#039;<br /> data.infoOtherParams = args[&#039;info-op&#039;]<br /> <br /> -- Get id values.<br /> data.showId = true<br /> data.id = args.logo or args[3] or args.id1 or &#039;id1&#039;<br /> data.idWidth = checkNumAndAddSuffix(args[&#039;id1-w&#039;], 45, &#039;px&#039;)<br /> data.idHeight = checkNumAndAddSuffix(args[&#039;id-h&#039;], 45, &#039;px&#039;)<br /> data.idBackgroundColor = args[&#039;id1-c&#039;] or args[1] or &#039;#dddddd&#039;<br /> data.idTextAlign = &#039;center&#039;<br /> data.idFontSize = checkNum(args[&#039;id1-s&#039;], 14)<br /> data.idLineHeight = args[&#039;id1-lh&#039;] or &#039;1.25em&#039;<br /> data.idColor = args[&#039;id1-fc&#039;] or data.infoColor<br /> data.idPadding = args[&#039;id1-p&#039;] or &#039;0 1px 0 0&#039;<br /> data.idOtherParams = args[&#039;id1-op&#039;]<br /> <br /> -- Get id2 values.<br /> data.showId2 = true<br /> data.id2 = args.logo or args[5] or args.id2 or &#039;id2&#039;<br /> data.id2Width = checkNumAndAddSuffix(args[&#039;id2-w&#039;], 45, &#039;px&#039;)<br /> data.id2Height = data.idHeight<br /> data.id2BackgroundColor = args[&#039;id2-c&#039;] or args[7] or args[1] or &#039;#dddddd&#039;<br /> data.id2TextAlign = &#039;center&#039;<br /> data.id2FontSize = checkNum(args[&#039;id2-s&#039;], 14)<br /> data.id2LineHeight = args[&#039;id2-lh&#039;] or &#039;1.25em&#039;<br /> data.id2Color = args[&#039;id2-fc&#039;] or data.infoColor<br /> data.id2Padding = args[&#039;id2-p&#039;] or &#039;0 0 0 1px&#039;<br /> data.id2OtherParams = args[&#039;id2-op&#039;]<br /> <br /> return data<br /> end<br /> <br /> p[&#039;_userbox-r&#039;] = function (args)<br /> -- Does argument processing for {{userbox-r}}.<br /> local data = {}<br /> <br /> -- Get div tag values.<br /> data.float = args.float or &#039;left&#039;<br /> local borderWidthNum = checkNum(args[&#039;border-width&#039;] or args[&#039;border-s&#039;], 1) -- Used to calculate width.<br /> data.borderWidth = addSuffix(borderWidthNum, &#039;px&#039;)<br /> data.borderColor = args[&#039;border-color&#039;] or args[&#039;border-c&#039;] or args[1] or args[&#039;id-c&#039;] or &#039;#999&#039;<br /> data.width = addSuffix(280 - 2 * borderWidthNum, &#039;px&#039;) -- Also used in the table tag.<br /> data.bodyClass = args.bodyclass<br /> <br /> -- Get table tag values.<br /> data.backgroundColor = args[&#039;info-background&#039;] or args[2] or args[&#039;info-c&#039;] or &#039;#eee&#039;<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 &quot;&lt;code&gt;{{{info}}}&lt;/code&gt;&quot;<br /> data.infoTextAlign = args[&#039;info-align&#039;] or args[&#039;info-a&#039;] or &#039;left&#039;<br /> data.infoFontSize = checkNumAndAddSuffix(args[&#039;info-size&#039;] or args[&#039;info-s&#039;], 8, &#039;pt&#039;)<br /> data.infoPadding = args[&#039;info-padding&#039;] or args[&#039;info-p&#039;] or &#039;0 4px 0 4px&#039;<br /> data.infoLineHeight = args[&#039;info-line-height&#039;] or args[&#039;info-lh&#039;] or &#039;1.25em&#039;<br /> data.infoColor = args[&#039;info-color&#039;] or args[&#039;info-fc&#039;] or &#039;black&#039;<br /> data.infoOtherParams = args[&#039;info-other-param&#039;] or args[&#039;info-op&#039;]<br /> <br /> -- Get id2 values.<br /> data.showId2 = true<br /> data.id2 = args.logo or args[3] or args.id or &#039;id&#039;<br /> data.id2Width = checkNumAndAddSuffix(args[&#039;logo-width&#039;] or args[&#039;id-w&#039;], 45, &#039;px&#039;)<br /> data.id2Height = checkNumAndAddSuffix(args[&#039;logo-height&#039;] or args[&#039;id-h&#039;], 45, &#039;px&#039;)<br /> data.id2BackgroundColor = args[&#039;logo-background&#039;] or args[1] or args[&#039;id-c&#039;] or &#039;#ddd&#039;<br /> data.id2TextAlign = args[&#039;id-a&#039;] or &#039;center&#039;<br /> data.id2FontSize = checkNum(args[&#039;logo-size&#039;] or args[5] or args[&#039;id-s&#039;], 14)<br /> data.id2Color = args[&#039;logo-color&#039;] or args[&#039;id-fc&#039;] or data.infoColor<br /> data.id2Padding = args[&#039;logo-padding&#039;] or args[&#039;id-p&#039;] or &#039;0 0 0 1px&#039;<br /> data.id2LineHeight = args[&#039;logo-line-height&#039;] or args[&#039;id-lh&#039;] or &#039;1.25em&#039;<br /> data.id2OtherParams = args[&#039;logo-other-param&#039;] or args[&#039;id-op&#039;]<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(&#039;div&#039;)<br /> root<br /> :css(&#039;float&#039;, data.float)<br /> :css(&#039;border&#039;, (data.borderWidth or &#039;&#039;) .. &#039; solid &#039; .. (data.borderColor or &#039;&#039;))<br /> :css(&#039;margin&#039;, &#039;1px&#039;)<br /> :css(&#039;width&#039;, data.width)<br /> :addClass(&#039;wikipediauserbox&#039;)<br /> :addClass(data.bodyClass)<br /> <br /> -- Render the table tag html.<br /> local tableroot = root:tag(&#039;table&#039;)<br /> tableroot<br /> :attr(&#039;role&#039;, &#039;presentation&#039;)<br /> :css(&#039;border-collapse&#039;, &#039;collapse&#039;)<br /> :css(&#039;width&#039;, data.width)<br /> :css(&#039;margin-bottom&#039;, &#039;0&#039;)<br /> :css(&#039;margin-top&#039;, &#039;0&#039;)<br /> :css(&#039;background&#039;, data.backgroundColor)<br /> :css(&#039;color&#039;, &#039;inherit&#039;)<br /> <br /> -- Render the id html.<br /> local tablerow = tableroot:tag(&#039;tr&#039;)<br /> if data.showId then<br /> tablerow:tag(&#039;td&#039;)<br /> :css(&#039;border&#039;, &#039;0&#039;)<br /> :css(&#039;width&#039;, data.idWidth)<br /> :css(&#039;height&#039;, data.idHeight)<br /> :css(&#039;background&#039;, data.idBackgroundColor)<br /> :css(&#039;text-align&#039;, data.idTextAlign)<br /> :css(&#039;font-size&#039;, data.idFontSize .. &#039;pt&#039;)<br /> :css(&#039;font-weight&#039;, &#039;bold&#039;)<br /> :css(&#039;color&#039;, data.idColor)<br /> :css(&#039;padding&#039;, data.idPadding)<br /> :css(&#039;line-height&#039;, data.idLineHeight)<br /> :css(&#039;vertical-align&#039;, &#039;middle&#039;)<br /> :cssText(data.idOtherParams)<br /> :addClass(data.idClass)<br /> :wikitext(data.id)<br /> end<br /> <br /> -- Render the info html.<br /> tablerow:tag(&#039;td&#039;)<br /> :css(&#039;border&#039;, &#039;0&#039;)<br /> :css(&#039;text-align&#039;, data.infoTextAlign)<br /> :css(&#039;font-size&#039;, data.infoFontSize)<br /> :css(&#039;padding&#039;, data.infoPadding)<br /> :css(&#039;height&#039;, data.infoHeight)<br /> :css(&#039;line-height&#039;, data.infoLineHeight)<br /> :css(&#039;color&#039;, data.infoColor)<br /> :css(&#039;vertical-align&#039;, &#039;middle&#039;)<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(&#039;td&#039;)<br /> :css(&#039;border&#039;, &#039;0&#039;)<br /> :css(&#039;width&#039;, data.id2Width)<br /> :css(&#039;height&#039;, data.id2Height)<br /> :css(&#039;background&#039;, data.id2BackgroundColor)<br /> :css(&#039;text-align&#039;, data.id2TextAlign)<br /> :css(&#039;font-size&#039;, data.id2FontSize .. &#039;pt&#039;)<br /> :css(&#039;font-weight&#039;, &#039;bold&#039;)<br /> :css(&#039;color&#039;, data.id2Color)<br /> :css(&#039;padding&#039;, data.id2Padding)<br /> :css(&#039;line-height&#039;, data.id2LineHeight)<br /> :css(&#039;vertical-align&#039;, &#039;middle&#039;)<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(&quot;/&quot;) 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(&#039;Module:Color contrast&#039;)._ratio<br /> <br /> local function has_text(wikitext)<br /> wikitext = wikitext:gsub(&quot;]]&quot;, &quot;|]]&quot;)<br /> wikitext = wikitext:gsub(&quot;%[%[%s*[Mm][Ee][Dd][Ii][Aa]%s*:[^|]-(|.-)]]&quot;, &quot;&quot;)<br /> wikitext = wikitext:gsub(&quot;%[%[%s*[Ii][Mm][Aa][Gg][Ee]%s*:[^|]-(|.-)]]&quot;, &quot;&quot;)<br /> wikitext = wikitext:gsub(&quot;%[%[%s*[Ff][Ii][Ll][Ee]%s*:[^|]-(|.-)]]&quot;, &quot;&quot;)<br /> return mw.text.trim(wikitext) ~= &quot;&quot;<br /> end<br /> <br /> if contrast { data.infoColor, data.backgroundColor, error = 0 } &lt; 4.5 then<br /> legible = false<br /> end<br /> <br /> -- For bold text &gt;= 14pt, requirement is only 3.<br /> local idContrastThreshold = 4.5<br /> local id2ContrastThreshold = 4.5<br /> if (data.idFontSize or 0) &gt;= 14 then<br /> idContrastThreshold = 3<br /> end<br /> if (data.id2FontSize or 0) &gt;= 14 then<br /> id2ContrastThreshold = 3<br /> end<br /> <br /> if data.showId and contrast { data.idColor, data.idBackgroundColor, error = 0 } &lt; idContrastThreshold then<br /> if has_text(data.id or &quot;&quot;) then<br /> legible = false<br /> end<br /> end<br /> <br /> if data.showId2 and contrast { data.id2Color, data.id2BackgroundColor, error = 0 } &lt; id2ContrastThreshold then<br /> if has_text(data.id2 or &quot;&quot;) then<br /> legible = false<br /> end<br /> end<br /> <br /> if not legible then<br /> root:wikitext(&#039;[[Category:Userboxes with insufficient color contrast]]&#039;)<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 = &#039;[[Category:Pages with templates in the wrong namespace]]&#039;<br /> if args.notcatsubpages then<br /> chargs.subpage = &#039;no&#039;<br /> end<br /> -- User namespace.<br /> local user = &#039;&#039;<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 = &#039;&#039;<br /> for i, cat in ipairs(cats) do<br /> template = template .. makeCat(cat, &#039; &#039; .. basepage)<br /> end<br /> chargs.template = template<br /> return categoryHandler(chargs)<br /> end<br /> <br /> return p</div> D4n2016