Module:Tree chart
Appearance
![]() | This Lua module is used on approximately 9,900 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
Usage
Implements Template:Tree chart; Full documentation on chart syntax exists at Template:Tree chart/doc
This module uses the mw.html library to create rows of table cells whose borders draw lines to show relationships between elements. When an unnamed parameter matches a key in Module:Tree chart/data, the module will create a block with stylings as defined in the table. Each key in the table has a subtable with 0, 1, or 2 keys of its own: t
for the "top" row and b
for the "bottom row". Any unnamed parameter whose value does not exist in the table will be used to create elements on the chart, and additional named parameters for that value will be looked for.
local p = {}
local cells = mw.loadData('Module:Sandbox/The Mol Man/data')
function p.main(frame)
local args = frame:getParent().args
local cell_args = {}
for i, v in ipairs(args) do
local w = mw.text.trim(v)
if not w:find('%S') then
w = '$'
end
local cell_x = { }
if cells[w] then
cell_x.box = nil
cell_x.name = w
else
cell_x.name = mw.text.trim(v)
cell_x.text = args[cell_x.name] or '{{{'..w..'}}}'
cell_x.box = true
cell_x.colspan = args['colspan_'..w] or args['colspan_ '..w] or args.colspan or '6'
cell_x.rowspan = args['rowspan_'..w] or args['rowspan_ '..w] or args.rowspan or '2'
cell_x.thick = args['border_'..w] or args['border_ '..w] or args.border or '2'
cell_x.css = args['boxstyle_'..w] or args['boxstyle_ '..w] or args.boxstyle or ''
end
table.insert(cell_args,cell_x)
end
return p._main(cell_args)
end
function p._main(cell_args)
local ret = mw.html.create('tr')
:css({ ['height'] = '1px',
['text-align'] = 'center' })
local ret2 = mw.html.create('tr')
:css({ ['height'] = '1px',
['text-align'] = 'center' })
for i, v in ipairs(cell_args) do
if v.box then
ret:node(make_box(ret,v))
else
ret:node(make_cell(ret,v.name['t']))
ret2:node(make_cell(ret2,v.name['b']))
end
end
return tostring(ret) .. tostring(ret2)
end
function make_cell(builder,name)
local props = cells[name]
if not props then
return
end
for i, v in ipairs(props) do
builder:tag(v.tag,{selfClosing=true})
:css(v.style or {})
:attr(v.attr or {})
end
end
function make_box(builder,tbl)
builder:tag('td')
:attr('colspan',tbl.colspan)
:attr('rowspan',tbl.rowspan)
:css({ ['padding'] = '0.2em',
['border'] = tbl.thick .. 'px solid black' })
:cssText(tbl.css)
:wikitext(tbl.text)
:done()
end
return p