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
Tag: Manual revert
CriticalResist (talk | contribs)
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 4: Line 4:
     local args = frame:getParent().args
     local args = frame:getParent().args
     local items = {}
     local items = {}
    local levels = {}
      
      
     -- Collect all TOC items
    -- Check if collapsed parameter is set
    local collapsed = args.collapsed == "true"
   
     -- Collect all TOC items and their levels
     local i = 1
     local i = 1
     while args['item' .. i] and args['link' .. i] do
     while args['item' .. i] do
         table.insert(items, {
         items[i] = args['item' .. i]
            title = args['item' .. i],
        levels[i] = tonumber(args['level' .. i]) or 2 -- Default to level 2
            link = args['link' .. i]
        })
         i = i + 1
         i = i + 1
     end
     end
      
      
     -- Generate TOC HTML
    if #items == 0 then
     local toc = mw.html.create('div'):addClass('book-toc')
        return ''
     local list = toc:tag('ol')
    end
   
     -- Find minimum level
     local min_level = math.min(unpack(levels))
   
     local list = {}
   
    for idx = 1, #items do
        local normalized_level = levels[idx] - min_level + 1
        local item = items[idx]
       
        -- Add list line
        table.insert(list, string.rep('#', normalized_level) .. ' ' .. item)
    end
      
      
     for _, item in ipairs(items) do
     -- Add mw-collapsed class if collapsed=true
        list:tag('li')
    local collapsible_class = "mw-collapsible mw-made-collapsible"
            :tag('a')
    if collapsed then
                :attr('href', '#' .. mw.uri.anchorEncode(item.title))
        collapsible_class = collapsible_class .. " mw-collapsed"
                :wikitext(item.title)
     end
     end
      
      
     return tostring(toc)
     return '<div class="book-toc">\n' ..
      '<div class="toc-header">\n' ..
      '<strong>Table of contents</strong>\n' ..
      '</div>\n' ..
      '<div class="' .. collapsible_class .. '">\n' ..
      table.concat(list, '\n') .. '\n' ..
      '</div>\n' ..
      '</div>'
end
end


return p
return p

Latest revision as of 11:13, 18 August 2025

local p = {}

function p.main(frame)
    local args = frame:getParent().args
    local items = {}
    local levels = {}
    
    -- Check if collapsed parameter is set
    local collapsed = args.collapsed == "true"
    
    -- 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
        i = i + 1
    end
    
    if #items == 0 then
        return ''
    end
    
    -- Find minimum level
    local min_level = math.min(unpack(levels))
    
    local list = {}
    
    for idx = 1, #items do
        local normalized_level = levels[idx] - min_level + 1
        local item = items[idx]
        
        -- Add list line
        table.insert(list, string.rep('#', normalized_level) .. ' ' .. item)
    end
    
    -- Add mw-collapsed class if collapsed=true
    local collapsible_class = "mw-collapsible mw-made-collapsible"
    if collapsed then
        collapsible_class = collapsible_class .. " mw-collapsed"
    end
    
    return '<div class="book-toc">\n' ..
       '<div class="toc-header">\n' ..
       '<strong>Table of contents</strong>\n' ..
       '</div>\n' ..
       '<div class="' .. collapsible_class .. '">\n' ..
       table.concat(list, '\n') .. '\n' ..
       '</div>\n' ..
       '</div>'
end

return p