Přeskočit na obsah

Modul:Aligned table

Z Wikipedie, otevřené encyklopedie
(rozdíl) ← Starší revize | zobrazit aktuální verzi (rozdíl) | Novější revize → (rozdíl)

Modul:Aligned table může být použit k vytváření tabulek s nastaveným zarovnáním obsahu buněk v jednotlivých sloupcích. Využit v šabloně {{Zarovnaná tabulka}}

Převzat z anglické Wikipedie, dokumentaci vizte na en:Module:Aligned table.

-- Tento modul implementuje {{Zarovnaná tabulka}}
require 'strict'

local keywords = {
	title				= 'titulek',
	cols				= 'sloupce',
	fullwidth			= 'celá šířka',
	class				= 'třída',
	style				= 'styl',
	colwidth			= 'šířka sloupců',
	leftright			= 'doleva-doprava',
	rightleft			= 'doprava-doleva',
	colstyle			= 'styl sloupců',
	rowstyle			= 'styl řádků',
	['col%dwidth']		= 'šířka s%d',
	['col%dclass']		= 'třída s%d',
	['col%dalign']		= 'zarovnání s%d',
	['col%dstyle']		= 'styl s%d',
	['col%dheader']		= 'záhlaví s%d',
	['col%dnowrap']		= 'nezalomení s%d',
	['row%dclass']		= 'třída ř%d',
	['row%dstyle']		= 'styl ř%d',
	['row%dheader']		= 'záhlaví ř%d',
	['class%d']			= 'třída%d',
	['class%d.%d']		= 'třída%d.%d',
	['style%d']			= 'styl%d',
	['style%d.%d']		= 'styl%d.%d',
}

local p = {}

local function getWithLocal(args, pattern, ...)
	-- funkce zajišťujicí rovnocenné fungování jak lokálního, tak globálního názvu pro parametr
	if keywords[pattern] then
		return args[mw.ustring.format(keywords[pattern], ...)] or args[string.format(pattern, ...)]
	else
		return args[string.format(pattern, ...)]
	end
