views:

218

answers:

4

As you may or may not know, you can generate a color syntax-higlighted HTML file from a PHP source file using php -s.

I know about the syntaxhighlighter that Stackoverflow uses and that's not really what I'm looking for. I'm looking for something will generate HTML output without Javascript.

Does anyone know of something equivalent to php -s for Python?
+10  A: 
$ pygmentize -O full -O style=native -o test.html test.py

To install Pygments:

$ easy_install Pygments

You can use it as a library.

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

code = '#!/usr/bin/python\nprint "Hello World!"'
lexer = guess_lexer(code) # or just pygments.lexers.PythonLexer()
formatter = HtmlFormatter(noclasses=True, nowrap=True, lineseparator="<br>\n")
print highlight(code, lexer, formatter)

Output:

<span style="color: #408080; font-style: italic">#!/usr/bin/python</span><br>
<span style="color: #008000; font-weight: bold">print</span> 
<span style="color: #BA2121">&quot;Hello World!&quot;</span><br>

(added whitespace for clarity)

As html:

#!/usr/bin/python
print "Hello World!"

J.F. Sebastian
Perfect! Thanks
Mark Biek
A: 

If you have access to kwrite from KDE, you can export a file as HTML which will have the same colorization that you use for editing. This works for all languages.

David Locke
A: 

if you need only a few files to convert to html pages and are on windows you can use Notepad++. It comes (as of last versions) with NppExport plugin, that let's one to convert source code to highlighted HTML and RTF (according to your colouring scheme). It works not only with python of course, but with any language you can use in Notepad++.

SilentGhost
A: 

I found Highlight at http://www.andre-simon.de to be an extremely good tool for doing this. It is Open-source (GPL'ed though!)