Jump to content

Module:ElementTLDFlair: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 8: Line 8:


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


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

Revision as of 13:13, 28 April 2025

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')

-- 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 = { "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