Module:Tree chart

From Ekatra Foundation
Revision as of 14:28, 9 November 2025 by Gurwinder (talk | contribs)
Jump to navigation Jump to search

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

-- Module:Tree chart
-- Renders small table-based "tree" pieces using Module:Tree chart/data
require('strict')

local p = {}

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

-- internal renderer: accept an array of cell_args where each entry is either:
--  * a string (symbol key found in `cells`) or
--  * a table describing a custom cell (text, colspan, rowspan, etc)
function p._main(cell_args)
    -- create outer fragment and a table wrapper so TRs are valid HTML
    local ret = mw.html.create()
    local tbl = ret:tag('table')
                 :addClass('tree-chart-table')
                 :css{ borderCollapse = 'collapse', ['vertical-align'] = 'top' }

    -- two-row design (top and bottom)
    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
                -- celldef.t / celldef.b in Module:Tree chart/data are strings with <td> elements
                if celldef.t then
                    top:wikitext(celldef.t)
                else
                    -- keep structure by emitting an empty cell
                    top:tag('td'):wikitext('')
                end

                if celldef.b then
                    bottom:wikitext(celldef.b)
                else
                    bottom:tag('td'):wikitext('')
                end
            else
                -- unknown symbol: emit empty placeholders so columns align
                top:tag('td'):wikitext('')
                bottom:tag('td'):wikitext('')
            end
        else
            -- custom cell supplied from unnamed params (table with text, colspan, rowspan, etc)
            local colspan = v.colspan or cell_args.colspan or 6
            local rowspan = v.rowspan or cell_args.rowspan or 2
            local border = (v.border or cell_args.border or '2') .. 'px solid'

            -- render the custom cell into the top row
            top:tag('td')
                :attr{ colspan = tostring(colspan), rowspan = tostring(rowspan) }
                :css{ padding = '0.2em', border = border }
                :cssText(v.boxstyle or cell_args.boxstyle or '')
                :wikitext(v.text or '')

            -- if rowspan covers both rows then we do not need a placeholder in bottom.
            -- if rowspan < 2 then add an empty td on the bottom row to preserve column counts
            if tonumber(rowspan) == nil or tonumber(rowspan) < 2 then
                bottom:tag('td'):wikitext('')
            end
        end
    end

    return tostring(tbl)
end

-- public entry point used by Template:Tree chart wrapper
function p.main(frame)
    local args = require('Module:Arguments').getArgs(frame, {
        wrappers = 'Template:Tree chart',
        trim = false,
        removeBlanks = false
    })

    -- carry through some global defaults to the constructed cell_args
    local cell_args = {
        colspan = args.colspan,
        rowspan = args.rowspan,
        border = args.border,
        boxstyle = args.boxstyle
    }

    -- iterate positional/unnamed params in order
    for _, val in ipairs(args) do
        -- normalize leading/trailing whitespace
        local trimmedVal = val:match('^%s*(.-)%s*$')
        if trimmedVal == '' then
            trimmedVal = '$'  -- preserve blank cells using $ key as in your data module
        end

        if cells[trimmedVal] then
            table.insert(cell_args, trimmedVal)
        else
            -- handle unnamed params that may be named keys (param1 could contain "name=value")
            -- right-trim only to match how Template params behave
            local rightTrimmedVal = val:gsub('%s+$','')

            -- prepare a custom cell table with possible per-cell overrides
            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

    -- render and return HTML string
    return p._main(cell_args)
end

return p