Немає опису редагування
Немає опису редагування
Рядок 1: Рядок 1:
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
-- ================================================
-- ================================================
-- АВТОМАТИЧНЕ МЕНЮ СЕКЦІЙ
-- АВТОМАТИЧНЕ МЕНЮ СЕКЦІЙ
Рядок 64: Рядок 88:
     return tostring(html)
     return tostring(html)
end
end
return p

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

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

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 section_name in mw.ustring.gmatch(content, "\n==%s*([^=]+)%s*==") do
        section_name = mw.text.trim(section_name)
        if section_name ~= "" then
            table.insert(sections, section_name)
        end
    end
    
    if #sections == 0 then
        return ""
    end
    
    local html = mw.html.create('div')
        :addClass('l-box')
        :css('position', 'fixed')
        :css('top', '20px')
        :css('left', '20px')
        :css('z-index', '100')
        :css('border-radius', '16px')
        :css('background-color', '#23232c')
        :css('width', '220px')
        :css('box-sizing', 'border-box')
        :css('overflow', 'hidden')
    
    -- Заголовок меню
    html:tag('div')
        :css('padding', '15px 20px')
        :css('font-weight', 'bold')
        :css('font-size', '15px')
        :css('color', '#fff')
        :css('border-bottom', '1px solid rgba(255,255,255,0.1)')
        :wikitext('Зміст')
    
    -- Пункти меню
    for i, section in ipairs(sections) do
        local anchor = mw.ustring.gsub(section, " ", "_")
        local link = html:tag('a')
            :attr('href', '#' .. anchor)
            :css('display', 'block')
            :css('padding', '12px 20px')
            :css('color', '#ccc')
            :css('text-decoration', 'none')
            :wikitext(section)
        
        if i < #sections then
            link:css('border-bottom', '1px solid rgba(255,255,255,0.05)')
        end
    end
    
    return tostring(html)
end

return p