Module:IslandTable: Difference between revisions

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


-- Fetch all pages in a given category
-- Fetch all pages in a given category using Category_handler
local function getPagesInCategory(catName)
local function getPagesInCategory(category)
     local pages = {}
     local category_handler = require('Module:Category_handler')
     for page in mw.site:category(catName):members() do
     local pages = category_handler.main({
         table.insert(pages, page.title)
         category = category,
    end
        list = 'pages'
     table.sort(pages) -- optional: sort alphabetically
     })
     return pages
     return pages or {}
end
end


-- Main function to generate the table
-- Main function to generate the table
function p.render(frame)
function p.render()
     local islands = getPagesInCategory("Islands")
    -- Fetch islands and cities
     local citiesPerIsland = {}
     local islands = getPagesInCategory('Islands')
 
     local cities = {}
    -- Fetch cities for each island
     for _, island in ipairs(islands) do
     for _, island in ipairs(islands) do
         citiesPerIsland[island] = getPagesInCategory("Cities in " .. island)
         cities[island] = getPagesInCategory('Cities in ' .. island)
     end
     end


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


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


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



Revision as of 14:16, 17 October 2025

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

local p = {}

-- Fetch all pages in a given category using Category_handler
local function getPagesInCategory(category)
    local category_handler = require('Module:Category_handler')
    local pages = category_handler.main({
        category = category,
        list = 'pages'
    })
    return pages or {}
end

-- Main function to generate the table
function p.render()
    -- Fetch islands and cities
    local islands = getPagesInCategory('Islands')
    local cities = {}
    for _, island in ipairs(islands) do
        cities[island] = getPagesInCategory('Cities in ' .. island)
    end

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

    -- Start building the table HTML
    local html = '{| class="wikitable sortable"\n|+\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

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

return p