Немає опису редагування
Мітка: Скасовано
Немає опису редагування
Мітка: Ручний відкіт
Рядок 1: Рядок 1:
local p = {}
local p = {}


function p.section_menu(frame)
-- ================================================
     local page_name = frame.args.page or mw.title.getCurrentTitle().text
-- КЕШУВАННЯ
     local games = frame.args.games
-- ================================================
 
local page_cache = {}
 
local function get_page_content(page_name)
     if page_cache[page_name] then
        return page_cache[page_name]
     end
      
      
     local title = mw.title.new(page_name)
     local title = mw.title.new(page_name)
     if not title or not title.exists then
     if not title or not title.exists then
         return ""
        page_cache[page_name] = nil
         return nil
     end
     end
      
      
     local content = title:getContent()
     local content = title:getContent()
    page_cache[page_name] = content
    return content
end
-- ================================================
-- АВТОМАТИЧНЕ МЕНЮ СЕКЦІЙ
-- ================================================
function p.section_menu(frame)
    local page_name = frame.args.page or mw.title.getCurrentTitle().text
    local content = get_page_content(page_name)
   
     if not content then
     if not content then
         return ""
         return ""
Рядок 16: Рядок 36:
      
      
     local sections = {}
     local sections = {}
    local num = 0
      
      
     for section_name in mw.ustring.gmatch(content, "\n==%s*([^=]+)%s*==") do
     for line in mw.ustring.gmatch(content, "[^\n]+") do
         section_name = mw.text.trim(section_name)
         -- ТІЛЬКИ заголовки рівня 2: == Назва ==
       
        if mw.ustring.match(line, "^==[^=]") and mw.ustring.match(line, "[^=]==$") then
        local clean_name = mw.ustring.gsub(section_name, "%{%{.-%}%}", "")
            local section_name = mw.ustring.match(line, "^==%s*(.-)%s*==$")
        clean_name = mw.ustring.gsub(clean_name, "%s*%(%s*%)%s*", "")
        clean_name = mw.text.trim(clean_name)
       
        if clean_name ~= "" then
            num = num + 1
            local display = clean_name
            local anchor = clean_name
              
              
             if mw.ustring.match(clean_name, "Записи ігор") and games then
             if section_name and section_name ~= "" then
                 display = clean_name .. " (" .. games .. ")"
                -- Виконуємо шаблони щоб отримати реальний текст
                 anchor = clean_name .. "_(" .. games .. ")"
                local processed = frame:preprocess(section_name)
                processed = mw.text.trim(processed)
                  
                -- Anchor
                local anchor = mw.ustring.gsub(processed, " ", "_")
                  
                if processed ~= "" then
                    table.insert(sections, {
                        display = processed,
                        anchor = anchor
                    })
                end
             end
             end
           
            table.insert(sections, {
                num = num,
                display = display,
                anchor = mw.ustring.gsub(anchor, " ", "_")
            })
         end
         end
     end
     end
Рядок 47: Рядок 64:
     end
     end
      
      
     local html = '<nav class="toc-sidebar"><div class="toc-title">Зміст</div><ul class="toc-list">'
     -- Будуємо HTML
    local html_parts = {}
      
      
     for _, section in ipairs(sections) do
     table.insert(html_parts, '<div class="l-box" style="position:fixed; top:20px; left:20px; z-index:100; border-radius:16px; background-color:#23232c; width:220px; box-sizing:border-box; overflow:hidden;">')
        html = html .. '<li class="toc-item"><a href="#' .. section.anchor .. '"><span class="toc-number">' .. section.num .. '</span><span class="toc-text">' .. section.display .. '</span></a></li>'
   
    table.insert(html_parts, '<div style="padding:15px 20px; font-weight:bold; font-size:15px; color:#fff; border-bottom:1px solid rgba(255,255,255,0.1);">Зміст</div>')
   
    for i, section in ipairs(sections) do
        local border = ""
        if i < #sections then
            border = "border-bottom:1px solid rgba(255,255,255,0.05);"
        end
       
        table.insert(html_parts, string.format(
            '<div class="l-box-item" style="padding:12px 20px; %s">[[#%s|%s]]</div>',
            border, section.anchor, section.display
        ))
     end
     end
      
      
     html = html .. '</ul></nav>'
     table.insert(html_parts, '</div>')
      
      
     return html
     return table.concat(html_parts, '\n')
end
end


return p
return p

Версія за 19:36, 8 грудня 2025

Документацію для цього модуля можна створити у Модуль:FetchData7/документація

local p = {}

-- ================================================
-- КЕШУВАННЯ
-- ================================================

local page_cache = {}

local function get_page_content(page_name)
    if page_cache[page_name] then
        return page_cache[page_name]
    end
    
    local title = mw.title.new(page_name)
    if not title or not title.exists then
        page_cache[page_name] = nil
        return nil
    end
    
    local content = title:getContent()
    page_cache[page_name] = content
    return content
end

-- ================================================
-- АВТОМАТИЧНЕ МЕНЮ СЕКЦІЙ
-- ================================================

function p.section_menu(frame)
    local page_name = frame.args.page or mw.title.getCurrentTitle().text
    local content = get_page_content(page_name)
    
    if not content then
        return ""
    end
    
    local sections = {}
    
    for line in mw.ustring.gmatch(content, "[^\n]+") do
        -- ТІЛЬКИ заголовки рівня 2: == Назва ==
        if mw.ustring.match(line, "^==[^=]") and mw.ustring.match(line, "[^=]==$") then
            local section_name = mw.ustring.match(line, "^==%s*(.-)%s*==$")
            
            if section_name and section_name ~= "" then
                -- Виконуємо шаблони щоб отримати реальний текст
                local processed = frame:preprocess(section_name)
                processed = mw.text.trim(processed)
                
                -- Anchor
                local anchor = mw.ustring.gsub(processed, " ", "_")
                
                if processed ~= "" then
                    table.insert(sections, {
                        display = processed,
                        anchor = anchor
                    })
                end
            end
        end
    end
    
    if #sections == 0 then
        return ""
    end
    
    -- Будуємо HTML
    local html_parts = {}
    
    table.insert(html_parts, '<div class="l-box" style="position:fixed; top:20px; left:20px; z-index:100; border-radius:16px; background-color:#23232c; width:220px; box-sizing:border-box; overflow:hidden;">')
    
    table.insert(html_parts, '<div style="padding:15px 20px; font-weight:bold; font-size:15px; color:#fff; border-bottom:1px solid rgba(255,255,255,0.1);">Зміст</div>')
    
    for i, section in ipairs(sections) do
        local border = ""
        if i < #sections then
            border = "border-bottom:1px solid rgba(255,255,255,0.05);"
        end
        
        table.insert(html_parts, string.format(
            '<div class="l-box-item" style="padding:12px 20px; %s">[[#%s|%s]]</div>',
            border, section.anchor, section.display
        ))
    end
    
    table.insert(html_parts, '</div>')
    
    return table.concat(html_parts, '\n')
end

return p