Jump to content

Module:ElementTLDFlair

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

--[[ 
 * Module:ElementTLDFlair
 * Renders a flair element for TLD styling based on type, subtype, and IDN.
 * Designed for integration with the Blueprint template system.
 ]]

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()
            local tldType   = args.tld_type or ""
            local subtype   = args.tld_subtype or ""
            local isIDN     = args._idnFlag
            local punycode  = args.ascii or ""

            -- Build CSS class list
            local classes = { "tld-flair" }
            if subtype ~= "" then
                local _, css = CanonicalForms.normalize(subtype, template.config.mappings.tld_subtype)
                if css then
                    table.insert(classes, css)
                end
            end
            if isIDN then
                local idnClass = args._type == "ccTLD" and "tld-template-idn-cctld" or "tld-template-idn-gtld"
                table.insert(classes, idnClass)
            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