/*
===============================================================================
Chili is the jQuery code highlighter plugin
...............................................................................
LICENSE: http://www.opensource.org/licenses/mit-license.php
WEBSITE: http://noteslog.com/chili/
Copyright 2008 / Andrea Ercolino
===============================================================================
*/
( function($) {
ChiliBook = { //implied global
version: "2.2" // 2008-07-06
// options --------------------------------------------------------------------
, automatic: true
, automaticSelector: "pre"
, lineNumbers: !true
, codeLanguage: function( el ) {
var recipeName = $( el ).attr( "class" );
return recipeName ? recipeName : '';
}
, recipeLoading: true
, recipeFolder: "" // used like: recipeFolder + recipeName + '.js'
// IE and FF convert to " ", Safari and Opera do not
, replaceSpace: " "
, replaceTab: " "
, replaceNewLine: "
"
, selectionStyle: [ "position:absolute; z-index:3000; overflow:scroll;"
, "width:16em;"
, "height:9em;"
, "border:1px solid gray;"
, "padding:15px;"
, "background-color:yellow;"
].join( ' ' )
// ------------------------------------------------------------- end of options
, defaultReplacement: '$$' // TODO: make this an option again
, recipes: {} //repository
, queue: {} //registry
, unique: function() {
return (new Date()).valueOf();
}
};
$.fn.chili = function( options ) {
var book = $.extend( {}, ChiliBook, options || {} );
function cook( ingredients, recipe, blockName ) {
function prepareBlock( recipe, blockName ) {
var steps = [];
for( var stepName in recipe[ blockName ] ) {
steps.push( prepareStep( recipe, blockName, stepName ) );
}
return steps;
} // prepareBlock
function prepareStep( recipe, blockName, stepName ) {
var step = recipe[ blockName ][ stepName ];
var exp = ( typeof step._match == "string" ) ? step._match : step._match.source;
return {
recipe: recipe
, blockName: blockName
, stepName: stepName
, exp: "(" + exp + ")"
, length: 1 // add 1 to account for the newly added parentheses
+ (exp // count number of submatches in here
.replace( /\\./g, "%" ) // disable any escaped character
.replace( /\[.*?\]/g, "%" ) // disable any character class
.match( /\((?!\?)/g ) // match any open parenthesis, not followed by a ?
|| [] // make sure it is an empty array if there are no matches
).length // get the number of matches
, replacement: step._replace ? step._replace : book.defaultReplacement
};
} // prepareStep
function knowHow( steps ) {
var prevLength = 1;
var exps = [];
for (var i = 0; i < steps.length; i++) {
var exp = steps[ i ].exp;
// adjust backreferences
exp = exp.replace( /\\\\|\\(\d+)/g, function( m, aNum ) {
return !aNum ? m : "\\" + ( prevLength + 1 + parseInt( aNum, 10 ) );
} );
exps.push( exp );
prevLength += steps[ i ].length;
}
var prolog = '((?:\\s|\\S)*?)';
var epilog = '((?:\\s|\\S)+)';
var source = '(?:' + exps.join( "|" ) + ')';
source = prolog + source + '|' + epilog;
return new RegExp( source, recipe._case ? "g" : "gi" );
} // knowHow
function escapeHTML( str ) {
return str.replace( /&/g, "&" ).replace( /' + filter( subject ) + '';
}
return filter( subject );
}
else {
return filter( subject );
}
}
} // applyModule
function addPrefix( prefix, replacement ) {
var aux = replacement.replace( /()/ig, "$1" + prefix + "__$3" );
return aux;
} // addPrefix
function chef() {
if (! arguments[ 0 ]) {
return '';
}
var steps = this.steps;
var i = 0; // iterate steps
var j = 2; // iterate chef's arguments
var prolog = arguments[ 1 ];
var epilog = arguments[ arguments.length - 3 ];
if (! epilog) {
var step;
while( step = steps[ i++ ] ) {
var aux = arguments; // this unmasks chef's arguments inside the next function
if( aux[ j ] ) {
var replacement = '';
if( $.isFunction( step.replacement ) ) {
var matches = []; //Array.slice.call( aux, j, step.length );
for (var k = 0, kTop = step.length; k < kTop; k++) {
matches.push( aux[ j + k ] );
}
matches.push( aux[ aux.length - 2 ] );
matches.push( aux[ aux.length - 1 ] );
replacement = step.replacement
.apply( {
x: function() {
var subject = arguments[0];
var module = arguments[1];
var context = {
recipe: step.recipe
, blockName: step.blockName
};
return applyModule( subject, module, context );
}
}, matches );
}
else { //we expect step.replacement to be a string
replacement = step.replacement
.replace( /(\\\$)|(?:\$\$)|(?:\$(\d+))/g, function( m, escaped, K ) {
if( escaped ) { /* \$ */
return "$";
}
else if( !K ) { /* $$ */
return filter( aux[ j ] );
}
else if( K == "0" ) { /* $0 */
return step.stepName;
}
else { /* $K */
return filter( aux[ j + parseInt( K, 10 ) ] );
}
} );
}
replacement = addPrefix( step.recipe._name, replacement );
return filter( prolog ) + replacement;
}
else {
j+= step.length;
}
}
}
else {
return filter( epilog );
}
} // chef
if( ! blockName ) {
blockName = '_main';
checkSpices( recipe );
}
if( ! (blockName in recipe) ) {
return filter( ingredients );
}
var replaceSpace = book.replaceSpace;
var steps = prepareBlock( recipe, blockName );
var kh = knowHow( steps );
var perfect = ingredients.replace( kh, function() {
return chef.apply( { steps: steps }, arguments );
} );
return perfect;
} // cook
function loadStylesheetInline( sourceCode ) {
if( document.createElement ) {
var e = document.createElement( "style" );
e.type = "text/css";
if( e.styleSheet ) { // IE
e.styleSheet.cssText = sourceCode;
}
else {
var t = document.createTextNode( sourceCode );
e.appendChild( t );
}
document.getElementsByTagName( "head" )[0].appendChild( e );
}
} // loadStylesheetInline
function checkSpices( recipe ) {
var name = recipe._name;
if( ! book.queue[ name ] ) {
var content = ['/* Chili -- ' + name + ' */'];
for (var blockName in recipe) {
if( blockName.search( /^_(?!main\b)/ ) < 0 ) {
for (var stepName in recipe[ blockName ]) {
var step = recipe[ blockName ][ stepName ];
if( '_style' in step ) {
if( step[ '_style' ].constructor == String ) {
content.push( '.' + name + '__' + stepName + ' { ' + step[ '_style' ] + ' }' );
}
else {
for (var className in step[ '_style' ]) {
content.push( '.' + name + '__' + className + ' { ' + step[ '_style' ][ className ] + ' }' );
}
}
}
}
}
}
content = content.join('\n');
loadStylesheetInline( content );
book.queue[ name ] = true;
}
} // checkSpices
function askDish( el ) {
var recipeName = book.codeLanguage( el );
if( '' != recipeName ) {
var path = getPath( recipeName );
if( book.recipeLoading ) {
/* dynamic setups come here */
if( ! book.queue[ path ] ) {
/* this is a new recipe to download */
try {
book.queue[ path ] = [ el ];
$.getJSON( path, function( recipeLoaded ) {
book.recipes[ path ] = recipeLoaded;
var q = book.queue[ path ];
for( var i = 0, iTop = q.length; i < iTop; i++ ) {
makeDish( q[ i ], path );
}
} );
}
catch( recipeNotAvailable ) {
alert( "the recipe for '" + recipeName + "' was not found in '" + path + "'" );
}
}
else {
/* not a new recipe, so just enqueue this element */
book.queue[ path ].push( el );
}
/* a recipe could have been already downloaded */
makeDish( el, path );
}
else {
/* static setups come here */
makeDish( el, path );
}
}
} // askDish
function makeDish( el, recipePath ) {
var recipe = book.recipes[ recipePath ];
if( ! recipe ) {
return;
}
var $el = $( el );
var ingredients = $el.text();
if( ! ingredients ) {
return;
}
//fix for msie: \r (13) is used instead of \n (10)
//fix for opera: \r\n is used instead of \n
ingredients = ingredients.replace(/\r\n?/g, "\n");
//reverse fix for safari: msie, mozilla and opera render the initial \n
if( $el.parent().is('pre') ) {
if( ! $.browser.safari ) {
ingredients = ingredients.replace(/^\n/g, "");
}
}
var dish = cook( ingredients, recipe ); // all happens here
if( book.replaceTab ) {
dish = dish.replace( /\t/g, book.replaceTab );
}
if( book.replaceNewLine ) {
dish = dish.replace( /\n/g, book.replaceNewLine );
}
el.innerHTML = dish; //much faster than $el.html( dish );
//tried also the function replaceHtml from http://blog.stevenlevithan.com/archives/faster-than-innerhtml
//but it was not faster nor without sideffects (it was not possible to count spans into el)
//opera and safari select PRE text correctly
if( $.browser.msie || $.browser.mozilla ) {
enableSelectionHelper( el );
}
var $that = $el.parent();
var classes = $that.attr( 'class' );
var ln = /ln-(\d+)-([\w][\w\-]*)|ln-(\d+)|ln-/.exec( classes );
if( ln ) {
addLineNumbers( el );
var start = 0;
if( ln[1] ) {
start = parseInt( ln[1], 10 );
var $pieces = $( '.ln-' + ln[1] + '-' + ln[2] );
var pos = $pieces.index( $that[0] );
$pieces.slice( 0, pos ).each( function() {
start += $( this ).find( 'li' ).length;
} );
}
else if( ln[3] ) {
start = parseInt( ln[3], 10 );
}
else {
start = 1;
}
$el.find( 'ol' )[0].start = start;
$('body').width( $('body').width() - 1 ).width( $('body').width() + 1 );
}
else if( book.lineNumbers ) {
addLineNumbers( el );
}
} // makeDish
function enableSelectionHelper( el ) {
var element = null;
$( el )
.parents()
.filter( "pre" )
.bind( "mousedown", function() {
element = this;
if( $.browser.msie ) {
document.selection.empty();
}
else {
window.getSelection().removeAllRanges();
}
} )
.bind( "mouseup", function( event ) {
if( element && (element == this) ) {
element = null;
var selected = '';
if( $.browser.msie ) {
selected = document.selection.createRange().htmlText;
if( '' == selected ) {
return;
}
selected = preserveNewLines( selected );
var container_tag = '' : '';
var aux = '';
if( not_last_line ) {
aux = '