4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
11 /* Macgirvin Aug-2010 changed from punbb to dfrn dialect */
14 tinymce.create('tinymce.plugins.BBCodePlugin', {
15 init : function(ed, url) {
16 var t = this, dialect = ed.getParam('bbcode_dialect', 'dfrn').toLowerCase();
18 ed.onBeforeSetContent.add(function(ed, o) {
19 o.content = t['_' + dialect + '_bbcode2html'](o.content);
22 ed.onPostProcess.add(function(ed, o) {
24 o.content = t['_' + dialect + '_bbcode2html'](o.content);
27 o.content = t['_' + dialect + '_html2bbcode'](o.content);
31 getInfo : function() {
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
43 // HTML -> BBCode in DFRN dialect
44 _dfrn_html2bbcode : function(s) {
47 function rep(re, str) {
50 s = s.replace(re,str);
52 //modify code to keep stuff intact within [code][/code] blocks
53 //Waitman Gobble NO WARRANTY
55 /* This doesn't seem to work well with
58 commenting out for now
63 var x = s.split("[code]");
68 si = si.replace(re,str);
71 for (i = 0; i < x.length; i++) {
74 var g = j.split("[/code]");
77 si = si.replace(re,str);
79 o.push(no.join("[/code]"));
93 function _h2b_cb(match) {
95 function s_h2b(data) {
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]);
117 tor.push("[EMBED]" + m2[1] + "[/EMBED]");
121 for (var i = 0; i < tof.length; i++) match = match.replace(tof[i], tor[i]);
126 if (s.indexOf('class="oembed')>=0){
127 //alert("request oembed html2bbcode");
134 // Preserve HTML tags inside code blocks
135 var codes = get(/<code>(.*?)<\/code>/gi);
136 rep(/<code>(.*?)<\/code>/gi,"[$!$!CODEBLOCK!$!$]");
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");
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]");
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]');
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]");
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");
177 rep(/ |\u00a0/gi," ");
178 rep(/"/gi,"\"");
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
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]");
200 // BBCode -> HTML from DFRN dialect
201 _dfrn_bbcode2html : function(s) {
205 function rep(re, str) {
208 /*//modify code to keep stuff intact within [code][/code] blocks
209 //Waitman Gobble NO WARRANTY
213 var x = s.split("[code]");
218 si = si.replace(re,str);
221 for (i = 0; i < x.length; i++) {
222 var no = new Array();
224 var g = j.split("[/code]");
227 si = si.replace(re,str);
229 o.push(no.join("[/code]"));
232 s = o.join("[code]");*/
234 s = s.replace(re, str);
246 // Preserve HTML tags inside code blocks
247 var codes = get(/\[code\](.*?)\[\/code\]/gi);
248 rep(/\[code\](.*?)\[\/code\]/gi,"[$!$!CODEBLOCK!$!$]");
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\" />");
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>");
279 function _b2h_cb(match, url) {
281 function s_b2h(data) {
285 url: 'oembed/b2h?url=' + url,
293 s = s.replace(/\[embed\](.*?)\[\/embed\]/gi, _b2h_cb);
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>");
310 tinymce.PluginManager.add('bbcode', tinymce.plugins.BBCodePlugin);