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
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(/<(.*?)>/ig,"<<span class="blue">$1</span>>");
// 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. |

