Module:Location map/multi: Difference between revisions

No edit summary
Tag: Reverted
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)
     mw.log("DEBUG: Entered container function")
     debugLog("DEBUG: Entered container function")
local caption_list = {}
    local caption_list = {}
if not args then
 
args = getArgs(frame, {wrappers = 'Template:Location map+', valueFunc = locmap.valueFunc})
    -- Retrieve args if not provided
end
    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


    mw.logObject(args, "DEBUG: args")
        -- Generate output for each map
    mw.logObject(map, "DEBUG: 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


if not map then
        local finalOutput = '<div class="switcher-container">' .. table.concat(outputs) .. '</div>'
if args[1] then
        debugLog("DEBUG: Final output for multiple maps -> " .. finalOutput)
map = {}
        return finalOutput .. renderDebugLogs()
for mapname in string.gmatch(args[1], '[^#]+') do
    else
map[#map + 1] = locmap.getMapParams(mapname, frame)
        -- Single map handling
end
        debugLog("DEBUG: Processing single map")
if args['caption'] then
        local finalOutput = locmap.top(frame, args, map) ..
if args['caption'] == "" then
            (args.places and args.places:gsub('%s*\n%s*', '') or '') ..
while #caption_list < #map do
            locmap.bottom(frame, args, map)
caption_list[#caption_list + 1] = args['caption']
        debugLog("DEBUG: Final output for single map -> " .. finalOutput)
end
        return finalOutput .. renderDebugLogs()
else
    end
for caption in mw.text.gsplit(args['caption'], '##', true) do
caption_list[#caption_list + 1] = caption
end
end
end
if #map == 1 then map = map[1] end
else
map = locmap.getMapParams('World', frame)
end
end
if type(map) == 'table' then
local placeslist = mw.text.gsplit(args.places, '#PlaceList#')
local permaplaces = {}
local numbermaps = #map
local count = 0
for i = 1,numbermaps do
permaplaces[i] = {}
end
for place in placeslist do
table.insert(permaplaces[count%numbermaps + 1],place)
count = count + 1
end
local altmaps = switcherSeparate(args.AlternativeMap)
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)
if #overlays > #map then
error(string.format('%d overlay_images were provided, but only %d maps were provided', #overlays, #map))
end
if #caption_list > #map then
error(string.format('%d captions were provided, but only %d maps were provided', #caption_list, #map))
end
local outputs = {}
args.autoSwitcherLabel = true
for k,v in ipairs(map) do
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
return '<div class="switcher-container">' .. table.concat(outputs) .. '</div>'
else
return locmap.top(frame, args, map) .. (args.places and args.places:gsub('%s*\n%s*', '') or '') .. locmap.bottom(frame, args, map)
end
end
end


local function manyMakeArgs(fullArgs, n)
local function manyMakeArgs(fullArgs, n)