Jump to content
Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:CategoryTranscluder: Difference between revisions

From Teriock
Created page with "local p = {} -- Function to retrieve and transclude pages in a given category function p.transcludeCategory(frame) -- Get the category name from the arguments local args = frame:getParent().args local categoryName = args[1] if not categoryName or categoryName == "" then return "Error: No category specified." end -- Initialize a buffer to hold the transcluded content local contentBuffer = {} -- Iterate through pages in the categ..."
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Function to retrieve and transclude pages in a given category
-- Helper function to get all pages in a category
local function getPagesInCategory(categoryName)
    -- Remove "Category:" prefix if present
    categoryName = categoryName:gsub('^[Cc]ategory:', '')
   
    local pages = {}
    local category = mw.site.categories[categoryName]
   
    if not category then
        return pages
    end
   
    -- Iterate through all pages in the category
    for title in category:pages() do
        table.insert(pages, title.prefixedText)
    end
   
    -- Sort pages alphabetically
    table.sort(pages)
   
    return pages
end
 
-- Main function to transclude all pages in a category
function p.transcludeCategory(frame)
function p.transcludeCategory(frame)
    -- Get the category name from the arguments
     local categoryName = frame.args[1]
     local args = frame:getParent().args
   
    local categoryName = args[1]
     if not categoryName then
 
         return '<strong class="error">Error: Category name not provided</strong>'
     if not categoryName or categoryName == "" then
         return "Error: No category specified."
     end
     end
 
      
     -- Initialize a buffer to hold the transcluded content
     local pages = getPagesInCategory(categoryName)
     local contentBuffer = {}
   
 
     if #pages == 0 then
    -- Iterate through pages in the category
         return string.format('<strong class="error">Error: No pages found in category "%s"</strong>', categoryName)
    local category = mw.site.getCategory(categoryName)
     if not category then
         return "Error: Category not found."
     end
     end
 
   
     local count = 0
    -- Build the output with transclusions
     for _, page in ipairs(category:members()) do
     local output = {}
        count = count + 1
     for _, pageName in ipairs(pages) do
 
         -- Skip the category page itself
         -- Only transclude articles, not files or subcategories
         if not pageName:match('^[Cc]ategory:') then
         if page.ns == 0 then
             table.insert(output, string.format('{{:%s}}', pageName))
             table.insert(contentBuffer, string.format("<includeonly>{{:%s}}</includeonly>", page.text))
         end
         end
    end
   
    -- Join all transclusions with newlines
    return table.concat(output, '\n')
end


        -- Optional: Stop after a set limit to prevent excessive processing
-- Function to get a simple list of pages without transclusion
         if count > 100 then
function p.listPages(frame)
            table.insert(contentBuffer, "<!-- Output truncated to the first 100 items. -->")
    local categoryName = frame.args[1]
             break
   
    if not categoryName then
         return '<strong class="error">Error: Category name not provided</strong>'
    end
   
    local pages = getPagesInCategory(categoryName)
   
    if #pages == 0 then
        return string.format('<strong class="error">Error: No pages found in category "%s"</strong>', categoryName)
    end
   
    -- Build the output as a bullet list
    local output = {}
    for _, pageName in ipairs(pages) do
        if not pageName:match('^[Cc]ategory:') then
             table.insert(output, string.format('* [[%s]]', pageName))
         end
         end
     end
     end
 
      
     -- Combine the transcluded content and return it
     return table.concat(output, '\n')
     return table.concat(contentBuffer, "\n")
end
end


return p
return p

Latest revision as of 11:26, 14 January 2025

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

local p = {}

-- Helper function to get all pages in a category
local function getPagesInCategory(categoryName)
    -- Remove "Category:" prefix if present
    categoryName = categoryName:gsub('^[Cc]ategory:', '')
    
    local pages = {}
    local category = mw.site.categories[categoryName]
    
    if not category then
        return pages
    end
    
    -- Iterate through all pages in the category
    for title in category:pages() do
        table.insert(pages, title.prefixedText)
    end
    
    -- Sort pages alphabetically
    table.sort(pages)
    
    return pages
end

-- Main function to transclude all pages in a category
function p.transcludeCategory(frame)
    local categoryName = frame.args[1]
    
    if not categoryName then
        return '<strong class="error">Error: Category name not provided</strong>'
    end
    
    local pages = getPagesInCategory(categoryName)
    
    if #pages == 0 then
        return string.format('<strong class="error">Error: No pages found in category "%s"</strong>', categoryName)
    end
    
    -- Build the output with transclusions
    local output = {}
    for _, pageName in ipairs(pages) do
        -- Skip the category page itself
        if not pageName:match('^[Cc]ategory:') then
            table.insert(output, string.format('{{:%s}}', pageName))
        end
    end
    
    -- Join all transclusions with newlines
    return table.concat(output, '\n')
end

-- Function to get a simple list of pages without transclusion
function p.listPages(frame)
    local categoryName = frame.args[1]
    
    if not categoryName then
        return '<strong class="error">Error: Category name not provided</strong>'
    end
    
    local pages = getPagesInCategory(categoryName)
    
    if #pages == 0 then
        return string.format('<strong class="error">Error: No pages found in category "%s"</strong>', categoryName)
    end
    
    -- Build the output as a bullet list
    local output = {}
    for _, pageName in ipairs(pages) do
        if not pageName:match('^[Cc]ategory:') then
            table.insert(output, string.format('* [[%s]]', pageName))
        end
    end
    
    return table.concat(output, '\n')
end

return p