Jump to content
Wikipedia The Free Encyclopedia

Module:Userbox

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
Warning This Lua module is used on approximately 336,000 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.
id {{{info}}}
{{{info}}}
{{userbox-2 }} Makes userboxes with an id on both the left- and right-hand sides.
id1 {{{info}}} id2
{{userbox-r }} Makes userboxes with an id on the right-hand side.
{{{info}}} id

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.

localuserbox=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

The above documentation is transcluded from Module:Userbox/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Subpages of this module.

 -- This module implements {{userbox}}.

 localcategoryHandler=require('Module:Category handler').main

 localp={}

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

 localfunctioncheckNum(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)
 ifvalandval>=0then
 returnval
 else
 returndefault
 end
 end

 localfunctionaddSuffix(num,suffix)
 -- Turns a number into a string and adds a suffix.
 ifnumthen
 returntostring(num)..suffix
 else
 returnnil
 end
 end

 localfunctioncheckNumAndAddSuffix(num,default,suffix)
 -- Checks a value with checkNum and adds a suffix.
 num=checkNum(num,default)
 returnaddSuffix(num,suffix)
 end

 localfunctionmakeCat(cat,sort)
 -- Makes a category link.
 ifsortthen
 returnmw.ustring.format('[[Category:%s|%s]]',cat,sort)
 else
 returnmw.ustring.format('[[Category:%s]]',cat)
 end
 end

 --------------------------------------------------------------------------------
 -- Argument processing
 --------------------------------------------------------------------------------

 localfunctionmakeInvokeFunc(funcName)
 returnfunction(frame)
 localorigArgs=require('Module:Arguments').getArgs(frame)
 localargs={}
 fork,vinpairs(origArgs)do
 args[k]=v
 end
 returnp.main(funcName,args)
 end
 end

 p.userbox=makeInvokeFunc('_userbox')
 p['userbox-2']=makeInvokeFunc('_userbox-2')
 p['userbox-r']=makeInvokeFunc('_userbox-r')

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

 functionp.main(funcName,args)
 localuserboxData=p[funcName](args)
 localuserbox=p.render(userboxData)
 localcats=p.categories(args)
 returnuserbox..(catsor'')
 end

 functionp._userbox(args)
 -- Does argument processing for {{userbox}}.
 localdata={}

 -- Get div tag values.
 data.float=args.float
 localborderWidthNum=checkNum(args['border-width']orargs['border-s'],nil)
 data.borderWidth=addSuffix(borderWidthNum,'px')
 data.borderColor=args['border-color']orargs['border-c']orargs[1]orargs['id-c']
 data.width=borderWidthNumandaddSuffix(240-2*borderWidthNum,'px')
 data.bodyClass=args.bodyclass

 -- Get table tag values.
 data.backgroundColor=args['info-background']orargs[2]orargs['info-c']

 -- Get info values.
 data.info=args.infoorargs[4]or"<code>{{{info}}}</code>"
 data.infoTextAlign=args['info-a']
 data.infoFontSize=checkNumAndAddSuffix(args['info-size']orargs['info-s'],nil,'pt')
 data.infoHeight=checkNumAndAddSuffix(args['logo-height']orargs['id-h'],nil,'px')
 data.infoPadding=args['info-padding']orargs['info-p']
 data.infoLineHeight=args['info-line-height']orargs['info-lh']
 data.infoColor=args['info-color']orargs['info-fc']
 data.infoOtherParams=args['info-other-param']orargs['info-op']
 data.infoClass=args['info-class']

 -- Get id values.
 localid=args.logoorargs[3]orargs.id
 data.id=id
 data.showId=idandtrueorfalse
 data.idWidth=checkNumAndAddSuffix(args['logo-width']orargs['id-w'],nil,'px')
 data.idHeight=checkNumAndAddSuffix(args['logo-height']orargs['id-h'],nil,'px')
 data.idBackgroundColor=args['logo-background']orargs[1]orargs['id-c']
 data.idTextAlign=args['id-a']
 data.idFontSize=checkNum(args['logo-size']orargs[5]orargs['id-s'],nil)
 data.idColor=args['logo-color']orargs['id-fc']ordata.infoColor
 data.idPadding=args['logo-padding']orargs['id-p']
 data.idLineHeight=args['logo-line-height']orargs['id-lh']
 data.idOtherParams=args['logo-other-param']orargs['id-op']
 data.idClass=args['id-class']

 returndata
 end

 p['_userbox-2']=function(args)
 -- Does argument processing for {{userbox-2}}.
 localdata={}

 -- Get div tag values.
 data.float=args.float
 localborderWidthNum=checkNum(args['border-s']orargs[9],nil)
 data.borderWidth=addSuffix(borderWidthNum,'px')
 data.borderColor=args['border-c']orargs[6]orargs['id1-c']orargs[1]
 data.width=borderWidthNumandaddSuffix(240-2*borderWidthNum,'px')
 data.bodyClass=args.bodyclass

 -- Get table tag values.
 data.backgroundColor=args['info-c']orargs[2]

 -- Get info values.
 data.info=args.infoorargs[4]or"<code>{{{info}}}</code>"
 data.infoTextAlign=args['info-a']
 data.infoFontSize=checkNumAndAddSuffix(args['info-s'],nil,'pt')
 data.infoColor=args['info-fc']orargs[8]
 data.infoPadding=args['info-p']
 data.infoLineHeight=args['info-lh']
 data.infoOtherParams=args['info-op']

 -- Get id values.
 data.showId=true
 data.id=args.logoorargs[3]orargs.id1or'id1'
 data.idWidth=checkNumAndAddSuffix(args['id1-w'],nil,'px')
 data.idHeight=checkNumAndAddSuffix(args['id-h'],nil,'px')
 data.idBackgroundColor=args['id1-c']orargs[1]
 data.idTextAlign=args['id-a']
 data.idFontSize=checkNum(args['id1-s'],nil)
 data.idLineHeight=args['id1-lh']
 data.idColor=args['id1-fc']ordata.infoColor
 data.idPadding=args['id1-p']
 data.idOtherParams=args['id1-op']

 -- Get id2 values.
 data.showId2=true
 data.id2=args.logoorargs[5]orargs.id2or'id2'
 data.id2Width=checkNumAndAddSuffix(args['id2-w'],nil,'px')
 data.id2Height=data.idHeight
 data.id2BackgroundColor=args['id2-c']orargs[7]orargs[1]
 data.id2TextAlign=nil-- Always center, but don't set if no content
 data.id2FontSize=checkNum(args['id2-s'],nil)
 data.id2LineHeight=args['id2-lh']
 data.id2Color=args['id2-fc']ordata.infoColor
 data.id2Padding=args['id2-p']
 data.id2OtherParams=args['id2-op']

 returndata
 end

 p['_userbox-r']=function(args)
 -- Does argument processing for {{userbox-r}}.
 localdata={}

 -- Get div tag values.
 data.float=args.float
 localborderWidthNum=checkNum(args['border-width']orargs['border-s'],nil)
 data.borderWidth=addSuffix(borderWidthNum,'px')
 data.borderColor=args['border-color']orargs['border-c']orargs[1]orargs['id-c']
 data.width=borderWidthNumandaddSuffix(240-2*borderWidthNum,'px')
 data.bodyClass=args.bodyclass

 -- Get table tag values.
 data.backgroundColor=args['info-background']orargs[2]orargs['info-c']

 -- Get id values.
 data.showId=false-- We only show id2 in userbox-r.

 -- Get info values.
 data.info=args.infoorargs[4]or"<code>{{{info}}}</code>"
 data.infoTextAlign=args['info-align']orargs['info-a']
 data.infoFontSize=checkNumAndAddSuffix(args['info-size']orargs['info-s'],nil,'pt')
 data.infoPadding=args['info-padding']orargs['info-p']
 data.infoLineHeight=args['info-line-height']orargs['info-lh']
 data.infoColor=args['info-color']orargs['info-fc']
 data.infoOtherParams=args['info-other-param']orargs['info-op']

 -- Get id2 values.
 data.showId2=true-- userbox-r always shows the ID cell (as id2)
 data.id2=args.logoorargs[3]orargs.idor'id'
 data.id2Width=checkNumAndAddSuffix(args['logo-width']orargs['id-w'],nil,'px')
 data.id2Height=checkNumAndAddSuffix(args['logo-height']orargs['id-h'],nil,'px')
 data.id2BackgroundColor=args['logo-background']orargs[1]orargs['id-c']
 data.id2TextAlign=args['id-a']
 data.id2FontSize=checkNum(args['logo-size']orargs[5]orargs['id-s'],nil)
 data.id2Color=args['logo-color']orargs['id-fc']ordata.infoColor
 data.id2Padding=args['logo-padding']orargs['id-p']
 data.id2LineHeight=args['logo-line-height']orargs['id-lh']
 data.id2OtherParams=args['logo-other-param']orargs['id-op']

 returndata
 end

 functionp.render(data)
 -- Renders the userbox html using the content of the data table.
 -- Add TemplateStyles
 localtemplateStyles=mw.getCurrentFrame():extensionTag{
 name='templatestyles',args={src='Template:Userbox/styles.css'}
 }

 -- Render the div tag html.
 localroot=mw.html.create('div')
 root:addClass('wikipediauserbox')

 -- Only add CSS properties if they have values
 ifdata.floatthenroot:css('float',data.float)end
 ifdata.borderWidththen
 root:css('border-width',data.borderWidth)
 end
 ifdata.borderColorthen
 root:css('border-color',data.borderColor)
 -- Set border style if we have a color but potentially no width set
 ifnotdata.borderWidththen
 root:css('border-width','1px')-- default border width
 end
 root:css('border-style','solid')
 end
 ifdata.widththenroot:css('width',data.width)end
 ifdata.bodyClassthenroot:addClass(data.bodyClass)end

 -- Render the table tag html.
 localtableroot=root:tag('table')
 tableroot:attr('role','presentation')

 -- Only add CSS if values exist
 ifdata.widththentableroot:css('width',data.width)end
 ifdata.backgroundColorthentableroot:css('background',data.backgroundColor):css('color','inherit')end

 localtablerow=tableroot:tag('tr')

 -- Create cells based on what should actually be shown
 ifdata.showId==falsethen
 -- userbox-r: info cell first, then id2 cell (if showId2 is true)
 localinfoCell=tablerow:tag('td')
 infoCell:addClass('userbox-info')
 ifdata.infoTextAligntheninfoCell:css('text-align',data.infoTextAlign)end
 ifdata.infoFontSizetheninfoCell:css('font-size',data.infoFontSize)end
 ifdata.infoPaddingtheninfoCell:css('padding',data.infoPadding)end
 ifdata.infoHeighttheninfoCell:css('height',data.infoHeight)end
 ifdata.infoLineHeighttheninfoCell:css('line-height',data.infoLineHeight)end
 ifdata.infoColortheninfoCell:css('color',data.infoColor)end
 ifdata.infoOtherParamstheninfoCell:cssText(data.infoOtherParams)end
 ifdata.infoClasstheninfoCell:addClass(data.infoClass)end
 ifdata.infotheninfoCell:wikitext(data.info)end

 -- Create id2 cell only if showId2 is true
 ifdata.showId2then
 localid2Cell=tablerow:tag('td')
 id2Cell:addClass('userbox-id2')
 ifdata.id2Widththenid2Cell:css('width',data.id2Width)end
 ifdata.id2Heightthenid2Cell:css('height',data.id2Height)end
 ifdata.id2BackgroundColorthenid2Cell:css('background',data.id2BackgroundColor)end
 ifdata.id2TextAlignthenid2Cell:css('text-align',data.id2TextAlign)end
 ifdata.id2FontSizethenid2Cell:css('font-size',data.id2FontSize..'pt')end
 ifdata.id2Colorthenid2Cell:css('color',data.id2Color)elseid2Cell:css('color','inherit')end
 ifdata.id2Paddingthenid2Cell:css('padding',data.id2Padding)end
 ifdata.id2LineHeightthenid2Cell:css('line-height',data.id2LineHeight)end
 ifdata.id2OtherParamsthenid2Cell:cssText(data.id2OtherParams)end
 ifdata.id2thenid2Cell:wikitext(data.id2)end
 end
 else
 -- userbox and userbox-2: create id cell only if showId is true
 ifdata.showIdthen
 localidCell=tablerow:tag('td')
 idCell:addClass('userbox-id')
 ifdata.idWidththenidCell:css('width',data.idWidth)end
 ifdata.idHeightthenidCell:css('height',data.idHeight)end
 ifdata.idBackgroundColorthenidCell:css('background',data.idBackgroundColor)end
 ifdata.idTextAlignthenidCell:css('text-align',data.idTextAlign)end
 ifdata.idFontSizethenidCell:css('font-size',data.idFontSize..'pt')end
 ifdata.idColorthenidCell:css('color',data.idColor)elseidCell:css('color','inherit')end
 ifdata.idPaddingthenidCell:css('padding',data.idPadding)end
 ifdata.idLineHeightthenidCell:css('line-height',data.idLineHeight)end
 ifdata.idOtherParamsthenidCell:cssText(data.idOtherParams)end
 ifdata.idClassthenidCell:addClass(data.idClass)end
 ifdata.idthenidCell:wikitext(data.id)end
 end

 -- Always create info cell for userbox and userbox-2
 localinfoCell=tablerow:tag('td')
 infoCell:addClass('userbox-info')
 ifdata.infoTextAligntheninfoCell:css('text-align',data.infoTextAlign)end
 ifdata.infoFontSizetheninfoCell:css('font-size',data.infoFontSize)end
 ifdata.infoPaddingtheninfoCell:css('padding',data.infoPadding)end
 ifdata.infoHeighttheninfoCell:css('height',data.infoHeight)end
 ifdata.infoLineHeighttheninfoCell:css('line-height',data.infoLineHeight)end
 ifdata.infoColortheninfoCell:css('color',data.infoColor)end
 ifdata.infoOtherParamstheninfoCell:cssText(data.infoOtherParams)end
 ifdata.infoClasstheninfoCell:addClass(data.infoClass)end
 ifdata.infotheninfoCell:wikitext(data.info)end

 -- Create id2 cell only for userbox-2 and only if showId2 is true
 ifdata.showId2then
 localid2Cell=tablerow:tag('td')
 id2Cell:addClass('userbox-id2')
 ifdata.id2Widththenid2Cell:css('width',data.id2Width)end
 ifdata.id2Heightthenid2Cell:css('height',data.id2Height)end
 ifdata.id2BackgroundColorthenid2Cell:css('background',data.id2BackgroundColor)end
 ifdata.id2TextAlignthenid2Cell:css('text-align',data.id2TextAlign)end
 ifdata.id2FontSizethenid2Cell:css('font-size',data.id2FontSize..'pt')end
 ifdata.id2Colorthenid2Cell:css('color',data.id2Color)elseid2Cell:css('color','inherit')end
 ifdata.id2Paddingthenid2Cell:css('padding',data.id2Padding)end
 ifdata.id2LineHeightthenid2Cell:css('line-height',data.id2LineHeight)end
 ifdata.id2OtherParamsthenid2Cell:cssText(data.id2OtherParams)end
 ifdata.id2thenid2Cell:wikitext(data.id2)end
 end
 end

 localtitle=mw.title.getCurrentTitle()
 if(title.namespace==2)andnottitle.text:match("/")then
 returntemplateStyles..tostring(root)-- regular user page
 elseiftitle.namespace==14then
 returntemplateStyles..tostring(root)-- category
 elseiftitle.isTalkPagethen
 returntemplateStyles..tostring(root)-- talk page
 end

 -- Color contrast checking (simplified for TemplateStyles)
 locallegible=true
 localcontrast=require('Module:Color contrast')._ratio
 localfunctionhas_text(wikitext)
 ifnotwikitextthenreturnfalseend
 wikitext=wikitext:gsub("]]","|]]")
 wikitext=wikitext:gsub("%[%[%s*[Mm][Ee][Dd][Ii][Aa]%s*:[^|]-(|.-)]]","")
 wikitext=wikitext:gsub("%[%[%s*[Ii][Mm][Aa][Gg][Ee]%s*:[^|]-(|.-)]]","")
 wikitext=wikitext:gsub("%[%[%s*[Ff][Ii][Ll][Ee]%s*:[^|]-(|.-)]]","")
 returnmw.text.trim(wikitext)~=""
 end

 ifdata.infoColoranddata.backgroundColorandcontrast{data.infoColor,data.backgroundColor,error=0}<4.5then
 legible=false
 end

 -- For bold text >= 14pt, requirement is only 3.
 localidContrastThreshold=4.5
 localid2ContrastThreshold=4.5
 if(data.idFontSizeor0)>=14thenidContrastThreshold=3end
 if(data.id2FontSizeor0)>=14thenid2ContrastThreshold=3end

 ifdata.showIdanddata.idColoranddata.idBackgroundColorandcontrast{data.idColor,data.idBackgroundColor,error=0}<idContrastThresholdthen
 ifhas_text(data.idor"")thenlegible=falseend
 end

 ifdata.showId2anddata.id2Coloranddata.id2BackgroundColorandcontrast{data.id2Color,data.id2BackgroundColor,error=0}<id2ContrastThresholdthen
 ifhas_text(data.id2or"")thenlegible=falseend
 end

 ifnotlegiblethen
 root:wikitext('[[Category:Userboxes with insufficient color contrast]]')
 end

 returntemplateStyles..tostring(root)
 end

 functionp.categories(args,page)
 -- Gets categories from [[Module:Category handler]].
 -- The page parameter makes the function act as though the module was being called from that page.
 -- It is included for testing purposes.
 localcats={}
 cats[#cats+1]=args.usercategory
 cats[#cats+1]=args.usercategory2
 cats[#cats+1]=args.usercategory3
 cats[#cats+1]=args.usercategory4
 cats[#cats+1]=args.usercategory5

 -- Get the title object
 localtitle
 ifpagethen
 title=mw.title.new(page)
 else
 title=mw.title.getCurrentTitle()
 end

 -- Build category handler arguments.
 localchargs={}
 chargs.page=page
 chargs.nocat=args.nocat
 chargs.main='[[Category:Pages with templates in the wrong namespace]]'
 ifargs.notcatsubpagesthen
 chargs.subpage='no'
 end

 -- User namespace.
 localuser=''
 fori,catinipairs(cats)do
 user=user..makeCat(cat)
 end
 chargs.user=user

 returncategoryHandler(chargs)
 end

 returnp

AltStyle によって変換されたページ (->オリジナル) /