Django/Textile/Pygments: " ' > being escaped

518 views Asked by At

I have a blog written in django that I am attempting to add syntax highlighting to. The posts are written and stored in the database as textile markup. Here is how they are supposed to be rendered via template engine:

{{ body|textile|pygmentize|safe }}

It renders all the HTML correctly and the code gets highlighted, but some characters within the code blocks are being escaped. Specifically double quotes, single quotes, and greater than signs.

Here is the Pygments filter I am using: http://djangosnippets.org/snippets/416/

I'm not sure which filter is actually putting the escaped characters in there or how to make it stop that. Any suggestions?

1

There are 1 answers

0
yarbelk On

shameless plug to me answering this on another page: https://stackoverflow.com/a/10138569/1224926

the problem is beautifulsoup (rightly) assumes code is unsafe. but if you parse it into a tree, and pass that in, it works. So your line:

code.replaceWith(highlight(code.string, lexer, HtmlFormatter()))

should become:

code.replaceWith(BeautifulSoup(highlight(code.string, lexer, HtmlFormatter())))

and you get what you would expect.