/*
* SyntaxHighlighter shortcode plugin
* by Andrew Ozz of Automattic
*/
// Avoid JS errors
if ( typeof syntaxHLcodes == 'undefined' ) {
var syntaxHLcodes = 'sourcecode';
}
(function() {
tinymce.create('tinymce.plugins.SyntaxHighlighterPlugin', {
init : function(ed, url) {
var t = this;
ed.onBeforeSetContent.add(function(ed, o) {
o.content = t._htmlToVisual(o.content);
});
ed.onPostProcess.add(function(ed, o) {
if ( o.save ) {
o.content = t._visualToHtml(o.content);
}
});
},
getInfo : function() {
return {
longname : 'SyntaxHighlighter Assister',
author : 'Automattic',
authorurl : 'http://wordpress.com/',
infourl : 'http://wordpress.org/extend/plugins/syntaxhighlighter/',
version : tinymce.majorVersion + "." + tinymce.minorVersion
};
},
// Private methods
_visualToHtml : function(content) {
content = tinymce.trim(content);
// 2
get converted to \n\n and are needed to preserve the next
content = content.replace(new RegExp('(
\\s*)?(\\[(' + syntaxHLcodes + ')[^\\]]*\\][\\s\\S]*?\\[\\/\\3\\])(\\s*<\\/pre>)?', 'gi'),
function(a) {
a = a.replace( /
([\t ])/g, '
<%%KEEPWHITESPACE%%>$1' );
return a + '
';
});
content = content.replace(/<\/pre>(
)?/gi, '\n');
return content;
},
_htmlToVisual : function(content) {
content = tinymce.trim(content);
content = content.replace(new RegExp('(\\s*)?(
\\s*)?(\\[(' + syntaxHLcodes + ')[^\\]]*\\][\\s\\S]*?\\[\\/\\4\\])(\\s*<\\/pre>)?(\\s*<\\/p>)?', 'gi'), '$3
');
content = content.replace(/<\/pre>/gi, '\n');
// Remove anonymous, empty paragraphs.
content = content.replace(/(\s| )*<\/p>/mg, '');
// Look for
in the [tag]s, replace with
content = content.replace(new RegExp('\\[(' + syntaxHLcodes + ')[^\\]]*\\][\\s\\S]+?\\[\\/\\1\\]', 'gi'),
function(a) {
return a.replace(/
[\r\n]*/g, '
').replace(/<\/?p( [^>]*)?>[\r\n]*/g, '
');
});
return content;
}
});
// Register plugin
tinymce.PluginManager.add('syntaxhighlighter', tinymce.plugins.SyntaxHighlighterPlugin);
})();
var syntaxHLlast = 0;
function pre_wpautop2(content) {
var d = new Date(), time = d.getTime();
if ( time - syntaxHLlast < 500 )
return content;
syntaxHLlast = time;
content = content.replace(new RegExp('
\\s*\\[(' + syntaxHLcodes + ')', 'gi'), '[$1');
content = content.replace(new RegExp('\\[\\/(' + syntaxHLcodes + ')\\]\\s*<\\/pre>', 'gi'), '[/$1]');
content = this._pre_wpautop(content);
content = content.replace(new RegExp('\\[(' + syntaxHLcodes + ')[^\\]]*\\][\\s\\S]+?\\[\\/\\1\\]', 'gi'),
function(a) {
return a.replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&').replace(/<%%KEEPWHITESPACE%%>/g, '');
});
return content;
}
function wpautop2(content) {
// js htmlspecialchars
content = content.replace(new RegExp('\\[(' + syntaxHLcodes + ')[^\\]]*\\][\\s\\S]+?\\[\\/\\1\\]', 'gi'),
function(a) {
return a.replace(/&/g, '&').replace(//g, '>');
});
return this._wpautop(content);
}
switchEditors._pre_wpautop = switchEditors.pre_wpautop;
switchEditors._wpautop = switchEditors.wpautop;
switchEditors.pre_wpautop = pre_wpautop2;
switchEditors.wpautop = wpautop2;