Adding a pattern for a table of content (ToC) based on Markdown2
Hello,
It would be great to be able to generate a ToC for an article. I see a possibility using Markdown2, the implementation of Markdown used in VenC. In processor.py, in this code:
def parse_markup_language(string, markup_language, ressource):
if markup_language == "Markdown":
string = markdown.markdown(string, extras=["header-ids"])
It would require to change extras
and to extract the generated ToC as explained here in that way:
string = markdown.markdown(string, extras=["header-ids", "toc"])
toc_html = string.toc_html
Since string
is not a string, but a 'UnicodeWithAttrs' object, as you can see in markdown2.py (on line 410):
# Attach attrs to output
rv = UnicodeWithAttrs(text)
if "toc" in self.extras and self._toc:
rv.toc_html = self._toc_html
if "metadata" in self.extras:
rv.metadata = self.metadata
return rv
And for that object (on line 2399):
class UnicodeWithAttrs(unicode):
"""A subclass of unicode used for the return value of conversion to
possibly attach some attributes. E.g. the "toc_html" attribute when
the "toc" extra is used.
"""
metadata = None
toc_html = None
So, could you use this possibility of Markdown2 to generate a ToC I could insert in an article with a VenC pattern? For example, if I use :
<div class="toc">.:ToC:.</div>
VenC would insert the ToC here. Optionally you could see if the implementation of reStructuredText is able to generate a ToC too, so that .:ToC:.
would work with reStructuredText as well as with Markdown.
It could be useful (for optimization's sake) to disable the ToC extra in Markdown2 if the .:ToC:.
pattern is not used (when each article is generated.)
Thank you for your time!
Jérémy :-)