Tutorial – How to Install & Use The Syntax Highlighter Website Plugin

Tutorial – How to Install & Use The Syntax Highlighter Website Plugin

Syntax Highlighting is a must have for any site that focuses on such subjects as Web Development tutorials. Mainly because a Syntax Highlighter can show you the actual code and stops the browser from interpreting it.

Which is very useful for any tutorial writer who wishes to show readers the code he/she is using in a given tutorial. Continue Reading...

Syntax Highlighter?

If you are a web developer and have never seen a Syntax Highlighting plugin before then here are the basics:

  • They’re mainly used for highlighting syntax, (showing the code), that would otherwise be interpreted by the browser as normal code. In short it stops the browser from outputting the code.
  • Most Syntax Highlighters can output many different languages such as (x)HTML, CSS, PHP, javaScript etc…

So lets take a look at the best Highlighter I personally know of: SyntaxHighlighter by Alex Gorbatchev. Yes i know that page with the bright yellow looks very intimidating right now but not to worry that’s why I’m here :)

Step #1: Downloading The Source Files

So lets go over and download the source files that we need to get the highlighting working.

You should have this:
Syntax Highlighter - Step #1

Step #2: Creating Our HTML File

Now we need to make a HTML page to test our new toy, (yes I am still talking about the Highlighter :) ).
So set up a page with the following values and Divs etc…

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>The SyntaxHighlighter Tutorial Files - By KayRoseDesign</title>
</head>
<body>
	<div id="wrap">
		<div id="container">
			<h2>Everybody Was 'Syntax Highlighting'!</h2>
		</div>
	</div>
</body>
</html>

Just a pretty basic html page.

Step #3: Setting Up All Of The Files

It’s time to get everything in place so that we can link to them the next step; your folder should look as follows:
Syntax Highlighter - Step #2

So we need to add the files named shBrushXml.js and shCore.js into our JS folder. The we need to add the shCore.css and shThemeDefault.css in our CSS folder.

Linking To The Files

In the header section below the title tag, add the following links:

<script type="text/javascript" src="js/shCore.js"></script>
<!-- At least one brush, here we choose XML. You need to include a brush for every language you want to highlight -->
<script type="text/javascript" src="js/shBrushXml.js"></script>

<!-- Include *at least* the core style and default theme -->
<link href="css/shCore.css" rel="stylesheet" type="text/css" />
<link href="css/shThemeDefault.css" rel="stylesheet" type="text/css" />

As you can see each part has been described very well be Alex Gorbatchev.

Step #4: Adding Some JavaScript

Now we’re onto the last step, yay!
So we only need to add one more piece of javaScript, and here it is:

<script type="text/javascript">
	SyntaxHighlighter.all()
</script>

Just put it below the links to our javaScript file and Voila!

Outputting Some HTML!

Lets show our code shall we?
Well all we need to do is out in some pre tags, like so:

<pre class="brush: xhtml">
<h2>Hello, World!</h2>
	<p>
		Paragraph Tags!
	</p>
	<p>
		<strong>Strong Tags within Paragraph Tags!</strong>
	</p>
</pre>

And the output will looks like so:
Syntax Highlighter - Step #4

I hope you enjoyed this quick tutorial. If you did then leave a comment telling me why you liked or telling what I did wrong! :-)

Related Posts

Enjoy This Article

PG

Seb Kay

Hello!, i'm KayRose, (as you probably guessed it's not my real name ), i'm one of the editors here at BeCreative Magazine, i also coded the entire thing for my friend and fellow web enthusiast Patrick Larsson, hqmStudios, who designed it. I love all things Wordpress, jQuery, HTML and CSS. You can also catch me on twitter @KayRoseDesign.

WooThemes - WordPress themes for everyone

Be The First To Comment!



Leave a Reply