Module:T-TLD

Revision as of 13:39, 28 April 2025 by MarkWD (talk | contribs) (// via Wikitext Extension for VSCode)

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

--Module:T-TLD
-- Makes use of ICANNWiki's "Template Blueprint Framework" to render the "TLD" template

local p = {}

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

-- 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('')
local getPunycode = lazyRequire('Module:Punycode')
local getCountryData = lazyRequire('Module:CountryData')
local getSemanticCategoryHelpers = lazyRequire('Module:SemanticCategoryHelpers')
local getCanonicalForms = lazyRequire('Module:CanonicalForms')

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

-- 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('TLD', {
    features = {
        title = true,
        logo = true,
        fields = true,
        socialMedia = true,
        semanticProperties = true,
        categories = true,
        errorReporting = true,
        tldflair = false,
        ntldstats = false
    }
})

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

-- CONTROL THE VISUAL ORDER OF THE DIFFERENT ASPECTS THAT COMPOSE THE TEMPLATE
template.config.blockSequence = {
    'title',
    'logo',
    'tldflair',
    'fields',
    'socialMedia',
    'ntldstats',
    'semanticProperties',
    'categories',
    'errors'
}

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

-- TEMPLATE-SPECIFIC CALLS AND CODE

-- ELEMENTS
if template.features.tldflair then
    local ElementTLDFlair = require('Module:ElementTLDFlair')
    if ElementTLDFlair and ElementTLDFlair.elementName then
        Blueprint.registerElement(ElementTLDFlair.elementName, ElementTLDFlair)
        Blueprint.addElementToTemplate(template, 'tldflair', 3)
    end
end

if template.features.ntldstats then
    local ElementNTLDStats = require('Module:ElementNTLDStats')
    if ElementNTLDStats and ElementNTLDStats.elementName then
        Blueprint.registerElement(ElementNTLDStats.elementName, ElementNTLDStats)
        Blueprint.addElementToTemplate(template, 'ntldstats', 6)
    end
end

-- ELEMENTS GO HERE

-- Custom title block override and block sequence
template.config.blocks = template.config.blocks or {}
template.config.blocks.title = {
    feature = 'title',
    render = function(template, args)
        return template.protectedExecute(
            'CustomBlock_title',
            function()
                local titleText = (args._type == 'ccTLD') and 'ccTLD' or 'gTLD'
                return TemplateHelpers.renderTitleBlock(
                    args,
                    'template-title template-title-tld',
                    titleText,
                    template.config.meta and template.config.meta.titleOptions or {}
                )
            end,
            '',
            args
        )
    end
}


-- SPECIAL SEMANTIC MAPPINGS GO HERE
template.config.processors = template.config.processors or {}
template.config.processors.tld_type = function(value, args)
    return select(1, getCanonicalForms().normalize(value, template.config.mappings.tld_type)) or value
end
template.config.processors.tld_subtype = function(value, args)
    return select(1, getCanonicalForms().normalize(value, template.config.mappings.tld_subtype)) or value
end
template.config.processors.introduced = function(value, args)
    return TemplateHelpers.formatDateRange(value, args.date or args.implemented, {outputMode='text'})
end
template.config.processors.date = function(value, args)
    return TemplateHelpers.formatDateRange(args.introduced, value, {outputMode='text'})
end
template.config.processors.website = function(value, args)
    return TemplateHelpers.normalizeWebsites(value)
end
template.config.processors.country = function(value, args)
    return TemplateHelpers.normalizeCountries(value)
end

Blueprint.registerPropertyProvider(template, function(template, args)
    local props = {}
    if args._idnFlag then props["Is IDN"] = "true" end
    return props
end)

Blueprint.registerCategoryProvider(template, function(template, args)
    local cats = {}
    local cond = template.config.categories and template.config.categories.conditional or {}
    if cond.rvc and ((args.RVC and args.RVC ~= "") or (args.PIC and args.PIC ~= "")) then
        table.insert(cats, cond.rvc)
    end
    if args._idnFlag and cond.idn then
        table.insert(cats, cond.idn)
    end
    if args._idnFlag and args._type == "ccTLD" and cond.idn_cctld then
        table.insert(cats, cond.idn_cctld)
    end
    -- Type mapping categories
    for _, cat in ipairs(getSemanticCategoryHelpers().addMappingCategories(args._type, template.config.mappings.tld_type) or {}) do
        table.insert(cats, cat)
    end
    -- Subtype mapping categories
    for _, cat in ipairs(getSemanticCategoryHelpers().addMappingCategories(args._type, template.config.mappings.tld_subtype) or {}) do
        table.insert(cats, cat)
    end
    return cats
end)

-- IDN detection preprocessor
local function detectIDNStatus(template, args)
    args._idnFlag = false
    if args.idn and args.idn ~= '' then
        args._idnFlag = true
        local puny = getPunycode().toASCII(args.idn)
        args._ascii = puny
    end
    return args
end

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

-- ==================== Preprocessors ====================
-- Basic preprocessors
Blueprint.addPreprocessor(template, detectIDNStatus)  -- IDN detection
Blueprint.addPreprocessor(template, 'setPageIdField')  -- Blueprint default
-- Blueprint.addPreprocessor(template, 'deriveRegionFromCountry')

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

return p