Jump to content

Module:Tree chart/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Jackmcbarn (talk | contribs)
move all default value logic to _main
Jackmcbarn (talk | contribs)
move the cell rendering into the data module, so it only has to happen once per page
Line 3: Line 3:
local p = {}
local p = {}


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

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)
function p._main(cell_args)
Line 27: Line 15:
for _, v in ipairs(cell_args) do
for _, v in ipairs(cell_args) do
if type(v) == 'string' then
if type(v) == 'string' then
make_cell(top,v,'t')
top:wikitext(cells[v].t)
make_cell(bottom,v,'b')
bottom:wikitext(cells[v].b)
else
else
top:tag('td')
top:tag('td')

Revision as of 00:44, 17 December 2014

require('Module:No globals')

local p = {}

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

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 type(v) == 'string' then
			top:wikitext(cells[v].t)
			bottom:wikitext(cells[v].b)
		else
			top:tag('td')
				:attr{ colspan = v.colspan or cell_args.colspan or 6,
						rowspan = v.rowspan or cell_args.rowspan or 2 }
				:css{ padding = '0.2em',
						border = (v.thick or cell_args.thick or '2') .. 'px solid black' }
				:cssText(v.css or cell_args.css)
				:wikitext(v.text)
		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 = {
		colspan = args.colspan,
		rowspan = args.rowspan,
		thick = args.border,
		css = args.boxstyle
	}
	for _, val in ipairs(args) do
		local trimmedVal = val:match('^%s*(.-)%s*$')
		if trimmedVal == '' then
			trimmedVal = '$'
		end
		if cells[trimmedVal] then
			table.insert(cell_args, trimmedVal)
		else
			-- Unnamed params behave weirdly
			-- white space at the front counts for param_{{{1}}}, but not whitespace at the end, so remove it
			local rightTrimmedVal = val:gsub('%s+$','')
			table.insert(cell_args, {
				text = args[trimmedVal] or ('{{{'..trimmedVal..'}}}'),
				colspan = args['colspan_'..rightTrimmedVal] ,
				rowspan = args['rowspan_'..rightTrimmedVal],
				thick = args['border_'..rightTrimmedVal],
				css = args['boxstyle_'..rightTrimmedVal]
			})
		end
	end
	
	return p._main(cell_args)
end

return p