Module:SemanticCategoryHelpers: Difference between revisions

// via Wikitext Extension for VSCode
 
// via Wikitext Extension for VSCode
Line 77: Line 77:
-- Category Utilities
-- Category Utilities
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Ensures a category string is properly wrapped in MediaWiki syntax
function p.formatCategoryName(categoryName)
    if not categoryName or categoryName == "" then return "" end
   
    -- Already has full MediaWiki syntax
    if categoryName:match("^%[%[Category:[^%]]+%]%]$") then
        return categoryName
    end
   
    -- Has partial syntax, normalize it
    if categoryName:match("^Category:") then
        return string.format("[[%s]]", categoryName)
    end
   
    -- Plain category name, add full syntax
    return string.format("[[Category:%s]]", categoryName)
end


-- Builds a category string from a table of category names
-- Builds a category string from a table of category names
Line 88: Line 106:
      
      
     for _, cat in ipairs(categories) do
     for _, cat in ipairs(categories) do
         -- Check if the category already has the [[ ]] wrapper
         -- Use the formatCategoryName function to ensure proper syntax
         if not string.match(cat, "^%[%[Category:") then
         formatted[index] = p.formatCategoryName(cat)
            formatted[index] = string.format("[[Category:%s]]", cat)
        else
            formatted[index] = cat
        end
         index = index + 1
         index = index + 1
     end
     end