Syntaxalator
Syntaxalator allows you to highlight JavaScript code snippits on a web page. Just include syntaxalator.js on your page, and add a class='syntaxalator' to whatever HTML element you want highligthed.This is kind of an ongoing project, but you can check out the latest code over at the syntaxalator.js GitHub page.
Customizing the colors and styles is easy to do in the source file.
Below is some example JavaScript in <pre> tags, one with styling and one without.
/* This is an improved rounding function that allows for specifying decimal place precision. */ function round(num, places){ // Set up variables num = num || 0; num += ''; places = parseInt(places) || 3; var splitarr = num.split('.'); if(splitarr.length === 1) return num*1; var left = splitarr[0] *= 1; var right = splitarr[1] += ''; // Check for decimal = 0 if(right*1 === 0) return left; // Turn decimal into a whole number // 'places' long plus one decimal point if(right.length > places){ var decdec = right.substring(0,places); decdec += '.'; decdec += right.substring(places); right = '' + Math.round(decdec*1); if(right*1 === 0) { // decimal places were rounded down to 0 return left; } else if (right.length > places){ // decimal places were rounded up, carry the 1 return (left + 1); } else { // rounded somewhere in between, pad zeros while(right.length < places) right = '0'+right; } } return ('' + left + '.' + right) * 1; }
And here is that same code with syntaxalator.js applied:
/*
This is an improved rounding function
that allows for specifying decimal
place precision.
*/
function round(num, places){
// Set up variables
num = num || 0;
num += '';
places = parseInt(places) || 3;
var splitarr = num.split('.');
if(splitarr.length === 1) return num*1;
var left = splitarr[0] *= 1;
var right = splitarr[1] += '';
// Check for decimal = 0
if(right*1 === 0) return left;
// Turn decimal into a whole number
// 'places' long plus one decimal point
if(right.length > places){
var decdec = right.substring(0,places);
decdec += '.';
decdec += right.substring(places);
right = '' + Math.round(decdec*1);
if(right*1 === 0) {
// decimal places were rounded down to 0
return left;
} else if (right.length > places){
// decimal places were rounded up, carry the 1
return (left + 1);
} else {
// rounded somewhere in between, pad zeros
while(right.length < places) right = '0'+right;
}
}
return ('' + left + '.' + right) * 1;
}