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