Module:ElementTLDFlair

Revision as of 12:49, 28 April 2025 by MarkWD (talk | contribs) (// via Wikitext Extension for VSCode)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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"

-- 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 and errorContext.protect then
                return errorContext.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 = { "tldflair" }
            if tldType ~= "" then
                table.insert(classes, "tldflair-type-" .. string.lower(tldType))
            end
            if subtype ~= "" then
                local sub = string.lower(subtype):gsub("%s+", "")
                table.insert(classes, "tldflair-subtype-" .. sub)
            end
            if isIDN then
                table.insert(classes, "tldflair-idn")
            end
            local classAttr = table.concat(classes, " ")

            -- Determine display text
            local display = tldType
            if isIDN and punycode ~= "" then
                display = punycode
            end

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

return p