Module:T-Event

Revision as of 02:01, 17 May 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
-- Makes use of ICANNWiki's "Template Blueprint Framework" to render the "Event" template

local p = {}

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

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

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

-- 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,
        navigation = true,
        socialMedia = true,
        semanticProperties = true,
        categories = true,
        errorReporting = true
    }
})

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

-- CONTROL THE VISUAL ORDER THAT EACH ASPECT IS RENDERED IN
template.config.blockSequence = {
    'title',
    'logo',
    'fields',
    'navigation',
    'socialMedia',
    'semanticProperties',
    'categories',
    'errors'
}

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

-- TEMPLATE-SPECIFIC CALLS AND CODE

-- ELEMENT:NAVIGATION
    local ElementNavigation = ErrorHandling.safeRequire(errorContext, 'Module:ElementNavigation', false)
if ElementNavigation and ElementNavigation.elementName then
    Blueprint.registerElement(ElementNavigation.elementName, ElementNavigation)
Blueprint.addElementToTemplate(template, 'navigation')
    template.config.navigation = {
        prevLabel = "← %s",
        nextLabel = "%s →",
        classPrefix = "event"
    }
end

-- CUSTOM DATE-RANGE PROCESSOR
-- In cases where there are both "start" and "ending" dates to the event, we join them into a single "Date" field, with the dates separated by a "–" (En dash)
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

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

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

-- ==================== Main Render Function ====================
-- Blueprint default: Render
function p.render(frame)
    return ErrorHandling.protect(
        errorContext,
        "render",
        function()
            -- Render using Blueprint
            local out = template.render(frame)
            -- Debug: list any numeric keys in raw args
            local rawArgs = frame.args or {}
            local nums = {}
            for k, _ in pairs(rawArgs) do
                if type(k) == "number" or (type(k) == "string" and tonumber(k) ~= nil) then
                    table.insert(nums, tostring(k))
                end
            end
            if #nums > 0 then
                -- Debug: print numeric keys to screen
                out = out .. string.format("<div style=\"border:1px solid red;padding:4px;\">T-Event numeric keys: %s</div>", table.concat(nums, ","))
            end
            return out
        end,
        ErrorHandling.getMessage("TEMPLATE_RENDER_ERROR"),
        frame
    )
end

return p