Upgrade TinyMCE to v3.4.5
[citadel.git] / webcit / 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 (function() {\r
12         tinymce.create('tinymce.plugins.BBCodePlugin', {\r
13                 init : function(ed, url) {\r
14                         var t = this, dialect = ed.getParam('bbcode_dialect', 'punbb').toLowerCase();\r
15 \r
16                         ed.onBeforeSetContent.add(function(ed, o) {\r
17                                 o.content = t['_' + dialect + '_bbcode2html'](o.content);\r
18                         });\r
19 \r
20                         ed.onPostProcess.add(function(ed, o) {\r
21                                 if (o.set)\r
22                                         o.content = t['_' + dialect + '_bbcode2html'](o.content);\r
23 \r
24                                 if (o.get)\r
25                                         o.content = t['_' + dialect + '_html2bbcode'](o.content);\r
26                         });\r
27                 },\r
28 \r
29                 getInfo : function() {\r
30                         return {\r
31                                 longname : 'BBCode Plugin',\r
32                                 author : 'Moxiecode Systems AB',\r
33                                 authorurl : 'http://tinymce.moxiecode.com',\r
34                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/bbcode',\r
35                                 version : tinymce.majorVersion + "." + tinymce.minorVersion\r
36                         };\r
37                 },\r
38 \r
39                 // Private methods\r
40 \r
41                 // HTML -> BBCode in PunBB dialect\r
42                 _punbb_html2bbcode : function(s) {\r
43                         s = tinymce.trim(s);\r
44 \r
45                         function rep(re, str) {\r
46                                 s = s.replace(re, str);\r
47                         };\r
48 \r
49                         // example: <strong> to [b]\r
50                         rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]");\r
51                         rep(/<font.*?color=\"(.*?)\".*?class=\"codeStyle\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");\r
52                         rep(/<font.*?color=\"(.*?)\".*?class=\"quoteStyle\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");\r
53                         rep(/<font.*?class=\"codeStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");\r
54                         rep(/<font.*?class=\"quoteStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");\r
55                         rep(/<span style=\"color: ?(.*?);\">(.*?)<\/span>/gi,"[color=$1]$2[/color]");\r
56                         rep(/<font.*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[color=$1]$2[/color]");\r
57                         rep(/<span style=\"font-size:(.*?);\">(.*?)<\/span>/gi,"[size=$1]$2[/size]");\r
58                         rep(/<font>(.*?)<\/font>/gi,"$1");\r
59                         rep(/<img.*?src=\"(.*?)\".*?\/>/gi,"[img]$1[/img]");\r
60                         rep(/<span class=\"codeStyle\">(.*?)<\/span>/gi,"[code]$1[/code]");\r
61                         rep(/<span class=\"quoteStyle\">(.*?)<\/span>/gi,"[quote]$1[/quote]");\r
62                         rep(/<strong class=\"codeStyle\">(.*?)<\/strong>/gi,"[code][b]$1[/b][/code]");\r
63                         rep(/<strong class=\"quoteStyle\">(.*?)<\/strong>/gi,"[quote][b]$1[/b][/quote]");\r
64                         rep(/<em class=\"codeStyle\">(.*?)<\/em>/gi,"[code][i]$1[/i][/code]");\r
65                         rep(/<em class=\"quoteStyle\">(.*?)<\/em>/gi,"[quote][i]$1[/i][/quote]");\r
66                         rep(/<u class=\"codeStyle\">(.*?)<\/u>/gi,"[code][u]$1[/u][/code]");\r
67                         rep(/<u class=\"quoteStyle\">(.*?)<\/u>/gi,"[quote][u]$1[/u][/quote]");\r
68                         rep(/<\/(strong|b)>/gi,"[/b]");\r
69                         rep(/<(strong|b)>/gi,"[b]");\r
70                         rep(/<\/(em|i)>/gi,"[/i]");\r
71                         rep(/<(em|i)>/gi,"[i]");\r
72                         rep(/<\/u>/gi,"[/u]");\r
73                         rep(/<span style=\"text-decoration: ?underline;\">(.*?)<\/span>/gi,"[u]$1[/u]");\r
74                         rep(/<u>/gi,"[u]");\r
75                         rep(/<blockquote[^>]*>/gi,"[quote]");\r
76                         rep(/<\/blockquote>/gi,"[/quote]");\r
77                         rep(/<br \/>/gi,"\n");\r
78                         rep(/<br\/>/gi,"\n");\r
79                         rep(/<br>/gi,"\n");\r
80                         rep(/<p>/gi,"");\r
81                         rep(/<\/p>/gi,"\n");\r
82                         rep(/&nbsp;|\u00a0/gi," ");\r
83                         rep(/&quot;/gi,"\"");\r
84                         rep(/&lt;/gi,"<");\r
85                         rep(/&gt;/gi,">");\r
86                         rep(/&amp;/gi,"&");\r
87 \r
88                         return s; \r
89                 },\r
90 \r
91                 // BBCode -> HTML from PunBB dialect\r
92                 _punbb_bbcode2html : function(s) {\r
93                         s = tinymce.trim(s);\r
94 \r
95                         function rep(re, str) {\r
96                                 s = s.replace(re, str);\r
97                         };\r
98 \r
99                         // example: [b] to <strong>\r
100                         rep(/\n/gi,"<br />");\r
101                         rep(/\[b\]/gi,"<strong>");\r
102                         rep(/\[\/b\]/gi,"</strong>");\r
103                         rep(/\[i\]/gi,"<em>");\r
104                         rep(/\[\/i\]/gi,"</em>");\r
105                         rep(/\[u\]/gi,"<u>");\r
106                         rep(/\[\/u\]/gi,"</u>");\r
107                         rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi,"<a href=\"$1\">$2</a>");\r
108                         rep(/\[url\](.*?)\[\/url\]/gi,"<a href=\"$1\">$1</a>");\r
109                         rep(/\[img\](.*?)\[\/img\]/gi,"<img src=\"$1\" />");\r
110                         rep(/\[color=(.*?)\](.*?)\[\/color\]/gi,"<font color=\"$1\">$2</font>");\r
111                         rep(/\[code\](.*?)\[\/code\]/gi,"<span class=\"codeStyle\">$1</span>&nbsp;");\r
112                         rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"<span class=\"quoteStyle\">$1</span>&nbsp;");\r
113 \r
114                         return s; \r
115                 }\r
116         });\r
117 \r
118         // Register plugin\r
119         tinymce.PluginManager.add('bbcode', tinymce.plugins.BBCodePlugin);\r
120 })();