I am writing a blog in Django
and I want to compose my posts with ReStructuredText
. I am using django-markup to convert the text to HTML which works perfectly fine.
Additionally, I want to create a table of content out of that text (my blog post). For this purpose I have created a template tag.
My template looks like the following:
[...]
{{ blog.content|rst_toc }}
<hr />
{{ blog.content|apply_markup:article.markup }}
[...]
tags.py
:
import docutils
from docutils import core
@register.filter
def rst_toc(text):
doctree = core.publish_doctree(text)
for section in doctree.traverse(docutils.nodes.section):
title = section.next_node(docutils.nodes.Titular)
if title:
print(title)
return 'TOC'
My main problem here is that this snippet is missing the document title.
=
1
=
One
2
#
Two
3
-
Three
returns 2, 3
not 1, 2, 3
Any further thoughts would be appreciated especially to the fact that I am publishing the doctree
twice. In |rst_toc
and |apply_markup:article
.