Jump to content

Module:ElementTLDFlair: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
--[[  
--[[
* Module:ElementTLDFlair
* Name: ElementTLDFlair
* Renders a flair element for TLD styling based on type, subtype, and IDN.
* Author: Mark W. Datysgeld
* Designed for integration with the Blueprint template system.
* Description: Element module that renders visual flair elements for TLD styling based on type, subtype, and IDN status
]]
* Notes: Designed for Blueprint template system integration; builds CSS classes from TLD properties; uses CanonicalForms for subtype mapping; differentiates IDN ccTLDs and gTLDs; styled via CSS pseudo-elements; protected execution with error handling
]]


local p = {}
local p = {}
Line 32: Line 33:


         return execute(function()
         return execute(function()
             local tldType   = args.tld_type or ""
            -- Get TLD type: prefer preprocessor-normalized _type, fall back to raw tld_type
             local subtype   = args.tld_subtype or ""
             local tldType = args._type or args.tld_type or ""
             local isIDN     = args._idnFlag
             local subtype = args.tld_subtype or ""
             local punycode = args.ascii or ""
             local isIDN   = (args.idn == "true")
             local punycode = args.ascii or ""


             -- Build CSS class list
             -- Build CSS class list
             local classes = { "tld-flair" }
             local classes = { "tld-flair" }
           
            -- Add subtype class if present
             if subtype ~= "" then
             if subtype ~= "" then
                 local _, css = CanonicalForms.normalize(subtype, template.config.mappings.tld_subtype)
                 local _, css = CanonicalForms.normalize(subtype, template.config.mappings.tld_subtype)
Line 45: Line 49:
                 end
                 end
             end
             end
           
            -- Add IDN class if applicable
             if isIDN then
             if isIDN then
                 local idnClass = args._type == "ccTLD" and "tld-template-idn-cctld" or "tld-template-idn-gtld"
                -- Normalize the type if it hasn't been normalized yet
                local normalizedType = tldType
                if tldType ~= "" and tldType ~= "gTLD" and tldType ~= "ccTLD" then
                    normalizedType = select(1, CanonicalForms.normalize(tldType, template.config.mappings.tld_type)) or tldType
                end
               
                 local idnClass = (normalizedType == "ccTLD") and "tld-template-idn-cctld" or "tld-template-idn-gtld"
                 table.insert(classes, idnClass)
                 table.insert(classes, idnClass)
            -- Otherwise, if no subtype, add base type class for non-IDN TLDs
            elseif subtype == "" and tldType ~= "" then
                -- Normalize the type if it hasn't been normalized yet
                local normalizedType = tldType
                if tldType ~= "gTLD" and tldType ~= "ccTLD" then
                    normalizedType = select(1, CanonicalForms.normalize(tldType, template.config.mappings.tld_type)) or tldType
                end
               
                local typeClass = (normalizedType == "ccTLD") and "tld-template-cctld" or "tld-template-gtld"
                table.insert(classes, typeClass)
             end
             end
           
             local classAttr = table.concat(classes, " ")
             local classAttr = table.concat(classes, " ")



Latest revision as of 13:24, 1 December 2025

Documentation for this module may be created at Module:ElementTLDFlair/doc

--[[
* Name: ElementTLDFlair
* Author: Mark W. Datysgeld
* Description: Element module that renders visual flair elements for TLD styling based on type, subtype, and IDN status
* Notes: Designed for Blueprint template system integration; builds CSS classes from TLD properties; uses CanonicalForms for subtype mapping; differentiates IDN ccTLDs and gTLDs; styled via CSS pseudo-elements; protected execution with error handling
]]

local p = {}

p.elementName = "tldflair"
local ErrorHandling = require('Module:ErrorHandling')
local CanonicalForms = require('Module:CanonicalForms')

-- Create a badge block for TLD styling
function p.createBlock()
    return function(template, args)
        local errorContext = template._errorContext
        -- Protected execution wrapper
        local function execute(func, ...)
            if errorContext then
                return ErrorHandling.protect(
                    errorContext,
                    "ElementBlock_tldflair",
                    func,
                    "",
                    ...
                )
            else
                local ok, result = pcall(func, ...)
                return ok and result or ""
            end
        end

        return execute(function()
            -- Get TLD type: prefer preprocessor-normalized _type, fall back to raw tld_type
            local tldType = args._type or args.tld_type or ""
            local subtype = args.tld_subtype or ""
            local isIDN   = (args.idn == "true")
            local punycode = args.ascii or ""

            -- Build CSS class list
            local classes = { "tld-flair" }
            
            -- Add subtype class if present
            if subtype ~= "" then
                local _, css = CanonicalForms.normalize(subtype, template.config.mappings.tld_subtype)
                if css then
                    table.insert(classes, css)
                end
            end
            
            -- Add IDN class if applicable
            if isIDN then
                -- Normalize the type if it hasn't been normalized yet
                local normalizedType = tldType
                if tldType ~= "" and tldType ~= "gTLD" and tldType ~= "ccTLD" then
                    normalizedType = select(1, CanonicalForms.normalize(tldType, template.config.mappings.tld_type)) or tldType
                end
                
                local idnClass = (normalizedType == "ccTLD") and "tld-template-idn-cctld" or "tld-template-idn-gtld"
                table.insert(classes, idnClass)
            -- Otherwise, if no subtype, add base type class for non-IDN TLDs
            elseif subtype == "" and tldType ~= "" then
                -- Normalize the type if it hasn't been normalized yet
                local normalizedType = tldType
                if tldType ~= "gTLD" and tldType ~= "ccTLD" then
                    normalizedType = select(1, CanonicalForms.normalize(tldType, template.config.mappings.tld_type)) or tldType
                end
                
                local typeClass = (normalizedType == "ccTLD") and "tld-template-cctld" or "tld-template-gtld"
                table.insert(classes, typeClass)
            end
            
            local classAttr = table.concat(classes, " ")

            -- Determine display text (styled via CSS pseudo-element)
            local display = ""

            -- Render as a table row
local output = {
    "|-",
    '! colspan="2" class="' .. classAttr .. '" | ' .. display
}
            return table.concat(output, "\n")
        end)
    end
end

return p