开发者

Display Pretty Code in Django

I'm looking for something I can use within django to display preformatted code. Ideally this would include out-of-the-box syntax highlighting for various programming languages, although just starting with something that displayed html and xml well would be a good starting point.

Does something like this exist?

Basically I am looking for something like the widget dpaste (and also stack ove开发者_高级运维rflow) use to display code.

e.g. http://dpaste.com/hold/102141/

or

<?xml version="1.0" encoding='UTF-8'?>
<painting>
  <img src="madonna.jpg" alt='Foligno Madonna, by Raphael'/>
  <caption>This is Raphael's "Foligno" Madonna, painted in
  <date>1511</date>-<date>1512</date>.</caption>
</painting>

I'm aware of this question, but mine is not about the mechanics of escaping the code, it's about the UI.


You could use Pygments to do the syntax highlighting and get HTML to display.

Example code :

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

highlighted = highlight('# Some Python code', PythonLexer(), HtmlFormatter())

Also see the official documentation.


I have found SyntaxHighlighter (http://alexgorbatchev.com) to work well within the Django part of my site.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