bgneal@312: /** bgneal@312: * editor_plugin_src.js bgneal@312: * bgneal@312: * Copyright 2009, Moxiecode Systems AB bgneal@312: * Released under LGPL License. bgneal@312: * bgneal@312: * License: http://tinymce.moxiecode.com/license bgneal@312: * Contributing: http://tinymce.moxiecode.com/contributing bgneal@312: */ bgneal@312: bgneal@312: (function() { bgneal@312: tinymce.create('tinymce.plugins.BBCodePlugin', { bgneal@312: init : function(ed, url) { bgneal@312: var t = this, dialect = ed.getParam('bbcode_dialect', 'punbb').toLowerCase(); bgneal@312: bgneal@312: ed.onBeforeSetContent.add(function(ed, o) { bgneal@312: o.content = t['_' + dialect + '_bbcode2html'](o.content); bgneal@312: }); bgneal@312: bgneal@312: ed.onPostProcess.add(function(ed, o) { bgneal@312: if (o.set) bgneal@312: o.content = t['_' + dialect + '_bbcode2html'](o.content); bgneal@312: bgneal@312: if (o.get) bgneal@312: o.content = t['_' + dialect + '_html2bbcode'](o.content); bgneal@312: }); bgneal@312: }, bgneal@312: bgneal@312: getInfo : function() { bgneal@312: return { bgneal@312: longname : 'BBCode Plugin', bgneal@312: author : 'Moxiecode Systems AB', bgneal@312: authorurl : 'http://tinymce.moxiecode.com', bgneal@312: infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/bbcode', bgneal@312: version : tinymce.majorVersion + "." + tinymce.minorVersion bgneal@312: }; bgneal@312: }, bgneal@312: bgneal@312: // Private methods bgneal@312: bgneal@312: // HTML -> BBCode in PunBB dialect bgneal@312: _punbb_html2bbcode : function(s) { bgneal@312: s = tinymce.trim(s); bgneal@312: bgneal@312: function rep(re, str) { bgneal@312: s = s.replace(re, str); bgneal@312: }; bgneal@312: bgneal@312: // example: to [b] bgneal@312: rep(/(.*?)<\/a>/gi,"[url=$1]$2[/url]"); bgneal@312: rep(/(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]"); bgneal@312: rep(/(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]"); bgneal@312: rep(/(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]"); bgneal@312: rep(/(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]"); bgneal@312: rep(/(.*?)<\/span>/gi,"[color=$1]$2[/color]"); bgneal@312: rep(/(.*?)<\/font>/gi,"[color=$1]$2[/color]"); bgneal@312: rep(/(.*?)<\/span>/gi,"[size=$1]$2[/size]"); bgneal@312: rep(/(.*?)<\/font>/gi,"$1"); bgneal@312: rep(//gi,"[img]$1[/img]"); bgneal@312: rep(/(.*?)<\/span>/gi,"[code]$1[/code]"); bgneal@312: rep(/(.*?)<\/span>/gi,"[quote]$1[/quote]"); bgneal@312: rep(/(.*?)<\/strong>/gi,"[code][b]$1[/b][/code]"); bgneal@312: rep(/(.*?)<\/strong>/gi,"[quote][b]$1[/b][/quote]"); bgneal@312: rep(/(.*?)<\/em>/gi,"[code][i]$1[/i][/code]"); bgneal@312: rep(/(.*?)<\/em>/gi,"[quote][i]$1[/i][/quote]"); bgneal@312: rep(/(.*?)<\/u>/gi,"[code][u]$1[/u][/code]"); bgneal@312: rep(/(.*?)<\/u>/gi,"[quote][u]$1[/u][/quote]"); bgneal@312: rep(/<\/(strong|b)>/gi,"[/b]"); bgneal@312: rep(/<(strong|b)>/gi,"[b]"); bgneal@312: rep(/<\/(em|i)>/gi,"[/i]"); bgneal@312: rep(/<(em|i)>/gi,"[i]"); bgneal@312: rep(/<\/u>/gi,"[/u]"); bgneal@312: rep(/(.*?)<\/span>/gi,"[u]$1[/u]"); bgneal@312: rep(//gi,"[u]"); bgneal@312: rep(/]*>/gi,"[quote]"); bgneal@312: rep(/<\/blockquote>/gi,"[/quote]"); bgneal@312: rep(/
/gi,"\n"); bgneal@312: rep(//gi,"\n"); bgneal@312: rep(/
/gi,"\n"); bgneal@312: rep(/

/gi,""); bgneal@312: rep(/<\/p>/gi,"\n"); bgneal@312: rep(/ /gi," "); bgneal@312: rep(/"/gi,"\""); bgneal@312: rep(/</gi,"<"); bgneal@312: rep(/>/gi,">"); bgneal@312: rep(/&/gi,"&"); bgneal@312: bgneal@312: return s; bgneal@312: }, bgneal@312: bgneal@312: // BBCode -> HTML from PunBB dialect bgneal@312: _punbb_bbcode2html : function(s) { bgneal@312: s = tinymce.trim(s); bgneal@312: bgneal@312: function rep(re, str) { bgneal@312: s = s.replace(re, str); bgneal@312: }; bgneal@312: bgneal@312: // example: [b] to bgneal@312: rep(/\n/gi,"
"); bgneal@312: rep(/\[b\]/gi,""); bgneal@312: rep(/\[\/b\]/gi,""); bgneal@312: rep(/\[i\]/gi,""); bgneal@312: rep(/\[\/i\]/gi,""); bgneal@312: rep(/\[u\]/gi,""); bgneal@312: rep(/\[\/u\]/gi,""); bgneal@312: rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi,"$2"); bgneal@312: rep(/\[url\](.*?)\[\/url\]/gi,"$1"); bgneal@312: rep(/\[img\](.*?)\[\/img\]/gi,""); bgneal@312: rep(/\[color=(.*?)\](.*?)\[\/color\]/gi,"$2"); bgneal@312: rep(/\[code\](.*?)\[\/code\]/gi,"$1 "); bgneal@312: rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"$1 "); bgneal@312: bgneal@312: return s; bgneal@312: } bgneal@312: }); bgneal@312: bgneal@312: // Register plugin bgneal@312: tinymce.PluginManager.add('bbcode', tinymce.plugins.BBCodePlugin); bgneal@312: })();