Module:Tree chart: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Tag: Reverted
Line 3: Line 3:
-- Avoids hard require('strict') so it won't fail on installs without that library.
-- Avoids hard require('strict') so it won't fail on installs without that library.


-- try to load 'strict' if available, but don't error if it's missing
pcall(function() require('strict') end)
pcall(function() require('strict') end)


local p = {}
local p = {}


-- load cell definitions (Module:Tree chart/data)
local cells = mw.loadData('Module:Tree chart/data')
local cells = mw.loadData('Module:Tree chart/data')


-- internal renderer: accepts an array where each entry is either:
--  * a string key (looks up cells[key]) or
--  * a table describing a custom cell (text, colspan, rowspan, etc)
function p._main(cell_args)
function p._main(cell_args)
     local ret = mw.html.create()
     local ret = mw.html.create()
    -- use exact CSS property names (strings) for hyphenated properties
     local tbl = ret:tag('table')
     local tbl = ret:tag('table')
                 :addClass('tree-chart-table')
                 :addClass('tree-chart-table')
                 :css{ borderCollapse = 'collapse', ['vertical-align'] = 'top' }
                 :css{
                    ['border-collapse'] = 'collapse',
                    ['border-spacing'] = '0',
                    ['vertical-align'] = 'top'
                }


     local top = tbl:tag('tr'):css{ height = '1px', ['text-align'] = 'center' }
     local top = tbl:tag('tr'):css{ ['height'] = '1px', ['text-align'] = 'center' }
     local bottom = tbl:tag('tr'):css{ height = '1px', ['text-align'] = 'center' }
     local bottom = tbl:tag('tr'):css{ ['height'] = '1px', ['text-align'] = 'center' }


     for _, v in ipairs(cell_args) do
     for _, v in ipairs(cell_args) do
Line 38: Line 38:
                 end
                 end
             else
             else
                -- unknown symbol => placeholder cells so columns align
                 top:tag('td'):wikitext('')
                 top:tag('td'):wikitext('')
                 bottom:tag('td'):wikitext('')
                 bottom:tag('td'):wikitext('')
             end
             end
         else
         else
            -- custom cell (table) from unnamed params
             local colspan = v.colspan or cell_args.colspan or 6
             local colspan = v.colspan or cell_args.colspan or 6
             local rowspan = v.rowspan or cell_args.rowspan or 2
             local rowspan = v.rowspan or cell_args.rowspan or 2
             local border = (v.border or cell_args.border or '2') .. 'px solid'
             local border_prop = v.border or cell_args.border or '2'
            local border = tostring(border_prop) .. 'px solid'


             top:tag('td')
             top:tag('td')
                 :attr{ colspan = tostring(colspan), rowspan = tostring(rowspan) }
                 :attr{ colspan = tostring(colspan), rowspan = tostring(rowspan) }
                 :css{ padding = '0.2em', border = border }
                 :css{ padding = '0.2em' }
                :attr{ style = 'border: ' .. border .. ';' } -- use explicit style when assembling string
                 :cssText(v.boxstyle or cell_args.boxstyle or '')
                 :cssText(v.boxstyle or cell_args.boxstyle or '')
                 :wikitext(v.text or '')
                 :wikitext(v.text or '')

Navigation menu