1+ -- Mô đun này thực hiện {{aligned table}}
2+ local p = {}
3+
4+ local function isnotempty (s )
5+ return s and s :match ( ' ^%s*(.-)%s*$' ) ~= ' '
6+ end
7+
8+ function p .table (frame )
9+ local args = (frame .args [3 ] ~= nil ) and frame .args or frame :getParent ().args
10+ local entries = {}
11+ local colclass = {}
12+ local colstyle = {}
13+ local cols = tonumber (args [' cols' ]) or 2
14+
15+ -- create the root table
16+ local root = mw .html .create (' table' )
17+
18+ -- add table style for fullwidth
19+ if isnotempty (args [' fullwidth' ]) then
20+ root
21+ :css (' width' , ' 100%' )
22+ :css (' border-collapse' , ' collapse' )
23+ :css (' border-spacing' , ' 0px 0px' )
24+ :css (' border' , ' none' )
25+ end
26+
27+ -- add table classes
28+ if isnotempty (args [' class' ]) then
29+ root :addClass (args [' class' ])
30+ end
31+
32+ -- add table style
33+ if isnotempty (args [' style' ]) then
34+ root :cssText (args [' style' ])
35+ end
36+
37+ -- build arrays with the column styles and classes
38+ if isnotempty (args [' leftright' ]) then
39+ colstyle [1 ] = ' text-align:left;'
40+ colstyle [2 ] = ' text-align:right;'
41+ end
42+ if isnotempty (args [' rightleft' ]) then
43+ colstyle [1 ] = ' text-align:right;'
44+ colstyle [2 ] = ' text-align:left;'
45+ end
46+ for i = 1 ,cols do
47+ colclass [ i ] = colclass [ i ] or ' '
48+ colstyle [ i ] = colstyle [ i ] or ' '
49+ if isnotempty (args [' colstyle' ]) then
50+ colstyle [ i ] = args [' colstyle' ] .. ' ;' .. colstyle [ i ]
51+ end
52+ if isnotempty (args [' colalign' .. tostring (i )]) then
53+ colstyle [ i ] = ' text-align:' .. args [' colalign' .. tostring (i )] .. ' ;' .. colstyle [ i ]
54+ elseif isnotempty (args [' col' .. tostring (i ) .. ' align' ]) then
55+ colstyle [ i ] = ' text-align:' .. args [' col' .. tostring (i ) .. ' align' ] .. ' ;' .. colstyle [ i ]
56+ elseif isnotempty (args [' align' .. tostring (i )]) then
57+ colstyle [ i ] = ' text-align:' .. args [' align' .. tostring (i )] .. ' ;' .. colstyle [ i ]
58+ end
59+ if isnotempty (args [' colnowrap' .. tostring (i )]) then
60+ colstyle [ i ] = ' white-space:nowrap;' .. colstyle [ i ]
61+ elseif isnotempty (args [' col' .. tostring (i ) .. ' nowrap' ]) then
62+ colstyle [ i ] = ' white-space:nowrap;' .. colstyle [ i ]
63+ elseif isnotempty (args [' nowrap' .. tostring (i )]) then
64+ colstyle [ i ] = ' white-space:nowrap;' .. colstyle [ i ]
65+ end
66+ if isnotempty (args [' colwidth' .. tostring (i )]) then
67+ colstyle [ i ] = ' width:' .. args [' colwidth' .. tostring (i )] .. ' ;' .. colstyle [ i ]
68+ elseif isnotempty (args [' col' .. tostring (i ) .. ' width' ]) then
69+ colstyle [ i ] = ' width:' .. args [' col' .. tostring (i ) .. ' width' ] .. ' ;' .. colstyle [ i ]
70+ elseif isnotempty (args [' colwidth' ]) then
71+ colstyle [ i ] = ' width:' .. args [' colwidth' ] .. ' ;' .. colstyle [ i ]
72+ end
73+ if isnotempty (args [' colstyle' .. tostring (i )]) then
74+ colstyle [ i ] = colstyle [ i ] .. args [' colstyle' .. tostring (i )]
75+ elseif isnotempty (args [' col' .. tostring (i ) .. ' style' ]) then
76+ colstyle [ i ] = colstyle [ i ] .. args [' col' .. tostring (i ) .. ' style' ]
77+ elseif isnotempty (args [' style' .. tostring (i )]) then
78+ colstyle [ i ] = colstyle [ i ] .. args [' style' .. tostring (i )]
79+ end
80+ if isnotempty (args [' colclass' .. tostring (i )]) then
81+ colclass [ i ] = args [' colclass' .. tostring (i )]
82+ elseif isnotempty (args [' col' .. tostring (i ) .. ' class' ]) then
83+ colclass [ i ] = args [' col' .. tostring (i ) .. ' class' ]
84+ elseif isnotempty (args [' class' .. tostring (i )]) then
85+ colclass [ i ] = args [' class' .. tostring (i )]
86+ end
87+ end
88+ -- compute the maximum cell index
89+ local cellcount = 0
90+ for k , v in pairs ( args ) do
91+ if type ( k ) == ' number' then
92+ cellcount = math.max (cellcount , k )
93+ end
94+ end
95+ -- compute the number of rows
96+ local rows = math.ceil (cellcount / cols )
97+
98+ -- build the table content
99+ if isnotempty (args [' title' ]) then
100+ local caption = root :tag (' caption' )
101+ caption :cssText (args [' titlestyle' ])
102+ caption :wikitext (args [' title' ])
103+ end
104+ if isnotempty (args [' above' ]) then
105+ local row = root :tag (' tr' )
106+ local cell = row :tag (' th' )
107+ cell :attr (' colspan' , cols )
108+ cell :cssText (args [' abovestyle' ])
109+ cell :wikitext (args [' above' ])
110+ end
111+ for j = 1 ,rows do
112+ -- start a new row
113+ local row = root :tag (' tr' )
114+ if isnotempty (args [' rowstyle' ]) then
115+ row :cssText (args [' rowstyle' ])
116+ else
117+ row :css (' vertical-align' , ' top' )
118+ end
119+ if isnotempty (args [' rowclass' ]) then
120+ row :addClass (args [' rowclass' ])
121+ end
122+ -- loop over the cells in the row
123+ for i = 1 ,cols do
124+ local cell
125+ if isnotempty (args [' row' .. tostring (j ) .. ' header' ]) then
126+ cell = row :tag (' th' ):attr (' scope' ,' col' )
127+ elseif isnotempty (args [' col' .. tostring (i ) .. ' header' ]) then
128+ cell = row :tag (' th' ):attr (' scope' ,' row' )
129+ else
130+ cell = row :tag (' td' )
131+ end
132+ if args [' class' .. tostring (j ) .. ' .' .. tostring (i )] then
133+ cell :addClass (args [' class' .. tostring (j ) .. ' .' .. tostring (i )])
134+ else
135+ if args [' rowclass' .. tostring (j )] then
136+ cell :addClass (args [' rowclass' .. tostring (j )])
137+ elseif args [' row' .. tostring (j ) .. ' class' ] then
138+ cell :addClass (args [' row' .. tostring (j ) .. ' class' ])
139+ elseif args [' rowevenclass' ] and math.fmod (j ,2 ) == 0 then
140+ cell :addClass (args [' rowevenclass' ])
141+ elseif args [' rowoddclass' ] and math.fmod (j ,2 ) == 1 then
142+ cell :addClass (args [' rowoddclass' ])
143+ end
144+ if colclass [i ] ~= ' ' then
145+ cell :addClass (colclass [i ])
146+ end
147+ end
148+ if args [' style' .. tostring (j ) .. ' .' .. tostring (i )] then
149+ cell :cssText (args [' style' .. tostring (j ) .. ' .' .. tostring (i )])
150+ else
151+ if args [' rowstyle' .. tostring (j )] then
152+ cell :cssText (args [' rowstyle' .. tostring (j )])
153+ elseif args [' rowevenstyle' ] and math.fmod (j ,2 ) == 0 then
154+ cell :cssText (args [' rowevenstyle' ])
155+ elseif args [' rowoddstyle' ] and math.fmod (j ,2 ) == 1 then
156+ cell :cssText (args [' rowoddstyle' ])
157+ elseif args [' row' .. tostring (j ) .. ' style' ] then
158+ cell :cssText (args [' row' .. tostring (j ) .. ' style' ])
159+ end
160+ if isnotempty (colstyle [i ]) then
161+ cell :cssText (colstyle [i ])
162+ end
163+ end
164+ cell :wikitext (mw .ustring .gsub (args [cols * (j - 1 ) + i ] or ' ' , ' ^(.-)%s*$' , ' %1' ) or ' ' )
165+ end
166+ end
167+ -- return the root table
168+ return tostring (root )
169+ end
170+
171+ return p
0 commit comments