Для документации этого модуля может быть создана страница Модуль:ТранспортнаяСхема/doc

local p = {}

-- Безопасная загрузка JSON из пространства Шаблонов
local function loadData(frame)
    local dataParam = frame.args['данные'] or frame.args[1]
    if not dataParam or dataParam == '' then
        error('Не указано имя базы данных или страница JSON (параметр 1 или |данные=)')
    end
    
    local dataPageTitle = dataParam
    if not mw.ustring.find(dataParam, ':') then
        dataPageTitle = 'Шаблон:Интерактивная схема ' .. dataParam .. '/data.json'
    end
    
    -- Способ 1: Стандартная загрузка через mw.loadJsonData (если тип контента страницы = JSON)
    local success, jsonData = pcall(mw.loadJsonData, dataPageTitle)
    if success and jsonData then
        return jsonData
    end
    
    -- Способ 2: Резервная загрузка текста и декодирование (если страница сохранена как wikitext)
    local titleObj = mw.title.new(dataPageTitle)
    if titleObj and titleObj.exists then
        local rawText = titleObj:getContent()
        if rawText and rawText ~= '' then
            -- Очистка от возможных noinclude / комментариев
            rawText = mw.ustring.gsub(rawText, '<noinclude>.-</noinclude>', '')
            rawText = mw.ustring.gsub(rawText, '<!--.--->', '')
            rawText = mw.text.trim(rawText)
            
            local decodeSuccess, decodedData = pcall(mw.text.jsonDecode, rawText)
            if decodeSuccess and decodedData then
                return decodedData
            else
                error('Ошибка в синтаксисе JSON на странице [[' .. dataPageTitle .. ']]! Проверьте запятые и кавычки.')
            end
        end
    end
    
    error('Страница данных [[' .. dataPageTitle .. ']] не найдена. Убедитесь, что она создана.')
end

-- Генерация кода <imagemap>
local function buildImageMap(jsonData, imageFile, width, defaultRadius)
    local radius = defaultRadius or '16'
    local mapText = 'Файл:' .. imageFile .. '|' .. width .. '|center|alt=' .. (jsonData.title or 'Схема скоростного транспорта Муроморска') .. '\n'
    
    for _, line in ipairs(jsonData.lines or {}) do
        if line.box and line.article then
            mapText = mapText .. 'rect ' .. line.box .. ' [[' .. line.article .. '|' .. (line.name or line.article) .. ']]\n'
        end
        
        for _, station in ipairs(line.stations or {}) do
            if station.coords and station.article then
                local stRadius = station.radius or radius
                mapText = mapText .. 'circle ' .. station.coords .. ' ' .. stRadius .. ' [[' .. station.article .. '|' .. (station.name or station.article) .. ']]\n'
            end
        end
    end
    
    mapText = mapText .. 'desc none\n'
    return mapText
end

-- Названия категорий транспорта
local function getModeLabel(mode)
    local modes = {
        metro = '🚇 Линии Муроморского метрополитена',
        train = '🚆 Направления Мурской железной дороги (МЖД)',
        other = '🚍 Прочие линии'
    }
    return modes[mode] or modes.other
end