end

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(getWithLocal(args, 'cols')) or 2

	-- create the root table
	local root = mw.html.create('table')

	-- add table style for fullwidth
	if isnotempty(getWithLocal(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(getWithLocal(args, 'class')) then
		root:addClass(getWithLocal(args, 'class'))
	end

	-- add table style
	if isnotempty(getWithLocal(args, 'style')) then
		root:cssText(getWithLocal(args, 'style'))
	end

	-- build arrays with the column styles and classes
	if isnotempty(getWithLocal(args, 'leftright')) then
		colstyle[1] = 'text-align:left;'
		colstyle[2] = 'text-align:right;'
	end
	if isnotempty(getWithLocal(args, 'rightleft')) then
		colstyle[1] = 'text-align:right;'
		colstyle[2] = 'text-align:left;'
	end
	for i = 1,cols do
		colclass[ i ] = colclass[ i ] or ''
		colstyle[ i ] = colstyle[ i ] or ''
		if isnotempty(getWithLocal(args, 'colstyle')) then
			colstyle[ i ] = getWithLocal(args, 'colstyle') .. ';' .. colstyle[ i ]
		end
		if isnotempty(getWithLocal(args, 'colalign%d', i)) then
			colstyle[ i ] = 'text-align:' .. getWithLocal(args, 'colalign%d', i) .. ';' .. colstyle[ i ]
		elseif isnotempty(getWithLocal(args, 'col%dalign', i)) then
			colstyle[ i ] = 'text-align:' .. getWithLocal(args, 'col%dalign', i) .. ';' .. colstyle[ i ]
		elseif isnotempty(getWithLocal(args, 'align%d', i)) then
			colstyle[ i ] = 'text-align:' .. getWithLocal(args, 'align%d', i) .. ';' .. colstyle[ i ]
		end
		if isnotempty(getWithLocal(args, 'colnowrap%d', i)) then
			colstyle[ i ] = 'white-space:nowrap;' .. colstyle[ i ]
		elseif isnotempty(getWithLocal(args, 'col%dnowrap', i)) then
			colstyle[ i ] = 'white-space:nowrap;' .. colstyle[ i ]
		elseif isnotempty(getWithLocal(args, 'nowrap%d', i)) then
			colstyle[ i ] = 'white-space:nowrap;' .. colstyle[ i ]
		end
		if isnotempty(getWithLocal(args, 'colwidth%d', i)) then
			colstyle[ i ] = 'width:' .. getWithLocal(args, 'colwidth%d', i) .. ';' .. colstyle[ i ]
		elseif isnotempty(getWithLocal(args, 'col%dwidth', i)) then
			colstyle[ i ] = 'width:' .. getWithLocal(args, 'col%dwidth', i) .. ';' .. colstyle[ i ]
		elseif isnotempty(getWithLocal(args, 'colwidth')) then
			colstyle[ i ] = 'width:' .. getWithLocal(args, 'colwidth') .. ';' .. colstyle[ i ]
		end
		if isnotempty(getWithLocal(args, 'colstyle%d', i)) then
			colstyle[ i ] = colstyle[ i ] .. getWithLocal(args, 'colstyle%d', i)
		elseif isnotempty(getWithLocal(args, 'col%dstyle', i)) then
			colstyle[ i ] = colstyle[ i ] .. getWithLocal(args, 'col%dstyle', i)
		elseif isnotempty(getWithLocal(args, 'style%d', i)) then
			colstyle[ i ] = colstyle[ i ] .. getWithLocal(args, 'style%d', i)
		end
		if isnotempty(getWithLocal(args, 'colclass%d', i)) then
			colclass[ i ] =  getWithLocal(args, 'colclass%d', i)
		elseif isnotempty(getWithLocal(args, 'col%dclass', i)) then
			colclass[ i ] =  getWithLocal(args, 'col%dclass', i)
		elseif isnotempty(getWithLocal(args, 'class%d', i)) then
			colclass[ i ] =  getWithLocal(args, 'class%d', 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(getWithLocal(args, 'title')) then
		local caption = root:tag('caption')
		caption:cssText(getWithLocal(args, 'titlestyle'))
		caption:wikitext(getWithLocal(args, 'title'))
	end
	if isnotempty(getWithLocal(args, 'above')) then
		local row = root:tag('tr')
		local cell = row:tag('th')
		cell:attr('colspan', cols)
		cell:cssText(getWithLocal(args, 'abovestyle'))
		cell:wikitext(getWithLocal(args, 'above'))
	end
	for j=1,rows do
		-- start a new row
		local row = root:tag('tr')
		if isnotempty(getWithLocal(args, 'rowstyle')) then
			row:cssText(getWithLocal(args, 'rowstyle'))
		else
			row:css('vertical-align', 'top')
		end
		if isnotempty(getWithLocal(args, 'rowclass')) then
			row:addClass(getWithLocal(args, 'rowclass'))
		end
		-- loop over the cells in the row
		for i=1,cols do
			local cell
			if isnotempty(getWithLocal(args, 'row%dheader', j)) then
				cell = row:tag('th'):attr('scope','col')
			elseif isnotempty(getWithLocal(args, 'col%dheader', i)) then
				cell = row:tag('th'):attr('scope','row')
			else
				cell = row:tag('td')
			end
			if getWithLocal(args, 'class%d.%d', j, i) then
				cell:addClass(getWithLocal(args, 'class%d.%d', j, i))
			else
				if getWithLocal(args, 'rowclass%d', j) then
					cell:addClass(getWithLocal(args, 'rowclass%d', j))
				elseif getWithLocal(args, 'row%dclass', j) then
					cell:addClass(getWithLocal(args, 'row%dclass', j))
				elseif getWithLocal(args, 'rowevenclass') and math.fmod(j,2) == 0 then
					cell:addClass(getWithLocal(args, 'rowevenclass'))
				elseif getWithLocal(args, 'rowoddclass') and math.fmod(j,2) == 1 then	
					cell:addClass(getWithLocal(args, 'rowoddclass'))
				end
				if colclass[i] ~= '' then
					cell:addClass(colclass[i])
				end
			end
			if getWithLocal(args, 'style%d.%d', j, i) then
				cell:cssText(getWithLocal(args, 'style%d.%d', j, i))
			else
				if getWithLocal(args, 'rowstyle%d', j) then
					cell:cssText(getWithLocal(args, 'rowstyle%d', j))
				elseif getWithLocal(args, 'rowevenstyle') and math.fmod(j,2) == 0 then
					cell:cssText(getWithLocal(args, 'rowevenstyle'))
				elseif getWithLocal(args, 'rowoddstyle') and math.fmod(j,2) == 1 then	
					cell:cssText(getWithLocal(args, 'rowoddstyle'))
				elseif getWithLocal(args, 'row%dstyle', j) then
					cell:cssText(getWithLocal(args, 'row%dstyle', j))
				end
				if isnotempty(colstyle[i]) then
					cell:cssText(colstyle[i])
				end
			end
			cell:wikitext(mw.ustring.gsub(args[cols*(j - 1) + i] or '', '^(.-)%s*$', '%1') or '')
		end
	end
	-- return the root table
	return tostring(root)
end

return p