Module:TemplateStructure: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| (6 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
-- | --[[ | ||
- | * Name: TemplateStructure | ||
* 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 | -- Support fullPage mode: render blocks inside a <div> wrapper when requested | ||
| Line 37: | Line 42: | ||
local out = {} | local out = {} | ||
local tag = config.containerTag | local tag = config.containerTag | ||
-- open container | -- open container without default template-table class when empty | ||
table.insert(out, string.format('<%s | 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 | for i, block in ipairs(config.blocks or {}) do | ||
local ok, blk = pcall(block, args, config) | local ok, blk = pcall(block, args, config) | ||
| Line 65: | 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 122: | 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, | -- 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 | ||