Перейти к содержанию

Модуль:Навигация

Материал из GravityWiki
Версия от 07:28, 20 октября 2025; Admin (обсуждение | вклад) (Новая страница: «local p = {} function p.navmenu(frame) local args = frame:getParent().args local currentTitle = mw.title.getCurrentTitle() local categories = {} for key, value in pairs(args) do if string.match(key, '^category%d+$') then local num = string.match(key, 'category(%d+)') categories[tonumber(num)] = { name = value, subitems = {} } end end fo...»)
(разн.) ← Предыдущая версия | Текущая версия (разн.) | Следующая версия → (разн.)

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

local p = {}

function p.navmenu(frame)
    local args = frame:getParent().args
    local currentTitle = mw.title.getCurrentTitle()
    
    local categories = {}
    
    for key, value in pairs(args) do
        if string.match(key, '^category%d+$') then
            local num = string.match(key, 'category(%d+)')
            categories[tonumber(num)] = {
                name = value,
                subitems = {}
            }
        end
    end
    
    for key, value in pairs(args) do
        if string.match(key, '^subitems%d+$') then
            local num = string.match(key, 'subitems(%d+)')
            num = tonumber(num)
            if categories[num] then
                -- Разделяем подпункты по точке с запятой
                local subitems = mw.text.split(value, ';')
                for _, item in ipairs(subitems) do
                    table.insert(categories[num].subitems, mw.text.trim(item))
                end
            end
        end
    end
    
    local sortedCategories = {}
    for num in pairs(categories) do
        table.insert(sortedCategories, num)
    end
    table.sort(sortedCategories)
    
    local html = {}
    table.insert(html, '<nav class="wiki-nav-menu">')
    table.insert(html, '<div class="nav-section-title">' .. (args.section or 'Навигация') .. '</div>')
    table.insert(html, '<ul class="nav-main-list">')
    
    for _, num in ipairs(sortedCategories) do
        local category = categories[num]
        
        table.insert(html, '<li class="nav-category">')
        
        if string.find(category.name, '&') then
            local parts = mw.text.split(category.name, '&')
            if #parts >= 3 then
                table.insert(html, parts[1] .. ' [[' .. parts[2] .. '|<span class="nav-category-text">' .. parts[3] .. '</span>]]')
            else
                table.insert(html, '[[' .. category.name .. '|<span class="nav-category-text">' .. category.name .. '</span>]]')
            end
        else
            table.insert(html, '[[' .. category.name .. '|<span class="nav-category-text">' .. category.name .. '</span>]]')
        end
        
        if #category.subitems > 0 then
            table.insert(html, '<ul class="nav-sub-list">')
            
            for _, subitem in ipairs(category.subitems) do
                table.insert(html, '<li class="nav-sub-item">' .. subitem .. '</li>')
            end
            
            table.insert(html, '</ul>')
        end
        
        table.insert(html, '</li>')
    end
    
    table.insert(html, '</ul>')
    table.insert(html, '</nav>')
    
    return table.concat(html, '\n')
end

return p