Module:TemplateHelpers: Difference between revisions

// via Wikitext Extension for VSCode
Tag: Manual revert
// via Wikitext Extension for VSCode
Line 273: Line 273:


-- Formats website URLs as an HTML unordered list of links, ensuring consistent emoji display
-- Formats website URLs as an HTML unordered list of links, ensuring consistent emoji display
-- Uses splitMultiValueString for more flexible delimiter handling
-- Uses the centralized ListGeneration module.
function p.normalizeWebsites(value)
function p.normalizeWebsites(value)
     if not value or value == "" then return "" end
     if not value or value == "" then return "" end
   
 
    -- Get websites as a table, handling both single and multiple cases
     local ListGeneration = require('Module:ListGeneration')
     local websites
 
   
     -- Define the item hook for website-specific formatting
    -- Quick check for single website (no delimiters)
     local function websiteItemHook(site)
    if not value:match(";") and not value:match("%s+and%s+") then
        -- Ensure the site has a protocol prefix for proper linking
        -- Single website case - create a single-item table
        local linkUrl = site
        websites = {value}
        if not linkUrl:match("^%a+://") then
     else
            linkUrl = "https://" .. linkUrl
        -- Multiple websites case
        websites = p.splitMultiValueString(value)
    end
   
    -- Handle all websites consistently using the list format
     if #websites > 0 then
        -- Pre-allocate listItems table based on number of websites
        local listItems = {}
        local index = 1
       
        for _, site in ipairs(websites) do
            -- Ensure the site has a protocol prefix for proper linking
            local linkUrl = site
            if not linkUrl:match("^%a+://") then
                linkUrl = "https://" .. linkUrl
            end
           
            local formattedLink = string.format("[%s %s]", linkUrl, linkParser.strip(site))
            listItems[index] = string.format("<li>%s</li>", formattedLink)
            index = index + 1
         end
         end
         return string.format("<ul class=\"template-list template-list-website\">%s</ul>", table.concat(listItems, ""))
        -- The hook returns the content of the li tag, which is the formatted link
         return string.format("[%s %s]", linkUrl, linkParser.strip(site))
     end
     end
      
 
     return ""
     -- Set the options for the list generation
    local options = {
        mode = 'bullet',
        listClass = 'template-list-website',
        itemHook = websiteItemHook
    }
 
     return ListGeneration.createList(value, options)
end
end