The Artima Developer Community
Sponsored Link

Python Buzz Forum
JavaScript Based Code Prettification

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ryan Tomayko

Posts: 408
Nickname: rtomayko
Registered: Nov, 2004

Ryan Tomayko is an instance of a human being...
JavaScript Based Code Prettification Posted: Mar 17, 2008 11:32 PM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Ryan Tomayko.
Original Post: JavaScript Based Code Prettification
Feed Title: Ryan Tomayko (weblog/python)
Feed URL: http://tomayko.com/feed/
Feed Description: Entries classified under Python.
Latest Python Buzz Posts
Latest Python Buzz Posts by Ryan Tomayko
Latest Posts From Ryan Tomayko (weblog/python)

Advertisement

It appears that the recent syntax highlighting enhancements to Google Code’s source browser are implemented with a slightly modified version of Mike Samuel’s JavaScript Code Prettifier: a 100% browser-side JavaScript/CSS syntax highlighting engine with a sufficiently simple implementation (~750 LOC / 37KB unpacked / 12KB gzipped), a liberal license, and decent language support. The README claims that C (and friends), Java, Python, Bash, SQL, HTML, XML, CSS, JavaScript, and Makefiles are highlighted well and Ruby, PHP, Awk, and Perl are handled passably. As luck would have it, I just went through the process of wiring this script up last week with a few interesting tweaks to get automatic highlighting on all my posts.

The prescribed usage was fairly simple: bring in the stylesheet and external script, add a “prettyprint” class to

 and/or  blocks, and call prettyPrint() on load/ready.

This setup has a few (easily remedied) drawbacks, however:

  1. Manually adding a “prettyprint” class is a bit of a pain with Markdown since there’s no way to add a class name to a code block.

  2. Manually adding a “prettyprint” class is a bit of a pain because I don’t feel like flipping through looking for every post I’ve ever written with a code block in it.

  3. Adding prettifier.js to a all pages means a 37K download even when no syntax highlighting is required.

  4. The stock stylesheet’s color palette is somewhat lacking.

Making it Automatic

The following wee bit of code is running on each page to automatically highlight all code blocks on the page. I used jQuery but this should be easily ported to other JavaScript libraries or standard DOM.

$(document).ready(function() {

    // add prettyprint class to all 
blocks var prettify = false; $("pre code").each(function() { $(this).addClass('prettyprint'); prettify = true; }); // if code blocks were found, bring in the prettifier ... if ( prettify ) { $.getScript("/js/prettify.js", function() { prettyPrint() }); } });

We run through the DOM after a page is fully loaded and look for elements nestled within a

 (this is what Markdown puts out for code blocks). We add the “prettyprint” class to each of the  elements and note that the prettifier is required. Lastly, we use jQuery’s getScript method to bring in the prettifier but only if we’ve detected that it’s required (jQuery.getScript is implemented by appending a  tag to 

Read: JavaScript Based Code Prettification

Topic: PyCon 2008: Supervisor as a Platform Previous Topic   Next Topic Topic: Supervisor 3.0a4 Released

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use