Module:T-Event

Revision as of 23:46, 27 April 2025 by MarkWD (talk | contribs) (// via Wikitext Extension for VSCode)

Documentation for this module may be created at Module:T-Event/doc

--Module:T-Event
-- Renders the "Event" template for events, making use of ICANNWiki's "Template Blueprint Framework"

local p = {}

-- ==================== Required modules ====================
local Blueprint = require('Module:LuaTemplateBlueprint')
local ErrorHandling = require('Module:ErrorHandling')
local ConfigRepository = require('Module:ConfigRepository')
local LinkParser = require('Module:LinkParser')
local TemplateHelpers = require('Module:TemplateHelpers')

-- Blueprint default: Module-level cache for lazy-loaded modules
local moduleCache = {}

-- Blueprint default: Lazy module loader
local function lazyRequire(moduleName)
    return function()
        if not moduleCache[moduleName] then
            moduleCache[moduleName] = require(moduleName)
        end
        return moduleCache[moduleName]
    end
end

-- Blueprint default: Modules to lazy load
    -- local getTemplateHelpers = lazyRequire('')

-- ==================== Helper Functions ====================
-- Blueprint default: Create error context for the module
local errorContext = ErrorHandling.createContext("T-Event")

-- Blueprint default: Helper for extracting semantic values from wiki links
local function extractSemanticValue(fieldValue, fieldName)
    return TemplateHelpers.extractSemanticValue(fieldValue, fieldName, errorContext)
end

-- ================================================================================

-- IMPORTANT! TEMPLATE BLUEPRINT FRAMEWORK INSTRUCTIONS
-- CONTROL OF TEMPLATE FEATURES: THIS LIST SPECIFIES IN AN EXPLICIT MANNER WHAT FEATURES ARE TO BE CALLED/RENDERED BY THE TEMPLATE.
local template = Blueprint.registerTemplate('Event', {
    features = {
        title = true,
        logo = true,
        fields = true,
        socialMedia = true,
        semanticProperties = true,
        categories = true,
        errorReporting = true,
        navigation = true
    }
})

-- Blueprint default: Initialize standard configuration
Blueprint.initializeConfig(template)

-- ================================================================================

-- Custom date-range processor: join start and ending if present
template.config.processors = template.config.processors or {}
template.config.processors.start = function(value, args, template)
    local endDate = args['ending']
    if endDate and endDate ~= '' then
        return TemplateHelpers.formatDateRange(value, endDate, {outputMode="complete"})
    end
    return TemplateHelpers.normalizeDates(value)
end
template.config.processors.ending = function(value, args, template)
    return false
end

-- ELEMENT:NAVIGATION
local ElementNavigation = require('Module:ElementNavigation')
if ElementNavigation and ElementNavigation.elementName then
    Blueprint.registerElement(ElementNavigation.elementName, ElementNavigation)
    Blueprint.addElementToTemplate(template, 'navigation', 4)
    template.config.navigation = {
        prevLabel = "← %s",
        nextLabel = "%s →",
        prevClass = "event-nav-prev",
        nextClass = "event-nav-next"
    }
end

-- ==================== Preprocessors ====================
-- Basic preprocessors
Blueprint.addPreprocessor(template, 'setPageIdField')
Blueprint.addPreprocessor(template, 'deriveRegionFromCountry')

-- ==================== Main Render Function ====================
function p.render(frame)
    return ErrorHandling.protect(
        errorContext,
        "render",
        function()
            return template.render(frame)
        end,
        ErrorHandling.getMessage("TEMPLATE_RENDER_ERROR"),
        frame
    )
end

return p