friendica/library/tinymce/jscripts/tiny_mce/plugins/bbcode/editor_plugin.js

1 line
6.0 KiB
JavaScript

(function(){tinymce.create("tinymce.plugins.BBCodePlugin",{init:function(ed,url){var t=this,dialect=ed.getParam("bbcode_dialect","dfrn").toLowerCase();ed.onBeforeSetContent.add(function(ed,o){o.content=t["_"+dialect+"_bbcode2html"](o.content)});ed.onPostProcess.add(function(ed,o){if(o.set)o.content=t["_"+dialect+"_bbcode2html"](o.content);if(o.get)o.content=t["_"+dialect+"_html2bbcode"](o.content)})},getInfo:function(){return{longname:"BBCode Plugin",author:"Moxiecode Systems AB",authorurl:"http://tinymce.moxiecode.com",infourl:"http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/bbcode",version:tinymce.majorVersion+"."+tinymce.minorVersion}},_dfrn_html2bbcode:function(s){s=tinymce.trim(s);function rep(re,str){s=s.replace(re,str)}function get(re){return s.match(re)}function _h2b_cb(match){var f,g,tof=[],tor=[];var find_spanc=/<span [^>]*class *= *[\"'](?:[^\"']* )*oembed(?: [^\"']*)*[\"'][^>]*>(.*?(?:<span[^>]*>(.*?)<\/span *>)*.*?)<\/span *>/gi;while(f=find_spanc.exec(match)){var find_a=/<a([^>]* rel=[\"']oembed[\"'][^>]*)>.*?<\/a *>/gi;if(g=find_a.exec(f[1])){var find_href=/href=[\"']([^\"']*)[\"']/gi;var m2=find_href.exec(g[1]);if(m2[1]){tof.push(f[0]);tor.push("[EMBED]"+m2[1]+"[/EMBED]")}}}for(var i=0;i<tof.length;i++)match=match.replace(tof[i],tor[i]);return match}if(s.indexOf('class="oembed')>=0){s=_h2b_cb(s)}var codes=get(/<code>(.*?)<\/code>/gi);rep(/<code>(.*?)<\/code>/gi,"[$!$!CODEBLOCK!$!$]");rep(/<a class=\"bookmark\" href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[bookmark=$1]$2[/bookmark]");rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]");rep(/<span style=\"font-size:(.*?);\">(.*?)<\/span>/gi,"[size=$1]$2[/size]");rep(/<span style=\"color:(.*?);\">(.*?)<\/span>/gi,"[color=$1]$2[/color]");rep(/<font>(.*?)<\/font>/gi,"$1");rep(/<img[^>]*?width=\"([^>]*?)\"[^>]*?height=\"([^>]*?)\"[^>]*?src=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$1x$2]$3[/img]");rep(/<img[^>]*?height=\"([^>]*?)\"[^>]*?width=\"([^>]*?)\"[^>]*?src=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$2x$1]$3[/img]");rep(/<img[^>]*?src=\"([^>]*?)\"[^>]*?height=\"([^>]*?)\"[^>]*?width=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$3x$2]$1[/img]");rep(/<img[^>]*?src=\"([^>]*?)\"[^>]*?width=\"([^>]*?)\"[^>]*?height=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$2x$3]$1[/img]");rep(/<img[^>]*?src=\"([^>]*?)\"[^>]*?\/>/gi,"[img]$1[/img]");rep(/<ul class=\"listbullet\" style=\"list-style-type\: circle\;\">(.*?)<\/ul>/gi,"[list]$1[/list]");rep(/<ul class=\"listnone\" style=\"list-style-type\: none\;\">(.*?)<\/ul>/gi,"[list=]$1[/list]");rep(/<ul class=\"listdecimal\" style=\"list-style-type\: decimal\;\">(.*?)<\/ul>/gi,"[list=1]$1[/list]");rep(/<ul class=\"listlowerroman\" style=\"list-style-type\: lower-roman\;\">(.*?)<\/ul>/gi,"[list=i]$1[/list]");rep(/<ul class=\"listupperroman\" style=\"list-style-type\: upper-roman\;\">(.*?)<\/ul>/gi,"[list=I]$1[/list]");rep(/<ul class=\"listloweralpha\" style=\"list-style-type\: lower-alpha\;\">(.*?)<\/ul>/gi,"[list=a]$1[/list]");rep(/<ul class=\"listupperalpha\" style=\"list-style-type\: upper-alpha\;\">(.*?)<\/ul>/gi,"[list=A]$1[/list]");rep(/<li>(.*?)<\/li>/gi,"[li]$1[/li]");rep(/<\/(strong|b)>/gi,"[/b]");rep(/<(strong|b)>/gi,"[b]");rep(/<\/(em|i)>/gi,"[/i]");rep(/<(em|i)>/gi,"[i]");rep(/<\/u>/gi,"[/u]");rep(/<span style=\"text-decoration: ?underline;\">(.*?)<\/span>/gi,"[u]$1[/u]");rep(/<u>/gi,"[u]");rep(/<blockquote[^>]*>/gi,"[quote]");rep(/<\/blockquote>/gi,"[/quote]");rep(/<hr \/>/gi,"[hr]");rep(/<br (.*?)\/>/gi,"\n");rep(/<br\/>/gi,"\n");rep(/<br>/gi,"\n");rep(/<p>/gi,"");rep(/<\/p>/gi,"\n");rep(/&nbsp;|\u00a0/gi," ");rep(/&quot;/gi,'"');rep(/&lt;/gi,"<");rep(/&gt;/gi,">");rep(/&amp;/gi,"&");rep(/<div>/gi,"");rep(/<\/div>/gi,"");if(codes!=null){for(var i=0;i<codes.length;i++){codes[i]=codes[i].replace("<code>","");codes[i]=codes[i].replace("</code>","");rep(/\[\$!\$!CODEBLOCK!\$!\$\]/i,"[code]"+codes[i]+"[/code]")}}return s},_dfrn_bbcode2html:function(s){s=tinymce.trim(s);function rep(re,str){s=s.replace(re,str)}function get(re){return s.match(re)}var codes=get(/\[code\](.*?)\[\/code\]/gi);rep(/\[code\](.*?)\[\/code\]/gi,"[$!$!CODEBLOCK!$!$]");rep(/\n/gi,"<br />");rep(/\[b\]/gi,"<strong>");rep(/\[\/b\]/gi,"</strong>");rep(/\[i\]/gi,"<em>");rep(/\[\/i\]/gi,"</em>");rep(/\[u\]/gi,"<u>");rep(/\[\/u\]/gi,"</u>");rep(/\[hr\]/gi,"<hr />");rep(/\[bookmark=([^\]]+)\](.*?)\[\/bookmark\]/gi,'<a class="bookmark" href="$1">$2</a>');rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi,'<a href="$1">$2</a>');rep(/\[url\](.*?)\[\/url\]/gi,'<a href="$1">$1</a>');rep(/\[img=(.*?)x(.*?)\](.*?)\[\/img\]/gi,'<img width="$1" height="$2" src="$3" />');rep(/\[img\](.*?)\[\/img\]/gi,'<img src="$1" />');rep(/\[list\](.*?)\[\/list\]/gi,'<ul class="listbullet" style="list-style-type: circle;">$1</ul>');rep(/\[list=\](.*?)\[\/list\]/gi,'<ul class="listnone" style="list-style-type: none;">$1</ul>');rep(/\[list=1\](.*?)\[\/list\]/gi,'<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>');rep(/\[list=i\](.*?)\[\/list\]/gi,'<ul class="listlowerroman" style="list-style-type: lower-roman;">$1</ul>');rep(/\[list=I\](.*?)\[\/list\]/gi,'<ul class="listupperroman" style="list-style-type: upper-roman;">$1</ul>');rep(/\[list=a\](.*?)\[\/list\]/gi,'<ul class="listloweralpha" style="list-style-type: lower-alpha;">$1</ul>');rep(/\[list=A\](.*?)\[\/list\]/gi,'<ul class="listupperalpha" style="list-style-type: upper-alpha;">$1</ul>');rep(/\[li\](.*?)\[\/li\]/gi,"<li>$1</li>");rep(/\[color=(.*?)\](.*?)\[\/color\]/gi,'<span style="color: $1;">$2</span>');rep(/\[size=(.*?)\](.*?)\[\/size\]/gi,'<span style="font-size: $1;">$2</span>');rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"<blockquote>$1</blockquote>");function _b2h_cb(match,url){url=bin2hex(url);function s_b2h(data){match=data}$.ajax({url:"oembed/b2h?url="+url,async:false,success:s_b2h,dataType:"html"});return match}s=s.replace(/\[embed\](.*?)\[\/embed\]/gi,_b2h_cb);if(codes!=null){for(var i=0;i<codes.length;i++){codes[i]=codes[i].replace("[code]","");codes[i]=codes[i].replace("[/code]","");rep(/\[\$!\$!CODEBLOCK!\$!\$\]/i,"<code>"+codes[i]+"</code>")}}return s}});tinymce.PluginManager.add("bbcode",tinymce.plugins.BBCodePlugin)})();