Module:IslandTable: Difference between revisions
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( | local function getPagesInCategory(category) | ||
local | local category_handler = require('Module:Category_handler') | ||
local pages = category_handler.main({ | |||
category = category, | |||
list = 'pages' | |||
}) | |||
return pages | return pages or {} | ||
end | end | ||
-- Main function to generate the table | -- Main function to generate the table | ||
function p.render( | function p.render() | ||
local islands = getPagesInCategory( | -- Fetch islands and cities | ||
local | local islands = getPagesInCategory('Islands') | ||
local cities = {} | |||
for _, island in ipairs(islands) do | for _, island in ipairs(islands) do | ||
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 _, | for _, cityList in pairs(cities) do | ||
if # | if #cityList > maxRows then | ||
maxRows = # | 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' | |||
html = html .. '! ' | |||
-- | -- 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 = | 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