Module:Location map/multi: Difference between revisions
EnWikiAdmin (talk | contribs) No edit summary Tag: Reverted |
EnWikiAdmin (talk | contribs) No edit summary Tag: Reverted |
||
Line 1: | Line 1: | ||
require('strict') | require('strict') | ||
local debugLogs = {} | |||
local function debugLog(msg) | |||
table.insert(debugLogs, tostring(msg)) | |||
end | |||
local function renderDebugLogs() | |||
if #debugLogs > 0 then | |||
return '<pre style="background: #f9f9f9; border: 1px solid #ccc; padding: 10px; margin-top: 10px;">' | |||
.. table.concat(debugLogs, "\n") | |||
.. '</pre>' | |||
end | |||
return '' | |||
end | |||
local p = {} | local p = {} | ||
Line 17: | Line 34: | ||
function p.container(frame, args, map) | function p.container(frame, args, map) | ||
debugLog("DEBUG: Entered container function") | |||
local caption_list = {} | |||
-- Retrieve args if not provided | |||
if not args then | |||
args = getArgs(frame, {wrappers = 'Template:Location map+', valueFunc = locmap.valueFunc}) | |||
end | |||
debugLog("DEBUG: Retrieved args -> " .. mw.text.dumpObject(args)) | |||
debugLog("DEBUG: Initial map value -> " .. mw.text.dumpObject(map or {})) | |||
-- Process map if not provided | |||
if not map then | |||
if args[1] then | |||
map = {} | |||
for mapname in string.gmatch(args[1], '[^#]+') do | |||
debugLog("DEBUG: Processing mapname -> " .. mapname) | |||
table.insert(map, locmap.getMapParams(mapname, frame)) | |||
end | |||
-- Handle caption logic | |||
if args['caption'] then | |||
debugLog("DEBUG: Processing captions -> " .. args['caption']) | |||
if args['caption'] == "" then | |||
while #caption_list < #map do | |||
table.insert(caption_list, args['caption']) | |||
end | |||
else | |||
for caption in mw.text.gsplit(args['caption'], '##', true) do | |||
table.insert(caption_list, caption) | |||
end | |||
end | |||
end | |||
if #map == 1 then | |||
debugLog("DEBUG: Single map detected, simplifying structure") | |||
map = map[1] | |||
end | |||
else | |||
debugLog("DEBUG: No map specified, defaulting to 'World'") | |||
map = locmap.getMapParams('World', frame) | |||
end | |||
end | |||
-- Handle multiple maps | |||
if type(map) == 'table' then | |||
local placeslist = mw.text.gsplit(args.places, '#PlaceList#') | |||
local permaplaces = {} | |||
local numbermaps = #map | |||
local count = 0 | |||
-- Initialize map-specific places | |||
for i = 1, numbermaps do | |||
permaplaces[i] = {} | |||
end | |||
-- Distribute places across maps | |||
for place in placeslist do | |||
table.insert(permaplaces[count % numbermaps + 1], place) | |||
count = count + 1 | |||
end | |||
debugLog("DEBUG: Distributed places across maps -> " .. mw.text.dumpObject(permaplaces)) | |||
-- Handle AlternativeMap and overlays | |||
local altmaps = switcherSeparate(args.AlternativeMap) | |||
debugLog("DEBUG: Alternative maps -> " .. mw.text.dumpObject(altmaps)) | |||
if #altmaps > #map then | |||
error(string.format('%d AlternativeMaps were provided, but only %d maps were provided', #altmaps, #map)) | |||
end | |||
local overlays = switcherSeparate(args.overlay_image) | |||
debugLog("DEBUG: Overlay images -> " .. mw.text.dumpObject(overlays)) | |||
if #overlays > #map then | |||
error(string.format('%d overlay_images were provided, but only %d maps were provided', #overlays, #map)) | |||
end | |||
-- Validate caption count | |||
if #caption_list > #map then | |||
error(string.format('%d captions were provided, but only %d maps were provided', #caption_list, #map)) | |||
end | |||
-- Generate output for each map | |||
local outputs = {} | |||
args.autoSwitcherLabel = true | |||
for k, v in ipairs(map) do | |||
debugLog("DEBUG: Processing map " .. k) | |||
args.AlternativeMap = altmaps[k] | |||
args.overlay_image = overlays[k] | |||
args.caption = caption_list[k] | |||
args.places = table.concat(permaplaces[k]) | |||
outputs[k] = p.container(frame, args, v) | |||
end | |||
local finalOutput = '<div class="switcher-container">' .. table.concat(outputs) .. '</div>' | |||
debugLog("DEBUG: Final output for multiple maps -> " .. finalOutput) | |||
return finalOutput .. renderDebugLogs() | |||
else | |||
-- Single map handling | |||
debugLog("DEBUG: Processing single map") | |||
local finalOutput = locmap.top(frame, args, map) .. | |||
(args.places and args.places:gsub('%s*\n%s*', '') or '') .. | |||
locmap.bottom(frame, args, map) | |||
debugLog("DEBUG: Final output for single map -> " .. finalOutput) | |||
return finalOutput .. renderDebugLogs() | |||
end | |||
end | end | ||
local function manyMakeArgs(fullArgs, n) | local function manyMakeArgs(fullArgs, n) |