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


return p
return p

Revision as of 13:19, 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] do
        table.insert(items, args['item' .. i])
        i = i + 1
    end
    
    -- Generate TOC as wikitext ordered list
    local toc = "{| class=\"wikitable\"\n|+\n!Table of Contents\n|-\n|"
    toc = toc .. "'''Table of Contents'''\n\n"
    toc = toc .. "<ol>\n"
    
    for _, item in ipairs(items) do
        toc = toc .. "<li>" .. item .. "</li>\n"
    end
    
    toc = toc .. "</ol>\n"
    
    return toc
end

return p