Subversion Repositories pub

Compare Revisions

Ignore whitespace Rev 82 → Rev 83

/wp-js-syntax-highlighter/trunk/full-js/loader.js
0,0 → 1,190
/* $Id: loader.js 547 2007-10-24 21:06:03Z $
*
* Less intrusive loading and usage of SyntaxHighlighter:
*
* Changes:
* * No "brush" (language syntax) is loaded by default
* * With standard SyntaxHighlighter code blocks must have name="whatever" (and options
* are passed as 'class="html:collapse"'). Assigning a name to a pre is not valid
* hence a workaraound is used here: code blocks must have class 'syntax-highlight'.
* * Note that without base2 language loading must be done by the user
*/
 
// Global object/namespace
var dpLoader = {
// A note on WAIT_TO_LOAD's value: I'm assuming 200ms is probably a
// valid value for most languages on broadband (2K @ 256kbps take
// less than 100ms)
WAIT_TO_LOAD: 200, // (ms) Period to wait between checks for language loading
FAIL_TIMEOUT: 120000, // (ms) After this period will stop trying to load languages
basePath: null, // Path of the WordPress plugin
mustLoad: [],
times: [],
// {{{ // init(basePath) Adds SyntaxHighlighter to the onload event
init: function(baseP) {
if (!document.getElementsByTagName) { return; }
 
var oldOnLoad = (null !== window.onload && 'function' == window.onload) ?
window.onload : function(){};
basePath = baseP;
 
window.onload = function() {
oldOnLoad();
 
if (!base2 || !base2.DOM) {
dpLoader.prepareCodeBlocks_DOM_();
dp.SyntaxHighlighter.HighlightAll('syntaxhighlight', true, false);
}
else {
dpLoader.prepareCodeBlocks_base2_();
dpLoader.autoLoadLanguages_();
// Wait for all needed languages to load...
// Inspirated by <http://ajaxpatterns.org/On-Demand_Javascript>
// mustLoad contains the language modules that must be loaded,
// if it's empty there's no need to trigger syntax highlighting at all
for (var i=0; i<dpLoader.mustLoad.length; ++i) {
// Required translation:
var bName = dpLoader.mustLoad[i];
if (bName == 'Css') {
bName = 'CSS';
}
dpLoader.times[bName] = 0;
dpLoader.sync(bName);
}
}
//dp.SyntaxHighlighter.ClipboardSwf = basePath + '/clipboard.swf';
};
} // }}} // init()
,
// {{{ // sync(brushName) Waits for required languages to load
// and then enables the syntax highlighter
sync: function(brushName) {
// var brush = eval('dp.sh.Brushes.' + brushName);
var brush = dp.sh.Brushes[brushName];
 
if (!brush) {
if (this.times[brushName] < this.FAIL_TIMEOUT) {
setTimeout('dpLoader.sync(\''+brushName+'\')', this.WAIT_TO_LOAD);
this.times[brushName] += this.WAIT_TO_LOAD;
}
else {
// Timeout: Forget about this language, don't block the others
this.considerLoaded_(brushName);
}
}
else {
this.considerLoaded_(brushName);
}
} // }}} // sync()
,
// {{{ // considerLoaded_(brushName) Remove a language-to-be-loaded from the pending list
// and trigger syntax highlighting if needed
considerLoaded_: function(brushName) {
// Remove the language from the pending list...
this.mustLoad.splice(this.mustLoad.indexOf(brushName), 1);
// ... and if it was the last, trigger syntax highlighting
if (0 === this.mustLoad.length) {
dp.SyntaxHighlighter.HighlightAll('syntaxhighlight', true, false);
this.times = null;
}
} // }}} // considerLoaded_()
,
/* // {{{ // prepareCodeBlocks_() Do the class -> name replacement
// to support standard SyntaxHighlighter
prepareCodeBlocks_: function() {
// If base2 is available use it, otherwise do the manual search
if (!base2 || !base2.DOM) {
this.prepareCodeBlocks_DOM_();
}
else {
this.prepareCodeBlocks_base2_();
}
}, */ // }}} // prepareCodeBlocks_()
// {{{ // prepareCodeBlocks_base2_() base2 implementation of _prepareCodeBlocks()
prepareCodeBlocks_base2_: function() {
base2.DOM.bind(document);
document.matchAll('.syntax-highlight').forEach( function(e) {
e.removeClass('syntax-highlight');
e.setAttribute('name', 'syntaxhighlight');
} );
} // }}} // prepareCodeBlocks_base2_()
,
// {{{ // prepareCodeBlocks_DOM_() DOM implementation of _prepareCodeBlocks_DOM()
prepareCodeBlocks_DOM_: function() {
var re = new RegExp('\\s*syntax-highlight\\s*');
function getCodeBlocks(tagName) {
var elems = document.getElementsByTagName(tagName);
 
var i; var arr = [];
for (i=0;i<elems.length;++i) {
if (re.test(elems[i].className)) {
arr[ arr.length ] = elems[i];
}
}
 
return arr;
}
var a = getCodeBlocks('PRE').concat( getCodeBlocks('TEXTAREA') );
for (var i=0;i<a.length;++i) {
var element = a[i];
 
element.setAttribute('class', element.className.replace(re, ''));
element.setAttribute('name', 'syntaxhighlight');
}
} // }}} // prepareCodeBlocks_DOM_()
,
// {{{ // autoLoadLanguages_() Search the document and load any used languages
autoLoadLanguages_: function() {
// <http://code.google.com/p/syntaxhighlighter/wiki/Languages>
var langs = [/* // Uncomment as appropriate
'cpp', 'c', 'c++',
'c#', 'c-sharp', 'csharp',
'css',
'delphi', 'pascal',
'java',
'js', 'jscript', 'javascript',
'php',
'py', 'python',
'rb', 'ruby', 'rails', 'ror',
'sql',
'vb', 'vb.net',
'xml', 'html', 'xhtml', 'xslt'
*/
'cpp', 'csharp', 'css', 'java', 'php',
'javascript', 'sql', 'html', 'xml', 'xhtml'];
for (var i=0; i<langs.length; ++i) {
var discard = document.matchSingle('*[name="syntaxhighlight"].'+langs[i]);
if (null !== discard) {
switch (langs[i]) {
case 'cpp': case 'c': case 'c++': this.load('Cpp'); break;
case 'c#': case 'c-sharp': case 'csharp': this.load('CSharp'); break;
case 'css': this.load('Css'); break;
case 'delphi': case 'pascal': this.load('Delphi'); break;
case 'java': this.load('Java'); break;
case 'js': case 'jscript': case 'javascript': this.load('JScript'); break;
case 'php': this.load('Php'); break;
case 'py': case 'python': this.load('Python'); break;
case 'rb': case 'ruby': case 'rails': case 'ror': this.load('Ruby'); break;
case 'sql': this.load('Sql'); break;
case 'vb': case 'vb.net': this.load('Vb'); break;
case 'xml': case 'html': case 'xhtml': case 'xslt': this.load('Xml'); break;
default:
}
}
}
} // }}} // autoLoadLanguages_()
,
// {{{ load(lang) // Loads dynamically the script corresponding to a language
load: function(lang) {
var sc = document.createElement('SCRIPT');
sc.type = 'text/javascript';
sc.src = basePath + '/js/shBrush' + lang + '.js';
 
this.mustLoad[ this.mustLoad.length ] = lang;
 
document.getElementsByTagName('HEAD')[0].appendChild( sc );
} // }}} // load()
};
 
// vim:set ts=4 sw=4 smarttab et filetype=javascript foldmethod=marker: