jinja2 + reStructured Markup

The idea is the following. I send some text to jinja2 using tags similar to stackoverflow's. How do I tell jinja2 to treat them as a markup containing text and to generate bold, italic and so on text in html?

Thank you.

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 node is the Jinja 2 node to actually include in your scope.


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

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

I'm not aware if such filter exists for jinja2 but it should be very easy to write. I guess something in the line of this (untested code):

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

Need Your Help

Bootstrap data-toggle radio button/Checkbox checked property

javascript jquery twitter-bootstrap jsp

I saw many questions related to this issue. But that questions are not related to my scenario.

PHP MySQL search cyrillic characters

php mysql search cyrillic

I would like to optimize my search sql query from cyrillic input.