Module:T-TLD: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
 
(36 intermediate revisions by the same user not shown)
Line 6: Line 6:
-- ==================== Required modules ====================
-- ==================== Required modules ====================
local Blueprint = require('Module:LuaTemplateBlueprint')
local Blueprint = require('Module:LuaTemplateBlueprint')
local TemplateHelpers = require('Module:TemplateHelpers')
local ErrorHandling = require('Module:ErrorHandling')
local ErrorHandling = require('Module:ErrorHandling')
local ConfigRepository = require('Module:ConfigRepository')
local LinkParser = require('Module:LinkParser')
local LinkParser = require('Module:LinkParser')


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


-- Blueprint default: Lazy module loader
-- Blueprint possible default: Lazy module loader
local function lazyRequire(moduleName)
local function lazyRequire(moduleName)
     return function()
     return function()
Line 25: Line 23:


-- Blueprint default: Modules to lazy load
-- Blueprint default: Modules to lazy load
    -- local getTemplateHelpers = lazyRequire('')
local getPunycode = lazyRequire('Module:Punycode')
local getPunycode = lazyRequire('Module:Punycode')
local getCountryData = lazyRequire('Module:CountryData')
local getSemanticCategoryHelpers = lazyRequire('Module:SemanticCategoryHelpers')
local getCanonicalForms = lazyRequire('Module:CanonicalForms')
local getCanonicalForms = lazyRequire('Module:CanonicalForms')


Line 34: Line 29:
-- Blueprint default: Create error context for the module
-- Blueprint default: Create error context for the module
local errorContext = ErrorHandling.createContext("T-TLD")
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


-- ================================================================================
-- ================================================================================
Line 48: Line 38:
         title = true,
         title = true,
         logo = true,
         logo = true,
        tldflair = true,
         fields = true,
         fields = true,
         socialMedia = true,
         socialMedia = true,
        ntldstats = true,
         semanticProperties = true,
         semanticProperties = true,
         categories = true,
         categories = true,
         errorReporting = true,
         errorReporting = true
        tldflair = false,
        ntldstats = false
     }
     }
})
})
Line 61: Line 51:
Blueprint.initializeConfig(template)
Blueprint.initializeConfig(template)


-- CONTROL THE VISUAL ORDER OF THE DIFFERENT ASPECTS THAT COMPOSE THE TEMPLATE
-- CONTROL THE VISUAL ORDER THAT EACH ASPECT IS RENDERED IN
template.config.blockSequence = {
template.config.blockSequence = {
     'title',
     'title',
Line 78: Line 68:
-- TEMPLATE-SPECIFIC CALLS AND CODE
-- TEMPLATE-SPECIFIC CALLS AND CODE


-- ELEMENTS
-- ELEMENT:TLD FLAIR
if template.features.tldflair then
if template.features.tldflair then
     local ElementTLDFlair = require('Module:ElementTLDFlair')
     local ElementTLDFlair = ErrorHandling.safeRequire(errorContext, 'Module:ElementTLDFlair', false)
     if ElementTLDFlair and ElementTLDFlair.elementName then
     if ElementTLDFlair and ElementTLDFlair.elementName then
         Blueprint.registerElement(ElementTLDFlair.elementName, ElementTLDFlair)
         Blueprint.registerElement(ElementTLDFlair.elementName, ElementTLDFlair)
         Blueprint.addElementToTemplate(template, 'tldflair', 3)
         Blueprint.addElementToTemplate(template, 'tldflair')
     end
     end
end
end


-- ELEMENT:NTLDSTATS
if template.features.ntldstats then
if template.features.ntldstats then
     local ElementNTLDStats = require('Module:ElementNTLDStats')
     local ElementNTLDStats = ErrorHandling.safeRequire(errorContext, 'Module:ElementNTLDStats', false)
     if ElementNTLDStats and ElementNTLDStats.elementName then
     if ElementNTLDStats and ElementNTLDStats.elementName then
         Blueprint.registerElement(ElementNTLDStats.elementName, ElementNTLDStats)
         Blueprint.registerElement(ElementNTLDStats.elementName, ElementNTLDStats)
         Blueprint.addElementToTemplate(template, 'ntldstats', 6)
         Blueprint.addElementToTemplate(template, 'ntldstats')
     end
     end
end
end


-- ELEMENTS GO HERE
-- Custom title row based on whether the TLD is a "g" or a "cc"
 
-- Custom title block override and block sequence
template.config.blocks = template.config.blocks or {}
template.config.blocks = template.config.blocks or {}
template.config.blocks.title = {
template.config.blocks.title = {
Line 107: Line 96:
             function()
             function()
                 local titleText = (args._type == 'ccTLD') and 'ccTLD' or 'gTLD'
                 local titleText = (args._type == 'ccTLD') and 'ccTLD' or 'gTLD'
                 return TemplateHelpers.renderTitleBlock(
                 return require('Module:TemplateStructure').renderTitleBlock(
                     args,
                     args,
                     'template-title template-title-tld',
                     'template-title template-title-tld',
                     titleText,
                     titleText
                    template.config.meta and template.config.meta.titleOptions or {}
                 )
                 )
             end,
             end,
Line 120: Line 108:
}
}


 
-- PROCESSORS
-- SPECIAL SEMANTIC MAPPINGS GO HERE
template.config.processors = template.config.processors or {}
template.config.processors = template.config.processors or {}
template.config.processors.tld_type = function(value, args)
-- PIC/RVC link processor
     return select(1, getCanonicalForms().normalize(value, template.config.mappings.tld_type)) or value
template.config.processors.RVC = function(value, args)
     local val = tostring(value)
    if val:match("^https://gtldresult%.icann%.org/") then
        -- Full URL provided, use it directly
        return string.format("[%s Here]", val)
    elseif val:match("^%d+$") then
        -- Only application number provided, construct the full URL
        return string.format("[https://gtldresult.icann.org/applicationstatus/applicationdetails/%s Here]", val)
    end
    return nil
end
end
template.config.processors.tld_subtype = function(value, args)
template.config.processors.PIC = template.config.processors.RVC
    return select(1, getCanonicalForms().normalize(value, template.config.mappings.tld_subtype)) or value
 
end
-- Override singular 'language' field to skip normalization
template.config.processors.introduced = function(value, args)
template.config.processors.language = function(value, args)
    return TemplateHelpers.formatDateRange(value, args.date or args.implemented, {outputMode='text'})
     return value
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
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)
-- ==================== Preprocessors ====================
    local cats = {}
-- IDN detection preprocessor - sets idn="true" for boolean transform to handle
    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)
