\ Code Formatter & Syntax Highlighter jQuery Plugin
Author: Rodrigo Silveira
Today I decided to write a simple plugin to extend the functionality of everybody’s favorite Javascript library: jQuery. The entire process only took around 20 minutes, so I won’t be surprised if to find major bugs in it in the next few days. I did test the plug in a little bit, but I can’t promise buglessness in this version.
All the plugin does is take source code that you write inside your HTML file, and format it with line numbers, and syntax highlighting. The highlighting part is styled through an external CSS file, which you can theme to your liking. Since I’m writing this in mid-December, I decided to style to resemble the Christmas season.
How it works
Using the plugin is pretty simple: Import jQuery, my plugin (which I decided to call Rokko Code, after the great Brazilian coder), and the accompanying style sheet (or a custom style sheet if you prefer).
<script type="text/javascript" src=" jquery.js"></script> <script type="text/javascript" src=" rokkocode.js"></script> <link ref="stylesheet" href="rokkocode.css"/>
The next step is to identify where in your HTML you want the plugin to be applied. In this example, I’ll write all my code inside a DIV tag with a class of “src_code”.
<div class="src_code">
function Person(pName)
{
this.name = pName;
this.greet = function()
{
return 'Hello. My name is ' + this.name + '.';
};
}
</div>
Right now, if you look at your file, you should see something like this:
function Person(pName){this.name = pName;this.greet = function(){return ‘Hello. My name is ‘ + this.name + ‘.’;};}
Pretty boring, since you haven’t called upon RokkoCode. So now the next step is to call the Rokko Code plugin on that DIV. If you have multiple elements that are matched in the jQuery expression, they will all be formatted.
<script>
$('src_code').rokkoCode();
</script>
And the final result after the call to $().rokkoCode() is this:
{
this.name = pName;
this.greet = function()
{
return ‘Hello. My name is ‘ + this.name + ‘.’;
};
}
var me = new Person(‘Rodrigo’);
alert( me.greet() );
Live Demo
- View a demo of my RokkoCode jQuery Plugin.
Download Source Code
Download my RokkoCode plugin and start formatting and highlighting any C, C++, C#, Java, Javascript, and PHP code in your HTML files today.
\ Post your comments
\ Recent Posts
- Custom CSS Filters with GLSL Shaders
- Building a custom web analytics system
- Next Project: HTML5 Gamepad Demo
- HTML5 Demo: Web Messaging API – Multi-window Bouncy Ball
- Becoming a Git Master
- Blogging from Android Phone
- Learn Google Web Toolkit: Day #1
- 15 Things I Love: Software Development
- Things I learned in 2012: Software Development
- HTML5 Tic Tac Toe Challenge
- WebGL 101: Video Lessons and Tutorials
- 17 SEO Tips from Google’s Maile Ohye
\ Categories
Archives
3D
Artificial Intelligence
BYU-I
C++
C++ to Javascript port
Chromium WebGL
Debugging
demos
Discrete Math
Eclipse
Firebug
Free Download
Git
Google Web Toolkit
Goolers
GWT
HTML5 Demo
Java
Javascript Games
jQuery
Math & Programming
MySQL
Notes from YouTube video
OBJ File
OpenGL
Papers
Parser
Plug-in
plugins
quick tip
Recurrence Relations
RokkoCode
Scalability
School Projects
Screen Cast
Screencast
SEO
Sudoku
Sudoku Algorithm
The Matrix
WebGL
Web Sockets
wegbl
Writing
writting 3D (6)
Algorithms (2)
Articles (9)
BYU-I (6)
C++ (9)
CSS3 (1)
Database (1)
Fun Projects (9)
GAE (1)
Gaming (1)
GWT (4)
HTML5 (8)
Java (2)
Javascript (9)
PHP (3)
SEO (1)
Technology (1)
Web 2.0 (2)
WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.

