Module:IslandTable: Difference between revisions

From Good Creations! MC Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
Line 1: Line 1:
local p = {}
local p = {}


-- Fetch all pages in a given category using Category_handler
local function getPagesInCategory(category)
local function getPagesInCategory(category)
     local category_handler = require('Module:Category_handler')
     local category_handler = require('Module:Category_handler')
Line 11: Line 10:
end
end


-- Main function to generate the table
function p.render()
function p.render()
    -- Fetch islands and cities
     local islands = getPagesInCategory('Islands')
     local islands = getPagesInCategory('Islands')
     local cities = {}
     local cities = {}
Line 20: Line 17:
     end
     end


     -- Determine the maximum number of rows needed
     -- Debugging: Output the lists of islands and cities
     local maxRows = 0
     local debug_output = 'Islands:\n'
     for _, cityList in pairs(cities) do
     for _, island in ipairs(islands) do
         if #cityList > maxRows then
         debug_output = debug_output .. island .. '\n'
            maxRows = #cityList
        end
     end
     end
 
     debug_output = debug_output .. '\nCities:\n'
    -- Start building the table HTML
     for island, cityList in pairs(cities) do
    local html = '{| class="wikitable sortable"\n|+\n'
         debug_output = debug_output .. island .. ': ' .. table.concat(cityList, ', ') .. '\n'
     html = html .. '! ' .. table.concat(islands, ' !! ') .. '\n'
 
    -- Add city rows
     for i = 1, maxRows do
         html = html .. '|-\n'
        for _, island in ipairs(islands) do
            local city = cities[island][i] or ''
            html = html .. '| [[' .. city .. ']]\n'
        end
        html = html .. '|-\n'
     end
     end


    html = html .. '|}'
     return debug_output
     return html
end
end


return p
return p

Latest revision as of 14:16, 17 October 2025

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

local p = {}

local function getPagesInCategory(category)
    local category_handler = require('Module:Category_handler')
    local pages = category_handler.main({
        category = category,
        list = 'pages'
    })
    return pages or {}
end

function p.render()
    local islands = getPagesInCategory('Islands')
    local cities = {}
    for _, island in ipairs(islands) do
        cities[island] = getPagesInCategory('Cities in ' .. island)
    end

    -- Debugging: Output the lists of islands and cities
    local debug_output = 'Islands:\n'
    for _, island in ipairs(islands) do
        debug_output = debug_output .. island .. '\n'
    end
    debug_output = debug_output .. '\nCities:\n'
    for island, cityList in pairs(cities) do
        debug_output = debug_output .. island .. ': ' .. table.concat(cityList, ', ') .. '\n'
    end

    return debug_output
end

return p