(Створена сторінка: 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...)
 
Немає опису редагування
Рядок 27: Рядок 27:
-- ================================================
-- ================================================


-- Знаходимо кінець секції (початок наступної == або кінець файлу)
local function get_section_content(content, section_start)
local function get_section_content(content, section_start)
     local next_section = mw.ustring.find(content, "\n==[^=]", section_start + 1)
     local next_section = mw.ustring.find(content, "\n==[^=]", section_start + 1)
Рядок 37: Рядок 36:
end
end


-- Рахуємо рядки в таблиці
local function count_table_rows(section_content)
local function count_table_rows(section_content)
     local table_start = mw.ustring.find(section_content, "{|")
     local table_start = mw.ustring.find(section_content, "{|")
Рядок 52: Рядок 50:
     end
     end
      
      
    -- Перший |- це заголовок
     return math.max(0, count - 1)
     return math.max(0, count - 1)
end
end


-- Рахуємо bullet points (*)
local function count_bullet_points(section_content)
local function count_bullet_points(section_content)
     local count = 0
     local count = 0
     for _ in mw.ustring.gmatch(section_content, "\n%*[^%*]") do
     for _ in mw.ustring.gmatch(section_content, "\n%*") do
         count = count + 1
         count = count + 1
    end
    -- Перевіряємо чи секція починається з *
    if mw.ustring.match(section_content, "^%*[^%*]") or mw.ustring.match(section_content, "\n%*[^%*]") then
        -- вже пораховано
     end
     end
     return count
     return count
Рядок 84: Рядок 76:
      
      
     for line in mw.ustring.gmatch(content, "[^\n]+") do
     for line in mw.ustring.gmatch(content, "[^\n]+") do
        -- ТІЛЬКИ заголовки рівня 2: == Назва ==
         if mw.ustring.match(line, "^==[^=]") and mw.ustring.match(line, "[^=]==$") then
         if mw.ustring.match(line, "^==[^=]") and mw.ustring.match(line, "[^=]==$") then
             local section_name = mw.ustring.match(line, "^==%s*(.-)%s*==$")
             local section_name = mw.ustring.match(line, "^==%s*(.-)%s*==$")
              
              
             if section_name and section_name ~= "" then
             if section_name and section_name ~= "" then
                -- Виконуємо шаблони
                 local display_name = section_name
                 local processed = frame:preprocess(section_name)
                 local anchor = section_name
                 processed = mw.text.trim(processed)
               
                -- Знаходимо позицію секції
                 local section_pos = mw.ustring.find(content, line, 1, true)
                 local section_pos = mw.ustring.find(content, line, 1, true)
                local count = nil
                  
                  
                 if section_pos then
                -- Визначаємо тип секції
                     local section_content = get_section_content(content, section_pos)
                 if mw.ustring.match(section_name, "Нагороди") then
                    -- Прибираємо шаблони з назви
                    display_name = mw.ustring.gsub(section_name, "%{%{.-%}%}", "")
                    display_name = mw.text.trim(display_name)
                    anchor = display_name
                   
                    if section_pos then
                        local section_content = get_section_content(content, section_pos)
                        count = count_table_rows(section_content)
                    end
                   
                elseif mw.ustring.match(section_name, "Сезони") then
                     display_name = "Сезони"
                    anchor = "Сезони"
                   
                    if section_pos then
                        local section_content = get_section_content(content, section_pos)
                        count = count_table_rows(section_content)
                    end
                      
                      
                    -- Нагороди - рахуємо рядки таблиці
                elseif mw.ustring.match(section_name, "Записи ігор") then
                    if mw.ustring.match(section_name, "Нагороди") then
                    -- Тут є шаблон - виконуємо тільки для цієї секції
                        local count = count_table_rows(section_content)
                    local processed = frame:preprocess(section_name)
                        if count > 0 then
                    processed = mw.text.trim(processed)
                            processed = processed .. " (" .. count .. ")"
                    display_name = processed
                        end
                    anchor = processed
                      
                      
                    -- Сезони - рахуємо рядки таблиці
                elseif mw.ustring.match(section_name, "Цікаві факти") then
                    elseif mw.ustring.match(section_name, "Сезони") then
                    display_name = "Цікаві факти"
                        local count = count_table_rows(section_content)
                    anchor = "Цікаві факти"
                        if count > 0 then
                            processed = processed .. " (" .. count .. ")"
                        end
                      
                      
                     -- Цікаві факти - рахуємо bullet points
                     if section_pos then
                    elseif mw.ustring.match(section_name, "Цікаві факти") then
                         local section_content = get_section_content(content, section_pos)
                         local count = count_bullet_points(section_content)
                         count = count_bullet_points(section_content)
                         if count > 0 then
                            processed = processed .. " (" .. count .. ")"
                        end
                     end
                     end
                   
                else
                    -- Інші секції - просто прибираємо шаблони
                    display_name = mw.ustring.gsub(section_name, "%{%{.-%}%}", "")
                    display_name = mw.text.trim(display_name)
                    anchor = display_name
                end
               
                -- Додаємо кількість якщо є
                if count and count > 0 then
                    display_name = display_name .. " (" .. count .. ")"
                 end
                 end
                  
                  
                 -- Anchor (без доданих чисел для стандартних секцій)
                 -- Формуємо anchor
                local anchor = frame:preprocess(section_name)
                anchor = mw.text.trim(anchor)
                 anchor = mw.ustring.gsub(anchor, " ", "_")
                 anchor = mw.ustring.gsub(anchor, " ", "_")
                  
                  
                 if processed ~= "" then
                 if display_name ~= "" then
                     table.insert(sections, {
                     table.insert(sections, {
                         display = processed,
                         display = display_name,
                         anchor = anchor
                         anchor = anchor
                     })
                     })

