Модуль:ТранспортнаяСхема: различия между версиями

мНет описания правки
м Пока вырежу картинки
Строка 1: Строка 1:
local p = {}
local p = {}


-- Универсальное слияние аргументов
local function getArgs(frame)
local function getArgs(frame)
     local args = {}
     local args = {}
Строка 16: Строка 15:
end
end


-- Безопасная загрузка JSON
local function loadData(args)
local function loadData(args)
     local dataParam = args['данные'] or args[1]
     local dataParam = args['данные'] or args[1]
Строка 51: Строка 49:
      
      
     error('Страница данных [[' .. dataPageTitle .. ']] не найдена.')
     error('Страница данных [[' .. dataPageTitle .. ']] не найдена.')
end
-- Генерация кода <imagemap> с защитой от пустых параметров
local function buildImageMap(jsonData, imageFile, width, defaultRadius)
    local radius = defaultRadius or '16'
   
    -- Жесткая проверка на наличие имени файла
    if not imageFile or imageFile == '' then
        imageFile = 'Схема_Муроморского_метрополитена.png'
    end
   
    -- Жесткая проверка на наличие ширины
    if not width or width == '' then
        width = '850px'
    end
    -- Первая строка imagemap (используем File: для стабильности парсера)
    local mapText = 'File:' .. imageFile .. '|' .. width .. '|center\n'
   
    if jsonData and jsonData.lines then
        for _, line in ipairs(jsonData.lines) do
            if line.box and line.article then
                mapText = mapText .. 'rect ' .. line.box .. ' [[' .. line.article .. '|' .. (line.name or line.article) .. ']]\n'
            end
           
            if line.stations then
                for _, station in ipairs(line.stations) 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
        end
    end
   
    mapText = mapText .. 'desc none\n'
    return mapText
