Jump to content

Module:Userbox/sandbox

From Wikipedia, the free encyclopedia
-- This module implements {{userbox}}.localcategoryHandler=require('Module:Category handler').mainlocalp={}---------------------------------------------------------------------------------- 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>=0thenreturnvalelsereturndefaultendendlocalfunctionaddSuffix(num,suffix)-- Turns a number into a string and adds a suffix.ifnumthenreturntostring(num)..suffixelsereturnnilendendlocalfunctioncheckNumAndAddSuffix(num,default,suffix)-- Checks a value with checkNum and adds a suffix.num=checkNum(num,default)returnaddSuffix(num,suffix)endlocalfunctionmakeCat(cat,sort)-- Makes a category link.ifsortthenreturnmw.ustring.format('[[Category:%s|%s]]',cat,sort)elsereturnmw.ustring.format('[[Category:%s]]',cat)endend---------------------------------------------------------------------------------- Argument processing--------------------------------------------------------------------------------localfunctionmakeInvokeFunc(funcName)returnfunction(frame)localorigArgs=require('Module:Arguments').getArgs(frame)localargs={}fork,vinpairs(origArgs)doargs[k]=vendreturnp.main(funcName,args)endendp.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'')endfunctionp._userbox(args)-- Does argument processing for {{userbox}}.localdata={}-- Get div tag values.data.float=args.floator'left'localborderWidthNum=checkNum(args['border-width']orargs['border-s'],1)-- Used to calculate width.data.borderWidth=addSuffix(borderWidthNum,'px')data.borderColor=args['border-color']orargs['border-c']orargs[1]orargs['id-c']or'#999'data.width=addSuffix(240-2*borderWidthNum,'px')-- Also used in the table tag.data.bodyClass=args.bodyclass-- Get table tag values.data.backgroundColor=args['info-background']orargs[2]orargs['info-c']or'#eee'-- Get info values.data.info=args.infoorargs[4]or"<code>{{{info}}}</code>"data.infoTextAlign=args['info-a']or'left'data.infoFontSize=checkNumAndAddSuffix(args['info-size']orargs['info-s'],8,'pt')data.infoHeight=checkNumAndAddSuffix(args['logo-height']orargs['id-h'],45,'px')data.infoPadding=args['info-padding']orargs['info-p']or'0 4px 0 4px'data.infoLineHeight=args['info-line-height']orargs['info-lh']or'1.25em'data.infoColor=args['info-color']orargs['info-fc']or'black'data.infoOtherParams=args['info-other-param']orargs['info-op']data.infoClass=args['info-class']-- Get id values.localid=args.logoorargs[3]orargs.iddata.id=iddata.showId=idandtrueorfalsedata.idWidth=checkNumAndAddSuffix(args['logo-width']orargs['id-w'],45,'px')data.idHeight=checkNumAndAddSuffix(args['logo-height']orargs['id-h'],45,'px')data.idBackgroundColor=args['logo-background']orargs[1]orargs['id-c']or'#ddd'data.idTextAlign=args['id-a']or'center'data.idFontSize=checkNum(args['logo-size']orargs[5]orargs['id-s'],14)data.idColor=args['logo-color']orargs['id-fc']ordata.infoColordata.idPadding=args['logo-padding']orargs['id-p']or'0 1px 0 0'data.idLineHeight=args['logo-line-height']orargs['id-lh']or'1.25em'data.idOtherParams=args['logo-other-param']orargs['id-op']data.idClass=args['id-class']returndataendp['_userbox-2']=function(args)-- Does argument processing for {{userbox-2}}.localdata={}-- Get div tag values.data.float=args.floator'left'localborderWidthNum=checkNum(args['border-s']orargs[9],1)-- Used to calculate width.data.borderWidth=addSuffix(borderWidthNum,'px')data.borderColor=args['border-c']orargs[6]orargs['id1-c']orargs[1]or'#999999'data.width=addSuffix(240-2*borderWidthNum,'px')-- Also used in the table tag.data.bodyClass=args.bodyclass-- Get table tag values.data.backgroundColor=args['info-c']orargs[2]or'#eeeeee'-- Get info values.data.info=args.infoorargs[4]or"<code>{{{info}}}</code>"data.infoTextAlign=args['info-a']or'left'data.infoFontSize=checkNumAndAddSuffix(args['info-s'],8,'pt')data.infoColor=args['info-fc']orargs[8]or'black'data.infoPadding=args['info-p']or'0 4px 0 4px'data.infoLineHeight=args['info-lh']or'1.25em'data.infoOtherParams=args['info-op']-- Get id values.data.showId=truedata.id=args.logoorargs[3]orargs.id1or'id1'data.idWidth=checkNumAndAddSuffix(args['id1-w'],45,'px')data.idHeight=checkNumAndAddSuffix(args['id-h'],45,'px')data.idBackgroundColor=args['id1-c']orargs[1]or'#dddddd'data.idTextAlign='center'data.idFontSize=checkNum(args['id1-s'],14)data.idLineHeight=args['id1-lh']or'1.25em'data.idColor=args['id1-fc']ordata.infoColordata.idPadding=args['id1-p']or'0 1px 0 0'data.idOtherParams=args['id1-op']-- Get id2 values.data.showId2=truedata.id2=args.logoorargs[5]orargs.id2or'id2'data.id2Width=checkNumAndAddSuffix(args['id2-w'],45,'px')data.id2Height=data.idHeightdata.id2BackgroundColor=args['id2-c']orargs[7]orargs[1]or'#dddddd'data.id2TextAlign='center'data.id2FontSize=checkNum(args['id2-s'],14)data.id2LineHeight=args['id2-lh']or'1.25em'data.id2Color=args['id2-fc']ordata.infoColordata.id2Padding=args['id2-p']or'0 0 0 1px'data.id2OtherParams=args['id2-op']returndataendp['_userbox-r']=function(args)-- Does argument processing for {{userbox-r}}.localdata={}-- Get div tag values.data.float=args.floator'left'localborderWidthNum=checkNum(args['border-width']orargs['border-s'],1)-- Used to calculate width.data.borderWidth=addSuffix(borderWidthNum,'px')data.borderColor=args['border-color']orargs['border-c']orargs[1]orargs['id-c']or'#999'data.width=addSuffix(240-2*borderWidthNum,'px')-- Also used in the table tag.data.bodyClass=args.bodyclass-- Get table tag values.data.backgroundColor=args['info-background']orargs[2]orargs['info-c']or'#eee'-- 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']or'left'data.infoFontSize=checkNumAndAddSuffix(args['info-size']orargs['info-s'],8,'pt')data.infoPadding=args['info-padding']orargs['info-p']or'0 4px 0 4px'data.infoLineHeight=args['info-line-height']orargs['info-lh']or'1.25em'data.infoColor=args['info-color']orargs['info-fc']or'black'data.infoOtherParams=args['info-other-param']orargs['info-op']-- Get id2 values.data.showId2=truedata.id2=args.logoorargs[3]orargs.idor'id'data.id2Width=checkNumAndAddSuffix(args['logo-width']orargs['id-w'],45,'px')data.id2Height=checkNumAndAddSuffix(args['logo-height']orargs['id-h'],45,'px')data.id2BackgroundColor=args['logo-background']orargs[1]orargs['id-c']or'#ddd'data.id2TextAlign=args['id-a']or'center'data.id2FontSize=checkNum(args['logo-size']orargs[5]orargs['id-s'],14)data.id2Color=args['logo-color']orargs['id-fc']ordata.infoColordata.id2Padding=args['logo-padding']orargs['id-p']or'0 0 0 1px'data.id2LineHeight=args['logo-line-height']orargs['id-lh']or'1.25em'data.id2OtherParams=args['logo-other-param']orargs['id-op']returndataendfunctionp.render(data)-- Renders the userbox html using the content of the data table. -- Render the div tag html.localroot=mw.html.create('div')root:css('float',data.float):css('border',(data.borderWidthor'')..' solid '..(data.borderColoror'')):css('margin','1px'):css('width',data.width):addClass('wikipediauserbox'):addClass(data.bodyClass)-- Render the table tag html.localtableroot=root:tag('table')tableroot:attr('role','presentation'):css('border-collapse','collapse'):css('width',data.width):css('margin-bottom','0'):css('margin-top','0'):css('background',data.backgroundColor):css('color','inherit')-- Render the id html.localtablerow=tableroot:tag('tr')ifdata.showIdthentablerow:tag('td'):css('border','0'):css('width',data.idWidth):css('height',data.idHeight):css('background',data.idBackgroundColor):css('text-align',data.idTextAlign):css('font-size',data.idFontSize..'pt'):css('font-weight','bold'):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',data.infoFontSize):css('padding',data.infoPadding):css('height',data.infoHeight):css('line-height',data.infoLineHeight):css('color',data.infoColor):css('vertical-align','middle'):cssText(data.infoOtherParams):addClass(data.infoClass):wikitext(data.info)-- Render the second id html.ifdata.showId2thentablerow:tag('td'):css('border','0'):css('width',data.id2Width):css('height',data.id2Height):css('background',data.id2BackgroundColor):css('text-align',data.id2TextAlign):css('font-size',data.id2FontSize..'pt'):css('font-weight','bold'):css('color',data.id2Color):css('padding',data.id2Padding):css('line-height',data.id2LineHeight):css('vertical-align','middle'):cssText(data.id2OtherParams):wikitext(data.id2)endlocaltitle=mw.title.getCurrentTitle()if(title.namespace==2)andnottitle.text:match("/")thenreturntostring(root)-- regular user pageelseiftitle.namespace==14thenreturntostring(root)-- categoryelseiftitle.isTalkPagethenreturntostring(root)-- talk pageendlocallegible=truelocalcontrast=require('Module:Color contrast')._ratiolocalfunctionhas_text(wikitext)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)~=""endifcontrast{data.infoColor,data.backgroundColor,error=0}<4.5thenlegible=falseend-- For bold text >= 14pt, requirement is only 3.localidContrastThreshold=4.5localid2ContrastThreshold=4.5if(data.idFontSizeor0)>=14thenidContrastThreshold=3endif(data.id2FontSizeor0)>=14thenid2ContrastThreshold=3endifdata.showIdandcontrast{data.idColor,data.idBackgroundColor,error=0}<idContrastThresholdthenifhas_text(data.idor"")thenlegible=falseendendifdata.showId2andcontrast{data.id2Color,data.id2BackgroundColor,error=0}<id2ContrastThresholdthenifhas_text(data.id2or"")thenlegible=falseendendifnotlegiblethenroot:wikitext('[[Category:Userboxes with insufficient color contrast]]')endreturntostring(root)endfunctionp.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.usercategorycats[#cats+1]=args.usercategory2cats[#cats+1]=args.usercategory3cats[#cats+1]=args.usercategory4cats[#cats+1]=args.usercategory5-- Get the title objectlocaltitleifpagethentitle=mw.title.new(page)elsetitle=mw.title.getCurrentTitle()end-- Build category handler arguments.localchargs={}chargs.page=pagechargs.nocat=args.nocatchargs.main='[[Category:Pages with templates in the wrong namespace]]'ifargs.notcatsubpagesthenchargs.subpage='no'end-- User namespace.localuser=''fori,catinipairs(cats)douser=user..makeCat(cat)endchargs.user=user-- Template namespace.localbasepage=title.baseTextlocaltemplate=''fori,catinipairs(cats)dotemplate=template..makeCat(cat,' '..basepage)endchargs.template=templatereturncategoryHandler(chargs)endreturnp
close