Repository navigation
Expand file tree
/
Copy pathplugin.js
More file actions
375 lines (325 loc) · 13.2 KB
/
Copy pathplugin.js
File metadata and controls
375 lines (325 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
function isInArray(list,string){
return list.indexOf(string.toLowerCase()) > -1
}
function conv(o,htmlbb){
// var test_patt = /\[|\</;
// var bb = test_patt.exec(main_in)=='[';
// define some elements
var brac, open_b, li_elem_patt
function pre(temp_o) {
// define the regex for open and close paranthesis
var pt_patt = /([^\[\]<>\n]+)(?![^\[\]<>\n]*[\]>])/g;
var nl_patt = new RegExp ('(\\\\n|'+open_b+'br /'+close_b+')','g');
var p_patt = new RegExp ('('+open_b+'\\s*br\\s*'+close_b+open_b+'\\s*\\/br\\s*'+close_b+'){2}','g');
temp_o = temp_o.replace(li_elem_patt,brac("list_element")+"$1"+brac("/list_element"));
temp_o = temp_o.replace(nl_patt,brac("br")+brac("/br"));
temp_o = temp_o.replace(p_patt,brac("p")+brac("/p"));
temp_o = temp_o.replace(pt_patt,brac("plain")+"$1"+brac("/plain"));
return temp_o;
}
function init(htmlbb) {
// conditions for if html or bbcode
if (htmlbb) {
brac = function (str) { return "["+str+"]";};
open_b ="\\[", close_b="\\]", type = "BB";
li_elem_patt = new RegExp (open_b+"\\*"+close_b+"(.*?)("+open_b+"\\*"+close_b+"|\\n)","g");
} else {
brac = function (str) { return "<"+str+">";};
open_b="\\<", close_b="\\>", type = "HTML";
li_elem_patt = new RegExp (open_b+"li"+close_b+"(.*?)"+open_b+"li"+close_b,"g");
}
}
function calc(temp_o) {
// find the tags, put them all into objects
var out = [];
var curr = 0;
var all = "(?:.|\n)"
var main_patt = new RegExp (open_b+"\\s*(\\w+)"+all+"*?"+open_b+"\\s*\\/\\s*\\1\\s*"+close_b);
var tag_patt = new RegExp (open_b+"\\s*(\\w+)\\s*");
var att_patt = new RegExp ("(\\w+)=(\\d+|([\\\"']).*?\\3|\\w+)\\s*");
var cont_patt = new RegExp (open_b+"\\s*(\\w+).*?"+close_b+"("+all+"*?)"+open_b+"\\s*\\/\\s*\\1\\s*"+close_b);
var brac_tag = new RegExp ("^[^"+close_b+"]");
while (main_patt.test(temp_o)) {
var input = main_patt.exec(temp_o)[0];
var match = tag_patt.exec(input);
out.push({format:type, type:match[1], content:"", attributes:{}, sub_layer:false});
match=cont_patt.exec(input);
out[curr].content=calc(match[2]);
if (typeof out[curr].content == typeof []) { out[curr].sub_layer=true; }
input = input.replace(tag_patt,"");
while (brac_tag.test(input)) {
match=att_patt.exec(input);
if (match) {
out[curr].attributes[match[1]] = match[2].replace(/["']/g,"");
input = input.replace(att_patt,"");
} else {
alert('Bad '+type+'Code');
return [];
}
}
temp_o = temp_o.replace(main_patt,"");
curr++;
}
if (out.length == 0) {
return temp_o;
} else if (out.length == 1 && out[0].type == "plain") {
return out[0].content;
} else {
return out;
}
}
// function preproc_insert_image(temp_o){
// console.log("prec image");
// var html_patt = new RegExp(/\<img\s(:?.+)\/\>/);
// console.log(html_patt.test(temp_o));
// while (html_patt.test(temp_o)) {
// var match = html_patt.exec(temp_o);
// //var att_patt = new RegExp ("(\\w+)=(\\d+|([\\\"']).*?\\3|\\w+)\\s*");
// var replace_str = "[img " + match[1] + "][/img]";
// temp_o = temp_o.replace(match[0],replace_str);
// }
// return temp_o;
// }
function res_to_string(res,htmlbb_flip) {
// process those objects and do the conversion
var temp_o = "";
var i = 0;
// if (res.hasOwnProperty("content") ){
// if (res.content != "" && res.hasOwnProperty("length")) {
for (i = 0; i < res.length; i++) {
var sub_res = res[i];
if (sub_res.sub_layer == false) {
if (sub_res.type != "plain" && sub_res.type != "br"){
if (sub_res.type == "p" && htmlbb_flip){
// if html
if (sub_res.attributes.hasOwnProperty("style")){
temp_o += brac(inner_brac_process(sub_res,htmlbb_flip) + " ") + sub_res.content + brac("/" + sub_res.type) + "\n\n";
} else {
temp_o += sub_res.content + "\n\n";
}
} else {
temp_o += brac(inner_brac_process(sub_res,htmlbb_flip)) + sub_res.content + brac("/" + sub_res.type);
}
} else if (sub_res.type == "br"){
if (htmlbb == true) {
temp_o += "<br />";
} else {
temp_o += "\n";
}
} else {
temp_o += sub_res.content;
}
} else {
if (sub_res.type != "plain" && sub_res.type != "br"){
if (sub_res.type == "p" && htmlbb_flip){
// if html
if (sub_res.attributes.hasOwnProperty("style")){
temp_o += brac(inner_brac_process(sub_res,htmlbb_flip) + " ") + res_to_string(sub_res.content) + brac("/" + sub_res.type) + "\n\n";
} else {
temp_o += res_to_string(sub_res.content) + "\n\n";
}
} else {
temp_o += brac(inner_brac_process(sub_res,htmlbb_flip)) + res_to_string(sub_res.content) + brac("/" + sub_res.type);
}
} else if (sub_res.type == "br"){
if (htmlbb == true) {
temp_o += "<br />";
} else {
temp_o += "\n";
}
} else {
temp_o += res.content;
}
}
}
// }
// }
return temp_o;
}
function inner_brac_process(res,htmlbb_flip) {
var ostring = "";
// lets define the type
//ostring += //res.type.toString();
ostring += res.type
// now list the attributes
var attributes = res.attributes;
// loop over the properties and convert them to string
for (var property in attributes) {
if (attributes.hasOwnProperty(property)) {
/* if (htmlbb_flip){
// html
if (property == "style"){ // we need to do something different with this
ostring += " " + property + "=\"" + attributes[property] + "\"";
} else {
ostring += " " + property + "=\"" + attributes[property] + "\"";
}
} else {
// bbcode
if (property == "style"){ // we need to do something different with this
ostring += " " + property + "=\"" + attributes[property] + "\"";
} else {
ostring += " " + property + "=\"" + attributes[property] + "=\"";
}
} */
ostring += " " + property + "=\"" + attributes[property] + "\"";
}
}
return ostring;
}
function html_emot(r,gif,temp_o){
// May need to alter this variable
i = "/tinymce/";
// var string = '<img src="' + i + 'plugins/emoticons/img/' + gif + '">';
// for some reason, does not
var string = '<img alt="' + r + '" src="' + i + 'plugins/emoticons/img/' + gif + '" />';
var regthing = RegExp(string,'gi');
rep_string = ":" + r + ":";
temp_o = temp_o.replace(regthing,rep_string);
var string = '<img alt="' + r + '" src="' + i + 'plugins/emoticons/img/' + gif + '" data-mce-src="' + i + 'plugins/emoticons/img/' + gif + '" />';
var regthing = RegExp(string,'gi');
rep_string = ":" + r + ":";
temp_o = temp_o.replace(regthing,rep_string);
// var string = '[img alt="' + r + '"]' + i + 'plugins/emoticons/img/' + gif + '[/img]';
// rep_string = ":" + r + ":";
// temp_o = temp_o.replace(string,rep_string);
// var string = '[img]' + i + 'plugins/emoticons/img/' + gif + '[/img]';
// rep_string = ":" + r + ":";
// temp_o = temp_o.replace(string,rep_string);
return temp_o;
}
function bb_emot(e,gif,temp_o){);
var rep_string = ":" + e + ":";
// may need to alter this
var i = "/tinymce/";
var img_string = '<img alt="' + e + '" src="' + i + 'plugins/emoticons/img/' + gif + '" />';
var regthing = RegExp(rep_string,'gi');
temp_o = temp_o.replace(regthing,img_string);
var img_string = '<img alt="' + e + '" src="' + i + 'plugins/emoticons/img/' + gif + '" data-mce-src="' + i + 'plugins/emoticons/img/' + gif + '" />';
var regthing = RegExp(rep_string,'gi');
temp_o = temp_o.replace(regthing,img_string);
return temp_o;
}
var temp_o = o;
if (htmlbb == true) {
// preprocessing
// btw this bbcode to html
temp_o = bb_emot("cool","smiley-cool.gif",temp_o);
temp_o = bb_emot("cry", "smiley-cry.gif",temp_o);
temp_o = bb_emot("embarassed", "smiley-embarassed.gif",temp_o);
temp_o = bb_emot("foot-in-mouth", "smiley-foot-in-mouth.gif",temp_o);
temp_o = bb_emot("frown", "smiley-frown.gif",temp_o);
temp_o = bb_emot("innocent", "smiley-innocent.gif",temp_o);
temp_o = bb_emot("kiss", "smiley-kiss.gif",temp_o);
temp_o = bb_emot("laughing", "smiley-laughing.gif",temp_o);
temp_o = bb_emot("money-mouth", "smiley-money-mouth.gif",temp_o);
temp_o = bb_emot("sealed", "smiley-sealed.gif",temp_o);
temp_o = bb_emot("smile", "smiley-smile.gif",temp_o);
temp_o = bb_emot("suprised", "smiley-suprised.gif",temp_o);
temp_o = bb_emot("tongue-out", "smiley-tongue-out.gif",temp_o);
temp_o = bb_emot("undecided", "smiley-undecided.gif",temp_o);
temp_o = bb_emot("wink", "smiley-wink.gif",temp_o);
temp_o = bb_emot("yell", "smiley-yell.gif",temp_o);
// this is messing up the pasting part, need some way of retaining this
// life hack right below
// somewhat solves all of lifes problems.
// ok for real, duplicate <p> tags are destroyed, and this allows for retention of <p> styles too
// likewise, wrapping <p> around the entire body of text is a bad idea since 1) tinymce handles this for you
// and 2) it has unfound consequences with pasting into tinymce.
temp_o = temp_o.replace(new RegExp('\<br \/\>','g'),"[br][/br]");
temp_o = temp_o.replace(new RegExp('([\s\S]?:\n\n|\n\n)','g'), '[plain]</p><p>[/plain]');
temp_o = temp_o.replace(new RegExp('([\s\S]?:\r\r|\r\r)','g'), '[plain]</p><p>[plain]');
temp_o = temp_o.replace(new RegExp('([\s\S]?:\n|\n)','g'), '[br][/br]');
temp_o = temp_o.replace(new RegExp('([\s\S]?:\r|\r)','g'), '[br][/br]');
// temp_o = temp_o.replace(new RegExp('\<([a-z][a-z0-9]*)\b(.*)[^>]\/\>$','gi'),"<$1 $2 ></$1>");
// temp_o = temp_o.replace(new RegExp('\<([a-z][a-z0-9]*)\b(.*)[^>]\>$','gi'),"<$1 $2 ></$1>");
temp_o = temp_o.replace(/\<img\b(.*)\>$/gi,'<img$1></img>[/plain]'); // handle pasting of images (hence the $)
temp_o = temp_o.replace(/\<img\b(.*)\s\/\>/gi,'[plain]<img$1></img>[/plain]'); // handle how tinymce handles things
temp_o = temp_o.replace(new RegExp('<span id="mce_marker" data-mce-type="bookmark">È</span>','g'),'');
temp_o = temp_o + "[/plain]";
} else {
temp_o = html_emot("cool","smiley-cool.gif",temp_o);
temp_o = html_emot("cry", "smiley-cry.gif",temp_o);
temp_o = html_emot("embarassed", "smiley-embarassed.gif",temp_o);
temp_o = html_emot("foot-in-mouth", "smiley-foot-in-mouth.gif",temp_o);
temp_o = html_emot("frown", "smiley-frown.gif",temp_o);
temp_o = html_emot("innocent", "smiley-innocent.gif",temp_o);
temp_o = html_emot("kiss", "smiley-kiss.gif",temp_o);
temp_o = html_emot("laughing", "smiley-laughing.gif",temp_o);
temp_o = html_emot("money-mouth", "smiley-money-mouth.gif",temp_o);
temp_o = html_emot("sealed", "smiley-sealed.gif",temp_o);
temp_o = html_emot("smile", "smiley-smile.gif",temp_o);
temp_o = html_emot("suprised", "smiley-suprised.gif",temp_o);
temp_o = html_emot("tongue-out", "smiley-tongue-out.gif",temp_o);
temp_o = html_emot("undecided", "smiley-undecided.gif",temp_o);
temp_o = html_emot("wink", "smiley-wink.gif",temp_o);
temp_o = html_emot("yell", "smiley-yell.gif",temp_o);
temp_o = temp_o + "</plain>";
temp_o = temp_o + "</p>";
temp_o = temp_o.replace(new RegExp('\<br \/\>','g'),"<plain>\n</plain>");
// temp_o = temp_o.replace(new RegExp('\<([a-z][a-z0-9]*)\b(.*)[^>]\/\>$','gi'),"[$1 $2 ][/$1]");
// temp_o = temp_o.replace(new RegExp('\<([a-z][a-z0-9]*)\b(.*)[^>]\>$','gi'),"[$1 $2 ][/$1]");
temp_o = temp_o.replace(/\<img\b(.*)\s\/\>/gi,'<plain>[img$1][/img]</plain>');
temp_o = temp_o.replace(/\<img\b(.*)\>$/gi,'<plain>[img$1][/img]</plain>');
// editor haxors
temp_o = temp_o.replace(new RegExp('<span id="mce_marker" data-mce-type="bookmark">È</span>','g'),'');
}
init(htmlbb);
temp_o = pre(temp_o);
var res = calc(temp_o);
if (typeof res == "string"){
// i.e if nothing much really changed
o = temp_o.replace(temp_o,res);
} else {
// change the html to bbcode
var htmlbb_flip = !htmlbb;
init(htmlbb_flip);
// process the string and replace
o = temp_o.replace(temp_o,res_to_string(res,htmlbb_flip));
}
return o;
}
! function() {
tinymce.create("tinymce.plugins.BBCodePlugin", {
init: function(o) {
var e = this,
t = o.getParam("bbcode_dialect", "punbb").toLowerCase();
o.on("beforeSetContent", function(o) {
o.content = e["_" + t + "_bbcode2html"](o.content)
}), o.on("postProcess", function(o) {
o.set && (o.content = e["_" + t + "_bbcode2html"](o.content)), o.get && (o.content = e["_" + t + "_html2bbcode"](o.content))
})
},
getInfo: function() {
return {
longname: "SACMS BBCode Plugin",
author: 'Cosmosquark',
authorurl: 'http://www.cosmosquark.com.com',
infourl: "http://www.tinymce.com/wiki.php/Plugin:bbcode"
}
},
_punbb_html2bbcode: function(o) {
o = tinymce.trim(o);
function rep(e, t) {
o = o.replace(e, t);
}
//rep(/<img.*?src=\"(.*?)\".*?\/>/gi,"[img]$1[/img]");
o = conv(o,false);
rep(/</gi, "<");
rep(/>/gi, ">");
rep(/&/gi, "&");
rep(/ |\u00a0/gi," ");
rep(/"/gi, '"');
return o
},
_punbb_bbcode2html: function(o) {
o = tinymce.trim(o);
function rep(e, t) {
// somehow want to do something so that if [break] is on the same line.. then don't replace
o = o.replace(e, t);
}
//rep(/\[img\](.*?)\[\/img\]/gi, '<img src="$1" />');
o = conv(o,true);
return o;
}
}), tinymce.PluginManager.add("bbcode_sacms", tinymce.plugins.BBCodePlugin)
}();