Jump to content

Module:Sidebar

Permanently protected module
From Wikipedia, the free encyclopedia

require('strict')localcfg=mw.loadData('Module:Sidebar/configuration')localp={}localgetArgs=require('Module:Arguments').getArgs--[[Categorizes calling templates and modules with a 'style' parameter of any sortfor tracking to convert to TemplateStyles.TODO after a long cleanup: Catch sidebars in other namespaces than Template and Module.TODO would probably want to remove /log and /archive as CS1 does]]localfunctioncategorizeTemplatesWithInlineStyles(args)localtitle=mw.title.getCurrentTitle()iftitle.namespace~=10andtitle.namespace~=828thenreturn''endfor_,patterninipairs(cfg.i18n.pattern.uncategorized_conversion_titles)doiftitle.text:match(pattern)thenreturn''endendforkey,_inpairs(args)doifmw.ustring.find(key,cfg.i18n.pattern.style_conversion)orkey=='width'thenreturncfg.i18n.category.conversionendendend--[[For compatibility with the original {{sidebar with collapsible lists}}implementation, which passed some parameters through {{#if}} to trim theirwhitespace. This also triggered the automatic newline behavior.]]-- See ([[meta:Help:Newlines and spaces#Automatic newline]])localfunctiontrimAndAddAutomaticNewline(s)s=mw.ustring.gsub(s,"^%s*(.-)%s*$","%1")ifmw.ustring.find(s,'^[#*:;]')ormw.ustring.find(s,'^{|')thenreturn'\n'..selsereturnsendend--[[Finds whether a sidebar has a subgroup sidebar.]]localfunctionhasSubgroup(s)ifmw.ustring.find(s,cfg.i18n.pattern.subgroup)thenreturntrueelsereturnfalseendendlocalfunctionhas_navbar(navbar_mode,sidebar_name)returnnavbar_mode~=cfg.i18n.navbar_noneandnavbar_mode~=cfg.i18n.navbar_offand(sidebar_nameormw.getCurrentFrame():getParent():getTitle():gsub(cfg.i18n.pattern.sandbox,'')~=cfg.i18n.title_not_to_add_navbar)endlocalfunctionhas_list_class(args,htmlclass)localpatterns={'^'..htmlclass..'$','%s'..htmlclass..'$','^'..htmlclass..'%s','%s'..htmlclass..'%s'}forarg,valueinpairs(args)doiftype(arg)=='string'andmw.ustring.find(arg,'class')thenfor_,patterninipairs(patterns)doifmw.ustring.find(args[arg]or'',pattern)thenreturntrueendendendendreturnfalseend-- there are a lot of list classes in the wild, so we add their TemplateStyleslocalfunctionadd_list_styles(args)localframe=mw.getCurrentFrame()localfunctionadd_list_templatestyles(htmlclass,templatestyles)ifhas_list_class(args,htmlclass)thenreturnframe:extensionTag{name='templatestyles',args={src=templatestyles}}elsereturn''endendlocalplainlist_styles=add_list_templatestyles('plainlist',cfg.i18n.plainlist_templatestyles)localhlist_styles=add_list_templatestyles('hlist',cfg.i18n.hlist_templatestyles)-- a second workaround for [[phab:T303378]]-- when that issue is fixed, we can actually use has_navbar not to emit the-- tag here if we wantifhas_navbar(args.navbar,args.name)andhlist_styles==''thenhlist_styles=frame:extensionTag{name='templatestyles',args={src=cfg.i18n.hlist_templatestyles}}end-- hlist -> plainlist is best-effort to preserve old Common.css ordering. [hlist_note]returnhlist_styles..plainlist_stylesend-- work around [[phab:T303378]]-- for each arg: find all the templatestyles strip markers, insert them into a-- table. then remove all templatestyles markers from the arglocalfunctionmove_hiding_templatestyles(args)localgfind=string.gfindlocalgsub=string.gsublocaltemplatestyles_markers={}localstrip_marker_pattern='(\127[^\127]*UNIQ%-%-templatestyles%-%x+%-QINU[^\127]*\127)'fork,arginpairs(args)doformarkeringfind(arg,strip_marker_pattern)dotable.insert(templatestyles_markers,marker)endargs[k]=gsub(arg,strip_marker_pattern,'')endreturntemplatestyles_markersend--[[Main sidebar function. Takes the frame, args, and an optional collapsibleClass.The collapsibleClass is and should be used only for sidebars with collapsiblelists, as in p.collapsible.]]functionp.sidebar(frame,args,collapsibleClass)ifnotargsthenargs=getArgs(frame)endlocalhiding_templatestyles=table.concat(move_hiding_templatestyles(args))localroot=mw.html.create()localchild=args.childandmw.text.trim(args.child)==cfg.i18n.child_yesroot=root:tag('table')ifnotchildthenroot:addClass(cfg.i18n.class.sidebar)-- force collapsibleclass to be sidebar-collapse otherwise output nothing:addClass(collapsibleClass==cfg.i18n.class.collapseandcfg.i18n.class.collapseornil):addClass('nomobile'):addClass(args.float==cfg.i18n.float_noneandcfg.i18n.class.float_noneornil):addClass(args.float==cfg.i18n.float_leftandcfg.i18n.class.float_leftornil):addClass(args.wraplinks~=cfg.i18n.wrap_trueandcfg.i18n.class.wraplinksornil):addClass(args.bodyclassorargs.class):css('width',args.widthornil):cssText(args.bodystyleorargs.style)ifargs.outertitlethenroot:tag('caption'):addClass(cfg.i18n.class.outer_title):addClass(args.outertitleclass):cssText(args.outertitlestyle):wikitext(args.outertitle)endifargs.topimagethenlocalimageCell=root:tag('tr'):tag('td')imageCell:addClass(cfg.i18n.class.top_image):addClass(args.topimageclass):cssText(args.topimagestyle):wikitext(args.topimage)ifargs.topcaptionthenimageCell:tag('div'):addClass(cfg.i18n.class.top_caption):cssText(args.topcaptionstyle):wikitext(args.topcaption)endendifargs.pretitlethenroot:tag('tr'):tag('td'):addClass(args.topimageandcfg.i18n.class.pretitle_with_top_imageorcfg.i18n.class.pretitle):addClass(args.pretitleclass):cssText(args.basestyle):cssText(args.pretitlestyle):wikitext(args.pretitle)endelseroot:addClass(cfg.i18n.class.subgroup):addClass(args.bodyclassorargs.class):cssText(args.bodystyleorargs.style)endifargs.titlethenifchildthenroot:wikitext(args.title)elseroot:tag('tr'):tag('th'):addClass(args.pretitleandcfg.i18n.class.title_with_pretitleorcfg.i18n.class.title):addClass(args.titleclass):cssText(args.basestyle):cssText(args.titlestyle):wikitext(args.title)endendifargs.imagethenlocalimageCell=root:tag('tr'):tag('td')imageCell:addClass(cfg.i18n.class.image):addClass(args.imageclass):cssText(args.imagestyle):wikitext(args.image)ifargs.captionthenimageCell:tag('div'):addClass(cfg.i18n.class.caption):cssText(args.captionstyle):wikitext(args.caption)endendifargs.abovethenroot:tag('tr'):tag('td'):addClass(cfg.i18n.class.above):addClass(args.aboveclass):cssText(args.abovestyle):newline()-- newline required for bullet-points to work:wikitext(args.above)endlocalrowNums={}fork,vinpairs(args)dok=''..klocalnum=k:match('^heading(%d+)$')ork:match('^content(%d+)$')ifnumthentable.insert(rowNums,tonumber(num))endendtable.sort(rowNums)-- remove duplicates from the list (e.g. 3 will be duplicated if both heading3-- and content3 are specified)fori=#rowNums,1,-1doifrowNums[i]==rowNums[i-1]thentable.remove(rowNums,i)endendfori,numinipairs(rowNums)dolocalheading=args['heading'..num]ifheadingthenroot:tag('tr'):tag('th'):addClass(cfg.i18n.class.heading):addClass(args.headingclass):addClass(args['heading'..num..'class']):cssText(args.basestyle):cssText(args.headingstyle):cssText(args['heading'..num..'style']):newline():wikitext(heading)endlocalcontent=args['content'..num]ifcontentthenroot:tag('tr'):tag('td'):addClass(hasSubgroup(content)andcfg.i18n.class.content_with_subgrouporcfg.i18n.class.content):addClass(args.contentclass):addClass(args['content'..num..'class']):cssText(args.contentstyle):cssText(args['content'..num..'style']):newline():wikitext(content):done()-- Without a linebreak after the </td>, a nested list like-- "* {{hlist| ...}}" doesn't parse correctly.:newline()endendifargs.belowthenroot:tag('tr'):tag('td'):addClass(cfg.i18n.class.below):addClass(args.belowclass):cssText(args.belowstyle):newline():wikitext(args.below)endifnotchildandhas_navbar(args.navbar,args.name)thenroot:tag('tr'):tag('td'):addClass(cfg.i18n.class.navbar):cssText(args.navbarstyle):wikitext(require('Module:Navbar')._navbar{args.name,mini=1,fontstyle=args.navbarfontstyle})endlocalbase_templatestyles=frame:extensionTag{name='templatestyles',args={src=cfg.i18n.templatestyles}}localtemplatestyles=''ifargs['templatestyles']andargs['templatestyles']~=''thentemplatestyles=frame:extensionTag{name='templatestyles',args={src=args['templatestyles']}}endlocalchild_templatestyles=''ifargs['child templatestyles']andargs['child templatestyles']~=''thenchild_templatestyles=frame:extensionTag{name='templatestyles',args={src=args['child templatestyles']}}endlocalgrandchild_templatestyles=''ifargs['grandchild templatestyles']andargs['grandchild templatestyles']~=''thengrandchild_templatestyles=frame:extensionTag{name='templatestyles',args={src=args['grandchild templatestyles']}}endreturntable.concat({add_list_styles(args),-- see [hlist_note] above about orderingbase_templatestyles,templatestyles,child_templatestyles,grandchild_templatestyles,hiding_templatestyles,tostring(root),(childandcfg.i18n.category.childor''),categorizeTemplatesWithInlineStyles(args)})endlocalfunctionlist_title(args,is_centered_list_titles,num)localtitle_text=trimAndAddAutomaticNewline(args['list'..num..'title']orcfg.i18n.default_list_title)localtitleifis_centered_list_titlesthen-- collapsible can be finicky, so provide some CSS/HTML to supporttitle=mw.html.create('div'):addClass(cfg.i18n.class.list_title_centered):wikitext(title_text)elsetitle=mw.html.create():wikitext(title_text)endlocaltitle_container=mw.html.create('div'):addClass(cfg.i18n.class.list_title)-- don't /need/ a listnumtitleclass because you can do-- .templateclass .listnumclass .sidebar-list-title:addClass(args.listtitleclass):cssText(args.basestyle):cssText(args.listtitlestyle):cssText('color: var(--color-base)'):cssText(args['list'..num..'titlestyle']):node(title):done()returntitle_containerend--[[Main entry point for sidebar with collapsible lists.Does the work of creating the collapsible lists themselves and including theminto the args.]]functionp.collapsible(frame)localargs=getArgs(frame)ifnotargs.nameandframe:getParent():getTitle():gsub(cfg.i18n.pattern.collapse_sandbox,'')==cfg.i18n.collapse_title_not_to_add_navbarthenargs.navbar=cfg.i18n.navbar_noneendlocalcontentArgs={}localis_centered_list_titles=falseifargs['centered list titles']andargs['centered list titles']~=''thenis_centered_list_titles=trueendfork,vinpairs(args)dolocalnum=string.match(k,'^list(%d+)$')ifnumthenlocalexpand=args.expandedand(args.expanded=='all'orargs.expanded==args['list'..num..'name'])localrow=mw.html.create('div')row:addClass(cfg.i18n.class.list):addClass('mw-collapsible'):addClass((notexpand)and'mw-collapsed'ornil):addClass(args['list'..num..'class']):cssText(args.listframestyle):cssText(args['list'..num..'framestyle']):node(list_title(args,is_centered_list_titles,num)):tag('div'):addClass(cfg.i18n.class.list_content):addClass('mw-collapsible-content')-- don't /need/ a listnumstyleclass because you can do-- .templatename .listnumclass .sidebar-list:addClass(args.listclass):cssText(args.liststyle):cssText(args['list'..num..'style']):wikitext(trimAndAddAutomaticNewline(args['list'..num]))contentArgs['content'..num]=tostring(row)endendfork,vinpairs(contentArgs)doargs[k]=vendreturnp.sidebar(frame,args,cfg.i18n.class.collapse)endreturnp
close