Write a Python script to highlight code on a Web page _python

Source: Internet
Author: User
Tags python script

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:

Easy_install pygments

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.