HTML code hightlight with javascipt (jquery)

I recently had a need for a javascript library that converts plain html into htmlentities and adds highlight. Those scripts exist in abundance (eg SyntaxHighlighter), do a great job but they are insanely large in size. I brought it back to just a couple of lines making is extremely fast and easy to implement and adjust. It has it's flaws but will handle 99% of my needs in a fraction of the time

Example

This will do nicely

Some text, a link

  • a list tiem

This will do nicely

Some text, a link

  • a list tiem

Usage

Just link to the javascript or place the code somewhere on your page. If you want to you can add document ready stuff. Wrap the html that you want converted in anything with class="code" and you are done

Code

Here is the code:

$('.code').each(function(e){ 
	var decoded = $(this).text($(this).html()).html(); 
	// remove if first thing is newline 
	decoded = decoded.replace(/^[n]/, ''); 
	// match element = value 
 	decoded = decoded.replace(/([a-z."]+)=("|')(.*?)("|')/ig,"<span class="red">$1</span>=$2<span class="orange">$3</span>$4");     
	// mach tags 
 	decoded = decoded.replace(/&lt;(.*?)&gt;/ig,"&lt;<span class="blue">$1</span>&gt;");     
	// wrap in pre element 
	$(this).wrap('<pre>'); 
	//change html 
	$(this).html(decoded);  
}); 

Waar gaat uw website over ?

Vul hieronder het domein van uw website in. Wij analyseren dan automatisch de inhoud van de site.


Ja, ik heb de voorwaarden gelezen en ga accoord.