Module:LuaTemplateBlueprint: Difference between revisions

// via Wikitext Extension for VSCode
Tag: Reverted
// via Wikitext Extension for VSCode
 
(21 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 = '%[%[.-%]%]'
-- Semantic property HTML templates
local PROPERTY_DIV_PREFIX = '<div style="display:none;">\n  {{#set: '
local PROPERTY_DIV_SUFFIX = ' }}\n</div>'
-- 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 ==========
-- Standard configuration sections used by templates
p.configSections = {
    'meta',
    'categories',
    'patterns',
    'fields',
    'mappings',
    'constants',
    'semantics'
}
-- 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)
   
 
     -- Apply overrides to each section
     -- Use ConfigHelpers to deep merge configurations
     local config = {}
     local config = ConfigHelpers.deepMerge(baseConfig, configOverrides)
    for _, section in ipairs(p.configSections) do
 
        config[section] = config[section] or {}
       
        -- Copy base config for this section if available
        if baseConfig[section] then
            for k, v in pairs(baseConfig[section]) do
                config[section][k] = v
            end
        end
       
        -- Apply overrides for this section if available
        if configOverrides[section] then
            for k, v in pairs(configOverrides[section]) do
                config[section][k] = v
            end
        end
    end
   
     -- 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
-- Get field value from args (delegated to TemplateHelpers)
-- @param field table The field definition
-- @param args table The template arguments
-- @return string|nil The field value or nil if not found
function p.getFieldValue(field, args, template)
    local _, value = TemplateHelpers.getFieldValue(args, field)
    return value
end
end


Line 1,036: Line 998:
     -- Override raw country/region with normalized names if country field exists
     -- Override raw country/region with normalized names if country field exists
     if args.country and args.country ~= '' then
     if args.country and args.country ~= '' then
        -- Debug: Check if we reach the country processing code
        if template._errorContext then
            local ErrorHandling = require('Module:ErrorHandling')
            ErrorHandling.addError(template._errorContext, "BlueprintDebug",
                string.format("About to process country='%s'", args.country),
                "", false)
        end
       
         local cr = require('Module:ConfigRepository')
         local cr = require('Module:ConfigRepository')
       
        -- Debug: Check if CountryData module loads
        if template._errorContext then
            local ErrorHandling = require('Module:ErrorHandling')
            ErrorHandling.addError(template._errorContext, "ModuleLoadDebug",
                "Attempting to load CountryData module",
                "", false)
        end
       
         local cd = require('Module:CountryData')
         local cd = require('Module:CountryData')
       
        -- Debug: Check if module loaded successfully
        if template._errorContext then
            local ErrorHandling = require('Module:ErrorHandling')
            ErrorHandling.addError(template._errorContext, "ModuleLoadedDebug",
                string.format("CountryData loaded: %s", cd and "success" or "failed"),
                "", false)
        end
       
         local norm = p.protectedExecute(
         local norm = p.protectedExecute(
             template,
             template,
             'CountryData_Override',
             'CountryData_Override',
             function() return cd.getSemanticCountryRegionProperties(args.country, template._errorContext) end,
             function() return cd.getSemanticCountryRegionProperties(args.country) end,
             {},
             {},
             args.country
             args.country
         )
         )
       
        -- Debug: Check what protectedExecute returned
        if template._errorContext then
            local ErrorHandling = require('Module:ErrorHandling')
            ErrorHandling.addError(template._errorContext, "ProtectedExecuteDebug",
                string.format("protectedExecute returned: %s", norm and "data" or "nil"),
                "", false)
        end
         if norm then
         if norm then
             local countryKey = cr.semanticProperties.country
             local countryKey = cr.semanticProperties.country
Line 1,240: Line 1,168:
function p.renderTemplate(template, frame)
function p.renderTemplate(template, frame)
     template.current_frame = frame -- Store frame on template instance
     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
     -- Check recursion depth to prevent infinite loops
Line 1,256: Line 1,188:
         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,277: 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,293: Line 1,228:
      
      
     local result = TemplateStructure.render(args, structureConfig, template._errorContext)
     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
     template.current_frame = nil -- Clear frame from template instance