$ pygmentize -O full -O style=native -o test.html test.py
Per installare Pygments:
$ easy_install Pygments
è possibile utilizzarlo come una libreria.
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)
uscita:
<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">"Hello World!"</span><br>
(spazio bianco aggiunto per chiarezza)
come HTML:
#/usr/bin/python
stampa " Ciao Mondo! "
Gee, non sapevo nulla dell'evidenziatore di sintassi – Xolve
In molti casi, il syntaxhighlighter è ottimo. C'è una versione di plugin per Wordpress che uso in modo estensivo sul mio blog. –
+1 Non sapevo né –