includes/clientside/tinymce/plugins/bbcode/editor_plugin_src.js
changeset 335 67bd3121a12e
parent 1 fe660c52c48f
child 395 fa4c5ecb7c9a
equal deleted inserted replaced
334:c72b545f1304 335:67bd3121a12e
     1 var TinyMCE_BBCodePlugin = {
     1 /**
     2 	getInfo : function() {
     2  * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
     3 		return {
     3  *
     4 			longname : 'BBCode Plugin',
     4  * @author Moxiecode
     5 			author : 'Moxiecode Systems AB',
     5  * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
     6 			authorurl : 'http://tinymce.moxiecode.com',
     6  */
     7 			infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/bbcode',
       
     8 			version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
       
     9 		};
       
    10 	},
       
    11 
     7 
    12 	cleanup : function(type, content) {
     8 (function() {
    13 		var dialect = tinyMCE.getParam('bbcode_dialect', 'punbb').toLowerCase();
     9 	tinymce.create('tinymce.plugins.BBCodePlugin', {
       
    10 		init : function(ed, url) {
       
    11 			var t = this, dialect = ed.getParam('bbcode_dialect', 'punbb').toLowerCase();
    14 
    12 
    15 		switch (type) {
    13 			ed.onBeforeSetContent.add(function(ed, o) {
    16 			case "insert_to_editor":
    14 				o.content = t['_' + dialect + '_bbcode2html'](o.content);
    17 				content = this['_' + dialect + '_bbcode2html'](content);
    15 			});
    18 				break;
       
    19 
    16 
    20 			case "get_from_editor":
    17 			ed.onPostProcess.add(function(ed, o) {
    21 				content = this['_' + dialect + '_html2bbcode'](content);
    18 				if (o.set)
    22 				break;
    19 					o.content = t['_' + dialect + '_bbcode2html'](o.content);
       
    20 
       
    21 				if (o.get)
       
    22 					o.content = t['_' + dialect + '_html2bbcode'](o.content);
       
    23 			});
       
    24 		},
       
    25 
       
    26 		getInfo : function() {
       
    27 			return {
       
    28 				longname : 'BBCode Plugin',
       
    29 				author : 'Moxiecode Systems AB',
       
    30 				authorurl : 'http://tinymce.moxiecode.com',
       
    31 				infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/bbcode',
       
    32 				version : tinymce.majorVersion + "." + tinymce.minorVersion
       
    33 			};
       
    34 		},
       
    35 
       
    36 		// Private methods
       
    37 
       
    38 		// HTML -> BBCode in PunBB dialect
       
    39 		_punbb_html2bbcode : function(s) {
       
    40 			s = tinymce.trim(s);
       
    41 
       
    42 			function rep(re, str) {
       
    43 				s = s.replace(re, str);
       
    44 			};
       
    45 
       
    46 			// example: <strong> to [b]
       
    47 			rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]");
       
    48 			rep(/<font.*?color=\"(.*?)\".*?class=\"codeStyle\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");
       
    49 			rep(/<font.*?color=\"(.*?)\".*?class=\"quoteStyle\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");
       
    50 			rep(/<font.*?class=\"codeStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");
       
    51 			rep(/<font.*?class=\"quoteStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");
       
    52 			rep(/<span style=\"color: ?(.*?);\">(.*?)<\/span>/gi,"[color=$1]$2[/color]");
       
    53 			rep(/<font.*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[color=$1]$2[/color]");
       
    54 			rep(/<span style=\"font-size:(.*?);\">(.*?)<\/span>/gi,"[size=$1]$2[/size]");
       
    55 			rep(/<font>(.*?)<\/font>/gi,"$1");
       
    56 			rep(/<img.*?src=\"(.*?)\".*?\/>/gi,"[img]$1[/img]");
       
    57 			rep(/<span class=\"codeStyle\">(.*?)<\/span>/gi,"[code]$1[/code]");
       
    58 			rep(/<span class=\"quoteStyle\">(.*?)<\/span>/gi,"[quote]$1[/quote]");
       
    59 			rep(/<strong class=\"codeStyle\">(.*?)<\/strong>/gi,"[code][b]$1[/b][/code]");
       
    60 			rep(/<strong class=\"quoteStyle\">(.*?)<\/strong>/gi,"[quote][b]$1[/b][/quote]");
       
    61 			rep(/<em class=\"codeStyle\">(.*?)<\/em>/gi,"[code][i]$1[/i][/code]");
       
    62 			rep(/<em class=\"quoteStyle\">(.*?)<\/em>/gi,"[quote][i]$1[/i][/quote]");
       
    63 			rep(/<u class=\"codeStyle\">(.*?)<\/u>/gi,"[code][u]$1[/u][/code]");
       
    64 			rep(/<u class=\"quoteStyle\">(.*?)<\/u>/gi,"[quote][u]$1[/u][/quote]");
       
    65 			rep(/<\/(strong|b)>/gi,"[/b]");
       
    66 			rep(/<(strong|b)>/gi,"[b]");
       
    67 			rep(/<\/(em|i)>/gi,"[/i]");
       
    68 			rep(/<(em|i)>/gi,"[i]");
       
    69 			rep(/<\/u>/gi,"[/u]");
       
    70 			rep(/<span style=\"text-decoration: ?underline;\">(.*?)<\/span>/gi,"[u]$1[/u]");
       
    71 			rep(/<u>/gi,"[u]");
       
    72 			rep(/<br \/>/gi,"\n");
       
    73 			rep(/<br\/>/gi,"\n");
       
    74 			rep(/<br>/gi,"\n");
       
    75 			rep(/<p>/gi,"");
       
    76 			rep(/<\/p>/gi,"\n");
       
    77 			rep(/&nbsp;/gi," ");
       
    78 			rep(/&quot;/gi,"\"");
       
    79 			rep(/&lt;/gi,"<");
       
    80 			rep(/&gt;/gi,">");
       
    81 			rep(/&amp;/gi,"&");
       
    82 
       
    83 			return s; 
       
    84 		},
       
    85 
       
    86 		// BBCode -> HTML from PunBB dialect
       
    87 		_punbb_bbcode2html : function(s) {
       
    88 			s = tinymce.trim(s);
       
    89 
       
    90 			function rep(re, str) {
       
    91 				s = s.replace(re, str);
       
    92 			};
       
    93 
       
    94 			// example: [b] to <strong>
       
    95 			rep(/\n/gi,"<br />");
       
    96 			rep(/\[b\]/gi,"<strong>");
       
    97 			rep(/\[\/b\]/gi,"</strong>");
       
    98 			rep(/\[i\]/gi,"<em>");
       
    99 			rep(/\[\/i\]/gi,"</em>");
       
   100 			rep(/\[u\]/gi,"<u>");
       
   101 			rep(/\[\/u\]/gi,"</u>");
       
   102 			rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi,"<a href=\"$1\">$2</a>");
       
   103 			rep(/\[url\](.*?)\[\/url\]/gi,"<a href=\"$1\">$1</a>");
       
   104 			rep(/\[img\](.*?)\[\/img\]/gi,"<img src=\"$1\" />");
       
   105 			rep(/\[color=(.*?)\](.*?)\[\/color\]/gi,"<font color=\"$1\">$2</font>");
       
   106 			rep(/\[code\](.*?)\[\/code\]/gi,"<span class=\"codeStyle\">$1</span>&nbsp;");
       
   107 			rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"<span class=\"quoteStyle\">$1</span>&nbsp;");
       
   108 
       
   109 			return s; 
    23 		}
   110 		}
       
   111 	});
    24 
   112 
    25 		return content;
   113 	// Register plugin
    26 	},
   114 	tinymce.PluginManager.add('bbcode', tinymce.plugins.BBCodePlugin);
    27 
   115 })();
    28 	// Private methods
       
    29 
       
    30 	// HTML -> BBCode in PunBB dialect
       
    31 	_punbb_html2bbcode : function(s) {
       
    32 		s = tinyMCE.trim(s);
       
    33 
       
    34 		function rep(re, str) {
       
    35 			s = s.replace(re, str);
       
    36 		};
       
    37 
       
    38 		// example: <strong> to [b]
       
    39 		rep(/<a href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url]$1[/url]");
       
    40 		rep(/<font.*?color=\"(.*?)\".*?class=\"codeStyle\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");
       
    41 		rep(/<font.*?color=\"(.*?)\".*?class=\"quoteStyle\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");
       
    42 		rep(/<font.*?class=\"codeStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");
       
    43 		rep(/<font.*?class=\"quoteStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");
       
    44 		rep(/<font.*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[color=$1]$2[/color]");
       
    45 		rep(/<font>(.*?)<\/font>/gi,"$1");
       
    46 		rep(/<img.*?src=\"(.*?)\".*?\/>/gi,"[img]$1[/img]");
       
    47 		rep(/<span class=\"codeStyle\">(.*?)<\/span>/gi,"[code]$1[/code]");
       
    48 		rep(/<span class=\"quoteStyle\">(.*?)<\/span>/gi,"[quote]$1[/quote]");
       
    49 		rep(/<strong class=\"codeStyle\">(.*?)<\/strong>/gi,"[code][b]$1[/b][/code]");
       
    50 		rep(/<strong class=\"quoteStyle\">(.*?)<\/strong>/gi,"[quote][b]$1[/b][/quote]");
       
    51 		rep(/<em class=\"codeStyle\">(.*?)<\/em>/gi,"[code][i]$1[/i][/code]");
       
    52 		rep(/<em class=\"quoteStyle\">(.*?)<\/em>/gi,"[quote][i]$1[/i][/quote]");
       
    53 		rep(/<u class=\"codeStyle\">(.*?)<\/u>/gi,"[code][u]$1[/u][/code]");
       
    54 		rep(/<u class=\"quoteStyle\">(.*?)<\/u>/gi,"[quote][u]$1[/u][/quote]");
       
    55 		rep(/<\/(strong|b)>/gi,"[/b]");
       
    56 		rep(/<(strong|b)>/gi,"[b]");
       
    57 		rep(/<\/(em|i)>/gi,"[/i]");
       
    58 		rep(/<(em|i)>/gi,"[i]");
       
    59 		rep(/<\/u>/gi,"[/u]");
       
    60 		rep(/<u>/gi,"[u]");
       
    61 		rep(/<br \/>/gi,"\n");
       
    62 		rep(/<br\/>/gi,"\n");
       
    63 		rep(/<br>/gi,"\n");
       
    64 		rep(/<p>/gi,"");
       
    65 		rep(/<\/p>/gi,"\n");
       
    66 		rep(/&nbsp;/gi," ");
       
    67 		rep(/&quot;/gi,"\"");
       
    68 		rep(/&lt;/gi,"<");
       
    69 		rep(/&gt;/gi,">");
       
    70 		rep(/&amp;/gi,"&");
       
    71 		rep(/&undefined;/gi,"'"); // quickfix
       
    72 
       
    73 		return s; 
       
    74 	},
       
    75 
       
    76 	// BBCode -> HTML from PunBB dialect
       
    77 	_punbb_bbcode2html : function(s) {
       
    78 		s = tinyMCE.trim(s);
       
    79 
       
    80 		function rep(re, str) {
       
    81 			s = s.replace(re, str);
       
    82 		};
       
    83 
       
    84 		// example: [b] to <strong>
       
    85 		rep(/\n/gi,"<br />");
       
    86 		rep(/\[b\]/gi,"<strong>");
       
    87 		rep(/\[\/b\]/gi,"</strong>");
       
    88 		rep(/\[i\]/gi,"<em>");
       
    89 		rep(/\[\/i\]/gi,"</em>");
       
    90 		rep(/\[u\]/gi,"<u>");
       
    91 		rep(/\[\/u\]/gi,"</u>");
       
    92 		rep(/\[url\](.*?)\[\/url\]/gi,"<a href=\"$1\">$1</a>");
       
    93 		rep(/\[img\](.*?)\[\/img\]/gi,"<img src=\"$1\" />");
       
    94 		rep(/\[color=(.*?)\](.*?)\[\/color\]/gi,"<font color=\"$1\">$2</font>");
       
    95 		rep(/\[code\](.*?)\[\/code\]/gi,"<span class=\"codeStyle\">$1</span>&nbsp;");
       
    96 		rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"<span class=\"quoteStyle\">$1</span>&nbsp;");
       
    97 
       
    98 		return s; 
       
    99 	}
       
   100 };
       
   101 
       
   102 tinyMCE.addPlugin("bbcode", TinyMCE_BBCodePlugin);