Zeroclanzhang(讨论 | 贡献)2024年1月16日 (二) 10:27的版本 (创建页面,内容为“-- This module implements {{aligned table}} local p = {} local function isnotempty(s) return s and s:match( '^%s*(.-)%s*$' ) ~= '' end function p.table(frame) local args = (frame.args[3] ~= nil) and frame.args or frame:getParent().args local entries = {} local colclass = {} local colstyle = {} local cols = tonumber(args['cols']) or 2 -- create the root table local root = mw.html.create('table') -- add table style for fullwidth if isnotempty(args['…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
This Lua module is used on 7,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 subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from 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'])or2-- 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;'endifisnotempty(args['rightleft'])thencolstyle[1]='text-align:right;'colstyle[2]='text-align:left;'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-- start a new rowlocalrow=root:tag('tr')ifisnotempty(args['rowstyle'])thenrow:cssText(args['rowstyle'])elserow:css('vertical-align','top')endifisnotempty(args['rowclass'])thenrow:addClass(args['rowclass'])end-- loop over the cells in the rowfori=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'])elseifargs['rowevenclass']andmath.fmod(j,2)==0thencell:addClass(args['rowevenclass'])elseifargs['rowoddclass']andmath.fmod(j,2)==1thencell:addClass(args['rowoddclass'])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['rowevenstyle']andmath.fmod(j,2)==0thencell:cssText(args['rowevenstyle'])elseifargs['rowoddstyle']andmath.fmod(j,2)==1thencell:cssText(args['rowoddstyle'])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'')endend-- return the root tablereturntostring(root)endreturnp