A Javascript Code "Prettifier"

I came across a link today that would allow you to post “pretty” code in any HTML page. I’ve had plenty of problems (as have others) posting well-formatted source code to my Blogger-based blog. I’ve wanted to correct this problem for some time, and this project appears to be a decent solution. Here’s an example:

function chooseWinner() {   $('p3').removeClassName('winner');   participantBo.chooseWinnerForEvent(eventId, function(data) {      people = $A(data);      updateDivs();      intervalId = setInterval(doShift, 500);   });}

Now that isn’t too bad is it? There seems to be a bit of a delay as the Javascript is loaded, but I think that’s more a symptom of my connection than it is a problem with the script.

Here’s how you can take advantage of this solution (quoted from http://google-code-prettify.googlecode.com/svn/trunk/README.html):

  1. Download a distribution
  2. Include the script and stylesheets in your document
    (you will need to make sure the css and js file are on your server, and
    adjust the paths in the script and link tag)
    <link href="prettify.css" type="text/css" rel="stylesheet" /><script type="text/javascript" src="prettify.js"></script>

  3. Add onload="prettyPrint()" to your
    document’s body tag.
  4. Modify the stylesheet to get the coloring you prefer
  5. Put code snippets in
    <pre class="prettyprint">...</pre>
    or <code class="prettyprint">...</code>
    and it will automatically be pretty printed.
Advertisement

One response to this post.

  1. Posted by Shane on February 6, 2008 at 10:17 pm

    I found this blog entry that generates formatted code:http://formatmysourcecode.blogspot.com/

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.