Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 285
DTailor Avatar asked Jul 03 '12 11:07

DTailor


2 Answers

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
like image 81
Paulo Scardine Avatar answered Sep 23 '22 17:09

Paulo Scardine


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.

like image 22
Wolph Avatar answered Sep 22 '22 17:09

Wolph