Use a script to format the sample code in the blog post (Part 2)

Source: Internet
Author: User

I often encounter a problem when I write articles on my blog and how to format and paste code. It is also convenient to use the formatting tools provided by the blog Park, but it is troublesome to make some changes. What's more, I prefer to directly edit HTML, rather than using these tools. To use these tools, you must change to the visual editing status. When I change back to the HTML state, the originally formatted HTML may be messed up by the editor: (so I plan to use javascript to write a tool for formatting code.

Now, the tool for formatting the code is almost done! Fixed previous bugs and added some new features. In addition, I also put this script into a class library project and write it as a control. In this way, you can directly operate it in the background code.

This is the latest file:

  • Http://files.cnblogs.com/Kellin/CodeFormatter.js
  • Http://files.cnblogs.com/Kellin/CodeFormatter.rar

At present, the main functions of this formatting tool include:

  • Add row number
  • Highlight keywords, strings, and comments for some specified language types. These languages include C #, C/C ++, JavaScript, and Java. HTML, XML, and general programming languages do not have the same highlighted mode.

You can visit this page to see the current effect: http://www.cnblogs.com/kellin/archive/2007/09/04/882005.html.

There are two ways to use this little stuff. First, directly reference the script file CodeFormatter. js:

  • Put the code to be formatted in a PRE element and add a SourceCode attribute (specify the language type of the Code ). Note: You need to convert some special characters in the PRE element code (replace & with & amp; or use the HttpUtility. HtmlEncode (str) method ). For example:

    <pre sourcecode="html">public class MyClass{    public MyClass()    {        System.Console.WriteLine( "Hello world!" );    }}</pre>
  • Add the following javascript statement to your blog:

    <script src="http://files.cnblogs.com/Kellin/CodeFormatter.js" type="text/javascript"></script>

The second is to use the Kellin. Web. UI. WebControls. CodeFormatter control in the compiled class library. You can download this class library project from here: http://files.cnblogs.com/kellin/codeformatter.rar.

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.