Module:LuaTemplateBlueprint: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| (34 intermediate revisions by the same user not shown) | |||
| Line 65: | Line 65: | ||
-- Wiki link pattern for regex matching | -- Wiki link pattern for regex matching | ||
local WIKI_LINK_PATTERN = '%[%[.-%]%]' | local WIKI_LINK_PATTERN = '%[%[.-%]%]' | ||
-- Empty table for returning when needed | -- Empty table for returning when needed | ||
local EMPTY_OBJECT = {} | local EMPTY_OBJECT = {} | ||
| Line 75: | Line 72: | ||
local ErrorHandling = require('Module:ErrorHandling') | local ErrorHandling = require('Module:ErrorHandling') | ||
local ConfigRepository = require('Module:ConfigRepository') | local ConfigRepository = require('Module:ConfigRepository') | ||
local ConfigHelpers = require('Module:ConfigHelpers') | |||
local TemplateHelpers = require('Module:TemplateHelpers') | local TemplateHelpers = require('Module:TemplateHelpers') | ||
local TemplateStructure = require('Module:TemplateStructure') | local TemplateStructure = require('Module:TemplateStructure') | ||
| Line 343: | Line 341: | ||
-- ========== Configuration Integration ========== | -- ========== Configuration Integration ========== | ||
-- Initialize the standard configuration for a template | -- Initialize the standard configuration for a template | ||
-- Combines base config from ConfigRepository with template overrides | -- Combines base config from ConfigRepository with template overrides | ||
| Line 361: | Line 348: | ||
local templateType = template.type | local templateType = template.type | ||
local configOverrides = template.config or {} | local configOverrides = template.config or {} | ||
-- Get base configuration from repository | -- Get base configuration from repository | ||
local baseConfig = ConfigRepository.getStandardConfig(templateType) | local baseConfig = ConfigRepository.getStandardConfig(templateType) | ||
-- | -- Use ConfigHelpers to deep merge configurations | ||
local config = | local config = ConfigHelpers.deepMerge(baseConfig, configOverrides) | ||
-- Store complete config in template | -- Store complete config in template | ||
template.config = config | template.config = config | ||
| Line 398: | Line 368: | ||
end) | end) | ||
end | end | ||
return config | return config | ||
end | end | ||
| Line 439: | Line 409: | ||
args, | args, | ||
TEMPLATE_TITLE_CLASS_PREFIX .. string.lower(templateId), | TEMPLATE_TITLE_CLASS_PREFIX .. string.lower(templateId), | ||
titleText | titleText, | ||
template.titleId | |||
) | ) | ||
end, | end, | ||
| Line 703: | Line 674: | ||
template._processors = TemplateFieldProcessor.initializeProcessors(template) | template._processors = TemplateFieldProcessor.initializeProcessors(template) | ||
return template._processors | return template._processors | ||
end | end | ||
| Line 846: | Line 808: | ||
return template | return template | ||
end | end | ||
-- Validate property value to prevent SMW parser issues | -- Validate property value to prevent SMW parser issues | ||
-- @param value string The value to validate | -- @param value string The value to validate | ||
-- @return string The validated value | -- @return string The validated value | ||
local function validatePropertyValue(value | local function validatePropertyValue(value) | ||
if not value or value == '' then | if not value or value == '' then | ||
return '' | return '' | ||
| Line 897: | Line 829: | ||
return value | return value | ||
end | end | ||
| Line 957: | Line 849: | ||
(not template._propertyProviders or #template._propertyProviders == 0) then | (not template._propertyProviders or #template._propertyProviders == 0) then | ||
return EMPTY_STRING | return EMPTY_STRING | ||
end | |||
-- Set time budget for semantic property processing (450ms) | |||
local startTime = os.clock() | |||
local timeLimit = 0.45 -- seconds | |||
local checkInterval = 10 -- check every N properties | |||
local propertyCounter = 0 | |||
local function checkTimeLimit() | |||
propertyCounter = propertyCounter + 1 | |||
if propertyCounter % checkInterval == 0 then | |||
if os.clock() - startTime > timeLimit then | |||
return true -- time exceeded | |||
end | |||
end | |||
return false | |||
end | end | ||
| Line 964: | Line 872: | ||
} | } | ||
-- Create collector | -- Build initial property mapping (like original code) | ||
local allProperties = {} | |||
-- Process basic properties - just map property names to field names | |||
for property, param in pairs(properties) do | |||
if not skipProperties[property] then | |||
-- Just map the property to the field name | |||
-- SemanticAnnotations will handle value extraction and transforms | |||
allProperties[property] = param | |||
end | |||
end | |||
-- Create collector for deduplication of additional properties and providers | |||
local collector = { | local collector = { | ||
seen = {}, -- Track property:value signatures | seen = {}, -- Track property:value signatures | ||
| Line 970: | Line 890: | ||
count = 0 -- Track total property count | count = 0 -- Track total property count | ||
} | } | ||
-- Process additional properties with early deduplication and multi-value handling | -- Process additional properties with early deduplication and multi-value handling | ||
| Line 1,002: | Line 907: | ||
end | end | ||
-- Process each value individually | -- Process each value individually | ||
for _, singleValue in ipairs(values) do | for _, singleValue in ipairs(values) do | ||
local trimmedValue = singleValue:match("^%s*(.-)%s*$") | local trimmedValue = singleValue:match("^%s*(.-)%s*$") | ||
if trimmedValue and trimmedValue ~= '' then | if trimmedValue and trimmedValue ~= '' then | ||
local | -- Apply transform if available | ||
if | local finalValue = trimmedValue | ||
if transform then | |||
finalValue = p.protectedExecute( | |||
template, | |||
'Transform_' .. property, | |||
function() return transform(trimmedValue, args, template) end, | |||
trimmedValue, | |||
trimmedValue, | |||
args, | |||
template | |||
) | |||
end | |||
-- Validate and add to collector | |||
finalValue = validatePropertyValue(finalValue) | |||
if finalValue and finalValue ~= '' then | |||
if not collector.properties[property] then | |||
collector.properties[property] = {} | |||
end | |||
table.insert(collector.properties[property], finalValue) | |||
collector.count = collector.count + 1 | |||
end | end | ||
end | end | ||
| Line 1,017: | Line 941: | ||
end | end | ||
-- Process property providers with early deduplication | -- Process property providers with early deduplication | ||
| Line 1,069: | Line 962: | ||
-- Provider returned an array of values | -- Provider returned an array of values | ||
for _, v in ipairs(value) do | for _, v in ipairs(value) do | ||
local validated = validatePropertyValue(v) | |||
if validated and validated ~= '' then | |||
if not collector.properties[property] then | |||
collector.properties[property] = {} | |||
end | |||
table.insert(collector.properties[property], validated) | |||
collector.count = collector.count + 1 | |||
end | |||
end | end | ||
else | else | ||
-- Provider returned a single value | -- Provider returned a single value | ||
local validated = validatePropertyValue(value) | |||
if validated and validated ~= '' then | |||
if not collector.properties[property] then | |||
collector.properties[property] = {} | |||
end | |||
table.insert(collector.properties[property], validated) | |||
collector.count = collector.count + 1 | |||
end | |||
end | end | ||
end | end | ||
| Line 1,108: | Line 1,015: | ||
if norm[regionKey] then | if norm[regionKey] then | ||
collector.properties[regionKey] = norm[regionKey] | collector.properties[regionKey] = norm[regionKey] | ||
end | |||
end | |||
end | |||
-- Merge basic properties mapping with deduplicated additional properties | |||
-- Basic properties (allProperties) contains field mappings | |||
-- Additional properties (collector.properties) contains actual values | |||
local finalProperties = {} | |||
-- Copy basic property mappings | |||
for property, fieldName in pairs(allProperties) do | |||
finalProperties[property] = fieldName | |||
end | |||
-- Add deduplicated additional properties (these have actual values) | |||
for property, value in pairs(collector.properties) do | |||
finalProperties[property] = value -- This might be an array of values or a single value | |||
end | |||
-- Process fixed properties | |||
if semanticConfig.fixedProperties and type(semanticConfig.fixedProperties) == 'table' then | |||
for propName, propValue in pairs(semanticConfig.fixedProperties) do | |||
if not skipProperties[propName] then -- Check skipProperties as well | |||
local validatedValue = validatePropertyValue(propValue) | |||
if validatedValue and validatedValue ~= '' then | |||
-- Pass as a single-item array to be processed by the | |||
-- 'Direct array of values' path in SemanticAnnotations.lua | |||
finalProperties[propName] = {validatedValue} | |||
end | |||
end | end | ||
end | end | ||
| Line 1,113: | Line 1,049: | ||
-- Add debug info as HTML comment (Phase 1 monitoring) | -- Add debug info as HTML comment (Phase 1 monitoring) | ||
local basicCount = 0 | |||
for _ in pairs(allProperties) do basicCount = basicCount + 1 end | |||
local debugInfo = string.format( | local debugInfo = string.format( | ||
"<!-- SMW Debug: | "<!-- SMW Debug: basic_props=%d, additional_props=%d, unique_signatures=%d -->", | ||
basicCount, | |||
collector.count or 0, | collector.count or 0, | ||
table.maxn(collector.seen or {}) | table.maxn(collector.seen or {}) | ||
) | ) | ||
-- Send all | -- Send all properties to SemanticAnnotations in one batch | ||
local semanticOutput = SemanticAnnotations.setSemanticProperties( | local semanticOutput = SemanticAnnotations.setSemanticProperties( | ||
args, | args, | ||
finalProperties, | |||
semanticOptions | semanticOptions | ||
) | ) | ||
| Line 1,227: | Line 1,167: | ||
-- @return string The rendered template HTML | -- @return string The rendered template HTML | ||
function p.renderTemplate(template, frame) | function p.renderTemplate(template, frame) | ||
template.current_frame = frame -- Store frame on template instance | |||
-- Generate a unique ID for the title element for ARIA | |||
local pageId = TemplateHelpers.getCurrentPageId() or '0' | |||
template.titleId = 'template-title-' .. template.type .. '-' .. pageId | |||
-- Check recursion depth to prevent infinite loops | |||
local depth = 0 | |||
if frame.args and frame.args._recursion_depth then | |||
depth = tonumber(frame.args._recursion_depth) or 0 | |||
elseif frame:getParent() and frame:getParent().args and frame:getParent().args._recursion_depth then | |||
depth = tonumber(frame:getParent().args._recursion_depth) or 0 | |||
end | |||
if depth > 3 then | |||
return '<span class="error">Template recursion depth exceeded (limit: 3)</span>' | |||
end | |||
if not template._errorContext then | if not template._errorContext then | ||
template._errorContext = p.createErrorContext(template) | template._errorContext = p.createErrorContext(template) | ||
end | end | ||
ErrorHandling.addStatus(template._errorContext, "LuaTemplateBlueprint", "Now rendering " .. template.type) | |||
if not template.config.meta then | if not template.config.meta then | ||
| Line 1,237: | Line 1,197: | ||
local args = frame:getParent().args or {} | local args = frame:getParent().args or {} | ||
args = TemplateHelpers.normalizeArgumentCase(args) | args = TemplateHelpers.normalizeArgumentCase(args) | ||
-- Increment recursion depth for any child template calls | |||
args._recursion_depth = tostring(depth + 1) | |||
args = p.runPreprocessors(template, args) | args = p.runPreprocessors(template, args) | ||
| Line 1,248: | Line 1,211: | ||
tableClass = tableClass, | tableClass = tableClass, | ||
blocks = {}, | blocks = {}, | ||
containerTag = template.features.fullPage and "div" or "table" | containerTag = template.features.fullPage and "div" or "table", | ||
ariaLabelledBy = template.titleId | |||
} | } | ||
| Line 1,263: | Line 1,227: | ||
end | end | ||
local result = TemplateStructure.render(args, structureConfig, template._errorContext) | |||
-- Append status and error divs to the final output | |||
result = result .. ErrorHandling.formatCombinedOutput(template._errorContext) | |||
template.current_frame = nil -- Clear frame from template instance | |||
return result | |||
end | end | ||