end
end


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


     -- Контейнер карты
     -- 1. Легенда линий и маршрутов
     local mapCard = root:tag('div')
     if jsonData.lines and #jsonData.lines > 0 then
        :addClass('ts-transport-map-card')
        local legend = root:tag('div')
        :css({
            :addClass('ts-transport-lines-legend')
            ['display'] = isInfobox and 'block' or 'inline-block',
            :css({
            ['width'] = isInfobox and '100%' or 'auto',
                ['margin'] = '12px auto',
            ['max-width'] = '100%',
                ['max-width'] = '950px',
            ['background'] = isInfobox and 'transparent' or 'var(--background-color-base, #ffffff)',
                ['padding'] = '8px 12px',
            ['border'] = isInfobox and 'none' or '1px solid var(--border-color-base, #d0d7de)',
                ['background'] = '#f6f8fa',
            ['border-radius'] = isInfobox and '0' or '10px',
                ['border'] = '1px solid #d0d7de',
            ['padding'] = isInfobox and '0' or '10px',
                ['border-radius'] = '8px'
             ['box-shadow'] = isInfobox and 'none' or '0 3px 12px rgba(0,0,0,0.06)'
             })
        })


    local mapContainer = mapCard:tag('div')
        legend:tag('div')
        :css({
            :css({
            ['overflow-x'] = 'auto',
                ['font-size'] = '11px',
            ['max-width'] = '100%',
                ['font-weight'] = 'bold',
            ['text-align'] = 'center',
                ['color'] = '#57606a',
            ['width'] = isInfobox and '100%' or 'auto'
                ['text-transform'] = 'uppercase',
        })
                ['margin-bottom'] = '6px'
            })
            :wikitext(jsonData.legendTitle or 'Линии скоростного транспорта')


    local imageMapContent = buildImageMap(jsonData, imageFile, width, defaultRadius)
        local legendBadges = legend:tag('div')
    mapContainer:wikitext(frame:preprocess('{{#tag:imagemap|' .. imageMapContent .. '}}'))
 
    if caption ~= '' and not isInfobox then
        mapCard:tag('div')
             :css({
             :css({
                 ['font-size'] = '12px',
                 ['display'] = 'flex',
                 ['color'] = '#57606a',
                 ['flex-wrap'] = 'wrap',
                ['margin-top'] = '8px',
                 ['justify-content'] = 'center',
                 ['padding-top'] = '6px',
                 ['gap'] = '5px'
                 ['border-top'] = '1px dashed var(--border-color-base, #e1e4e8)'
             })
             })
            :wikitext('🗺 ' .. caption)
    end


    -- Легенда и список (выводятся только вне карточки)
        for _, line in ipairs(jsonData.lines) do
    if not isInfobox then
             local badge = legendBadges:tag('span')
        if jsonData.lines and #jsonData.lines > 0 then
             local legend = root:tag('div')
                :addClass('ts-transport-lines-legend')
                 :css({
                 :css({
                     ['margin'] = '12px auto',
                     ['display'] = 'inline-flex',
                     ['max-width'] = '950px',
                     ['align-items'] = 'center',
                     ['padding'] = '8px 12px',
                     ['padding'] = '2px 8px 2px 4px',
                     ['background'] = '#f6f8fa',
                     ['background'] = '#ffffff',
                     ['border'] = '1px solid #d0d7de',
                     ['border'] = '1px solid #d0d7de',
                     ['border-radius'] = '8px'
                     ['border-radius'] = '20px',
                    ['font-size'] = '12px'
                 })
                 })


             legend:tag('div')
             badge:tag('span')
                 :css({
                 :css({
                     ['font-size'] = '11px',
                    ['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',
                     ['font-weight'] = 'bold',
                    ['color'] = '#57606a',
                     ['margin-right'] = '6px'
                    ['text-transform'] = 'uppercase',
                     ['margin-bottom'] = '6px'
                 })
                 })
                 :wikitext(jsonData.legendTitle or 'Линии скоростного транспорта')
                 :wikitext(line.symbol or line.number or '')


             local legendBadges = legend:tag('div')
             badge:tag('span')
                 :css({
                 :wikitext('[[' .. (line.article or '') .. '|' .. (line.displayName or line.name or line.article or '') .. ']]')
                    ['display'] = 'flex',
        end
                    ['flex-wrap'] = 'wrap',
    end
                    ['justify-content'] = 'center',
                    ['gap'] = '5px'
                })


            for _, line in ipairs(jsonData.lines) do
    -- 2. Сворачиваемый каталог станций
                local badge = legendBadges:tag('span')
    if jsonData.lines and #jsonData.lines > 0 then
                    :css({
        local listCard = root:tag('div')
                        ['display'] = 'inline-flex',
            :addClass('mw-collapsible toccolours')
                        ['align-items'] = 'center',
            :css({
                        ['padding'] = '2px 8px 2px 4px',
                ['margin'] = '12px auto 0 auto',
                        ['background'] = '#ffffff',
                ['max-width'] = '950px',
                        ['border'] = '1px solid #d0d7de',
                ['text-align'] = 'left',
                        ['border-radius'] = '20px',
                ['background'] = 'var(--background-color-neutral-subtle, #f8f9fa)',
                        ['font-size'] = '12px'
                ['border'] = '1px solid #d0d7de',
                    })
                ['border-radius'] = '8px',
                ['overflow'] = 'hidden'
            })


                badge:tag('span')
        listCard:tag('div')
                    :css({
            :css({
                        ['display'] = 'inline-flex',
                ['font-weight'] = 'bold',
                        ['align-items'] = 'center',
                ['font-size'] = '13.5px',
                        ['justify-content'] = 'center',
                ['background'] = 'linear-gradient(180deg, #f6f8fa 0%, #eaeef2 100%)',
                        ['min-width'] = '16px',
                ['padding'] = '9px 14px',
                        ['height'] = '16px',
                ['border-bottom'] = '1px solid #d0d7de'
                        ['padding'] = '0 4px',
            })
                        ['border-radius'] = '999px',
            :wikitext(jsonData.listTitle or '📋 Реестр станций')
                        ['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')
        local listContent = listCard:tag('div')
                    :wikitext('[[' .. (line.article or '') .. '|' .. (line.displayName or line.name or line.article or '') .. ']]')
            :addClass('mw-collapsible-content')
             end
            :css({
        end
                ['padding'] = '14px 18px',
                ['column-width'] = '20em',
                ['column-gap'] = '2em',
                ['column-rule'] = '1px solid #e1e4e8',
                ['font-size'] = '13px',
                ['line-height'] = '1.7'
             })


         if jsonData.lines and #jsonData.lines > 0 then
         local currentMode = nil
            local listCard = root:tag('div')
        local listWikitext = ''
                :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 #d0d7de',
                    ['border-radius'] = '8px',
                    ['overflow'] = 'hidden'
                })


            listCard:tag('div')
        for _, line in ipairs(jsonData.lines) do
                :css({
            local mode = line.mode or 'other'
                    ['font-weight'] = 'bold',
            if mode ~= currentMode then
                    ['font-size'] = '13.5px',
                 currentMode = mode
                    ['background'] = 'linear-gradient(180deg, #f6f8fa 0%, #eaeef2 100%)',
                 listWikitext = listWikitext .. "\n; " .. getModeLabel(mode) .. "\n"
                    ['padding'] = '9px 14px',
            end
                    ['border-bottom'] = '1px solid #d0d7de'
                 })
                 :wikitext(jsonData.listTitle or '📋 Реестр станций')


             local listContent = listCard:tag('div')
             listWikitext = listWikitext .. "* '''[[" .. (line.article or '') .. '|' .. (line.name or line.article or '') .. "]]'''\n"
                :addClass('mw-collapsible-content')
            for _, station in ipairs(line.stations or {}) do
                :css({
                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>"
                    ['padding'] = '14px 18px',
                 listWikitext = listWikitext .. '** ' .. badge .. ' [[' .. station.article .. '|' .. (station.name or station.article) .. ']]'
                    ['column-width'] = '20em',
                 if station.transfer then
                    ['column-gap'] = '2em',
                     listWikitext = listWikitext .. " <small style='color:#57606a;'>(" .. station.transfer .. ")</small>"
                    ['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
                 end
 
                 if station.future then
                 listWikitext = listWikitext .. "* '''[[" .. (line.article or '') .. '|' .. (line.name or line.article or '') .. "]]'''\n"
                     listWikitext = listWikitext .. " <small style='color:#8c959f; font-style:italic;'>(строит.)</small>"
                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
                listWikitext = listWikitext .. '\n'
             end
             end
        end


            listContent:wikitext(listWikitext)
        listContent:wikitext(listWikitext)
        end
     end
     end