Mar*_*iek 6 php python syntax-highlighting
您可能知道或不知道,您可以使用php -s从PHP源文件生成颜色语法高亮的HTML文件.
我知道Stackoverflow使用的syntaxhighlighter,这不是我真正想要的.我正在寻找能够在没有Javascript的情况下生成HTML输出的东西.
有谁知道的东西等同于PHP - 小号为Python?jfs*_*jfs 12
$ pygmentize -O full -O style=native -o test.html test.py
Run Code Online (Sandbox Code Playgroud)
要安装Pygments:
$ easy_install Pygments
Run Code Online (Sandbox Code Playgroud)
您可以将其用作库.
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)
Run Code Online (Sandbox Code Playgroud)
输出:
<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>
Run Code Online (Sandbox Code Playgroud)
(为了清晰起见,添加了空格)
作为HTML:
#!/ usr/bin/python
print"Hello World!"