Joe Mornin Joe Mornin - 10 months ago 53
Python Question

Syntax highlighting in <pre> tags

Are there any libraries that will allow me to display code in

tags and highlight the syntax according to the language? I'm imagining something like this:

<pre class="python">
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'

...where the CSS for
would highlight the Python code appropriately.

Does something like this exist?


There's SyntaxHighlighter:

<pre class="brush: python">
   # python code here

There's also highlight.js which has the option of automatically detecting the syntax and highlighting it appropriately; however, you would need to use both <pre><code> tags to wrap your code.

If you're looking for a server-side example, there's GeSHi or Pygments for Python.