CodeHighlighter 0.4 by Dan Webb

CodeHighlighter is a lightweight, unobstrusive and fully configurable script for displaying code examples highlighted in a way similar to the way many text editors highlight code. It weighs in at just under 4K, allows users to configure their own style sets so that you can highlight any language you like and is deployable simply by attaching it to a page with the script tag and adding class names as hooks. It should also play nicely with any other scripts on your page as it has a tiny footprint on the global namespace.

Many thanks to Dean Edwards who's star-light behaviour inspired this.

Deploying the script

  1. Add a <script> tag for CodeHighlighter.js and a script tag for each of the code types you want to highlight on your page. At this time there are only very basic style sets for JavaScript, CSS and HTML. But it's easy to make your own for any language you like if you know regular expressions. Let me know if you do and I'll link to them.
  2. Add an appropriate class to each <code> element that contains code. 'javascript', 'ruby', 'css' or 'html' will do the trick.
  3. Define CSS styles for each code element, the script simply parses the code and wraps a <span> tag around each element with the appropriate class name. You just need to write CSS to style the code the way you want. No programming or weirdness required. See the source of this code as an example.

Testing

Known to work on:

Known to degrade well on:

Any other feedback for any other browser would be greatly apprieciated. Please email Dan Webb at dan[at]danwebb[dot]net. Have a look through the small examples below.

Creating your own style sets for other languages

Have a look at this guide to creating style sets.

Cheers,
Dan

Inline code

Hopefully, you should be able to put some code inline like this: document.write("bong") and hopefully it should work.

JavaScript Example

/*
This script detects external links in a page
and attaches a behaviour to them so they open
in a external window.
*/

function initialiseLinks() {
    if (document.getElementsByTagName) {
        var links = document.getElementsByTagName("A");
        for (var i = 0; i < links.length; i++) {
            if (links[i].href.indexOf("http:")==0) {
                // if the links URL starts with http: then we assume it's an external link
                links[i].onclick = function() {
                    window.open(this.href);
                    return false; // stop normal link behaviour
                }
            }
        }
    }
}

window.onload = initialiseLinks();

CSS Example

.javascript  .comment {
	color : green; /* ffbgffg */
}

.javascript  .string {
	color : maroon;
}

.javascript  .keywords {
	font-weight : bold;
}

.javascript  .global {
	color : blue;
	font-weight: bolder;
}

.javascript  .brackets {
	color : Gray;
}

.javascript  .thing {
	font-size : 10px;
	background : url(ghgfhfg gh f.rtjhf);
}

HTML Example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>CodeHighlighter example</title>
		<!-- This is all you need to do to get CodeHighlighter working -->
		<script type="text/javascript" src="CodeHighlighter.js"> </script>
		<script type="text/javascript" src="html.js"> </script>
	</head>
	<body>
		<p>Put your pre tags here!</p>
	</body>
</html>

Ruby Example

def login
   if !@params[:key].nil? && @attendee = Attendee.find_by_hashkey(@params[:key])
      # coming in with valid key
      if !@attendee.password_set?
        # no password yet, let them in
        @session[:attendee] = @attendee
        redirect_to :action => 'preferences', :id => @attendee.event.uri
      else
        @event = @attendee.event
      end
    else 
      # if no key we need to know the event
      @event = get_event_by_id_or_uri
    end
    
    if @request.post?
      # posted login details
      if @attendee = Attendee.authenticate(@event, @params[:email], @params[:password])
         @session[:attendee] = @attendee
         redirect_to :action => 'preferences', :id => @event.uri
      else
        flash['notice'] = 'Login unsuccessful.'
      end
    end
    
    if @attendee.nil?
      @email = ''
    else
      @email = @attendee.email
    end
  end