]> git.mxchange.org Git - friendica.git/blob - library/tinymce/jscripts/tiny_mce/plugins/bbcode/editor_plugin_src.js
9e680e104c5b4568c026abafca34d259dcdcc5ad
[friendica.git] / library / tinymce / jscripts / tiny_mce / plugins / bbcode / editor_plugin_src.js
1 /**\r
2  * editor_plugin_src.js\r
3  *\r
4  * Copyright 2009, Moxiecode Systems AB\r
5  * Released under LGPL License.\r
6  *\r
7  * License: http://tinymce.moxiecode.com/license\r
8  * Contributing: http://tinymce.moxiecode.com/contributing\r
9  */\r
10 \r
11 /* Macgirvin Aug-2010 changed from punbb to dfrn dialect */\r
12 \r
13 (function() {\r
14         tinymce.create('tinymce.plugins.BBCodePlugin', {\r
15                 init : function(ed, url) {\r
16                         var t = this, dialect = ed.getParam('bbcode_dialect', 'dfrn').toLowerCase();\r
17 \r
18                         ed.onBeforeSetContent.add(function(ed, o) {\r
19                                 o.content = t['_' + dialect + '_bbcode2html'](o.content);\r
20                         });\r
21 \r
22                         ed.onPostProcess.add(function(ed, o) {\r
23                                 if (o.set)\r
24                                         o.content = t['_' + dialect + '_bbcode2html'](o.content);\r
25 \r
26                                 if (o.get)\r
27                                         o.content = t['_' + dialect + '_html2bbcode'](o.content);\r
28                         });\r
29                 },\r
30 \r
31                 getInfo : function() {\r
32                         return {\r
33                                 longname : 'BBCode Plugin',\r
34                                 author : 'Moxiecode Systems AB',\r
35                                 authorurl : 'http://tinymce.moxiecode.com',\r
36                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/bbcode',\r
37                                 version : tinymce.majorVersion + "." + tinymce.minorVersion\r
38                         };\r
39                 },\r
40 \r
41                 // Private methods\r
42 \r
43                 // HTML -> BBCode in DFRN dialect\r
44                 _dfrn_html2bbcode : function(s) {\r
45                         s = tinymce.trim(s);\r
46 \r
47                         function rep(re, str) {\r
48                                 s = s.replace(re, str);\r
49                         };\r
50
51
52
53
54                         /* oembed */
55                         function _h2b_cb(match) {
56                                 function s_h2b(data) {
57                                                 match = data;
58                                 }\r
59                                 $.ajax({\r
60                                         type:"POST",
61                                         url: 'oembed/h2b',\r
62                                         data: {text: match},
63                                         async: false,
64                                         success: s_h2b,
65                                         dataType: 'html'
66                                 });
67                                 return match;
68                         }
69                         if (s.indexOf('class="oembed')>=0){\r
70                                 //alert("request oembed html2bbcode");\r
71                                 s = _h2b_cb(s);\r
72                         }\r
73                         
74                         /* /oembed */
75
76 \r
77                         // example: <strong> to [b]\r
78                         rep(/<a class=\"bookmark\" href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[bookmark=$1]$2[/bookmark]");\r
79                         rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]");\r
80                         rep(/<span style=\"font-size:(.*?);\">(.*?)<\/span>/gi,"[size=$1]$2[/size]");\r
81                         rep(/<span style=\"color:(.*?);\">(.*?)<\/span>/gi,"[color=$1]$2[/color]");\r
82                         rep(/<font>(.*?)<\/font>/gi,"$1");\r
83                         rep(/<img.*?width=\"(.*?)\".*?height=\"(.*?)\".*?src=\"(.*?)\".*?\/>/gi,"[img=$1x$2]$3[/img]");\r
84                         rep(/<img.*?height=\"(.*?)\".*?width=\"(.*?)\".*?src=\"(.*?)\".*?\/>/gi,"[img=$2x$1]$3[/img]");\r
85                         rep(/<img.*?src=\"(.*?)\".*?height=\"(.*?)\".*?width=\"(.*?)\".*?\/>/gi,"[img=$3x$2]$1[/img]");\r
86                         rep(/<img.*?src=\"(.*?)\".*?width=\"(.*?)\".*?height=\"(.*?)\".*?\/>/gi,"[img=$2x$3]$1[/img]");\r
87                         rep(/<img.*?src=\"(.*?)\".*?\/>/gi,"[img]$1[/img]");\r
88                         rep(/<code>(.*?)<\/code>/gi,"[code]$1[/code]");\r
89                         rep(/<\/(strong|b)>/gi,"[/b]");\r
90                         rep(/<(strong|b)>/gi,"[b]");\r
91                         rep(/<\/(em|i)>/gi,"[/i]");\r
92                         rep(/<(em|i)>/gi,"[i]");\r
93                         rep(/<\/u>/gi,"[/u]");\r
94                         rep(/<span style=\"text-decoration: ?underline;\">(.*?)<\/span>/gi,"[u]$1[/u]");\r
95                         rep(/<u>/gi,"[u]");\r
96                         rep(/<blockquote[^>]*>/gi,"[quote]");\r
97                         rep(/<\/blockquote>/gi,"[/quote]");\r
98                         rep(/<br \/>/gi,"\n\n");\r
99                         rep(/<br\/>/gi,"\n\n");\r
100                         rep(/<br>/gi,"\n");\r
101                         rep(/<p>/gi,"");\r
102                         rep(/<\/p>/gi,"\n");\r
103                         rep(/&nbsp;/gi," ");\r
104                         rep(/&quot;/gi,"\"");\r
105                         rep(/&lt;/gi,"<");\r
106                         rep(/&gt;/gi,">");\r
107                         rep(/&amp;/gi,"&");\r
108 \r
109                         return s; \r
110                 },\r
111 \r
112                 // BBCode -> HTML from DFRN dialect\r
113                 _dfrn_bbcode2html : function(s) {\r
114                         s = tinymce.trim(s);\r
115 \r
116                         function rep(re, str) {\r
117                                 s = s.replace(re, str);\r
118                         };\r
119 \r
120                         // example: [b] to <strong>\r
121                         rep(/\n/gi,"<br />");\r
122                         rep(/\[b\]/gi,"<strong>");\r
123                         rep(/\[\/b\]/gi,"</strong>");\r
124                         rep(/\[i\]/gi,"<em>");\r
125                         rep(/\[\/i\]/gi,"</em>");\r
126                         rep(/\[u\]/gi,"<u>");\r
127                         rep(/\[\/u\]/gi,"</u>");\r
128                         rep(/\[bookmark=([^\]]+)\](.*?)\[\/bookmark\]/gi,"<a class=\"bookmark\" href=\"$1\">$2</a>");\r
129                         rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi,"<a href=\"$1\">$2</a>");\r
130                         rep(/\[url\](.*?)\[\/url\]/gi,"<a href=\"$1\">$1</a>");\r
131                         rep(/\[img=(.*?)x(.*?)\](.*?)\[\/img\]/gi,"<img width=\"$1\" height=\"$2\" src=\"$3\" />");\r
132                         rep(/\[img\](.*?)\[\/img\]/gi,"<img src=\"$1\" />");\r
133                         rep(/\[color=(.*?)\](.*?)\[\/color\]/gi,"<span style=\"color: $1;\">$2</span>");\r
134                         rep(/\[size=(.*?)\](.*?)\[\/size\]/gi,"<span style=\"font-size: $1;\">$2</span>");\r
135                         rep(/\[code\](.*?)\[\/code\]/gi,"<code>$1</code>");\r
136                         rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"<blockquote>$1</blockquote>");\r
137
138                         /* oembed */
139                         function _b2h_cb(match, url) {
140                                 url = bin2hex(url);
141                                 function s_b2h(data) {
142                                                 match = data;
143                                 }
144                                 $.ajax({
145                                         url: 'oembed/b2h?url=' + url,
146                                         async: false,
147                                         success: s_b2h,
148                                         dataType: 'html'
149                                 });
150                                 return match;
151                         }
152                         s = s.replace(/\[embed\](.*?)\[\/embed\]/gi, _b2h_cb);
153                         
154                         /* /oembed */
155 \r
156                         return s; \r
157                 }\r
158         });\r
159 \r
160         // Register plugin\r
161         tinymce.PluginManager.add('bbcode', tinymce.plugins.BBCodePlugin);\r
162 })();\r