JQuery geshi

Wednesday, March 17th, 2010

Code highlighting using ajax with jquery and geshi

  1. $(document).ready(function(){
  2.  
  3.         // Initialise highlighting
  4.         if($('pre').length){
  5.                 $('pre').each(function(){
  6.                         highLight($(this));
  7.                 });
  8.         }
  9.  
  10. });
  11.  
  12. // Highlight item
  13. function highLight(thisItem){
  14.         snippet  = thisItem.html();
  15.         snippet = snippet.replace(/&/ig, escape('&'));
  16.         snippet = snippet.replace(/&lt;/ig, '<');
  17.         snippet = snippet.replace(/&gt;/ig, '>');
  18.         language = thisItem.attr('class');
  19.         $.ajax({
  20.                 type: "POST",
  21.                 url: '/scripts/highlight.php',
  22.                 data: 'language='+language+'&snippet='+snippet,
  23.                 dataType: "html",
  24.                 success: function(msg){
  25.                         if (msg){
  26.                                 msg = msg.replace('&amp;', '&');
  27.                                 thisItem.after(msg);
  28.                                 thisItem.hide();
  29.                         }
  30.                 }
  31.         });
  32. }

Display clean javascript code for copying

$(document).ready(function(){

	// Initialise highlighting
	if($('pre').length){
		$('pre').each(function(){
			highLight($(this));
		});
	}

});

// Highlight item
function highLight(thisItem){
	snippet  = thisItem.html();
	snippet = snippet.replace(/&amp;amp;/ig, escape('&'));
	snippet = snippet.replace(/&lt;/ig, '<');
	snippet = snippet.replace(/&gt;/ig, '>');
	language = thisItem.attr('class');
	$.ajax({
		type: "POST",
		url: '/scripts/highlight.php',
		data: 'language='+language+'&snippet='+snippet,
		dataType: "html",
		success: function(msg){
			if (msg){
				msg = msg.replace('&amp;', '&');
				thisItem.after(msg);
				thisItem.hide();
			}
		}
	});
}

Make a safe url

Wednesday, March 17th, 2010

This is a function that makes a string url safe.
Nice for creating friendly urls that are actually friendly.

  1. // Function for making sure text only uses url safe symbols
  2. function makeSafe(thisText, allowSpace){
  3.         var w = "!@#$%^&*()+=[]\\\';,./{}|\":<>?";
  4.         var s = 'abcdefghijklmnopqrstuvwxyz0123456789-_';
  5.         var x = new Array('àáâãäå', 'ç', 'èéêë', 'ìíîï', 'ñ', 'ðóòôõöø', 'ùúûü', 'ýÿ');
  6.         var r = new Array('a', 'c', 'e', 'i', 'n', 'o', 'u', 'y');
  7.  
  8.         if(allowSpace){
  9.                 s = s + ' ';
  10.         }
  11.  
  12.         thisText = thisText.toLowerCase();
  13.         var newText = new Array();
  14.  
  15.         for (i = 0; i < thisText.length; i++){
  16.                 thisChar = thisText.charAt(i);
  17.                 if(w.indexOf(thisChar) == -1){
  18.                         if(s.match(''+thisChar+'')){
  19.                                 newText[i] = thisChar;
  20.                         }else{
  21.                                 for (j = 0; j < x.length; j++){
  22.                                         if(x[j].match(thisChar)){
  23.                                                 newText[i] = r[j];
  24.                                         }
  25.                                 }
  26.                         }
  27.                 }
  28.         }
  29.  
  30.         return newText.join('');
  31. }

Display clean javascript code for copying

// Function for making sure text only uses url safe symbols
function makeSafe(thisText, allowSpace){
	var w = "!@#$%^&*()+=[]\\\';,./{}|\":<>?";
	var s = 'abcdefghijklmnopqrstuvwxyz0123456789-_';
	var x = new Array('àáâãäå', 'ç', 'èéêë', 'ìíîï', 'ñ', 'ðóòôõöø', 'ùúûü', 'ýÿ');
	var r = new Array('a', 'c', 'e', 'i', 'n', 'o', 'u', 'y');

	if(allowSpace){
		s = s + ' ';
	}

	thisText = thisText.toLowerCase();
	var newText = new Array();

	for (i = 0; i < thisText.length; i++){
		thisChar = thisText.charAt(i);
		if(w.indexOf(thisChar) == -1){
			if(s.match(''+thisChar+'')){
				newText[i] = thisChar;
			}else{
				for (j = 0; j < x.length; j++){
					if(x[j].match(thisChar)){
						newText[i] = r[j];
					}
				}
			}
		}
	}

	return newText.join('');
}

Find root folder

Wednesday, March 17th, 2010

I use this tiny snippet that uses jquery to find the root folder of my website.
It assumes jquery.js is included in the page.
It’s usefull for finding out where to post ajax stuff to (so you have a complete root).

For this website it would look in the header of the html for:
<script type="text/javascript" src="http://www.macouno.com/js/jquery.js"></script>

  1. // Get the source directory of this script
  2. root = $('script[@src$=jquery.js]').attr('src').replace('js/jquery.js', '');
  3. if(!location.href.match('www')){
  4.         root = root.replace('www.', '');
  5. }

Display clean javascript code for copying

// Get the source directory of this script
root = $('script[@src$=jquery.js]').attr('src').replace('js/jquery.js', '');
if(!location.href.match('www')){
	root = root.replace('www.', '');
}

Add to tinymce

Wednesday, March 17th, 2010

Because I will forget unless I save a copy here.
This is a very basic function for adding a string to tinymce (I use it to add images that are listed elsewhere on a page).

  1. // Adding content to the end of the tinymce editor
  2. function addToTiny(newContent){
  3.         var inst = tinyMCE;
  4.         inst.execCommand('mceFocus',false,'content');
  5.         inst.execCommand('mceInsertContent', true, newContent);
  6. }

Display clean javascript code for copying

// Adding content to the end of the tinymce editor
function addToTiny(newContent){
	var inst = tinyMCE;
	inst.execCommand('mceFocus',false,'content');
	inst.execCommand('mceInsertContent', true, newContent);
}