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


function p.section_menu(frame)
local page_cache = {}
     local page_name = frame.args.page or mw.title.getCurrentTitle().text
 
     local games = frame.args.games
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
local function get_section_content(content, section_start)
    local section_end = mw.ustring.find(content, "\n==[^=]", section_start + 1)
   
    if section_end then
        return mw.ustring.sub(content, section_start, section_end - 1)
    else
        return mw.ustring.sub(content, section_start)
    end
end
local function count_facts(section_content)
    local count = 0
    for line in mw.ustring.gmatch(section_content, "[^\n]+") do
        if mw.ustring.match(line, "^%*[^%*]") then
            count = count + 1
        end
    end
    return count
end
local function count_awards(section_content)
    local count = 0
    for _ in mw.ustring.gmatch(section_content, "%[%[File:[^%]]+%.png") 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
     if not content then
         return ""
         return ""
Рядок 16: Рядок 56:
      
      
     local sections = {}
     local sections = {}
    local num = 0
      
      
     for section_name in mw.ustring.gmatch(content, "\n==%s*([^=]+)%s*==") do
    table.insert(sections, {
         section_name = mw.text.trim(section_name)
        display = "Біографія",
       
        anchor = "top"
        local clean_name = mw.ustring.gsub(section_name, "%{%{.-%}%}", "")
    })
        clean_name = mw.ustring.gsub(clean_name, "%s*%(%s*%)%s*", "")
   
        clean_name = mw.text.trim(clean_name)
     for line in mw.ustring.gmatch(content, "[^\n]+") do
       
         if mw.ustring.match(line, "^==[^=]") and mw.ustring.match(line, "[^=]==$") then
        if clean_name ~= "" then
            local section_name = mw.ustring.match(line, "^==%s*(.-)%s*==$")
            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 .. ")"
                local processed = frame:preprocess(section_name)
                anchor = clean_name .. "_(" .. games .. ")"
                processed = mw.text.trim(processed)
                local anchor = mw.ustring.gsub(processed, " ", "_")
                local display = processed
               
                local section_pos = mw.ustring.find(content, line, 1, true)
               
                if section_pos then
                    local section_content = get_section_content(content, section_pos)
                   
                    if mw.ustring.match(processed, "Цікаві факти") then
                        local facts_count = count_facts(section_content)
                        if facts_count > 0 then
                            display = processed .. " (" .. facts_count .. ")"
                        end
                    elseif mw.ustring.match(processed, "Нагороди") then
                        local awards_count = count_awards(section_content)
                        if awards_count > 0 then
                            display = processed .. " (" .. awards_count .. ")"
                        end
                    end
                end
               
                if processed ~= "" then
                    table.insert(sections, {
                        display = display,
                        anchor = anchor
                    })
                end
             end
             end
           
            table.insert(sections, {
                num = num,
                display = display,
                anchor = mw.ustring.gsub(anchor, " ", "_")
            })
         end
         end
     end
     end
Рядок 47: Рядок 104:
     end
     end
      
      
     local html = '<nav class="toc-sidebar">'
     local html_parts = {}
     html = html .. '<div class="toc-title">Зміст</div>'
      
     html = html .. '<ul class="toc-list">'
     table.insert(html_parts, '<div class="l-box">')
      
      
     for _, section in ipairs(sections) do
     for i, section in ipairs(sections) do
         html = html .. '<li class="toc-item">'
         local last_class = ""
         html = html .. '<span class="toc-number">' .. section.num .. '</span>'
        if i == #sections then
        html = html .. '[[#' .. section.anchor .. '|' .. section.display .. ']]'
            last_class = " l-box-item-last"
        html = html .. '</li>'
         end
       
        table.insert(html_parts, '<div class="l-box-item' .. last_class .. '" data-target="' .. section.anchor .. '"><span>' .. section.display .. '</span></div>')
     end
     end
      
      
     html = html .. '</ul>'
     table.insert(html_parts, '</div>')
    html = html .. '</nav>'
      
      
     return html
     return table.concat(html_parts, '\n')
end
end


return p
return p

Поточна версія на 20:03, 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 section_end = mw.ustring.find(content, "\n==[^=]", section_start + 1)
    
    if section_end then
        return mw.ustring.sub(content, section_start, section_end - 1)
    else
        return mw.ustring.sub(content, section_start)
    end
end

local function count_facts(section_content)
    local count = 0
    for line in mw.ustring.gmatch(section_content, "[^\n]+") do
        if mw.ustring.match(line, "^%*[^%*]") then
            count = count + 1
        end
    end
    return count
end

local function count_awards(section_content)
    local count = 0
    for _ in mw.ustring.gmatch(section_content, "%[%[File:[^%]]+%.png") 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 = {}
    
    table.insert(sections, {
        display = "Біографія",
        anchor = "top"
    })
    
    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 processed = frame:preprocess(section_name)
                processed = mw.text.trim(processed)
                local anchor = mw.ustring.gsub(processed, " ", "_")
                local display = processed
                
                local section_pos = mw.ustring.find(content, line, 1, true)
                
                if section_pos then
                    local section_content = get_section_content(content, section_pos)
                    
                    if mw.ustring.match(processed, "Цікаві факти") then
                        local facts_count = count_facts(section_content)
                        if facts_count > 0 then
                            display = processed .. " (" .. facts_count .. ")"
                        end
                    elseif mw.ustring.match(processed, "Нагороди") then
                        local awards_count = count_awards(section_content)
                        if awards_count > 0 then
                            display = processed .. " (" .. awards_count .. ")"
                        end
                    end
                end
                
                if processed ~= "" then
                    table.insert(sections, {
                        display = display,
                        anchor = anchor
                    })
                end
            end
        end
    end
    
    if #sections == 0 then
        return ""
    end
    
    local html_parts = {}
    
    table.insert(html_parts, '<div class="l-box">')
    
    for i, section in ipairs(sections) do
        local last_class = ""
        if i == #sections then
            last_class = " l-box-item-last"
        end
        
        table.insert(html_parts, '<div class="l-box-item' .. last_class .. '" data-target="' .. section.anchor .. '"><span>' .. section.display .. '</span></div>')
    end
    
    table.insert(html_parts, '</div>')
    
    return table.concat(html_parts, '\n')
end

return p