Writing a Python script to highlight code on a Web page

Source: Internet
Author: User
made an online code highlighting project, strong python as always did not disappoint me, a powerful pygments module can be in a variety of (many) languages code highlighting

Here's a look at this:

The first installation is simple, using Easy_install to install:

Easy_install pygments

After the installation we use, Python's simplicity will not let everyone down:

From pygments.lexers import pythonlexverfrom pygments.formatters import htmlformatterfrom pygments Import Highlightformatter = Htmlformatter (encoding= ' utf-8 ', style = ' emacs ', Linenos = True) code = Highlight (' print ' Hello, world "', Pythonlexer (), formatter) Print code

Results

'
 
  
  
1
Print "Hello, world"\ n
\ n
'

This is simple to highlight the code, of course, if you do the above, and then input the content into a file to view, certainly big call pit dad, because there is no highlight, because the default is not output CSS We also want to get CSS added to the HTML:

CSS = Formatter.get_style_defs ()

Then the CSS content and the above HTML to write to the HTML file to see the highlighted code (don't tell me you don't know where CSS should be placed)

  • 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.