Версія за 19:16, 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

-- ================================================
-- ПІДРАХУНОК ЕЛЕМЕНТІВ
-- ================================================

local function get_section_content(content, section_start)
    local next_section = mw.ustring.find(content, "\n==[^=]", section_start + 1)
    if next_section then
        return mw.ustring.sub(content, section_start, next_section - 1)
    else
        return mw.ustring.sub(content, section_start)
    end
end

local function count_table_rows(section_content)
    local table_start = mw.ustring.find(section_content, "{|")
    if not table_start then return 0 end
    
    local table_end = mw.ustring.find(section_content, "|}", table_start)
    if not table_end then return 0 end
    
    local table_content = mw.ustring.sub(section_content, table_start, table_end)
    
    local count = 0
    for _ in mw.ustring.gmatch(table_content, "|-") do
        count = count + 1
    end
    
    return math.max(0, count - 1)
end

local function count_bullet_points(section_content)
    local count = 0
    for _ in mw.ustring.gmatch(section_content, "\n%*") do
        count = count + 1
    end
    return count
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
        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 display_name = section_name
                local anchor = section_name
                local section_pos = mw.ustring.find(content, line, 1, true)
                local count = nil
                
                -- Визначаємо тип секції
                if mw.ustring.match(section_name, "Нагороди") then
                    -- Прибираємо шаблони з назви
                    display_name = mw.ustring.gsub(section_name, "%{%{.-%}%}", "")
                    display_name = mw.text.trim(display_name)
                    anchor = display_name
                    
                    if section_pos then
                        local section_content = get_section_content(content, section_pos)
                        count = count_table_rows(section_content)
                    end
                    
                elseif mw.ustring.match(section_name, "Сезони") then
                    display_name = "Сезони"
                    anchor = "Сезони"
                    
                    if section_pos then
                        local section_content = get_section_content(content, section_pos)
                        count = count_table_rows(section_content)
                    end
                    
                elseif mw.ustring.match(section_name, "Записи ігор") then
                    -- Тут є шаблон - виконуємо тільки для цієї секції
                    local processed = frame:preprocess(section_name)
                    processed = mw.text.trim(processed)
                    display_name = processed
                    anchor = processed
                    
                elseif mw.ustring.match(section_name, "Цікаві факти") then
                    display_name = "Цікаві факти"
                    anchor = "Цікаві факти"
                    
                    if section_pos then
                        local section_content = get_section_content(content, section_pos)
                        count = count_bullet_points(section_content)
                    end
                    
                else
                    -- Інші секції - просто прибираємо шаблони
                    display_name = mw.ustring.gsub(section_name, "%{%{.-%}%}", "")
                    display_name = mw.text.trim(display_name)
                    anchor = display_name
                end
                
                -- Додаємо кількість якщо є
                if count and count > 0 then
                    display_name = display_name .. " (" .. count .. ")"
                end
                
                -- Формуємо anchor
                anchor = mw.ustring.gsub(anchor, " ", "_")
                
                if display_name ~= "" then
                    table.insert(sections, {
                        display = display_name,
                        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