This Lua module is used on 14,000+ pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them.
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
-- This module implements {{aligned table}}localp={}localfunctionisnotempty(s)returnsands:match('^%s*(.-)%s*$')~=''endfunctionp.table(frame)localargs=(frame.args[3]~=nil)andframe.argsorframe:getParent().argslocalentries={}localcolclass={}localcolstyle={}localcols=tonumber(args['cols'])or2localnoblankrows=args['noblankrows']or''-- create the root tablelocalroot=mw.html.create('table')-- add table style for fullwidthifisnotempty(args['fullwidth'])thenroot:css('width','100%'):css('border-collapse','collapse'):css('border-spacing','0px 0px'):css('border','none')end-- add table classesifisnotempty(args['class'])thenroot:addClass(args['class'])end-- add table styleifisnotempty(args['style'])thenroot:cssText(args['style'])end-- build arrays with the column styles and classesifisnotempty(args['leftright'])thencolstyle[1]='text-align:left;'colstyle[2]='text-align:right;'endfori=1,colsdocolclass[i]=colclass[i]or''colstyle[i]=colstyle[i]or''ifisnotempty(args['colstyle'])thencolstyle[i]=args['colstyle']..';'..colstyle[i]endifisnotempty(args['colalign'..tostring(i)])thencolstyle[i]='text-align:'..args['colalign'..tostring(i)]..';'..colstyle[i]elseifisnotempty(args['col'..tostring(i)..'align'])thencolstyle[i]='text-align:'..args['col'..tostring(i)..'align']..';'..colstyle[i]elseifisnotempty(args['align'..tostring(i)])thencolstyle[i]='text-align:'..args['align'..tostring(i)]..';'..colstyle[i]endifisnotempty(args['colnowrap'..tostring(i)])thencolstyle[i]='white-space:nowrap;'..colstyle[i]elseifisnotempty(args['col'..tostring(i)..'nowrap'])thencolstyle[i]='white-space:nowrap;'..colstyle[i]elseifisnotempty(args['nowrap'..tostring(i)])thencolstyle[i]='white-space:nowrap;'..colstyle[i]endifisnotempty(args['colwidth'..tostring(i)])thencolstyle[i]='width:'..args['colwidth'..tostring(i)]..';'..colstyle[i]elseifisnotempty(args['col'..tostring(i)..'width'])thencolstyle[i]='width:'..args['col'..tostring(i)..'width']..';'..colstyle[i]elseifisnotempty(args['colwidth'])thencolstyle[i]='width:'..args['colwidth']..';'..colstyle[i]endifisnotempty(args['colstyle'..tostring(i)])thencolstyle[i]=colstyle[i]..args['colstyle'..tostring(i)]elseifisnotempty(args['col'..tostring(i)..'style'])thencolstyle[i]=colstyle[i]..args['col'..tostring(i)..'style']elseifisnotempty(args['style'..tostring(i)])thencolstyle[i]=colstyle[i]..args['style'..tostring(i)]endifisnotempty(args['colclass'..tostring(i)])thencolclass[i]=args['colclass'..tostring(i)]elseifisnotempty(args['col'..tostring(i)..'class'])thencolclass[i]=args['col'..tostring(i)..'class']elseifisnotempty(args['class'..tostring(i)])thencolclass[i]=args['class'..tostring(i)]endend-- compute the maximum cell indexlocalcellcount=0fork,vinpairs(args)doiftype(k)=='number'thencellcount=math.max(cellcount,k)endend-- compute the number of rowslocalrows=math.ceil(cellcount/cols)-- build the table contentifisnotempty(args['title'])thenlocalcaption=root:tag('caption')caption:cssText(args['titlestyle'])caption:wikitext(args['title'])endifisnotempty(args['above'])thenlocalrow=root:tag('tr')localcell=row:tag('th')cell:attr('colspan',cols)cell:cssText(args['abovestyle'])cell:wikitext(args['above'])endforj=1,rowsdo-- skip blank rows, if feature is enabledlocalshowrow=1ifisnotempty(noblankrows)thenshowrow=0fori=1,colsdoifisnotempty(args[cols*(j-1)+i]or'')thenshowrow=1endendend-- start a new rowlocalrow=root:tag('tr')ifisnotempty(args['rowstyle'])thenrow:cssText(args['rowstyle'])else--row:css('vertical-align', 'top')end-- loop over the cells in the rowifshowrow==1thenfori=1,colsdolocalcellifisnotempty(args['row'..tostring(j)..'header'])thencell=row:tag('th'):attr('scope','col')elseifisnotempty(args['col'..tostring(i)..'header'])thencell=row:tag('th'):attr('scope','row')elsecell=row:tag('td')endifargs['class'..tostring(j)..'.'..tostring(i)]thencell:addClass(args['class'..tostring(j)..'.'..tostring(i)])elseifargs['rowclass'..tostring(j)]thencell:addClass(args['rowclass'..tostring(j)])elseifargs['row'..tostring(j)..'class']thencell:addClass(args['row'..tostring(j)..'class'])endifcolclass[i]~=''thencell:addClass(colclass[i])endendifargs['style'..tostring(j)..'.'..tostring(i)]thencell:cssText(args['style'..tostring(j)..'.'..tostring(i)])elseifargs['rowstyle'..tostring(j)]thencell:cssText(args['rowstyle'..tostring(j)])elseifargs['row'..tostring(j)..'style']thencell:cssText(args['row'..tostring(j)..'style'])endifisnotempty(colstyle[i])thencell:cssText(colstyle[i])endendcell:wikitext(mw.ustring.gsub(args[cols*(j-1)+i]or'','^(.-)%s*$','%1')or'')endendend-- return the root tablereturntostring(root)endreturnp