Subversion Repositories pub

Compare Revisions

Ignore whitespace Rev 84 → Rev 87

/wp-js-syntax-highlighter/trunk/full-js/loader.js
7,16 → 7,28
* * 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 = {
/********** 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: [],
26,34 → 38,38
 
var oldOnLoad = (null !== window.onload && 'function' == window.onload) ?
window.onload : function(){};
basePath = baseP;
 
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();
 
if (!base2 || !base2.DOM) {
dpLoader.prepareCodeBlocks_DOM_();
dp.SyntaxHighlighter.HighlightAll('syntaxhighlight', true, false);
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_();
}
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);
// 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);
}
//dp.SyntaxHighlighter.ClipboardSwf = basePath + '/clipboard.swf';
};
} // }}} // init()
,
85,74 → 101,37
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);
dp.SyntaxHighlighter.HighlightAll('syntaxhighlight', this.opts[0], this.opts[1], this.opts[2], this.opts[3], this.opts[4]);
this.times = null;
}
} // }}} // considerLoaded_()
,
/* // {{{ // prepareCodeBlocks_() Do the class -> name replacement
// to support standard SyntaxHighlighter
// {{{ // prepareCodeBlocks_() base2 implementation of _prepareCodeBlocks()
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_()
,
// {{{ // 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'];
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) {
175,11 → 154,20
}
} // }}} // 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 = basePath + '/js/shBrush' + lang + '.js';
sc.src = this.basePath + '/js/shBrush' + lang + '.js';
 
this.mustLoad[ this.mustLoad.length ] = lang;