Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python equivalent to "php -s"

Tags:

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?
like image 534
Mark Biek Avatar asked Mar 18 '09 16:03

Mark Biek


1 Answers

$ 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!"

like image 125
jfs Avatar answered Oct 21 '22 10:10

jfs