0,0 → 1,170 |
/* $Id$ |
* |
* Less intrusive loading and usage of SyntaxHighlighter. |
* REQUIRES jQuery. |
* |
* To define a block of syntax highlighted code use the same method as in |
* plain SyntaxHighlighter, i.e.: |
* <pre class="brush: css"> |
* body { |
* color:black; |
* } |
* </pre> |
* |
* When shLoader is initialised it will look for the "brush:" class and load |
* the appropriate shBrush. |
* |
* Initialisation: |
* shLoader.init() // Only if located at the root of the server |
* shLoader.init({path: '/mydir/wp-content/plugins/js-syntax-highlighter'}) |
* |
* A theme can be set either from the initialisation or later on, as long |
* as shLoader.load() hasn't been triggered (will be run when a DOMReady event |
* is triggered). |
* E.g.: |
* shLoader.init({path:'/mypath/...', theme:'Django'}); |
* OR |
* shLoader.theme = 'Django'; |
*/ |
|
if ('undefined' == typeof(jQuery)) { // Autload jQuery |
var s = document.createElement('SCRIPT'); |
s.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'; |
s.type = 'text/javascript'; |
document.getElementsByTagName('HEAD')[0].appendChild(s); |
} |
|
// Global object |
var shLoader = { |
/** {{{ // init() Schedule shLoader to initialise on DOMReady |
* init([path = '/wp-content/plugins/js-syntax-highlighter'], [initObj]) |
* initObject, if provided, can have the fields: |
* path |
* theme |
* configObj |
* defaultsObj |
* Use of other fields may produce unexpected behaviour |
*/ |
init: function(initObj) { |
if (initObj) { |
for (var p in initObj) { |
this[p] = initObj[p]; |
} |
} |
$(document).ready(function() { shLoader.load(); }); |
}, // }}} // init() |
load: function() { // {{{ |
if (!jQuery) { return; } |
var cssPath = this.path+'/sh/styles'; |
var jsPath = this.path+'/sh/scripts'; |
|
// SyntaxHighlighter has now its own autoloader BUT it must |
// receive a list of possible languages. |
// When used in blogs it might mean having to enable autoloading of all |
// possible languages. I'm unsure of the penalty of such approach but |
// I prefer detection. |
this.detectLanguages(); |
|
if (0 == this.loadURLs.length) { // No need to load sh |
return; |
} |
|
// Load the CSS |
$('<link rel="stylesheet" href="'+cssPath+'/shCore.css" type="text/css">').appendTo($('head')); |
$('<link rel="stylesheet" href="'+cssPath+'/shTheme'+this.theme+'.css" type="text/css">').appendTo($('head')); |
// Load sh Core |
$.ajax({ // Like jQuery.getScript() but in synchronous mode |
url: jsPath+'/shCore.js', |
dataType: 'script', |
async: false |
}); |
|
$(this.loadURLs).each(function (i,e) { |
$.ajax({ |
url: e, |
dataType: 'script', |
async: false, |
}); |
}); |
|
if ('undefined' == typeof(SyntaxHighlighter)) { |
// Something failed... |
return; |
} |
|
// Set any configuration and defaults |
for (var prop in this.configObj) { |
SyntaxHighlighter.config[prop] = this.configObj[prop]; |
} |
for (var prop in this.defaultsObj) { |
SyntaxHighlighter.defaults[prop] = this.defaultsObj[prop]; |
} |
|
// FIXME: It won't load sometimes. Why? |
SyntaxHighlighter.all(); // Actually apply syntax highlighting |
}, // }}} // load() |
/** {{{ // setDefaults() // Sets the defaults object of SyntaxHighlighter |
* Note the defaults won't be applied until init() is called and SyntaxHighlighter |
* loaded. |
* Successive calls will overwrite any previously set defaults, to "add" defaults |
* first use the getDefaults() method, e.g.: |
* shLoader.setDefaults( { auto-links:true } ); |
* var o = shLoader.getDefaults(); |
* o['class-name'] = 'someClass'; |
* shLoader.setDefaults(o); |
*/ |
setDefaults: function(obj) { |
this.defaultsObj = obj; |
}, // }}} // setDefaults |
/** {{{ // setConfig() // Sets the config object of SyntaxHighlighter |
* See the setDefaults() notes, everything there applies here too. |
*/ |
setConfig: function(obj) { |
this.configObj = obj; |
}, // }}} // setConfig |
detectLanguages: function() { // {{{ // Find any language used in the page |
var detected = []; |
var re = /brush: (\w+)/i; |
$('.brush\\:').each(function() { |
var cn = $(this).attr('class'); |
var lang = cn.match(re)[1]; |
shLoader.reqLoad(lang); |
}); |
}, // }}} // detectLanguages() |
reqLoad: function(lang) { // {{{ // Request Load |
// <http://code.google.com/p/syntaxhighlighter/wiki/Languages> |
// XXX: [left over from v0.2] Would create invalid or ambiguous selectors: 'c++', 'c#', 'vb.net' |
var l = null; |
switch (lang) { // Map language to brush |
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) { |
if (!this.loaded[l]) { |
this.loadURLs[this.loadURLs.length] = this.path+'/sh/scripts/shBrush'+l+'.js'; |
this.loaded[l] = true; |
} |
} |
}, // }}} // reqLoad() |
getDefaults: function() { return this.defaultsObj; }, |
getConfig: function() { return this.configObj; }, |
theme: 'Default',// sh Theme to use (files named shTheme*.css) |
path: '/wp-content/plugins/js-syntax-highlighter', // Base path of the plugin |
loaded: [], // List of requested languages [lang => bool,...] |
loadURLs: [], // List of languages (brushes) URLs to load |
configObj: {}, // Syntax Highlighter configutation (see setConfig()) |
defaultsObj: {}, // Syntax Highlighter defaults (see setDefaults()) |
}; |
|
// 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 |