Module:IslandTable

Revision as of 14:13, 17 October 2025 by Minionguyjpro (talk | contribs) (Create module page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

-- Fetch all pages in a given category
local function getPagesInCategory(catName)
    local pages = {}
    for page in mw.site:category(catName):members() do
        table.insert(pages, page.title)
    end
    table.sort(pages) -- optional: sort alphabetically
    return pages
end

-- Main function to generate the table
function p.render(frame)
    local islands = getPagesInCategory("Islands")
    local citiesPerIsland = {}

    -- Fetch cities for each island
    for _, island in ipairs(islands) do
        citiesPerIsland[island] = getPagesInCategory("Cities in " .. island)
    end

    -- Determine the maximum number of rows needed
    local maxRows = 0
    for _, cities in pairs(citiesPerIsland) do
        if #cities > maxRows then
            maxRows = #cities
        end
    end

    -- Start building table HTML
    local html = '{| class="wikitable sortable"\n|+\n'
    
    -- Header row
    html = html .. '! '
    html = html .. table.concat(islands, ' !! ') .. '\n'

    -- City rows
    for i = 1, maxRows do
        html = html .. '|-\n'
        for _, island in ipairs(islands) do
            local city = citiesPerIsland[island][i] or ''
            html = html .. '| [[' .. city .. ']]'
        end
        html = html .. '\n'
    end

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

return p