Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:BookTOC: Difference between revisions

From ProleWiki, the proletarian encyclopedia
CriticalResist (talk | contribs)
No edit summary
CriticalResist (talk | contribs)
No edit summary
Line 14: Line 14:
     end
     end
      
      
     -- Return empty if no items
     -- Generate TOC as wikitext nested list
     if #items == 0 then
     if #items == 0 then
         return ''
         return ''
     end
     end
   
    -- Find the minimum level to use as base
    local min_level = math.min(table.unpack(levels))
      
      
     local list = {}
     local list = {}
     local list_stack = {}
    local current_level = min_level
     local list_stack = {min_level} -- Start with the base level
      
      
     for idx = 1, #items do
     for idx = 1, #items do
         local level = levels[idx]
         local level = levels[idx]
         local item = items[idx]
         local item = items[idx]
       
         local normalized_level = level - min_level + 1
        -- Adjust for MediaWiki heading levels (== is level 2, but we want it as top-level)
         local depth = level - 1
          
          
         -- Close deeper levels
         -- Close deeper levels
         while #list_stack > 0 and list_stack[#list_stack] > depth do
         while #list_stack > 0 and list_stack[#list_stack] > normalized_level do
             table.remove(list_stack)
             table.remove(list_stack)
            table.insert(list, string.rep('#', list_stack[#list_stack]))
         end
         end
          
          
         -- Open new levels
         -- Open new levels if needed
         while #list_stack < depth do
         while normalized_level > list_stack[#list_stack] do
             table.insert(list_stack, #list_stack + 1)
             table.insert(list, string.rep('#', normalized_level - list_stack[#list_stack]))
            table.insert(list_stack, normalized_level)
         end
         end
          
          
         -- Add the item with the correct number of # markers
         -- Add the item
         table.insert(list, string.rep('#', depth) .. ' ' .. item .. '\n')
         table.insert(list, string.rep('#', normalized_level - list_stack[#list_stack] + 1))
        table.insert(list, ' ')
        table.insert(list, item)
        table.insert(list, '\n')
       
        current_level = normalized_level
     end
     end
      
      

Revision as of 18:41, 12 August 2025

local p = {}

function p.main(frame)
    local args = frame:getParent().args
    local items = {}
    local levels = {}
    
    -- Collect all TOC items and their levels
    local i = 1
    while args['item' .. i] do
        items[i] = args['item' .. i]
        levels[i] = tonumber(args['level' .. i]) or 2  -- Default to level 2 (==) if not provided
        i = i + 1
    end
    
    -- Generate TOC as wikitext nested list
    if #items == 0 then
        return ''
    end
    
    -- Find the minimum level to use as base
    local min_level = math.min(table.unpack(levels))
    
    local list = {}
    local current_level = min_level
    local list_stack = {min_level}  -- Start with the base level
    
    for idx = 1, #items do
        local level = levels[idx]
        local item = items[idx]
        local normalized_level = level - min_level + 1
        
        -- Close deeper levels
        while #list_stack > 0 and list_stack[#list_stack] > normalized_level do
            table.remove(list_stack)
            table.insert(list, string.rep('#', list_stack[#list_stack]))
        end
        
        -- Open new levels if needed
        while normalized_level > list_stack[#list_stack] do
            table.insert(list, string.rep('#', normalized_level - list_stack[#list_stack]))
            table.insert(list_stack, normalized_level)
        end
        
        -- Add the item
        table.insert(list, string.rep('#', normalized_level - list_stack[#list_stack] + 1))
        table.insert(list, ' ')
        table.insert(list, item)
        table.insert(list, '\n')
        
        current_level = normalized_level
    end
    
    return '<div class="book-toc">\n' .. table.concat(list) .. '</div>'
end

return p