Module:TemplateStructure: Difference between revisions
Appearance
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 1: | Line 1: | ||
-- Module:TemplateStructure | -- Module:TemplateStructure | ||
-- A module for rendering templates using a modular, block-based approach with error handling. | -- A module for rendering templates using a modular, block-based approach with error handling. | ||
-- Now supports integration with the ErrorHandling module for centralized error reporting. | |||
local ErrorHandling = require('Module:ErrorHandling') | |||
local p = {} | local p = {} | ||
| Line 16: | Line 18: | ||
--[[ | --[[ | ||
p.render(args, config) | p.render(args, config, errorContext) | ||
Renders a table using a modular, block-based approach. | Renders a table using a modular, block-based approach. | ||
Parameters: | Parameters: | ||
args | args - Template parameters (passed to each block function) | ||
config - Configuration options: | config - Configuration options: | ||
tableClass: CSS class for the table (default: "template-table") | tableClass: CSS class for the table (default: "template-table") | ||
tableAttrs: Additional table attributes (default: 'cellpadding="2"') | tableAttrs: Additional table attributes (default: 'cellpadding="2"') | ||
| Line 28: | Line 30: | ||
Each accepts (args, config) and returns a string | Each accepts (args, config) and returns a string | ||
continueOnError: Continue rendering if a block fails (default: true) | continueOnError: Continue rendering if a block fails (default: true) | ||
errorContext - Optional error context from ErrorHandling.createContext() | |||
If provided, errors will be reported to this context | |||
Returns: | Returns: | ||
Wikitext markup for the complete table | Wikitext markup for the complete table | ||
]] | ]] | ||
function p.render(args, config) | function p.render(args, config, errorContext) | ||
config = config or {} | config = config or {} | ||
local tableClass = config.tableClass or "template-table" | local tableClass = config.tableClass or "template-table" | ||
| Line 62: | Line 66: | ||
end | end | ||
else | else | ||
-- | -- Get error message | ||
local errorMsg = tostring(blockOutput) | |||
-- Report to error context if available | |||
if errorContext then | |||
ErrorHandling.addError( | |||
errorContext, | |||
"TemplateStructure", | |||
"Block #" .. i .. " execution failed", | |||
errorMsg, | |||
false | |||
) | |||
end | |||
-- Also record error with minimal data for backward compatibility | |||
errorCount = errorCount + 1 | errorCount = errorCount + 1 | ||
errorData["block-" .. i] = | errorData["block-" .. i] = errorMsg | ||
-- If we shouldn't continue on error, break the loop | -- If we shouldn't continue on error, break the loop | ||
| Line 72: | Line 90: | ||
end | end | ||
else | else | ||
-- | -- Report non-function blocks to error context if available | ||
-- | if errorContext then | ||
ErrorHandling.addError( | |||
errorContext, | |||
"TemplateStructure", | |||
"Block #" .. i .. " is not a function", | |||
"Expected function, got " .. type(block), | |||
false | |||
) | |||
end | |||
-- Also record error with minimal data for backward compatibility | |||
errorCount = errorCount + 1 | |||
errorData["block-" .. i] = "Block is not a function" | |||
end | end | ||
end | end | ||
Revision as of 18:24, 11 April 2025
Documentation for this module may be created at Module:TemplateStructure/doc
-- Module:TemplateStructure
-- A module for rendering templates using a modular, block-based approach with error handling.
-- Now supports integration with the ErrorHandling module for centralized error reporting.
local ErrorHandling = require('Module:ErrorHandling')
local p = {}
-- Local trim function: removes leading and trailing whitespace.
local function trim(s)
return (s:gsub("^%s+", ""):gsub("%s+$", ""))
end
-- Format block errors for data attribute output
local function formatBlockError(blockIndex, errorMsg)
-- No function body needed - we'll handle this differently
return nil
end
--[[
p.render(args, config, errorContext)
Renders a table using a modular, block-based approach.
Parameters:
args - Template parameters (passed to each block function)
config - Configuration options:
tableClass: CSS class for the table (default: "template-table")
tableAttrs: Additional table attributes (default: 'cellpadding="2"')
blocks: Functions that generate table rows
Each accepts (args, config) and returns a string
continueOnError: Continue rendering if a block fails (default: true)
errorContext - Optional error context from ErrorHandling.createContext()
If provided, errors will be reported to this context
Returns:
Wikitext markup for the complete table
]]
function p.render(args, config, errorContext)
config = config or {}
local tableClass = config.tableClass or "template-table"
local tableAttrs = config.tableAttrs or 'cellpadding="2"'
local blocks = config.blocks or {}
local continueOnError = config.continueOnError
if continueOnError == nil then continueOnError = true end -- Default to true
-- Begin the table markup.
local result = {}
table.insert(result, string.format('{| class="%s" %s', tableClass, tableAttrs))
-- Minimalist error tracking
local errorCount = 0
local errorData = {}
-- Process each block function in the supplied order
for i, block in ipairs(blocks) do
if type(block) == "function" then
-- Protect block execution to avoid template failure
local success, blockOutput = pcall(function()
return block(args, config)
end)
if success then
blockOutput = trim(blockOutput)
if blockOutput ~= "" then
table.insert(result, blockOutput)
end
else
-- Get error message
local errorMsg = tostring(blockOutput)
-- Report to error context if available
if errorContext then
ErrorHandling.addError(
errorContext,
"TemplateStructure",
"Block #" .. i .. " execution failed",
errorMsg,
false
)
end
-- Also record error with minimal data for backward compatibility
errorCount = errorCount + 1
errorData["block-" .. i] = errorMsg
-- If we shouldn't continue on error, break the loop
if not continueOnError then
break
end
end
else
-- Report non-function blocks to error context if available
if errorContext then
ErrorHandling.addError(
errorContext,
"TemplateStructure",
"Block #" .. i .. " is not a function",
"Expected function, got " .. type(block),
false
)
end
-- Also record error with minimal data for backward compatibility
errorCount = errorCount + 1
errorData["block-" .. i] = "Block is not a function"
end
end
-- Close the table
table.insert(result, "|}")
-- Add minimal error information using data attributes
if errorCount > 0 then
-- Build data attributes
local attrStr = ' data-structure-error="1"'
attrStr = attrStr .. ' data-error-count="' .. errorCount .. '"'
-- Add each error
for key, value in pairs(errorData) do
attrStr = attrStr .. ' data-error-' .. key .. '="' .. value .. '"'
end
-- Add hidden div with error data
table.insert(result, string.format('<div style="display:none"%s></div>', attrStr))
end
local finalOutput = table.concat(result, "\n")
return trim(finalOutput)
end
return p