Subversion Repositories pub

Compare Revisions

Problem with comparison.

Ignore whitespace Rev HEAD → Rev 87

/wp-js-syntax-highlighter/trunk/full-js/loader.js
0,0 → 1,178
/* $Id$
*
* 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'.
*/
 
// Global object/namespace
var dpLoader = {
/********** Stuff you might want to tweak **********/
// 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)
bloggerMode: false, // Set to true if the blogging software adds <br />'s
// See: <http://code.google.com/p/syntaxhighlighter/wiki/BloggerMode>
opts: [ // Options for HighlightAll, see <http://code.google.com/p/syntaxhighlighter/wiki/HighlightAll>
true, // Show Gutter
false, // Show Controls (!*not default*)
false, // Collapse all
1, // First line
false // Show columns
]
,
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
 
/********** Don't tweak below **********/
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(){};
 
var self = this;
 
if (this.bloggerMode) {
dp.SyntaxHighlighter.BloggerMode();
}
this.basePath = baseP;
// dp.SyntaxHighlighter.ClipboardSwf = dpLoader.basePath + '/clipboard.swf';
// Note inside of this function 'this' is 'window'
window.onload = function() {
if (!base2 || !base2.DOM) { return; }
oldOnLoad();
 
self.prepareCodeBlocks_();
self.autoLoadLanguages_();
// mustLoad contains the language modules that must be loaded,
// if it's empty there's no need to trigger syntax highlighting at all
if (0 !== self.mustLoad.length) {
self.loadStylesheet_();
}
// Wait for all needed languages to load...
// Inspired by <http://ajaxpatterns.org/On-Demand_Javascript>
for (var i=0; i<self.mustLoad.length; ++i) {
// Required translation:
var bName = self.mustLoad[i];
if (bName == 'Css') {
bName = 'CSS';
}
self.times[bName] = 0;
self.sync(bName);
}
};
} // }}} // 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', this.opts[0], this.opts[1], this.opts[2], this.opts[3], this.opts[4]);
this.times = null;
}
} // }}} // considerLoaded_()
,
// {{{ // prepareCodeBlocks_() base2 implementation of _prepareCodeBlocks()
prepareCodeBlocks_: function() {
base2.DOM.bind(document);
document.matchAll('.syntax-highlight').forEach( function(e) {
e.removeClass('syntax-highlight');
e.setAttribute('name', 'syntaxhighlight');
} );
} // }}} // prepareCodeBlocks_()
,
// {{{ // autoLoadLanguages_() Search the document and load any used languages
autoLoadLanguages_: function() {
// <http://code.google.com/p/syntaxhighlighter/wiki/Languages>
var langs = [ 'cpp', 'c',
'c-sharp', 'csharp',
'css',
'delphi', 'pascal',
'java',
'js', 'jscript', 'javascript',
'php',
'py', 'python',
'rb', 'ruby', 'rails', 'ror',
'sql',
'vb',
'xml', 'html', 'xhtml', 'xslt'
// Would create invalid or ambiguous selectors: 'c++', 'c#', 'vb.net'
];
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_()
,
// {{{ // loadStylesheet_() Loads the CSS file for SyntaxHighlighter
loadStylesheet_: function() {
var st = document.createElement('LINK');
st.type = 'text/css';
st.rel = 'stylesheet';
st.href = this.basePath + '/SyntaxHighlighter.css';
document.getElementsByTagName('HEAD')[0].appendChild( st );
} // }}} // loadStylesheet_()
,
// {{{ load(lang) // Loads dynamically the script corresponding to a language
load: function(lang) {
var sc = document.createElement('SCRIPT');
sc.type = 'text/javascript';
sc.src = this.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:
Property changes:
Added: svn:keywords
+Rev Id Date
\ No newline at end of property