I am trying to generate a table of contents from an HTML block (not a complete file - only content) based on <h2>and tags <h3>.
My plan so far has been as follows:
Retrieve the list of headers with beautifulsoup
Use a regular expression for content to place anchor links before / inside title tags (so that the user can click on the table of contents). Perhaps there is a method to replace inside beautifulsoup?
Display a nested list of heading links in a predefined location.
It sounds easy when I say it like that, but it hurts a little in the rear.
Is there something that does all this for me at a time, so I don’t waste the next couple of hours inventing the wheel?
Example:
<p>This is an introduction</p>
<h2>This is a sub-header</h2>
<p>...</p>
<h3>This is a sub-sub-header</h3>
<p>...</p>
<h2>This is a sub-header</h2>
<p>...</p>