• Designs Photo
  • Fantasy Photo
  • Code Photo
  • Silhouette Photo

Creative, Modern, Professional Designs

For the last 6 years, I have been creating innovative websites to capture attention and inspire the mind, balancing user experience with an attractive website interface. By the use of AJAX and CSS, websites become more interactive, and with Search Engine Optimization, they are easily found through Google, Yahoo and Bing.

Designs Photo
Fantasy Photo

Everything you see here is Original Work

Besides the jQuery library and syntax highlighting, this whole website has been created from scratch; No pre built frameworks or content management systems have been used.

This demonstrates dedication, personalization and skill, and is reflected in all of the work I produce.

Programming is a Passion

As you get to know me, you'll start to see that I've tried my hands at just about everything, ranging from the basics to the more advanced and finer points in programming.

That means you'll probably find lots of hints and tips here for many different languages, as well as anything that I find relatively interesting!

HTML Code Photo
Silhouette Photo

Inspiring Designs with Limitless Possibilities

Every website created here is a unique one, with limiting boundaries out of the questions.

Programming is always evolving and so I'm always improving my game to keep up with the changes. You'll be able to see this reflected in the high quality work that I produce.

Anuj Nair's Blog »

Lightweight and Fast Syntax Highlighting using jQuery.Syntax

jQuery.Syntax is a lightweight syntax highlighter developed in JavaScript with speed as its fundamental core

Posted: 22 May 2010 - Written by Anuj Nair
Whilst looking for a syntax highlighter for my website, many of which I found were incredibly bloated with features that I knew I would never use and without modifying all of the files massively, I knew I'd never cut the size of the highlighters down. Page loading times for my website would stay high, and that's not what I wanted.

After Googling around a bit, it seemed that most people were recommending GeSHi - The Generic Syntax Highlighter, but that wasn't quite what I was after. Putting GeSHi on a dynamic page containing a list of blog posts meant that CSS rules would repeat themselves, and the GeSHi PHP class certainly isn't lightweight. However, I stumbled across a JavaScript solution...

jQuery.Syntax - The Lightweight JavaScript Highlighter


Developed by Samuel Williams, jQuery.Syntax is an extremely fast and lightweight Syntax Highlighter written in JavaScript. It dynamically loads the source files it needs and plays nicely with CSS. I found it to be perfect for my website as I was using jQuery already.

Using jQuery.Syntax is easy: Simply include the core JavaScript file into your page header, and add a CSS class to either the pre or code tags in your HTML. This is the result:

<?php
	function foo($bar) {
		$bar = strtoupper($bar);
		echo 'Uppercase: ' . $bar;
	}
?>


Even though jQuery.Syntax supports about 20 different languages, telling it that I only have PHP code in my post means that it will only load it's PHP styling files.

Setting up jQuery.Syntax


To begin, browse over to the jQuery.Syntax GitHub page and download the latest release. Also, make sure you have jQuery version 1.4.1+.

On the page you wish to add syntax highlighting to, in the head of the document, add:

<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/jquery.syntax.min.js"></script>
<script type="text/javascript">
	$(function() {
		$.syntax({root: "scripts/"});
	});
</script>

  • jQuery is included for obvious reasons; The plugin doesn't work without it.
  • jquery.syntax.min.js contains all of the core files which will load all of the necessary files when it needs to. It has been minified for speed purposes, combining the 2 files "jquery.syntax.js" and "jquery.syntax.cache.js" together

In the custom function, change the root to where the jQuery.Syntax files are located, relative to the root of your website. This is for the dynamic loading of necessary files.

Now, to highlight a piece of code, simply wrap it in the pre HTML tags and add the class syntax {language} where {language} is one of the supported languages of the highlighter:

<pre class="syntax php">
<?php
	function foo($bar) {
		$bar = strtoupper($bar);
		echo 'Uppercase: ' . $bar;
	}
?>
</pre>


And that's it! It's as easy as that. Load your page and your code should now be highlighted.

As well as a table layout, jQuery.Syntax also supports the ability for inline styling, like shown above. to achieve this, simply add the syntax classes to the code HTML tags within a post:

Look at line 2 which says: <code class="syntax php">$bar = strtoupper($bar);</code> ...

The outcome:

Look at line 2 which says: $bar = strtoupper($bar); ...

A few more Advanced Features


For ultimate optimization, only include jQuert.Syntax when you need it. Here's a little piece of code to help you do that:

if ($('.syntax').length) {
	$.getScript(site_root + "/sh/jquery.syntax.min.js", function () {
		$.syntax({root: site_root + "/sh/"});
	});
}


Now there's no need to even load the minified version of jQuery.Syntax in your header - This function does it all dynamically for you.

You can also choose the block layout for large bodies of text. By default, jQuery.Syntax defaults to the "list" style type as shown in all of my code blocks above. However, there are also a few other block styles, namely a Table layout and a Fixed Layout. Exmaples of what these two different layouts look like can be viewed on the jQuery.Syntax Layout Examples page.

To change these on your website, simply include the option blockLayout in the $.syntax setup function like so:

$.syntax({root: site_root + "/sh/", blockLayout: "fixed"});


This function even supports custom layouts, if you make your own. The inline layout can also be changed using the option inlineLayout.

More advanced features are also supported - for a full list, I highly suggest checking out the Main jQuery.Syntax site.
Comments
No one has posted any comments yet; Why not be the first?
Name :
Email (hidden) :
Website (optional) :
Captcha :
 
How to Post   Comment :