Немає опису редагування
Немає опису редагування
Мітка: Ручний відкіт
 
(Не показано 47 проміжних версій цього користувача)
Рядок 1: Рядок 1:
local p = {}
local p = {}


--------------------------------------------------------------------
-- ================================================
-- UNIVERSAL TABLE PARSER
-- КЕШУВАННЯ
--------------------------------------------------------------------
-- ================================================
local function fetch_data_from_table(page_title, player_name, column_index)
    local title = mw.title.new(page_title)
    if not title then return nil end


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()
     local content = title:getContent()
     if not content then return nil end
     page_cache[page_name] = content
    return content
end


    -- Беремо будь-яку wikitable
-- ================================================
    local table_content = mw.ustring.match(content, "{|.-|}")
-- АВТОМАТИЧНЕ МЕНЮ СЕКЦІЙ
    if not table_content then return nil end
-- ================================================


     -- Escape special chars
function p.section_menu(frame)
     local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
    local page_name = frame.args.page or mw.title.getCurrentTitle().text
 
    local content = get_page_content(page_name)
    -- Патерн для пошуку імені
   
    local name_pattern =
    if not content then
         "%[%[" .. escaped .. "%]%]" ..
        return ""
        "|%[%[" .. escaped .. "|.-%]%]" ..
    end
        "|" .. escaped
      
 
     local sections = {}
    -- Знаходимо рядок
   
    local row = nil
    for line in mw.ustring.gmatch(content, "[^\n]+") do
    for line in mw.ustring.gmatch(table_content, "[^\n]+") do
        -- ТІЛЬКИ заголовки рівня 2: == Назва ==
        if mw.ustring.match(line, name_pattern) then
         if mw.ustring.match(line, "^==[^=]") and mw.ustring.match(line, "[^=]==$") then
            row = line
            local section_name = mw.ustring.match(line, "^==%s*(.-)%s*==$")
             break
           
            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
     end
     end
 
   
     if not row then return nil end
     if #sections == 0 then
 
        return ""
    -- Чистимо від |-
    row = mw.ustring.gsub(row, "^%s*|%-", "")
 
    -- Розбиваємо комірки
    local cells = {}
    for cell in mw.ustring.gmatch(row, "%s*|%s*([^|]+)") do
        table.insert(cells, mw.text.trim(cell))
     end
     end
 
   
     local result = cells[column_index]
    -- Будуємо HTML
     if not result then return nil end
     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;">')
     result = mw.ustring.gsub(result, "%[%[([^%]|]+)|?([^%]]*)%]%]", "%2")
      
    if result == "" then
    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>')
         result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1")
   
    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
 
      
     return mw.text.trim(result)
     table.insert(html_parts, '</div>')
end
      
 
     return table.concat(html_parts, '\n')
--------------------------------------------------------------------
-- PUBLIC FUNCTIONS
--------------------------------------------------------------------
 
-- 1) Дата приєднання
function p.join_date(frame)
     local name = frame.args[1]
    return fetch_data_from_table("Гравці", name, 3) or "Невідомо"
end
 
-- 2) Провідник
function p.leader(frame)
     local name = frame.args[1]
     return fetch_data_from_table("Гравці", name, 4) or "Відсутній"
end
 
-- 3) Фундація
function p.fund(frame)
    local name = frame.args[1]
    return fetch_data_from_table("Фундація", name, 2) or "0"
end
 
-- 4) Призовий фонд
function p.prize(frame)
    local name = frame.args[1]
    return fetch_data_from_table("Призовий_фонд", name, 2) or "0"
end
 
-- 5) Фіналіст
function p.finalist(frame)
    local name = frame.args[1]
    return fetch_data_from_table("Фіналіст", name, 2) or "Невідомо"
end
 
-- 6) Титул
function p.title(frame)
    local name = frame.args[1]
    return fetch_data_from_table("Гравці", name, 5) or "Відсутній"
end
end


--------------------------------------------------------------------
-- EXPORT MODULE
--------------------------------------------------------------------
return p
return p

Поточна версія на 10:24, 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 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