2 * editor_plugin_src.js
\r
4 * Copyright 2009, Moxiecode Systems AB
\r
5 * Released under LGPL License.
\r
7 * License: http://tinymce.moxiecode.com/license
\r
8 * Contributing: http://tinymce.moxiecode.com/contributing
\r
11 /* Macgirvin Aug-2010 changed from punbb to dfrn dialect */
14 tinymce.create('tinymce.plugins.BBCodePlugin', {
\r
15 init : function(ed, url) {
\r
16 var t = this, dialect = ed.getParam('bbcode_dialect', 'dfrn').toLowerCase();
18 ed.onBeforeSetContent.add(function(ed, o) {
\r
19 o.content = t['_' + dialect + '_bbcode2html'](o.content);
\r
22 ed.onPostProcess.add(function(ed, o) {
\r
24 o.content = t['_' + dialect + '_bbcode2html'](o.content);
\r
27 o.content = t['_' + dialect + '_html2bbcode'](o.content);
\r
31 getInfo : function() {
\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
43 // HTML -> BBCode in DFRN dialect
44 _dfrn_html2bbcode : function(s) {
45 s = tinymce.trim(s);
\r
47 function rep(re, str) {
\r
48 s = s.replace(re, str);
\r
58 function _h2b_cb(match) {
60 function s_h2b(data) {
73 var f, g, tof = [], tor = [];
74 var find_spanc = /<span [^>]*class *= *[\"'](?:[^\"']* )*oembed(?: [^\"']*)*[\"'][^>]*>(.*?(?:<span[^>]*>(.*?)<\/span *>)*.*?)<\/span *>/ig;
75 while (f = find_spanc.exec(match)) {
76 var find_a = /<a([^>]* rel=[\"']oembed[\"'][^>]*)>.*?<\/a *>/ig;
77 if (g = find_a.exec(f[1])) {
78 var find_href = /href=[\"']([^\"']*)[\"']/ig;
79 var m2 = find_href.exec(g[1]);
82 tor.push("[EMBED]" + m2[1] + "[/EMBED]");
86 for (var i = 0; i < tof.length; i++) match = match.replace(tof[i], tor[i]);
91 if (s.indexOf('class="oembed')>=0){
92 //alert("request oembed html2bbcode");
99 // Preserve HTML tags inside code blocks
100 var codes = get(/<code>(.*?)<\/code>/gi);
101 rep(/<code>(.*?)<\/code>/gi,"[$!$!CODEBLOCK!$!$]");
103 // example: <strong> to [b]
\r
104 rep(/<a class=\"bookmark\" href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[bookmark=$1]$2[/bookmark]");
105 rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]");
\r
106 rep(/<span style=\"font-size:(.*?);\">(.*?)<\/span>/gi,"[size=$1]$2[/size]");
\r
107 rep(/<span style=\"color:(.*?);\">(.*?)<\/span>/gi,"[color=$1]$2[/color]");
108 rep(/<font>(.*?)<\/font>/gi,"$1");
\r
110 // Use [^>]* instead of .* to prevent a match against two separate <img> tags
111 rep(/<img[^>]*?width=\"([^>]*?)\"[^>]*?height=\"([^>]*?)\"[^>]*?src=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$1x$2]$3[/img]");
112 rep(/<img[^>]*?height=\"([^>]*?)\"[^>]*?width=\"([^>]*?)\"[^>]*?src=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$2x$1]$3[/img]");
113 rep(/<img[^>]*?src=\"([^>]*?)\"[^>]*?height=\"([^>]*?)\"[^>]*?width=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$3x$2]$1[/img]");
114 rep(/<img[^>]*?src=\"([^>]*?)\"[^>]*?width=\"([^>]*?)\"[^>]*?height=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$2x$3]$1[/img]");
115 rep(/<img[^>]*?src=\"([^>]*?)\"[^>]*?\/>/gi,"[img]$1[/img]");
117 rep(/<ul class=\"listbullet\" style=\"list-style-type\: circle\;\">(.*?)<\/ul>/gi,"[list]$1[/list]");
118 rep(/<ul class=\"listnone\" style=\"list-style-type\: none\;\">(.*?)<\/ul>/gi,"[list=]$1[/list]");
119 rep(/<ul class=\"listdecimal\" style=\"list-style-type\: decimal\;\">(.*?)<\/ul>/gi,"[list=1]$1[/list]");
120 rep(/<ul class=\"listlowerroman\" style=\"list-style-type\: lower-roman\;\">(.*?)<\/ul>/gi,"[list=i]$1[/list]");
121 rep(/<ul class=\"listupperroman\" style=\"list-style-type\: upper-roman\;\">(.*?)<\/ul>/gi,"[list=I]$1[/list]");
122 rep(/<ul class=\"listloweralpha\" style=\"list-style-type\: lower-alpha\;\">(.*?)<\/ul>/gi,"[list=a]$1[/list]");
123 rep(/<ul class=\"listupperalpha\" style=\"list-style-type\: upper-alpha\;\">(.*?)<\/ul>/gi,"[list=A]$1[/list]");
124 rep(/<li>(.*?)<\/li>/gi,'[li]$1[/li]');
126 //rep(/<code>(.*?)<\/code>/gi,"[code]$1[/code]");
127 rep(/<\/(strong|b)>/gi,"[/b]");
\r
128 rep(/<(strong|b)>/gi,"[b]");
\r
129 rep(/<\/(em|i)>/gi,"[/i]");
\r
130 rep(/<(em|i)>/gi,"[i]");
\r
131 rep(/<\/u>/gi,"[/u]");
\r
132 rep(/<span style=\"text-decoration: ?underline;\">(.*?)<\/span>/gi,"[u]$1[/u]");
\r
133 rep(/<u>/gi,"[u]");
\r
134 rep(/<blockquote[^>]*>/gi,"[quote]");
\r
135 rep(/<\/blockquote>/gi,"[/quote]");
\r
136 rep(/<hr \/>/gi,"[hr]");
137 rep(/<br (.*?)\/>/gi,"\n");
138 rep(/<br \/>/gi,"\n");
\r
139 rep(/<br>/gi,"\n");
\r
141 rep(/<\/p>/gi,"\n");
\r
142 rep(/ |\u00a0/gi," ");
\r
143 rep(/"/gi,"\"");
\r
146 rep(/&/gi,"&");
\r
148 // Hack to fix an annoying bug of TinyMCE where block formats don't
149 // work when forced_root_block = ''. So set forced_root_block = 'div'
150 // and then strip out the divs manually
155 for(var i=0; i<codes.length; i++) {
156 codes[i] = codes[i].replace("<code>","");
157 codes[i] = codes[i].replace("</code>","");
158 rep(/\[\$!\$!CODEBLOCK!\$!\$\]/i,"[code]"+codes[i]+"[/code]");
165 // BBCode -> HTML from DFRN dialect
166 _dfrn_bbcode2html : function(s) {
167 s = tinymce.trim(s);
\r
169 function rep(re, str) {
\r
170 s = s.replace(re, str);
\r
181 // Preserve HTML tags inside code blocks
182 var codes = get(/\[code\](.*?)\[\/code\]/gi);
183 rep(/\[code\](.*?)\[\/code\]/gi,"[$!$!CODEBLOCK!$!$]");
185 // example: [b] to <strong>
\r
186 rep(/\n/gi,"<br />");
\r
187 rep(/\[b\]/gi,"<strong>");
\r
188 rep(/\[\/b\]/gi,"</strong>");
\r
189 rep(/\[i\]/gi,"<em>");
\r
190 rep(/\[\/i\]/gi,"</em>");
\r
191 rep(/\[u\]/gi,"<u>");
\r
192 rep(/\[\/u\]/gi,"</u>");
\r
193 rep(/\[hr\]/gi,"<hr />");
194 rep(/\[bookmark=([^\]]+)\](.*?)\[\/bookmark\]/gi,"<a class=\"bookmark\" href=\"$1\">$2</a>");
195 rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi,"<a href=\"$1\">$2</a>");
\r
196 rep(/\[url\](.*?)\[\/url\]/gi,"<a href=\"$1\">$1</a>");
\r
197 rep(/\[img=(.*?)x(.*?)\](.*?)\[\/img\]/gi,"<img width=\"$1\" height=\"$2\" src=\"$3\" />");
198 rep(/\[img\](.*?)\[\/img\]/gi,"<img src=\"$1\" />");
\r
200 rep(/\[list\](.*?)\[\/list\]/gi, '<ul class="listbullet" style="list-style-type: circle;">$1</ul>');
201 rep(/\[list=\](.*?)\[\/list\]/gi, '<ul class="listnone" style="list-style-type: none;">$1</ul>');
202 rep(/\[list=1\](.*?)\[\/list\]/gi, '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>');
203 rep(/\[list=i\](.*?)\[\/list\]/gi,'<ul class="listlowerroman" style="list-style-type: lower-roman;">$1</ul>');
204 rep(/\[list=I\](.*?)\[\/list\]/gi, '<ul class="listupperroman" style="list-style-type: upper-roman;">$1</ul>');
205 rep(/\[list=a\](.*?)\[\/list\]/gi, '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$1</ul>');
206 rep(/\[list=A\](.*?)\[\/list\]/gi, '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$1</ul>');
207 rep(/\[li\](.*?)\[\/li\]/gi, '<li>$1</li>');
208 rep(/\[color=(.*?)\](.*?)\[\/color\]/gi,"<span style=\"color: $1;\">$2</span>");
209 rep(/\[size=(.*?)\](.*?)\[\/size\]/gi,"<span style=\"font-size: $1;\">$2</span>");
210 //rep(/\[code\](.*?)\[\/code\]/gi,"<code>$1</code>");
211 rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"<blockquote>$1</blockquote>");
214 function _b2h_cb(match, url) {
216 function s_b2h(data) {
220 url: 'oembed/b2h?url=' + url,
228 s = s.replace(/\[embed\](.*?)\[\/embed\]/gi, _b2h_cb);
233 for(var i=0; i<codes.length; i++) {
234 codes[i] = codes[i].replace("[code]","");
235 codes[i] = codes[i].replace("[/code]","");
236 rep(/\[\$!\$!CODEBLOCK!\$!\$\]/i,"<code>"+codes[i]+"</code>");
245 tinymce.PluginManager.add('bbcode', tinymce.plugins.BBCodePlugin);
\r