Jinja2 + reStructured Markup

The idea is as follows. I am posting some text to jinja2 using tags similar to stackoverflow. How to tell jinja2 to consider them as markup containing text and generate bold, italics, etc. Text in html?

Thank.

+5
source share
2 answers

You should be able to do this:

from docutils.core import publish_string
import jinja2

html = publish_string(source=text, writer_name='html')
node = jinja2.Markup(html)

Where nodeis Jinja 2 node to actually include in your scope.

+3
source

I'm used to django-markdown , so I think using a filter is a good way to accomplish this:

   <div class="content">{{ article.body|rst }}</div>

, jinja2, . , - ( ):

from docutils.core import publish_parts
import jinja2

def rst_filter(s):
    return jinja2.Markup(publish_parts(source=s, writer_name='html')['body'])
environment.filters['rst'] = rst_filter
+3

All Articles