简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM