Use a script to format the sample code in the blog post (continued 1)

Source: Internet
Author: User

When you see the comments, it is said that adding a line number will affect the copied code. Some netizens also suggested using UL/LI. In fact, I have also tried this, and the effect is not ideal. On the one hand, even if UL/LI is used, the copied code will still contain leading characters (in Firefox ); in addition, LI does not seem to fully maintain the indent format of the original code like PRE (it seems that there is a problem when there are more than two levels of indentation ). So I am also considering other methods.

The first thought was to add a link at the top of the code or somewhere else. When the user clicks this link, the code without a line number will be automatically copied. However, it is a pity that this method is not passed in Firefox. For security reasons, scripts cannot execute the Copy command (Do you know if there are other methods ?). Therefore, this will not continue. The method I came up with was to use it now: Add a TABLE with one row and two columns in PRE; display the row number in the left column and the code content on the right. This method is quite good on my local machine. But when I tested it in the blog Park, I found that the two fonts are not the same size! It's strange. When can I check it again...

In addition, I added the following features in the script used this time:

  • C # coloring of keywords, comments, and strings
  • Different classes can be used to complete the coloring function for different code types.
  • No row number is allowed (Added by default)

To use this script, you only need two simple steps:

  • Add reference to this script in HTML
    <Script type = "text/javascript" src = "http://files.cnblogs.com/Kellin/CodeFormatter.js"> </script>
  • Put the code that needs to be written in the blog into a PRE element, and add an attribute called SourceCode for this PRE element. The value range of this attribute includes C #, C, C ++, Java, and JS. Currently, only C # (none) is completed.
    Case Sensitive ). For example:
    <Pre sourceCode = "C #">
  • No row number is allowed (Added by default)

Here is a practical example: http://www.cnblogs.com/Kellin/archive/2007/09/04/882005.html

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.