Jump to content

Module:Tree chart

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jackmcbarn (talk | contribs) at 17:05, 16 December 2014 (fix the wrapper). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('Module:No globals')

local p = {}

local cells = mw.loadData('Module:TreeChart/data')

local function make_cell(builder,name,key)
	local props = cells[name][key]
	if not props then
		return
	end
	for _, v in ipairs(props) do
		builder:tag('td',{selfClosing=true})
				:css(v.style or {})
				:attr(v.attr or {})
	end
end

function p._main(cell_args)
	local ret = mw.html.create()
	local top = ret:tag('tr')
						:css{ height = '1px',
								['text-align'] = 'center' }
	local bottom = ret:tag('tr')
						:css{ height = '1px',
								['text-align'] = 'center' }
	for _, v in ipairs(cell_args) do
		if v.box then
			top:tag('td')
				:attr{ colspan = v.colspan,
						rowspan = v.rowspan }
				:css{ padding = '0.2em',
						border = v.thick .. 'px solid black' }
				:cssText(v.css)
				:wikitext(v.text)
		else
			make_cell(top,v.name,'t')
			make_cell(bottom,v.name,'b')
		end
	end
	return tostring(ret)
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:Chart', trim = false, removeBlanks = false})
	local cell_args = {}
	for _, v in ipairs(args) do
		local w = v:match('^%s*(.-)%s*$')
		if w == '' then
			w = '$'
		end
		local cell_x = { }
		if cells[w] then
			cell_x.name = w
		else
			-- Unnamed params behave weirdly
			-- white space at the front counts for param_{{{1}}}, but not whitespace at the end, so remove it
			local w2 = v:gsub('%s+$','')
			cell_x.text = args[w] or '{{{'..w..'}}}'
			cell_x.box = true
			cell_x.colspan = args['colspan_'..w2] or args.colspan or '6'
			cell_x.rowspan = args['rowspan_'..w2] or args.rowspan or '2'
			cell_x.thick = args['border_'..w2] or args.border or '2'
			cell_x.css = args['boxstyle_'..w2] or args.boxstyle or ''
		end
		table.insert(cell_args,cell_x)
	end
	
	return p._main(cell_args)
end

return p