Highlight.js highlights syntax in code examples on blogs, forums and, in fact, on any web page. It's very easy to use because it works automatically: finds blocks of code, detects a language, highlights it.
getting and building:
git clone https://github.com/pumbur/highlight.js
# installing libs for make-script (need ruby and ruby-gems):
gem install haml sass yui-compressor closure-compiler uglifier
cd highlight.js
# if need, edit options in end of make file
ruby ./make
usage:
DOM:
<script src="highlight.js"></script>
<!-- add language file, if it not merged in highlight.js: -->
<!--<script src="languages/python.js"></script>-->
<link href="styles/default.css" rel="stylesheet"/>
<script>
// automatic initialization:
hljs.initHighlightingOnLoad();
hljs.tabReplace = ' '; // you can insert html: '<span class="indent">\t</span>'
// or custom initialization:
/*
onload = function(){
var e = document.getElementById("code");
hljs.highlightBlock(e, ' ', true);
console.log( hljs('...code...', 'language') );
}
*/
</script>
<!-- code for highlight: -->
<pre><code>...code...</code></pre>
<!-- explicitly set language -->
<pre><code class="python">...code...</code></pre>
<!-- disable highlighting -->
<pre><code class="no-highlight">...code...</code></pre>
JQuery:
<script src="jquery.js"></script>
<script src="highlight.js"></script>
<!-- add language file, if it not merged in highlight.js: -->
<!--<script src="languages/python.js"></script>-->
<link href="styles/default.css" rel="stylesheet"/>
<script>
onload = function(){
$('pre code:not(.no-highlight)').highlight();
//$('pre code.python').highlight('python');
//console.log( hljs('...code...', 'language') );
}
</script>
<!-- code for highlight: -->
<pre><code>...code...</code></pre>
<!-- explicitly set language: -->
<pre><code class="python">...code...</code></pre>
<!-- disable highlighting: -->
<pre><code class="no-highlight">...code...</code></pre>
CommonJS:
var hljs = require('./highlight.js');
console.log( hljs('...code...', 'language') );