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
-- Module:Tree chart
-- Renders small table-based "tree" pieces using Module:Tree chart/data
-- Defensive renderer for table-based tree pieces
require('strict')
-- 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)


local p = {}
local p = {}
Line 8: Line 11:
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:
-- internal renderer: accepts an array where each entry is either:
--  * a string (symbol key found in `cells`) or
--  * a string key (looks up cells[key]) or
--  * a table describing a custom cell (text, colspan, rowspan, etc)
--  * a table describing a custom cell (text, colspan, rowspan, etc)
function p._main(cell_args)
function p._main(cell_args)
    -- create outer fragment and a table wrapper so TRs are valid HTML
     local ret = mw.html.create()
     local ret = mw.html.create()
     local tbl = ret:tag('table')
     local tbl = ret:tag('table')
Line 18: Line 20:
                 :css{ borderCollapse = 'collapse', ['vertical-align'] = 'top' }
                 :css{ borderCollapse = 'collapse', ['vertical-align'] = 'top' }


    -- two-row design (top and bottom)
     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' }
Line 26: Line 27:
             local celldef = cells[v]
             local celldef = cells[v]
             if celldef then
             if celldef then
                -- celldef.t / celldef.b in Module:Tree chart/data are strings with <td> elements
                 if celldef.t then
                 if celldef.t then
                     top:wikitext(celldef.t)
                     top:wikitext(celldef.t)
                 else
                 else
                    -- keep structure by emitting an empty cell
                     top:tag('td'):wikitext('')
                     top:tag('td'):wikitext('')
                 end
                 end
                 if celldef.b then
                 if celldef.b then
                     bottom:wikitext(celldef.b)
                     bottom:wikitext(celldef.b)
Line 40: Line 38:
                 end
                 end
             else
             else
                 -- unknown symbol: emit empty placeholders so columns align
                 -- 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 supplied from unnamed params (table with text, colspan, rowspan, etc)
             -- 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 = (v.border or cell_args.border or '2') .. 'px solid'


            -- render the custom cell into the top row
             top:tag('td')
             top:tag('td')
                 :attr{ colspan = tostring(colspan), rowspan = tostring(rowspan) }
                 :attr{ colspan = tostring(colspan), rowspan = tostring(rowspan) }
Line 57: Line 54:
                 :wikitext(v.text 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
             if tonumber(rowspan) == nil or tonumber(rowspan) < 2 then
                 bottom:tag('td'):wikitext('')
                 bottom:tag('td'):wikitext('')
Line 68: Line 63:
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, {
     local args = require('Module:Arguments').getArgs(frame, {
Line 76: Line 70:
     })
     })


    -- carry through some global defaults to the constructed cell_args
     local cell_args = {
     local cell_args = {
         colspan = args.colspan,
         colspan = args.colspan,
Line 84: Line 77:
     }
     }


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


Line 95: Line 86:
             table.insert(cell_args, trimmedVal)
             table.insert(cell_args, trimmedVal)
         else
         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+$','')
             local rightTrimmedVal = val:gsub('%s+$','')
            -- prepare a custom cell table with possible per-cell overrides
             local custom = {
             local custom = {
                 text = args[trimmedVal] or ('{{{'..trimmedVal..'}}}'),
                 text = args[trimmedVal] or ('{{{'..trimmedVal..'}}}'),
Line 111: Line 98:
     end
     end


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


return p
return p

Revision as of 14:34, 9 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.

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

local p = {}

-- load cell definitions (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)
    local ret = mw.html.create()
    local tbl = ret:tag('table')
                 :addClass('tree-chart-table')
                 :css{ borderCollapse = 'collapse', ['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
                -- unknown symbol => placeholder cells so columns align
                top:tag('td'):wikitext('')
                bottom:tag('td'):wikitext('')
            end
        else
            -- custom cell (table) from unnamed params
            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'

            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 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