Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
//
// create namespaces
//
var dp = {
sh :
{
Toolbar : {},
Utils : {},
RegexLib: {},
Brushes : {},
Strings : {
AboutDialog : '<html><head><title>About...</title></head><body
class="dp-about"><table cellspacing="0"><tr><td class="copy"><p
class="title">dp.SyntaxHighlighter</div><div class="para">Version: {V}</p><p><a
href="http://www.dreamprojections.com/syntaxhighlighter/?ref=about"
target="_blank">http://www.dreamprojections.com/syntaxhighlighter</a></p>©200
4-2007 Alex Gorbatchev.</td></tr><tr><td class="footer"><input type="button"
class="close" value="OK"
onClick="window.close()"/></td></tr></table></body></html>'
},
ClipboardSwf : null,
Version : '1.5.1'
}
};
// make an alias
dp.SyntaxHighlighter = dp.sh;
//
// Toolbar functions
//
dp.sh.Toolbar.Commands = {
ExpandSource: {
label: '+ expand source',
check: function(highlighter) { return highlighter.collapse; },
func: function(sender, highlighter)
{
sender.parentNode.removeChild(sender);
highlighter.div.className =
highlighter.div.className.replace('collapsed', '');
}
},
// opens a new windows and puts the original unformatted source code inside.
ViewSource: {
label: 'view plain',
func: function(sender, highlighter)
{
var code =
dp.sh.Utils.FixForBlogger(highlighter.originalCode).replace(/</g, '<');
var wnd = window.open('', '_blank', 'width=750, height=400,
location=0, resizable=1, menubar=0, scrollbars=0');
wnd.document.write('<textarea style="width:99%;height:99%">' +
code + '</textarea>');
wnd.document.close();
}
},
// Copies the original source code in to the clipboard. Uses either IE only
method or Flash object if ClipboardSwf is set
CopyToClipboard: {
label: 'copy to clipboard',
check: function() { return window.clipboardData != null ||
dp.sh.ClipboardSwf != null; },
func: function(sender, highlighter)
{
var code = dp.sh.Utils.FixForBlogger(highlighter.originalCode)
.replace(/</g,'<')
.replace(/>/g,'>')
.replace(/&/g,'&')
;
if(window.clipboardData)
{
window.clipboardData.setData('text', code);
}
else if(dp.sh.ClipboardSwf != null)
{
var flashcopier = highlighter.flashCopier;
if(flashcopier == null)
{
flashcopier = document.createElement('div');
highlighter.flashCopier = flashcopier;
highlighter.div.appendChild(flashcopier);
}
// creates an invisible iframe, puts the original source code inside and
prints it
PrintSource: {
label: 'print',
func: function(sender, highlighter)
{
var iframe = document.createElement('IFRAME');
var doc = null;
document.body.appendChild(iframe);
doc = iframe.contentWindow.document;
dp.sh.Utils.CopyStyles(doc, window.document);
doc.write('<div class="' +
highlighter.div.className.replace('collapsed', '') + ' printing">' +
highlighter.div.innerHTML + '</div>');
doc.close();
iframe.contentWindow.focus();
iframe.contentWindow.print();
alert('Printing...');
document.body.removeChild(iframe);
}
},
About: {
label: '?',
func: function(highlighter)
{
var wnd = window.open('', '_blank',
'dialog,width=300,height=150,scrollbars=0');
var doc = wnd.document;
dp.sh.Utils.CopyStyles(doc, window.document);
doc.write(dp.sh.Strings.AboutDialog.replace('{V}',
dp.sh.Version));
doc.close();
wnd.focus();
}
}
};
return div;
}
if(n != null)
dp.sh.Toolbar.Commands[name].func(sender, n.highlighter);
}
dp.sh.Utils.FixForBlogger = function(str)
{
return (dp.sh.isBloggerMode == true) ? str.replace(/<br\s*\/?>|
<br\s*\/?>/gi, '\n') : str;
}
//
// Common reusable regular expressions
//
dp.sh.RegexLib = {
MultiLineCComments : new RegExp('/\\*[\\s\\S]*?\\*/', 'gm'),
SingleLineCComments : new RegExp('//.*$', 'gm'),
SingleLinePerlComments : new RegExp('#.*$', 'gm'),
DoubleQuotedString : new RegExp('"(?:\\.|(\\\\\\")|[^\\""\\n])*"','g'),
SingleQuotedString : new RegExp("'(?:\\.|(\\\\\\')|[^\\''\\n])*'", 'g')
};
//
// Match object
//
dp.sh.Match = function(value, index, css)
{
this.value = value;
this.index = index;
this.length = value.length;
this.css = css;
}
//
// Highlighter object
//
dp.sh.Highlighter = function()
{
this.noGutter = false;
this.addControls = true;
this.collapse = false;
this.tabsToSpaces = true;
this.wrapColumn = 80;
this.showColumns = true;
}
dp.sh.Highlighter.prototype.CreateElement = function(name)
{
var result = document.createElement(name);
result.highlighter = this;
return result;
}
this.div.appendChild(span);
if(c == null)
continue;
return false;
}
dp.sh.Highlighter.prototype.ProcessRegexList = function()
{
for(var i = 0; i < this.regexList.length; i++)
this.GetMatches(this.regexList[i].regex, this.regexList[i].css);
}
dp.sh.Highlighter.prototype.ProcessSmartTabs = function(code)
{
var lines = code.split('\n');
var result = '';
var tabSize = 4;
var tab = '\t';
var pos = 0;
return result;
}
dp.sh.Highlighter.prototype.SwitchToList = function()
{
// thanks to Lachlan Donald from SitePoint.com for this <br/> tag fix.
var html = this.div.innerHTML.replace(/<(br)\/?>/gi, '\n');
var lines = html.split('\n');
if(this.addControls == true)
this.bar.appendChild(dp.sh.Toolbar.Create(this));
columns.className = 'columns';
columns.appendChild(div);
this.bar.appendChild(columns);
}
li.appendChild(span);
this.ol.appendChild(li);
}
this.div.innerHTML = '';
}
dp.sh.Highlighter.prototype.Highlight = function(code)
{
function Trim(str)
{
return str.replace(/^\s*(.*?)[\s\n]*$/g, '$1');
}
function RemoveBr(str)
{
return str.replace(/<br>/g,'');
}
function AddSpaces(str)
{
var value="";
var space="";
//var value=str;
for (var i=0; i < str.length; i++) {
if (space.length > 0 && str.substring(i,i+1)=="}") {
space=space.substring(0,space.length-1);
value += space;
}
value += str.substring(i,i+1);
if (i<(str.length-1) && str.substring(i+1,i+2) == "}" &&
str.substring(i,i+1) != "\r" && str.substring(i,i+1) != "\t") value += "\n\r";
if (str.substring(i,i+1) == "{") space += "\t";
//if (str.substring(i,i+1) == "}" && space.length > 1) space =
space.substring(0,space.length-1);
//if (str.substring(i+1,i+2) == "}" && space.length >1) space =
space.substring(0,space.length-1);
//if (str.substring(i,i+1) == "\r" &&
str.substring(i+1).match()
if (str.substring(i,i+1) == "\r" &&
str.substring(i+1).match(/^\s*\}/)) {
//if (space.length > 0) { space =
space.substring(0,space.length-1); value += space + "}"; i= str.indexOf("}",i)+1;}
// }
// else { space=""; value="}"; i = str.indexOf("}",i)+1;}
}
else if (str.substring(i,i+1) == "\r") value += space;
//if (str.chatAt(i) == '\r') value += Array(indent).join(" ");
}
return value;
//return str;
}
function AddReturns(str)
{
//return
str.replace(/;/g,';\n\r').replace(/<\n\r/g,'<').replace(/>\n\r/g,'>').
replace(/{\s*/g,'{\n\r').replace(/}\s*/g,'}\n\r').replace(/;\n\r\\n/g,';\\n');
return
str.replace(/</g,'<').replace(/>/g,'>').replace(/;/g,';\n\r').replace(/{\s*/
g,'{\n\r').replace(/}\s*/g,'}\n\r').replace(/;\n\r\\n/g,';\\n').replace(/for\s*\((
[^;]+);\n\r([^;\n\r]+);\n\r/g,'for ($1;
$2;').replace(/</g,'<').replace(/>/g,'>');
}
function Chop(str)
{
return str.replace(/\n*$/, '').replace(/^\n*/, '');
}
function Unindent(str)
{
var lines = dp.sh.Utils.FixForBlogger(str).split('\n');
var indents = new Array();
var regex = new RegExp('^\\s*', 'g');
var min = 1000;
// trim minimum common number of white space from the begining of every
line
if(min > 0)
for(var i = 0; i < lines.length; i++)
lines[i] = lines[i].substr(min);
return lines.join('\n');
}
var pos = 0;
if(code == null)
code = '';
// remove <br>
code = RemoveBr(code);
code = AddReturns(code);
if (this.addSpaces) {
code = AddSpaces(code);
}
this.originalCode = code;
this.code = Chop(Unindent(code));
this.div = this.CreateElement('DIV');
this.bar = this.CreateElement('DIV');
this.ol = this.CreateElement('OL');
this.matches = new Array();
this.div.className = 'dp-highlighter';
this.div.highlighter = this;
this.bar.className = 'bar';
if(this.CssClass != null)
this.ol.className = this.CssClass;
if(this.collapse)
this.div.className += ' collapsed';
if(this.noGutter)
this.div.className += ' nogutter';
this.ProcessRegexList();
// The following loop checks to see if any of the matches are inside
// of other matches. This process would get rid of highligted strings
// inside comments, keywords inside strings and so on.
for(var i = 0; i < this.matches.length; i++)
if(this.IsInside(this.matches[i]))
this.matches[i] = null;
// Finally, go through the final list of matches and pull the all
// together adding everything in between that isn't a match.
for(var i = 0; i < this.matches.length; i++)
{
var match = this.matches[i];
if(match == null || match.length == 0)
continue;
this.AddBit(this.code.substr(pos), null);
this.SwitchToList();
this.div.appendChild(this.bar);
this.div.appendChild(this.ol);
}
dp.sh.Highlighter.prototype.GetKeywords = function(str)
{
return '\\b' + str.replace(/ /g, '\\b|\\b') + '\\b';
}
dp.sh.BloggerMode = function()
{
dp.sh.isBloggerMode = true;
}
// highlightes all elements identified by name and gets source code from specified
property
dp.sh.HighlightAll = function(name, showGutter /* optional */, showControls /*
optional */, collapseAll /* optional */, firstLine /* optional */, showColumns /*
optional */)
{
function FindValue()
{
var a = arguments;
return null;
}
return false;
}
return defaultValue;
}
// for some reason IE doesn't find <pre/> by name, however it does see them
just fine by tag name...
FindTagsByName(elements, name, 'pre');
FindTagsByName(elements, name, 'textarea');
if(elements.length == 0)
return;
if(aliases == null)
continue;
if(options == null)
continue;
options = options.split(':');
language = options[0].toLowerCase();
if(registered[language] == null)
continue;
// instantiate a brush
highlighter = new dp.sh.Brushes[registered[language]]();
if(styleNode.styleSheet) // for IE
{
styleNode.styleSheet.cssText = highlighter.Style;
}
else // for everyone else
{
var textNode = document.createTextNode(highlighter.Style);
styleNode.appendChild(textNode);
}
headNode.appendChild(styleNode);
}
highlighter.Highlight(element[propertyName]);
highlighter.source = element;
element.parentNode.insertBefore(highlighter.div, element);
}
}