]> git.mxchange.org Git - friendica.git/blob - library/tinymce/jscripts/tiny_mce/plugins/bbcode/editor_plugin_src.js
ed200e702056b02ee3c337dd8d5ba03bc5d64212
[friendica.git] / library / tinymce / jscripts / tiny_mce / plugins / bbcode / editor_plugin_src.js
1 /**
2  * editor_plugin_src.js
3  *
4  * Copyright 2009, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://tinymce.moxiecode.com/license
8  * Contributing: http://tinymce.moxiecode.com/contributing
9  */
10
11 /* Macgirvin Aug-2010 changed from punbb to dfrn dialect */
12
13 (function() {
14         tinymce.create('tinymce.plugins.BBCodePlugin', {
15                 init : function(ed, url) {
16                         var t = this, dialect = ed.getParam('bbcode_dialect', 'dfrn').toLowerCase();
17
18                         ed.onBeforeSetContent.add(function(ed, o) {
19                                 o.content = t['_' + dialect + '_bbcode2html'](o.content);
20                         });
21
22                         ed.onPostProcess.add(function(ed, o) {
23                                 if (o.set)
24                                         o.content = t['_' + dialect + '_bbcode2html'](o.content);
25
26                                 if (o.get)
27                                         o.content = t['_' + dialect + '_html2bbcode'](o.content);
28                         });
29                 },
30
31                 getInfo : function() {
32                         return {
33                                 longname : 'BBCode Plugin',
34                                 author : 'Moxiecode Systems AB',
35                                 authorurl : 'http://tinymce.moxiecode.com',
36                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/bbcode',
37                                 version : tinymce.majorVersion + "." + tinymce.minorVersion
38                         };
39                 },
40
41                 // Private methods
42
43                 // HTML -> BBCode in DFRN dialect
44                 _dfrn_html2bbcode : function(s) {
45                         s = tinymce.trim(s);
46
47                         function rep(re, str) {
48
49
50                                 s = s.replace(re,str);
51
52                                 //modify code to keep stuff intact within [code][/code] blocks
53                                 //Waitman Gobble NO WARRANTY
54
55                                 /* This doesn't seem to work well with
56                                 [code]line1
57                                 line2[/code]
58                                 commenting out for now
59                                 */
60
61 /*
62                                 var o = new Array();
63                                 var x = s.split("[code]");
64                                 var i = 0;
65
66                                 var si = "";
67                                 si = x.shift();
68                                 si = si.replace(re,str);
69                                 o.push(si);
70
71                                 for (i = 0; i < x.length; i++) {
72                                         var no = new Array();
73                                         var j = x.shift();
74                                         var g = j.split("[/code]");
75                                         no.push(g.shift());
76                                         si = g.shift();
77                                         si = si.replace(re,str);
78                                         no.push(si);
79                                         o.push(no.join("[/code]"));
80                                 }
81
82                                 s = o.join("[code]");
83 */
84                         };
85
86
87                         function get(re) {
88                                 return s.match(re);
89                         }
90
91
92                         /* oembed */
93                         function _h2b_cb(match) {
94                                 /*
95                                 function s_h2b(data) {
96                                                 match = data;
97                                 }
98                                 $.ajax({
99                                         type:"POST",
100                                         url: 'oembed/h2b',
101                                         data: {text: match},
102                                         async: false,
103                                         success: s_h2b,
104                                         dataType: 'html'
105                                 });
106                                 */
107                                 
108                                 var f, g, tof = [], tor = [];
109                                 var find_spanc = /<span [^>]*class *= *[\"'](?:[^\"']* )*oembed(?: [^\"']*)*[\"'][^>]*>(.*?(?:<span[^>]*>(.*?)<\/span *>)*.*?)<\/span *>/ig;
110                                 while (f = find_spanc.exec(match)) {
111                                         var find_a = /<a([^>]* rel=[\"']oembed[\"'][^>]*)>.*?<\/a *>/ig;
112                                         if (g = find_a.exec(f[1])) {
113                                                 var find_href = /href=[\"']([^\"']*)[\"']/ig;
114                                                 var m2 = find_href.exec(g[1]);
115                                                 if (m2[1]) {
116                                                         tof.push(f[0]);
117                                                         tor.push("[EMBED]" + m2[1] + "[/EMBED]");
118                                                 }
119                                         }
120                                 }
121                                 for (var i = 0; i < tof.length; i++) match = match.replace(tof[i], tor[i]);
122                                 
123                                 return match;
124                         }
125
126                         if (s.indexOf('class="oembed')>=0){
127                                 //alert("request oembed html2bbcode");
128                                 s = _h2b_cb(s);
129                         }
130                         
131                         /* /oembed */
132
133
134                         // Preserve HTML tags inside code blocks
135                         var codes = get(/<code>(.*?)<\/code>/gi);
136                         rep(/<code>(.*?)<\/code>/gi,"[$!$!CODEBLOCK!$!$]");
137
138                         // example: <strong> to [b]
139                         rep(/<a class=\"bookmark\" href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[bookmark=$1]$2[/bookmark]");
140                         rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]");
141                         rep(/<span style=\"font-size:(.*?);\">(.*?)<\/span>/gi,"[size=$1]$2[/size]");
142                         rep(/<span style=\"color:(.*?);\">(.*?)<\/span>/gi,"[color=$1]$2[/color]");
143                         rep(/<font>(.*?)<\/font>/gi,"$1");
144
145                         // Use [^>]* instead of .* to prevent a match against two separate <img> tags
146                         rep(/<img[^>]*?width=\"([^>]*?)\"[^>]*?height=\"([^>]*?)\"[^>]*?src=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$1x$2]$3[/img]");
147                         rep(/<img[^>]*?height=\"([^>]*?)\"[^>]*?width=\"([^>]*?)\"[^>]*?src=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$2x$1]$3[/img]");
148                         rep(/<img[^>]*?src=\"([^>]*?)\"[^>]*?height=\"([^>]*?)\"[^>]*?width=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$3x$2]$1[/img]");
149                         rep(/<img[^>]*?src=\"([^>]*?)\"[^>]*?width=\"([^>]*?)\"[^>]*?height=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$2x$3]$1[/img]");
150                         rep(/<img[^>]*?src=\"([^>]*?)\"[^>]*?\/>/gi,"[img]$1[/img]");
151
152                         rep(/<ul class=\"listbullet\" style=\"list-style-type\: circle\;\">(.*?)<\/ul>/gi,"[list]$1[/list]");
153                         rep(/<ul class=\"listnone\" style=\"list-style-type\: none\;\">(.*?)<\/ul>/gi,"[list=]$1[/list]");
154                         rep(/<ul class=\"listdecimal\" style=\"list-style-type\: decimal\;\">(.*?)<\/ul>/gi,"[list=1]$1[/list]");
155                         rep(/<ul class=\"listlowerroman\" style=\"list-style-type\: lower-roman\;\">(.*?)<\/ul>/gi,"[list=i]$1[/list]");
156                         rep(/<ul class=\"listupperroman\" style=\"list-style-type\: upper-roman\;\">(.*?)<\/ul>/gi,"[list=I]$1[/list]");
157                         rep(/<ul class=\"listloweralpha\" style=\"list-style-type\: lower-alpha\;\">(.*?)<\/ul>/gi,"[list=a]$1[/list]");
158                         rep(/<ul class=\"listupperalpha\" style=\"list-style-type\: upper-alpha\;\">(.*?)<\/ul>/gi,"[list=A]$1[/list]");
159                         rep(/<li>(.*?)<\/li>/gi,'[li]$1[/li]');
160
161                         //rep(/<code>(.*?)<\/code>/gi,"[code]$1[/code]");
162                         rep(/<\/(strong|b)>/gi,"[/b]");
163                         rep(/<(strong|b)>/gi,"[b]");
164                         rep(/<\/(em|i)>/gi,"[/i]");
165                         rep(/<(em|i)>/gi,"[i]");
166                         rep(/<\/u>/gi,"[/u]");
167                         rep(/<span style=\"text-decoration: ?underline;\">(.*?)<\/span>/gi,"[u]$1[/u]");
168                         rep(/<u>/gi,"[u]");
169                         rep(/<blockquote[^>]*>/gi,"[quote]");
170                         rep(/<\/blockquote>/gi,"[/quote]");
171                         rep(/<hr \/>/gi,"[hr]");
172                         rep(/<br (.*?)\/>/gi,"\n");
173                         rep(/<br\/>/gi,"\n");
174                         rep(/<br>/gi,"\n");
175                         rep(/<p>/gi,"");
176                         rep(/<\/p>/gi,"\n");
177                         rep(/&nbsp;|\u00a0/gi," ");
178                         rep(/&quot;/gi,"\"");
179                         rep(/&lt;/gi,"<");
180                         rep(/&gt;/gi,">");
181                         rep(/&amp;/gi,"&");
182
183                         // Hack to fix an annoying bug of TinyMCE where block formats don't
184                         // work when forced_root_block = ''. So set forced_root_block = 'div'
185                         // and then strip out the divs manually
186                         rep(/<div>/gi,"");
187                         rep(/<\/div>/gi,"");
188
189                         if(codes != null) {                     
190                                 for(var i=0; i<codes.length; i++) {
191                                         codes[i] = codes[i].replace("<code>","");
192                                         codes[i] = codes[i].replace("</code>","");
193                                         rep(/\[\$!\$!CODEBLOCK!\$!\$\]/i,"[code]"+codes[i]+"[/code]");
194                                 }
195                         }
196
197                         return s; 
198                 },
199
200                 // BBCode -> HTML from DFRN dialect
201                 _dfrn_bbcode2html : function(s) {
202                         s = tinymce.trim(s);
203
204
205             function rep(re, str) {
206
207
208                 /*//modify code to keep stuff intact within [code][/code] blocks
209                 //Waitman Gobble NO WARRANTY
210
211
212                 var o = new Array();
213                 var x = s.split("[code]");
214                 var i = 0;
215
216                 var si = "";
217                 si = x.shift();
218                 si = si.replace(re,str);
219                 o.push(si);
220
221                 for (i = 0; i < x.length; i++) {
222                         var no = new Array();
223                         var j = x.shift();
224                         var g = j.split("[/code]");
225                         no.push(g.shift());
226                         si = g.shift();
227                         si = si.replace(re,str);
228                         no.push(si);
229                         o.push(no.join("[/code]"));
230                 }
231
232                 s = o.join("[code]");*/
233
234                                 s = s.replace(re, str);
235
236             };
237
238
239
240                         function get(re) {
241                                 return s.match(re);
242                         }
243
244
245
246                         // Preserve HTML tags inside code blocks
247                         var codes = get(/\[code\](.*?)\[\/code\]/gi);
248                         rep(/\[code\](.*?)\[\/code\]/gi,"[$!$!CODEBLOCK!$!$]");
249
250                         // example: [b] to <strong>
251                         rep(/\n/gi,"<br />");
252                         rep(/\[b\]/gi,"<strong>");
253                         rep(/\[\/b\]/gi,"</strong>");
254                         rep(/\[i\]/gi,"<em>");
255                         rep(/\[\/i\]/gi,"</em>");
256                         rep(/\[u\]/gi,"<u>");
257                         rep(/\[\/u\]/gi,"</u>");
258                         rep(/\[hr\]/gi,"<hr />");
259                         rep(/\[bookmark=([^\]]+)\](.*?)\[\/bookmark\]/gi,"<a class=\"bookmark\" href=\"$1\">$2</a>");
260                         rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi,"<a href=\"$1\">$2</a>");
261                         rep(/\[url\](.*?)\[\/url\]/gi,"<a href=\"$1\">$1</a>");
262                         rep(/\[img=(.*?)x(.*?)\](.*?)\[\/img\]/gi,"<img width=\"$1\" height=\"$2\" src=\"$3\" />");
263                         rep(/\[img\](.*?)\[\/img\]/gi,"<img src=\"$1\" />");
264
265                         rep(/\[list\](.*?)\[\/list\]/gi, '<ul class="listbullet" style="list-style-type: circle;">$1</ul>');
266                         rep(/\[list=\](.*?)\[\/list\]/gi, '<ul class="listnone" style="list-style-type: none;">$1</ul>');
267                         rep(/\[list=1\](.*?)\[\/list\]/gi, '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>');
268                         rep(/\[list=i\](.*?)\[\/list\]/gi,'<ul class="listlowerroman" style="list-style-type: lower-roman;">$1</ul>');
269                         rep(/\[list=I\](.*?)\[\/list\]/gi, '<ul class="listupperroman" style="list-style-type: upper-roman;">$1</ul>');
270                         rep(/\[list=a\](.*?)\[\/list\]/gi, '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$1</ul>');
271                         rep(/\[list=A\](.*?)\[\/list\]/gi, '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$1</ul>');
272                         rep(/\[li\](.*?)\[\/li\]/gi, '<li>$1</li>');
273                         rep(/\[color=(.*?)\](.*?)\[\/color\]/gi,"<span style=\"color: $1;\">$2</span>");
274                         rep(/\[size=(.*?)\](.*?)\[\/size\]/gi,"<span style=\"font-size: $1;\">$2</span>");
275                         //rep(/\[code\](.*?)\[\/code\]/gi,"<code>$1</code>");
276                         rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"<blockquote>$1</blockquote>");
277
278                         /* oembed */
279                         function _b2h_cb(match, url) {
280                                 url = bin2hex(url);
281                                 function s_b2h(data) {
282                                                 match = data;
283                                 }
284                                 $.ajax({
285                                         url: 'oembed/b2h?url=' + url,
286                                         async: false,
287                                         success: s_b2h,
288                                         dataType: 'html'
289                                 });
290                                 return match;
291                         }
292
293                         s = s.replace(/\[embed\](.*?)\[\/embed\]/gi, _b2h_cb);
294                         
295                         /* /oembed */
296
297                         if(codes != null) {                     
298                                 for(var i=0; i<codes.length; i++) {
299                                         codes[i] = codes[i].replace("[code]","");
300                                         codes[i] = codes[i].replace("[/code]","");
301                                         rep(/\[\$!\$!CODEBLOCK!\$!\$\]/i,"<code>"+codes[i]+"</code>");
302                                 }
303                         }
304
305                         return s; 
306                 }
307         });
308
309         // Register plugin
310         tinymce.PluginManager.add('bbcode', tinymce.plugins.BBCodePlugin);
311 })();