-- Рендеринг схемы
function p.render(frame)
    local args = frame:getParent().args
    if not args['данные'] and not frame.args['данные'] then
        args = frame.args
    end

    local jsonData = loadData(frame)
    local imageFile = args['файл'] or jsonData.image or 'Схема_Муроморского_метрополитена.png'
    local width = args['ширина'] or jsonData.width or '850px'
    local caption = args['подпись'] or jsonData.caption or 'Интерактивная схема линий Муроморска'
    local defaultRadius = args['радиус'] or jsonData.defaultRadius or '16'
    
    local root = mw.html.create('div')
        :addClass('ts-transport-scheme')
        :css({
            ['clear'] = 'both',
            ['margin'] = '1.2em auto',
            ['width'] = '100%',
            ['text-align'] = 'center'
        })

    -- 1. Карточка карты с imagemap
    local mapCard = root:tag('div')
        :addClass('ts-transport-map-card')
        :css({
            ['display'] = 'inline-block',
            ['max-width'] = '100%',
            ['background'] = 'var(--background-color-base, #ffffff)',
            ['border'] = '1px solid var(--border-color-base, #d0d7de)',
            ['border-radius'] = '10px',
            ['padding'] = '10px',
            ['box-shadow'] = '0 3px 12px rgba(0,0,0,0.06)'
        })

    local mapContainer = mapCard:tag('div')
        :css({ ['overflow-x'] = 'auto', ['max-width'] = '100%' })

    local imageMapContent = buildImageMap(jsonData, imageFile, width, defaultRadius)
    mapContainer:wikitext(frame:preprocess('{{#tag:imagemap|' .. imageMapContent .. '}}'))

    if caption ~= '' then
        mapCard:tag('div')
            :css({
                ['font-size'] = '12px',
                ['color'] = '#57606a',
                ['margin-top'] = '8px',
                ['padding-top'] = '6px',
                ['border-top'] = '1px dashed var(--border-color-base, #e1e4e8)'
            })
            :wikitext('🗺 ' .. caption)
    end

    -- 2. Легенда линий
    if jsonData.lines and #jsonData.lines > 0 then
        local legend = root:tag('div')
            :addClass('ts-transport-lines-legend')
            :css({
                ['margin'] = '12px auto',
                ['max-width'] = '950px',
                ['padding'] = '8px 12px',
                ['background'] = '#f6f8fa',
                ['border'] = '1px solid #d0d7de',
                ['border-radius'] = '8px'
            })

        legend:tag('div')
            :css({
                ['font-size'] = '11px',
                ['font-weight'] = 'bold',
                ['color'] = '#57606a',
                ['text-transform'] = 'uppercase',
                ['margin-bottom'] = '6px',
                ['letter-spacing'] = '0.5px'
            })
            :wikitext(jsonData.legendTitle or 'Линии скоростного транспорта')

        local legendBadges = legend:tag('div')
            :css({
                ['display'] = 'flex',
                ['flex-wrap'] = 'wrap',
                ['justify-content'] = 'center',
                ['gap'] = '5px'
            })

        for _, line in ipairs(jsonData.lines) do
            local badge = legendBadges:tag('span')
                :css({
                    ['display'] = 'inline-flex',
                    ['align-items'] = 'center',
                    ['padding'] = '2px 8px 2px 4px',
                    ['background'] = '#ffffff',
                    ['border'] = '1px solid #d0d7de',
                    ['border-radius'] = '20px',
                    ['font-size'] = '12px'
                })

            badge:tag('span')
                :css({
                    ['display'] = 'inline-flex',
                    ['align-items'] = 'center',
                    ['justify-content'] = 'center',
                    ['min-width'] = '16px',
                    ['height'] = '16px',
                    ['padding'] = '0 4px',
                    ['border-radius'] = '999px',
                    ['background-color'] = line.color or '#888888',
                    ['color'] = line.textColor or '#ffffff',
                    ['font-size'] = '9.5px',
                    ['font-weight'] = 'bold',
                    ['margin-right'] = '6px'
                })
                :wikitext(line.symbol or line.number or '•')

            badge:tag('span')
                :wikitext('[[' .. (line.article or '') .. '|' .. (line.displayName or line.name or line.article or '') .. ']]')
        end
    end

    -- 3. Каталог станций
    if jsonData.lines and #jsonData.lines > 0 then
        local listCard = root:tag('div')
            :addClass('mw-collapsible toccolours')
            :css({
                ['margin'] = '12px auto 0 auto',
                ['max-width'] = '950px',
                ['text-align'] = 'left',
                ['background'] = 'var(--background-color-neutral-subtle, #f8f9fa)',
                ['border'] = '1px solid var(--border-color-base, #d0d7de)',
                ['border-radius'] = '8px',
                ['overflow'] = 'hidden'
            })

        listCard:tag('div')
            :css({
                ['font-weight'] = 'bold',
                ['font-size'] = '13.5px',
                ['background'] = 'linear-gradient(180deg, #f6f8fa 0%, #eaeef2 100%)',
                ['padding'] = '9px 14px',
                ['border-bottom'] = '1px solid var(--border-color-base, #d0d7de)'
            })
            :wikitext(jsonData.listTitle or '📋 Реестр станций метрополитена и МЖД')

        local listContent = listCard:tag('div')
            :addClass('mw-collapsible-content')
            :css({
                ['padding'] = '14px 18px',
                ['column-width'] = '20em',
                ['column-gap'] = '2em',
                ['column-rule'] = '1px solid #e1e4e8',
                ['font-size'] = '13px',
                ['line-height'] = '1.7'
            })

        local currentMode = nil
        local listWikitext = ''

        for _, line in ipairs(jsonData.lines) do
            local mode = line.mode or 'other'
            if mode ~= currentMode then
                currentMode = mode
                listWikitext = listWikitext .. "\n; " .. getModeLabel(mode) .. "\n"
            end

            listWikitext = listWikitext .. "* '''[[" .. (line.article or '') .. '|' .. (line.name or line.article or '') .. "]]'''\n"
            for _, station in ipairs(line.stations or {}) do
                local badge = "<span style='display:inline-flex;align-items:center;justify-content:center;min-width:14px;height:14px;padding:0 3px;border-radius:999px;background:" .. (line.color or '#888') .. ";color:" .. (line.textColor or '#fff') .. ";font-size:9px;font-weight:bold;margin-right:4px;'>" .. (line.symbol or line.number or '•') .. "</span>"
                listWikitext = listWikitext .. '** ' .. badge .. ' [[' .. station.article .. '|' .. (station.name or station.article) .. ']]'
                if station.transfer then
                    listWikitext = listWikitext .. " <small style='color:#57606a;'>(" .. station.transfer .. ")</small>"
                end
                if station.future then
                    listWikitext = listWikitext .. " <small style='color:#8c959f; font-style:italic;'>(строит.)</small>"
                end
                listWikitext = listWikitext .. '\n'
            end
        end

        listContent:wikitext(listWikitext)
    end

    return tostring(root)
end

return p