Naar inhoud springen

Module:PuntTabel

Uit Wikisource

Documentatie voor deze module kan aangemaakt worden op de volgende pagina: Module:PuntTabel/doc

local p = {}

local getArgs = require('Module:Arguments').getArgs

function p.tabel(frame)
	local args = getArgs(frame)
	
	-- Hier wordt de kolom bepaald, waarin de puntenleader komt te staan.
	-- Standaard is de eerste kolom, kan worden verplaatst met 'puntkolom=''
	local puntkolom = 1
	if args.puntkolom then
		puntkolom = tonumber(args.puntkolom)
		if not puntkolom or puntkolom < 1 then
			error("'puntkolom' moet een kolom aangeven (dus getal van 1 of hoger)")
		end
	end
	
	local s = '<table class="ws-dottable"><tr>'
	
	local dezekolom = {
		rowspan = nil,
		colspan = nil,
		style = nil,
		class = nil,
	}
	
	local eersterij = true
	local kolom = 0
	for k, v in ipairs(args) do
		if v == '-' then
			-- nieuwe rij (met '|-')
			if kolom == 0 and eersterij then
				-- niks
			else
				s = s .. '</tr><tr>'
				kolom = 0
			end
			eersterij = false


		-- Attributen (met dubbele punt ipv '=', zodat we de hele argumenten krijgen.)
		elseif string.find(v, '^rowspan:') then
			-- rowspan argument
			local rowspan = string.gsub(v, '^rowspan:%s*(.*)%s*$', '%1')
			dezekolom.rowspan = tonumber(rowspan)
		elseif string.find(v, '^colspan:') then
			local colspanstr = string.gsub(v, '^colspan:%s*(.*)%s*$', '%1')
			dezekolom.colspan = tonumber(colspanstr)
		elseif string.find(v, '^class:') then
			dezekolom.class = string.gsub(v, '^class:%s*(.*)%s*$', '%1')
		elseif string.find(v, '^style:') then
			dezekolom.style = string.gsub(v, '^style:%s*(.*)%s*$', '%1')


		-- Nieuwe kolom met inhoud
		else
			
			-- rowspan verpest de (toch maar heel eenvoudige) kolomdetectie,
			-- dus mag (voor nu) alleen ná de puntkolom
			if rowspan and rowspan>1 and kolom<puntkolom then
				error('rowspan kan alleen gebruikt worden in kolommen ná de puntkolom')
			end
			
			-- kolom bijhouden (zie, heel eenvoudig)
			if dezekolom.colspan then
				kolom = kolom + dezekolom.colspan
			else
				kolom = kolom + 1
			end
			
			s = s .. '<td'
			
			-- Classes
			local classes = nil
			if dezekolom.class then
				classes = dezekolom.class
			end
			-- class voor de puntkolom
			if kolom == puntkolom then
				if classes then
					classes = classes .. ' ws-dotcolumn'
				else
					classes = 'ws-dotcolumn'
				end
			end

			if classes then
				s = s .. ' class="' .. classes .. '"'
			end


			local styles = nil

			-- de alignment mag worden aangepast met 'kolom<n>=<l,c,r>' (bijv. 'kolom1=c')
			local alignment = args['kolom' .. tostring(kolom)]
			if alignment then
				styles = 'text-align:'

				if alignment == 'l' then
					styles = styles .. 'left'
				elseif alignment == 'c' then
					styles = styles .. 'center'
				elseif alignment == 'r' then
					styles = styles .. 'right'
				else
					error("'Argument 'kolom" .. tostring(kolom) .. "' moet l, c, of r zijn.")
				end
				styles = styles .. ';'
			end
			if dezekolom.style then
				if styles then
					styles = styles .. dezekolom.style
				else
					styles = dezekolom.style
				end
			end
			
			if styles then
				s = s .. ' style="' .. styles .. '"'
			end
			
			if dezekolom.colspan then
				s = s .. ' colspan=' .. tostring(dezekolom.colspan)
			end
			
			if dezekolom.rowspan then
				s = s .. ' rowspan=' .. tostring(dezekolom.rowspan)
			end

			-- dan de inhoud
			s = s .. '>' .. v .. '</td>'
			
			-- en de waardes resetten
			dezekolom.rowspan = nil
			dezekolom.colspan = nil
			dezekolom.style = nil
			dezekolom.class = nil
		end
	end
	
	s = s .. '</tr></table>'
	
	return s
end

return p