local function detectIDNStatus(template, args)
    args._idnFlag = false
     if args.idn and args.idn ~= '' then
     if args.idn and args.idn ~= '' then
         args._idnFlag = true
         local idnVal = string.lower(args.idn)
        local puny = getPunycode().toASCII(args.idn)
        if idnVal == 'yes' or idnVal == 'true' or idnVal == '1' then
        args._ascii = puny
            -- Set idn directly to "true" - will be handled by "boolean" transform
            args.idn = "true"
           
            -- Generate ASCII representation for IDN TLDs
            local titleObj = mw.title.getCurrentTitle()
            local pageName = titleObj and titleObj.text or ''
            local puny = Blueprint.protectedExecute(
                template,
                'IDN_toASCII',
                function()
                    return getPunycode().encode(pageName:gsub('^%.',''))
                end,
                pageName
            )
            args.ascii = 'xn--' .. puny
        end
     end
     end
     return args
     return args
end
end


-- ================================================================================
-- Map tld_type to internal _type for title block display logic
 
local function mapTldTypeToInternalType(template, args)
-- ==================== Preprocessors ====================
-- Basic preprocessors
Blueprint.addPreprocessor(template, function(template, args)
     if args.tld_type and args.tld_type ~= '' then
     if args.tld_type and args.tld_type ~= '' then
         args._type = select(1, getCanonicalForms().normalize(args.tld_type, template.config.mappings.tld_type)) or args.tld_type
         args._type = select(1, getCanonicalForms().normalize(args.tld_type, template.config.mappings.tld_type)) or args.tld_type
     end
     end
     return args
     return args
end-- Map tld_type to internal _type
end
Blueprint.addPreprocessor(template, detectIDNStatus) -- IDN detection
 
Blueprint.addPreprocessor(template, 'setPageIdField') -- Blueprint default
-- Call preprocessors
-- Blueprint.addPreprocessor(template, 'deriveRegionFromCountry')
Blueprint.addPreprocessor(template, 'setPageIdField') -- Blueprint default
Blueprint.addPreprocessor(template, 'deriveRegionFromCountry') -- Blueprint possible default
Blueprint.addPreprocessor(template, mapTldTypeToInternalType) -- TLD type detection
Blueprint.addPreprocessor(template, detectIDNStatus) -- IDN detection


-- ==================== Main Render Function ====================
-- ==================== Main Render Function ====================