Module:Tree chart: Difference between revisions

From Ekatra Foundation
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
-- Module:Tree chart
-- Renders small table-based "tree" pieces using Module:Tree chart/data
require('strict')
require('strict')


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: 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)
function p._main(cell_args)
local ret = mw.html.create()
    -- create outer fragment and a table wrapper so TRs are valid HTML
local top = ret:tag('tr')
    local ret = mw.html.create()
:css{ height = '1px',
    local tbl = ret:tag('table')
['text-align'] = 'center' }
                :addClass('tree-chart-table')
local bottom = ret:tag('tr')
                :css{ borderCollapse = 'collapse', ['vertical-align'] = 'top' }
:css{ height = '1px',
 
['text-align'] = 'center' }
    -- two-row design (top and bottom)
for _, v in ipairs(cell_args) do
    local top = tbl:tag('tr'):css{ height = '1px', ['text-align'] = 'center' }
if type(v) == 'string' then
    local bottom = tbl:tag('tr'):css{ height = '1px', ['text-align'] = 'center' }
top:wikitext(cells[v].t)
 
bottom:wikitext(cells[v].b)
    for _, v in ipairs(cell_args) do
else
        if type(v) == 'string' then
top:tag('td')
            local celldef = cells[v]
:attr{ colspan = v.colspan or cell_args.colspan or 6,
            if celldef then
rowspan = v.rowspan or cell_args.rowspan or 2 }
                -- celldef.t / celldef.b in Module:Tree chart/data are strings with <td> elements
:css{ padding = '0.2em',
                if celldef.t then
border = (v.border or cell_args.border or '2') .. 'px solid' }
                    top:wikitext(celldef.t)
:cssText(v.boxstyle or cell_args.boxstyle)
                else
:wikitext(v.text)
                    -- keep structure by emitting an empty cell
end
                    top:tag('td'):wikitext('')
end
                end
return tostring(ret)
 
                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
end


-- public entry point used by Template:Tree chart wrapper
function p.main(frame)
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:Tree chart', trim = false, removeBlanks = false})
    local args = require('Module:Arguments').getArgs(frame, {
local cell_args = {
        wrappers = 'Template:Tree chart',
colspan = args.colspan,
        trim = false,
rowspan = args.rowspan,
        removeBlanks = false
border = args.border,
    })
boxstyle = args.boxstyle
 
}
    -- carry through some global defaults to the constructed cell_args
for _, val in ipairs(args) do
    local cell_args = {
local trimmedVal = val:match('^%s*(.-)%s*$')
        colspan = args.colspan,
if trimmedVal == '' then
        rowspan = args.rowspan,
trimmedVal = '$'
        border = args.border,
end
        boxstyle = args.boxstyle
if cells[trimmedVal] then
    }
table.insert(cell_args, trimmedVal)
 
else
    -- iterate positional/unnamed params in order
-- Unnamed params behave weirdly
    for _, val in ipairs(args) do
-- white space at the front counts for param_{{{1}}}, but not whitespace at the end, so remove it
        -- normalize leading/trailing whitespace
local rightTrimmedVal = val:gsub('%s+$','')
        local trimmedVal = val:match('^%s*(.-)%s*$')
table.insert(cell_args, {
        if trimmedVal == '' then
text = args[trimmedVal] or ('{{{'..trimmedVal..'}}}'),
            trimmedVal = '$' -- preserve blank cells using $ key as in your data module
colspan = args['colspan_'..rightTrimmedVal],
        end
rowspan = args['rowspan_'..rightTrimmedVal],
 
border = args['border_'..rightTrimmedVal],
        if cells[trimmedVal] then
boxstyle = args['boxstyle_'..rightTrimmedVal]
            table.insert(cell_args, trimmedVal)
})
        else
end
            -- handle unnamed params that may be named keys (param1 could contain "name=value")
end
            -- right-trim only to match how Template params behave
            local rightTrimmedVal = val:gsub('%s+$','')
return p._main(cell_args)
 
            -- 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
end


return p
return p

Revision as of 14:28, 9 November 2025

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