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)
v2
CriticalResist (talk | contribs)
No edit summary
Tag: Manual revert
Line 7: Line 7:
     -- Collect all TOC items
     -- Collect all TOC items
     local i = 1
     local i = 1
     while args['item' .. i] do
     while args['item' .. i] and args['link' .. i] do
         table.insert(items, args['item' .. i])
         table.insert(items, {
            title = args['item' .. i],
            link = args['link' .. i]
        })
         i = i + 1
         i = i + 1
     end
     end
      
      
     -- Generate TOC as wikitext ordered list
     -- Generate TOC HTML
     local toc = "{| class=\"wikitable\"\n|+\n!Table of Contents\n|-\n|"
     local toc = mw.html.create('div'):addClass('book-toc')
    toc = toc .. "'''Table of Contents'''\n\n"
     local list = toc:tag('ol')
     toc = toc .. "<ol>\n"
      
      
     for _, item in ipairs(items) do
     for _, item in ipairs(items) do
         toc = toc .. "<li>" .. item .. "</li>\n"
         list:tag('li')
            :tag('a')
                :attr('href', '#' .. mw.uri.anchorEncode(item.title))
                :wikitext(item.title)
     end
     end
      
      
    toc = toc .. "</ol>\n"
     return tostring(toc)
   
     return toc
end
end


return p
return p

Revision as of 13:12, 12 August 2025

local p = {}

function p.main(frame)
    local args = frame:getParent().args
    local items = {}
    
    -- Collect all TOC items
    local i = 1
    while args['item' .. i] and args['link' .. i] do
        table.insert(items, {
            title = args['item' .. i],
            link = args['link' .. i]
        })
        i = i + 1
    end
    
    -- Generate TOC HTML
    local toc = mw.html.create('div'):addClass('book-toc')
    local list = toc:tag('ol')
    
    for _, item in ipairs(items) do
        list:tag('li')
            :tag('a')
                :attr('href', '#' .. mw.uri.anchorEncode(item.title))
                :wikitext(item.title)
    end
    
    return tostring(toc)
end

return p