1

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.

Yannic Hamann
  • 4,655
  • 32
  • 50
  • The reStructuredText markup is incorrect for sections. The over/underlines must be the same length as the section name. See http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#sections – Steve Piercy Oct 03 '17 at 20:37
  • Thanks for your reply I changed it but my question still persists. – Yannic Hamann Oct 04 '17 at 09:21

0 Answers0