Module:TemplateStructure: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:TemplateStructure
--[[
-- A module for rendering templates block-based approach. Integrates with Module:ErrorHandling for centralized error reporting.
* Name: TemplateStructure
-- Provides block rendering functions for templates, including title blocks, divider blocks, and field tables.
* Author: Mark W. Datysgeld
* Description: Block-based template rendering module with centralized error reporting integration
* Notes: Block rendering functions for templates; title blocks; divider blocks; field tables; configurable CSS classes and attributes; ARIA support; container tag support for div wrappers; error handling integration
]]


local ErrorHandling = require('Module:ErrorHandling')
local ErrorHandling = require('Module:ErrorHandling')
Line 32: Line 35:
function p.render(args, config, errorContext)
function p.render(args, config, errorContext)
     config = config or {}
     config = config or {}
    -- ARIA support
    local ariaLabelledBy = config.ariaLabelledBy and string.format('aria-labelledby="%s"', config.ariaLabelledBy) or ''
    -- Support fullPage mode: render blocks inside a <div> wrapper when requested
    if config.containerTag == 'div' then
        local out = {}
        local tag = config.containerTag
        -- open container without default template-table class when empty
        local cls = config.tableClass or ''
        local attr = {ariaLabelledBy}
        if cls ~= '' then
            table.insert(attr, 1, string.format('class="%s"', cls))
        end
       
        table.insert(out, string.format('<%s %s>', tag, table.concat(attr, ' ')))
        for i, block in ipairs(config.blocks or {}) do
            local ok, blk = pcall(block, args, config)
            if ok and blk and blk ~= "" then
                table.insert(out, trim(blk))
            elseif not ok and errorContext then
                ErrorHandling.addError(
                    errorContext,
                    "TemplateStructure",
                    "Block #" .. i .. " execution failed",
                    tostring(blk),
                    false
                )
            end
        end
        -- close container
        table.insert(out, string.format('</%s>', tag))
        return table.concat(out, "\n")
    end
     local tableClass = config.tableClass or "template-table"
     local tableClass = config.tableClass or "template-table"
     local tableAttrs = config.tableAttrs or 'cellpadding="2"'
     local tableAttrs = config.tableAttrs or 'cellpadding="2"'
Line 40: Line 76:
     -- Begin the table markup
     -- Begin the table markup
     local result = {}
     local result = {}
     table.insert(result, string.format('{| class="%s" %s', tableClass, tableAttrs))
     table.insert(result, string.format('{| class="%s" %s %s', tableClass, tableAttrs, ariaLabelledBy))
      
      
     -- Process each block function in the supplied order
     -- Process each block function in the supplied order
Line 97: Line 133:


-- Renders a standard title block with configurable class and text (basic version)
-- Renders a standard title block with configurable class and text (basic version)
function p.renderTitleBlock(args, titleClass, titleText)
function p.renderTitleBlock(args, titleClass, titleText, titleId)
     titleClass = titleClass or "template-title"
     titleClass = titleClass or "template-title"
     return string.format('|-\n! colspan="2" class="%s" | %s', titleClass, titleText)
    -- ARIA title
    local idAttr = titleId and string.format('id="%s"', titleId) or ''
    local titleSpan = string.format('<span %s>%s</span>', idAttr, titleText)
     return string.format('|-\n! colspan="2" class="%s" | %s', titleClass, titleSpan)
end
end