bgneal@312: /**
bgneal@312:  * editor_plugin_src.js
bgneal@312:  *
bgneal@312:  * Copyright 2009, Moxiecode Systems AB
bgneal@312:  * Released under LGPL License.
bgneal@312:  *
bgneal@312:  * License: http://tinymce.moxiecode.com/license
bgneal@312:  * Contributing: http://tinymce.moxiecode.com/contributing
bgneal@312:  */
bgneal@312: 
bgneal@312: (function() {
bgneal@312: 	tinymce.create('tinymce.plugins.BBCodePlugin', {
bgneal@312: 		init : function(ed, url) {
bgneal@312: 			var t = this, dialect = ed.getParam('bbcode_dialect', 'punbb').toLowerCase();
bgneal@312: 
bgneal@312: 			ed.onBeforeSetContent.add(function(ed, o) {
bgneal@312: 				o.content = t['_' + dialect + '_bbcode2html'](o.content);
bgneal@312: 			});
bgneal@312: 
bgneal@312: 			ed.onPostProcess.add(function(ed, o) {
bgneal@312: 				if (o.set)
bgneal@312: 					o.content = t['_' + dialect + '_bbcode2html'](o.content);
bgneal@312: 
bgneal@312: 				if (o.get)
bgneal@312: 					o.content = t['_' + dialect + '_html2bbcode'](o.content);
bgneal@312: 			});
bgneal@312: 		},
bgneal@312: 
bgneal@312: 		getInfo : function() {
bgneal@312: 			return {
bgneal@312: 				longname : 'BBCode Plugin',
bgneal@312: 				author : 'Moxiecode Systems AB',
bgneal@312: 				authorurl : 'http://tinymce.moxiecode.com',
bgneal@312: 				infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/bbcode',
bgneal@312: 				version : tinymce.majorVersion + "." + tinymce.minorVersion
bgneal@312: 			};
bgneal@312: 		},
bgneal@312: 
bgneal@312: 		// Private methods
bgneal@312: 
bgneal@312: 		// HTML -> BBCode in PunBB dialect
bgneal@312: 		_punbb_html2bbcode : function(s) {
bgneal@312: 			s = tinymce.trim(s);
bgneal@312: 
bgneal@312: 			function rep(re, str) {
bgneal@312: 				s = s.replace(re, str);
bgneal@312: 			};
bgneal@312: 
bgneal@312: 			// example: <strong> to [b]
bgneal@312: 			rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]");
bgneal@312: 			rep(/<font.*?color=\"(.*?)\".*?class=\"codeStyle\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");
bgneal@312: 			rep(/<font.*?color=\"(.*?)\".*?class=\"quoteStyle\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");
bgneal@312: 			rep(/<font.*?class=\"codeStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");
bgneal@312: 			rep(/<font.*?class=\"quoteStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");
bgneal@312: 			rep(/<span style=\"color: ?(.*?);\">(.*?)<\/span>/gi,"[color=$1]$2[/color]");
bgneal@312: 			rep(/<font.*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[color=$1]$2[/color]");
bgneal@312: 			rep(/<span style=\"font-size:(.*?);\">(.*?)<\/span>/gi,"[size=$1]$2[/size]");
bgneal@312: 			rep(/<font>(.*?)<\/font>/gi,"$1");
bgneal@312: 			rep(/<img.*?src=\"(.*?)\".*?\/>/gi,"[img]$1[/img]");
bgneal@312: 			rep(/<span class=\"codeStyle\">(.*?)<\/span>/gi,"[code]$1[/code]");
bgneal@312: 			rep(/<span class=\"quoteStyle\">(.*?)<\/span>/gi,"[quote]$1[/quote]");
bgneal@312: 			rep(/<strong class=\"codeStyle\">(.*?)<\/strong>/gi,"[code][b]$1[/b][/code]");
bgneal@312: 			rep(/<strong class=\"quoteStyle\">(.*?)<\/strong>/gi,"[quote][b]$1[/b][/quote]");
bgneal@312: 			rep(/<em class=\"codeStyle\">(.*?)<\/em>/gi,"[code][i]$1[/i][/code]");
bgneal@312: 			rep(/<em class=\"quoteStyle\">(.*?)<\/em>/gi,"[quote][i]$1[/i][/quote]");
bgneal@312: 			rep(/<u class=\"codeStyle\">(.*?)<\/u>/gi,"[code][u]$1[/u][/code]");
bgneal@312: 			rep(/<u class=\"quoteStyle\">(.*?)<\/u>/gi,"[quote][u]$1[/u][/quote]");
bgneal@312: 			rep(/<\/(strong|b)>/gi,"[/b]");
bgneal@312: 			rep(/<(strong|b)>/gi,"[b]");
bgneal@312: 			rep(/<\/(em|i)>/gi,"[/i]");
bgneal@312: 			rep(/<(em|i)>/gi,"[i]");
bgneal@312: 			rep(/<\/u>/gi,"[/u]");
bgneal@312: 			rep(/<span style=\"text-decoration: ?underline;\">(.*?)<\/span>/gi,"[u]$1[/u]");
bgneal@312: 			rep(/<u>/gi,"[u]");
bgneal@312: 			rep(/<blockquote[^>]*>/gi,"[quote]");
bgneal@312: 			rep(/<\/blockquote>/gi,"[/quote]");
bgneal@312: 			rep(/<br \/>/gi,"\n");
bgneal@312: 			rep(/<br\/>/gi,"\n");
bgneal@312: 			rep(/<br>/gi,"\n");
bgneal@312: 			rep(/<p>/gi,"");
bgneal@312: 			rep(/<\/p>/gi,"\n");
bgneal@312: 			rep(/&nbsp;/gi," ");
bgneal@312: 			rep(/&quot;/gi,"\"");
bgneal@312: 			rep(/&lt;/gi,"<");
bgneal@312: 			rep(/&gt;/gi,">");
bgneal@312: 			rep(/&amp;/gi,"&");
bgneal@312: 
bgneal@312: 			return s; 
bgneal@312: 		},
bgneal@312: 
bgneal@312: 		// BBCode -> HTML from PunBB dialect
bgneal@312: 		_punbb_bbcode2html : function(s) {
bgneal@312: 			s = tinymce.trim(s);
bgneal@312: 
bgneal@312: 			function rep(re, str) {
bgneal@312: 				s = s.replace(re, str);
bgneal@312: 			};
bgneal@312: 
bgneal@312: 			// example: [b] to <strong>
bgneal@312: 			rep(/\n/gi,"<br />");
bgneal@312: 			rep(/\[b\]/gi,"<strong>");
bgneal@312: 			rep(/\[\/b\]/gi,"</strong>");
bgneal@312: 			rep(/\[i\]/gi,"<em>");
bgneal@312: 			rep(/\[\/i\]/gi,"</em>");
bgneal@312: 			rep(/\[u\]/gi,"<u>");
bgneal@312: 			rep(/\[\/u\]/gi,"</u>");
bgneal@312: 			rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi,"<a href=\"$1\">$2</a>");
bgneal@312: 			rep(/\[url\](.*?)\[\/url\]/gi,"<a href=\"$1\">$1</a>");
bgneal@312: 			rep(/\[img\](.*?)\[\/img\]/gi,"<img src=\"$1\" />");
bgneal@312: 			rep(/\[color=(.*?)\](.*?)\[\/color\]/gi,"<font color=\"$1\">$2</font>");
bgneal@312: 			rep(/\[code\](.*?)\[\/code\]/gi,"<span class=\"codeStyle\">$1</span>&nbsp;");
bgneal@312: 			rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"<span class=\"quoteStyle\">$1</span>&nbsp;");
bgneal@312: 
bgneal@312: 			return s; 
bgneal@312: 		}
bgneal@312: 	});
bgneal@312: 
bgneal@312: 	// Register plugin
bgneal@312: 	tinymce.PluginManager.add('bbcode', tinymce.plugins.BBCodePlugin);
bgneal@312: })();