Module:Aligned table

From Wikisource
Jump to navigation Jump to search
Module documentation[view] [edit] [history] [purge]


Known limitations[edit]

  • It is not currently recommended to combine use of this template with conventional table syntax due to differing approaches.
  • It is not possible to use a custom header line which contains rowspan elements.
  • When used in a multi-page transclusion (see usage notes below) , no indvidual page numbers will be displayed next to the relevant table portions,

(owing to interaction limitations with Proofread Page and LST).

Full syntax[edit]

For parameter explanations, see table below.

{{Aligned table
| cols =
| fullwidth =
| class =
| style =
| colwidth =
| leftright =
| colstyle =
| colalign =
| rowstyle =
| title =
| titlestyle =
| above =
| abovestyle =
| nobody =
| nofooter =
| noheader =

<!--for Cth column:-->
| colCwidth =
| colwidthC =
| colCclass =
| colclassC =
| classC =
| colCalign =
| alignC =
| colCstyle =
| colstyleC =
| styleC =
| colCnowrap =
| colnowrapC =
| nowrapC =
| colCheader =

<!--for Rth row:-->
| rowRclass =
| rowclassR =
| rowRstyle =
| rowstyleR =
| rowRheader =
| aboveR =
| aboveRnormal =
| aboveRclass =
| aboveRstyle =

<!--for cell R,C:-->
| classR.C =
| styleR.C =

| (cell 1,1) | (cell 1,2) | ... | ...
| (cell 2,1) | (cell 2,2) | ... | ...
}}

Usage over multiple Page:[edit]

First Page (content) :

{{Aligned table
| nofooter=yes <!--  required if data extends over more than one page. -->
| row1header=yes <!-- to mark first row as the headers.  -->
| (cell 1,1) | (cell 1,2) | ... | ...
| (cell 2,1) | (cell 2,2) | ... | ...
}}

First Page (footer) :

{{Aligned table|noheader=yes|nobody=yes}}

Continuing pages (header):

{{Aligned table
| nofooter=yes <!--  required if data extends over more than one page.-->
| row1header=yes <!-- to mark first row as the headers.  -->
| (header call 1,1) | (header cell 1,2)|... |...
}}

Continuing pages (body):

{{Aligned table
| noheader=yes; nofooter=yes <!--  required if data extends over more than one page.-->
... 
| (data cell x,1) | (data cell x,2)|... |... 
| (data cell x+1,1) | (data cell x+1,2)|... |... 
}}

Continuing pages (footer) as for first page. Last page header as for continuing pages.

Last page body.

{{Aligned table
| noheader=yes; <!--  required if data extends over more than one page. nofooter line is absent -->
... 
| (data cell x,1) | (data cell x,2)|... |... 
| (data cell x+1,1) | (data cell x+1,2)|... |... 
}}
{{nop}}<!-- Optional but needed in some instances to create an appropriate break.-->

Parameters[edit]

