0,0 → 1,171 |
/* $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 (!jQuery) { return; } |
|
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 'document' |
$(document).ready(function() { |
if (!dp || !dp.SyntaxHighlighter) { return; } |
|
self.internal.prepareCodeBlocks(); |
self.internal.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.internal.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_() |
, |
internal: { // {{{ // internal{} |
prepareCodeBlocks: function() { |
$('.syntax-highlight').each(function() { |
$(this).removeClass('syntax-highlight'); |
$(this).attr('name', 'syntaxhighlight'); |
elem = $(this).get(0); |
}); |
}, // 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' |
]; |
self = dpLoader; |
$(langs).each(function(i,e) { |
var discard = $('*[name="syntaxhighlight"].'+e); |
l = null; |
if (0 !== discard.length) { |
switch (e) { |
case 'cpp': case 'c': case 'c++': l='Cpp'; break; |
case 'c#': case 'c-sharp': case 'csharp': l='CSharp'; break; |
case 'css': l='Css'; break; |
case 'delphi': case 'pascal': l='Delphi'; break; |
case 'java': l='Java'; break; |
case 'js': case 'jscript': case 'javascript': l='JScript'; break; |
case 'php': l='Php'; break; |
case 'py': case 'python': l='Python'; break; |
case 'rb': case 'ruby': case 'rails': case 'ror': l='Ruby'; break; |
case 'sql': l='Sql'; break; |
case 'vb': case 'vb.net': l='Vb'; break; |
case 'xml': case 'html': case 'xhtml': case 'xslt': l='Xml'; break; |
default: |
} |
} |
if (l) { self.load(l); } |
}); |
}, // autoLoadLanguages() |
loadStylesheet: function() { |
var href = dpLoader.basePath + '/SyntaxHighlighter.css'; |
$('head').append($('<link type="text/css" rel="stylesheet" href="'+href+'">')); |
} // loadStylesheet() |
} // }}} // internal{} |
, |
// {{{ load(lang) // Loads dynamically the script corresponding to a language |
load: function(lang) { |
var s = this.basePath + '/js/shBrush' + lang + '.js'; |
$('head').append($('<script type="text/javascript" src="'+s+'">')); |
// Alternative: <http://colourgray.wordpress.com/2008/09/22/jquery-loading-external-javascript-files-using-getscript/> |
//$.ajaxSetup({async: false}); |
//$.getScript(s); |
//$.ajaxSetup({async: true}); |
this.mustLoad[ this.mustLoad.length ] = lang; |
} // }}} // 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 |