Module:RegionalLinks
Jump to navigation
Jump to search
[Create] Documentation
local p = {}
function orderedKeys(tab)
local keyset={}
local n=1
for k,v in pairs(tab) do
keyset[n]=k
n = n+1
end
table.sort(keyset)
return ipairs(keyset)
end
-- debug with e.g. =p.main(nil, {de='Germany', 'de openstreetmap.de'})
function p.main (frame, debugArgs)
local args = debugArgs or frame:getParent().args
local ul = mw.html.create('ul')
local countryNames = {}
for key, val in pairs(args) do
if not tonumber(key) then
countryNames[key] = val
end
end
local countries = {}
for key, val in pairs(args) do
if tonumber(key) then
local parts = mw.text.split(mw.text.trim(val), ' ')
local codes = parts[1]
table.remove(parts, 1)
local link = table.concat(parts, ' ')
for _, code in pairs(mw.text.split(codes, ',')) do
local codeParts = mw.text.split(code, '/')
local code = codeParts[1]
local subcode = codeParts[2]
local name = countryNames[code] or code
if not countries[name] then
countries[name] = {links = {}, subregions = {}, undefined=countryNames[code] == nil}
end
if subcode then
if not countries[name].subregions[subcode] then
countries[name].subregions[subcode] = {}
end
table.insert(countries[name].subregions[subcode], link)
else
table.insert(countries[name].links, link)
end
end
end
end
for _, name in orderedKeys(countries) do
local country = countries[name]
local li = ul:tag('li')
if country.undefined then
li:wikitext(name .. ' (please define name in template)')
else
li:wikitext('[[' .. name .. ']]')
end
li:wikitext(': ' .. table.concat(country.links, ', '))
if next(country.subregions) then
local sublist = li:tag('ul')
for subregion, links in pairs(country.subregions) do
sublist:tag('li'):wikitext('[[' .. subregion .. ']]: ' .. table.concat(links, ', '))
end
end
end
return tostring(ul)
end
return p