/**
* plugin.js
*
* Copyright, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/*global tinymce:true */
(function() {
tinymce.create('tinymce.plugins.BBCodePlugin', {
init : function(ed) {
var t = this, dialect = ed.getParam('bbcode_dialect', 'punbb').toLowerCase();
ed.on('beforeSetContent', function(e) {
e.content = t['_' + dialect + '_bbcode2html'](e.content);
});
ed.on('postProcess', function(e) {
if (e.set) {
e.content = t['_' + dialect + '_bbcode2html'](e.content);
}
if (e.get) {
e.content = t['_' + dialect + '_html2bbcode'](e.content);
}
});
},
getInfo: function() {
return {
longname: 'BBCode Plugin',
author: 'Moxiecode Systems AB',
authorurl: 'http://www.tinymce.com',
infourl: 'http://www.tinymce.com/wiki.php/Plugin:bbcode'
};
},
// Private methods
// HTML -> BBCode in PunBB dialect
_punbb_html2bbcode : function(s) {
s = tinymce.trim(s);
function rep(re, str) {
s = s.replace(re, str);
}
// example: to [b]
rep(/ /gi,"");
rep(/<\/p>/gi,"\n");
rep(/ |\u00a0/gi," ");
rep(/"/gi,"\"");
rep(/</gi,"<");
rep(/>/gi,">");
rep(/&/gi,"&");
return s;
},
// BBCode -> HTML from PunBB dialect
_punbb_bbcode2html : function(s) {
s = tinymce.trim(s);
function rep(re, str) {
s = s.replace(re, str);
}
// example: [b] to
rep(/\n/gi,"]*>/gi,"[quote]");
rep(/<\/blockquote>/gi,"[/quote]");
rep(/
/gi,"\n");
rep(/
/gi,"\n");
rep(/
/gi,"\n");
rep(/
");
rep(/\[b\]/gi,"");
rep(/\[\/b\]/gi,"");
rep(/\[i\]/gi,"");
rep(/\[\/i\]/gi,"");
rep(/\[u\]/gi,"");
rep(/\[\/u\]/gi,"");
rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi,"$2");
rep(/\[url\](.*?)\[\/url\]/gi,"$1");
rep(/\[img\](.*?)\[\/img\]/gi,"");
rep(/\[color=(.*?)\](.*?)\[\/color\]/gi,"$2");
rep(/\[code\](.*?)\[\/code\]/gi,"$1 ");
rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"$1 ");
return s;
}
});
// Register plugin
tinymce.PluginManager.add('bbcode', tinymce.plugins.BBCodePlugin);
})();