Module:Tree chart: Difference between revisions

From Ekatra Foundation
Jump to navigation Jump to search
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 '')

Revision as of 14:25, 11 November 2025

Documentation for this module may be created at Module:Tree chart/doc

-- Module:Tree chart
-- Defensive renderer for table-based tree pieces
-- Avoids hard require('strict') so it won't fail on installs without that library.

pcall(function() require('strict') end)

local p = {}

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

function p._main(cell_args)
    local ret = mw.html.create()
    -- use exact CSS property names (strings) for hyphenated properties
    local tbl = ret:tag('table')
                 :addClass('tree-chart-table')
                 :css{
                     ['border-collapse'] = 'collapse',
                     ['border-spacing'] = '0',
                     ['vertical-align'] = 'top'
                 }

    local top = 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
        if type(v) == 'string' then
            local celldef = cells[v]
            if celldef then
                if celldef.t then
                    top:wikitext(celldef.t)
                else
                    top:tag('td'):wikitext('')
                end
                if celldef.b then
                    bottom:wikitext(celldef.b)
                else
                    bottom:tag('td'):wikitext('')
                end
            else
                top:tag('td'):wikitext('')
                bottom:tag('td'):wikitext('')
            end
        else
            local colspan = v.colspan or cell_args.colspan or 6
            local rowspan = v.rowspan or cell_args.rowspan or 2
            local border_prop = v.border or cell_args.border or '2'
            local border = tostring(border_prop) .. 'px solid'

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

            if tonumber(rowspan) == nil or tonumber(rowspan) < 2 then
                bottom:tag('td'):wikitext('')
            end
        end
    end

    return tostring(tbl)
end

function p.main(frame)
    local args = require('Module:Arguments').getArgs(frame, {
        wrappers = 'Template:Tree chart',
        trim = false,
        removeBlanks = false
    })

    local cell_args = {
        colspan = args.colspan,
        rowspan = args.rowspan,
        border = args.border,
        boxstyle = 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
            local rightTrimmedVal = val:gsub('%s+$','')
            local custom = {
                text = args[trimmedVal] or ('{{{'..trimmedVal..'}}}'),
                colspan = args['colspan_'..rightTrimmedVal],
                rowspan = args['rowspan_'..rightTrimmedVal],
                border = args['border_'..rightTrimmedVal],
                boxstyle = args['boxstyle_'..rightTrimmedVal]
            }
            table.insert(cell_args, custom)
        end
    end

    return p._main(cell_args)
end

return p