Parameter Description Examples Default
cols Number of columns in the table. |cols=4 2
fullwidth Set to anything ("on", "true", "yes", etc) to ensure table spans entire width available.
(Effect is to remove border-spacing and add width:100% to the table's styling.)
|fullwidth=on not set
class CSS/HTML class/es applied to the table. |class=wikitable   |class=infobox
|class=sortable nowrap   |class=sidebar
none
style CSS styling applied to the table. |style=width:80%;background:transparent; none
colwidth Width of each column (or include "width:...;" in colstyle). |colwidth=6.0em   |colwidth=25% not set (only applied if no relevant colCwidth or colwidthC present)
colstyle CSS styling applied to each column. |colstyle=background:transparent; none
colalign Alignment (left/right/center/justify) applied as "text-align:" prepended to all column stylings. |colalign=justify none set
rowstyle CSS styling applied to each row (includes "aboveR" rows.) |rowstyle=text-align:center; none
title text to be applied as overall table caption. |title=Summary of Data not caption
titlestyle CSS styling applied to table caption. |titlestyle=font-style:italic;font-variant:small-caps; empty
above text to be applied as full span (covers all columns) table header. |above=Precis discussing the following… no spanning header
abovestyle CSS styling applied to spanning header cell. |avovestyle=background:color:grey empty
leftright Set to anything ("on", "true", "yes", etc) in place of |col1align=left and |col2align=right |leftright=on not set
colCwidth Width of column number C (or include "width:...;" in colCstyle). |col2width=9.0em   |col3width=35% none set
colwidthC Width of column number C (or include "width:...;" in colCstyle). |colwidth4=5em   |colwidth5=50% none set
colCclass Class/es applied to column number C. |col1class=navbox-group "
colclassC Class/es applied to column number C. |colclass3=prose "
classC Class/es applied to column number C. |class4=redalert "
colCalign Alignment (left\right\center) for column C's cell contents (or include "text-align:...;" in colCstyle). |col4align=right none set
alignC Alignment (left\right\center) for column C's cell contents (or include "text-align:...;" in colCstyle). |align4=right none set
colCnowrap Set to anything ("on", "true", "yes", etc) to add "white-space:nowrap;" to column C's cell styling (i.e. to prevent lines within its cells from wrapping; or include "nowrap" in colCclass or "white-space:nowrap;" in colCstyle). |col5nowrap=on "
colnowrapC Set to anything ("on", "true", "yes", etc) to add "white-space:nowrap;" to column C's cell styling (i.e. to prevent lines within its cells from wrapping; or include "nowrap" in colCclass or "white-space:nowrap;" in colCstyle). |colnowrap2=on "
nowrapC Set to anything ("on", "true", "yes", etc) to add "white-space:nowrap;" to column C's cell styling (i.e. to prevent lines within its cells from wrapping; or include "nowrap" in colCclass or "white-space:nowrap;" in colCstyle). |nowrap6=on "
colCstyle CSS styling applied to column number C. |col4style=font-style:italic; "
colstyleC CSS styling applied to column number C. |colstyle3=font-weight:bold; "
styleC CSS styling applied to column number C. |colstyle2=background-color:gold; "
colCheader Set to any value (e.g. Y) to change col's cells from data cells to table headers (headings). |col1header=on not set
rowRclass Class/es applied to row number R. |row10class=navbox-abovebelow none set
rowclassR Class/es applied to row number R. |rowclass3=references-small none set
rowRstyle CSS styling applied to row number R. |row2style=font-weight:bold; "
rowstyleR CSS styling applied to row number R. |rowstyle6=color:green; "
rowRheader Set to any value (e.g. Y) to change row's cells from data cells to table headers (headings). |row1header=on not set
aboveR text to be applied as full span (covers all columns) entry above nominated row. |above5=Section II: A New Start empty/no spanning entry
aboveRnormal Unset indicates spanning entry will be inserted as a table header cell with all that implies (centred; bold etc.) If set to anything ("on", "true", "yes", etc) spanning entry will be instead inserted as a standard table cell and only explicitly specified CSS classes or styling will be applied. |above5normal=yes empty
aboveRclass Class/es to be applied to spanning entry above row number R. |above5class=quote empty
aboveRclass CSS styling to be applied to spanning entry above row number R. |above5style=text-align:center empty
classR.C Class/es applied to cell in row R, column C. Overrides any other class attributions (rowRclass, colCclass). |class1.2=adr none set
styleR.C CSS styling applied to cell in row R, column C. Overrides any more general styling (leftright, colCalign, rowRstyle, etc). |style5.3=text-align:right; "
noheader Used to suppress generation of table header in page namespace | noheader=yes <table> is included
nofooter Used to suppress generation of a table footer in page namespace | nofooter=yes </table> is included
nobody Used to suppress generation of a table contents | nobody=yes Only <table></table> is emitted. This can be used when spanning tables over multiple pages. ( see below)
Unnamed parameters Each unnamed parameter is taken to be the next cell in the table, starting at the table's top left corner and working left to right from top to bottom. | Azerbaijan | Malta | Zaire empty

Examples[edit]

{{Aligned table
|cols=3 |class=wikitable
| A | 1 | a
| B | 2 | b
}}
A 1 a
B 2 b
{{Aligned table
|cols=4 |class=wikitable
|col1align=center |col2align=center
|col3align=right  |col4align=right
| AAA | aa    | 123  | 12345
| B   | bbbbb | 1234 | 2
}}
AAA aa 123 12345
B bbbbb 1234 2
{{Aligned table
|class=wikitable |style=width:20em;
|col1align=center |col2align=right
| AAA | 123
| B   | 2
}}
AAA 123
B 2
{{Aligned table
|cols=3 |class=wikitable sortable
|row1header=y
| U | # | l
| A | 1 | a
| B | 2 | b
}}
U # l
A 1 a
B 2 b

See also[edit]

-- 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['fullwidth']) then
		root
			:css('width', '100%')
			:css('border-collapse', 'collapse')
			:css('border-spacing', '0px 0px')
			:css('border', 'none')
	end

	-- add table classes
	if isnotempty(args['class']) then
		root:addClass(args['class'])
	end

	-- add table style
	if isnotempty(args['style']) then
		root:cssText(args['style'])
	end

	-- build arrays with the column styles and classes
	if isnotempty(args['leftright']) then
		colstyle[1] = 'text-align:left;'
		colstyle[2] = 'text-align:right;'
	end
	for i = 1,cols do
		colclass[ i ] = colclass[ i ] or ''
		colstyle[ i ] = colstyle[ i ] or ''
		if isnotempty(args['colstyle']) then
			colstyle[ i ] = args['colstyle'] .. ';' .. colstyle[ i ]
		end
		if isnotempty(args['colalign' .. tostring(i)]) then
			colstyle[ i ] = 'text-align:' .. args['colalign' .. tostring(i)] .. ';' .. colstyle[ i ]
		elseif isnotempty(args['col' .. tostring(i) .. 'align']) then
			colstyle[ i ] = 'text-align:' .. args['col' .. tostring(i) .. 'align'] .. ';' .. colstyle[ i ]
		elseif isnotempty(args['align' .. tostring(i)]) then
			colstyle[ i ] = 'text-align:' .. args['align' .. tostring(i)] .. ';' .. colstyle[ i ]
		end
		if isnotempty(args['colnowrap' .. tostring(i)]) then
			colstyle[ i ] = 'white-space:nowrap;' .. colstyle[ i ]
		elseif isnotempty(args['col' .. tostring(i) .. 'nowrap']) then
			colstyle[ i ] = 'white-space:nowrap;' .. colstyle[ i ]
		elseif isnotempty(args['nowrap' .. tostring(i)]) then
			colstyle[ i ] = 'white-space:nowrap;' .. colstyle[ i ]
		end
		if isnotempty(args['colwidth' .. tostring(i)]) then
			colstyle[ i ] = 'width:' .. args['colwidth' .. tostring(i)] .. ';' .. colstyle[ i ]
		elseif isnotempty(args['col' .. tostring(i) .. 'width']) then
			colstyle[ i ] = 'width:' .. args['col' .. tostring(i) .. 'width'] .. ';' .. colstyle[ i ]
		elseif isnotempty(args['colwidth']) then
			colstyle[ i ] = 'width:' .. args['colwidth'] .. ';' .. colstyle[ i ]
		end
		if isnotempty(args['colstyle' .. tostring(i)]) then
			colstyle[ i ] = colstyle[ i ] .. args['colstyle' .. tostring(i)]
		elseif isnotempty(args['col' .. tostring(i) .. 'style']) then
			colstyle[ i ] = colstyle[ i ] .. args['col' .. tostring(i) .. 'style']
		elseif isnotempty(args['style' .. tostring(i)]) then
			colstyle[ i ] = colstyle[ i ] .. args['style' .. tostring(i)]
		end
		if isnotempty(args['colclass' .. tostring(i)]) then
			colclass[ i ] =  args['colclass' .. tostring(i)]
		elseif isnotempty(args['col' .. tostring(i) .. 'class']) then
			colclass[ i ] =  args['col' .. tostring(i) .. 'class']
		elseif isnotempty(args['class' .. tostring(i)]) then
			colclass[ i ] =  args['class' .. tostring(i)]
		end
	end
	-- compute the maximum cell index
	local cellcount = 0
	for k, v in pairs( args ) do
		if type( k ) == 'number' then
			cellcount = math.max(cellcount, k)
		end
	end
	-- compute the number of rows
	local rows = math.ceil(cellcount / cols)

	-- build the table content
	if isnotempty(args['title']) then
		local caption = root:tag('caption')
		caption:cssText(args['titlestyle'])
		caption:wikitext(args['title'])
	end
	if isnotempty(args['above']) then
		local row = root:tag('tr')
		local cell = row:tag('th')
		cell:attr('colspan', cols)
		cell:cssText(args['abovestyle'])
		cell:wikitext(args['above'])
	end
	for j=1,rows do
		if isnotempty(args['above' .. tostring(j)]) then
			-- new row just for internal spanning heading
			local rowj = root:tag('tr')
			rowj:css('vertical-align', 'top')
			local cellj
			if isnotempty(args['above' .. tostring(j) .. 'normal']) then
				cellj = rowj:tag('td')
			else
				cellj = rowj:tag('th')
			end
			cellj:attr('colspan', cols)
			if isnotempty(args['rowstyle']) then
				cellj:cssText(args['rowstyle'])
			end
			if args['above' .. tostring(j) .. 'class'] then
				cellj:addClass(args['above' .. tostring(j) .. 'class'])
			end
			if args['above' .. tostring(j) .. 'style'] then
				cellj:cssText(args['above' .. tostring(j) .. 'style'])
			end
			cellj:wikitext(args['above' .. tostring(j)] or '')
		end
		-- start a new row
		local row = root:tag('tr')
		row:css('vertical-align', 'top')
		-- loop over the cells in each row
		for i=1,cols do
			local cell
			if isnotempty(args['row' .. tostring(j) .. 'header']) then
				cell = row:tag('th'):attr('scope','col')
			elseif isnotempty(args['col' .. tostring(i) .. 'header']) then
				cell = row:tag('th'):attr('scope','row')
			else
				cell = row:tag('td')
			end
			if args['class' .. tostring(j) .. '.' .. tostring(i)] then
				cell:addClass(args['class' .. tostring(j) .. '.' .. tostring(i)])
			else
				if args['rowclass' .. tostring(j)] then
					cell:addClass(args['rowclass' .. tostring(j)])
				elseif args['row' .. tostring(j) .. 'class'] then
					cell:addClass(args['row' .. tostring(j) .. 'class'])
				end
				if colclass[i] ~= '' then
					cell:addClass(colclass[i])
				end
			end
			if isnotempty(args['rowstyle']) then
				cell:cssText(args['rowstyle'])
			end
			if args['style' .. tostring(j) .. '.' .. tostring(i)] then
				cell:cssText(args['style' .. tostring(j) .. '.' .. tostring(i)])
			else
				if args['rowstyle' .. tostring(j)] then
					cell:cssText(args['rowstyle' .. tostring(j)])
				elseif args['row' .. tostring(j) .. 'style'] then
					cell:cssText(args['row' .. tostring(j) .. 'style'])
				end
				if isnotempty(colstyle[i]) then
					cell:cssText(colstyle[i])
				end
			end
			cell:wikitext(args[cols*(j - 1) + i] or '')
		end
	end
	-- convert the root table to string form
	local textform = tostring(root)

	if isnotempty(args['nofooter']) then
		textform = mw.ustring.gsub(textform,'</table>$','')
	end

	if isnotempty(args['noheader']) then
		textform = mw.ustring.gsub(textform,'^<table.->','')
	end

	if isnotempty(args['nobody']) then
		textform = mw.ustring.gsub(textform,'<tr.*/tr>','')
	end

	return textform
end

return p