Немає опису редагування
Немає опису редагування
Рядок 52: Рядок 52:
     end
     end
      
      
     local container = mw.html.create('div'):addClass('l-box')
     local html_parts = {}
   
    table.insert(html_parts, '<div class="l-box">')
      
      
     for i, section in ipairs(sections) do
     for i, section in ipairs(sections) do
         local item = container:tag('a')
         local last_class = ""
            :attr('href', '#' .. section.anchor)
            :addClass('l-box-item')
            :wikitext(section.display)
       
         if i == #sections then
         if i == #sections then
             item:addClass('l-box-item-last')
             last_class = " l-box-item-last"
         end
         end
       
        table.insert(html_parts, '<div class="l-box-item' .. last_class .. '">[[#' .. section.anchor .. '|' .. section.display .. ']]</div>')
     end
     end
      
      
     return tostring(container)
    table.insert(html_parts, '</div>')
   
     return table.concat(html_parts, '\n')
end
end


return p
return p

Версія за 19:46, 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
        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, " ", "_")
                
                if processed ~= "" then
                    table.insert(sections, {
                        display = processed,
                        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 .. '">[[#' .. section.anchor .. '|' .. section.display .. ']]</div>')
    end
    
    table.insert(html_parts, '</div>')
    
    return table.concat(html_parts, '\n')
end

return p