Module:IslandTable
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