made an online code highlighting project, powerful Python never let me Down, a powerful pygments module can be used for many (many) languages code highlighting
Here's how to introduce it:
The first installation is simple, using Easy_install to install:
We'll use it after installation, and Python's simplicity will not disappoint you:
From pygments.lexers import Pythonlexver to
pygments.formatters import htmlformatter from
pygments Import Highlight
formatter = Htmlformatter (encoding= ' utf-8 ', style = ' emacs ', Linenos = True)
code = Highlight (' print ' H Ello, World "', Pythonlexer (), formatter)
print code
Results
' <table class= "highlighttable" ><tr><td class= "Linenos" ><div class= "Linenodiv" ><pre> 1</PRE></DIV></TD><TD class= "code" ><div class= "highlight" ><pre><span class = "K" >print</span> <span class= "s" > "Hello, World" </span>\n</pre></div>\n</td ></tr></table> '
This is a simple code for the highlight, of course, if you do the above operation, and then the content into a file to view, it must be big mouth pit dad, because there is no highlight, because the default is not output CSS We also want to get CSS to add to the HTML:
CSS = Formatter.get_style_defs ()
Then the CSS content and the above HTML to write to the HTML file can see the highlighted code (do not tell me you do not know where the CSS should be placed)