-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
1139 lines (1039 loc) · 38 KB
/
index.js
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2011 Patrick Gillespie, http://patorjk.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
Extendible BBCode Parser v1.0.0
By Patrick Gillespie ([email protected])
Website: http://patorjk.com/
Edited by:
Edward Kim
Slickage Studios
For Epochtalk
This module allows you to parse BBCode and to extend to the mark-up language
to add in your own tags.
*/
var XBBCODE = (function() {
"use strict";
// -----------------------------------------------------------------------------
// Set up private variables
// -----------------------------------------------------------------------------
var me = {},
urlPattern = /^(?:https?|file|c):(?:\/{1,3}|\\{1})[-a-zA-Z0-9:;@#%&()~_?\+=\/\\\.]*$/,
ftpPattern = /^(?:ftps?|c):(?:\/{1,3}|\\{1})[-a-zA-Z0-9:;@#%&()~_?\+=\/\\\.]*$/,
colorNamePattern = /^(?:red|green|blue|orange|yellow|pink|black|white|beige|brown|grey|gray|silver|purple|maroon|lime|limegreen|olive|navy|teal|aqua)$/,
colorCodePattern = /^#?[a-fA-F0-9]{6}$/,
emailPattern = /[^\s@]+@[^\s@]+\.[^\s@]+/,
fontFacePattern = /^([a-z][a-z0-9_]+|"[a-z][a-z0-9_\s]+")$/i,
sizePattern = /([1-9][\d]?p[xt]|(?:x-)?small(?:er)?|(?:x-)?large[r]?)/,
anchorPattern = /[#]?([A-Za-z][A-Za-z0-9_-]*)/,
smfQuotePattern = /(?:board=\d+;)?((?:topic|threadid)=[\dmsg#\.\/]{1,40}(?:;start=[\dmsg#\.\/]{1,40})?|action=profile;u=\d+)/,
eptQuotePattern = /^\/threads\/[a-z0-9\-]*\/posts[a-z0-9?\=\-#\&]*/i,
tags,
tagList,
tagsNoParseList = [],
bbRegExp,
pbbRegExp,
pbbRegExp2,
openTags,
closeTags;
/* -----------------------------------------------------------------------------
* tags
* This object contains a list of tags that your code will be able to understand.
* Each tag object has the following properties:
*
* openTag - A function that takes in the tag's parameters (if any) and its
* contents, and returns what its HTML open tag should be.
* Example: [color=red]test[/color] would take in "=red" as a
* parameter input, and "test" as a content input.
* It should be noted that any BBCode inside of "content" will have
* been processed by the time it enter the openTag function.
*
* closeTag - A function that takes in the tag's parameters (if any) and its
* contents, and returns what its HTML close tag should be.
*
* displayContent - Defaults to true. If false, the content for the tag will
* not be displayed. This is useful for tags like IMG where
* its contents are actually a parameter input.
*
* restrictChildrenTo - A list of BBCode tags which are allowed to be nested
* within this BBCode tag. If this property is omitted,
* any BBCode tag may be nested within the tag.
*
* restrictParentsTo - A list of BBCode tags which are allowed to be parents of
* this BBCode tag. If this property is omitted, any BBCode
* tag may be a parent of the tag.
*
* noParse - true or false. If true, none of the content WITHIN this tag will be
* parsed by the XBBCode parser.
*
*
*
* LIMITIONS on adding NEW TAGS:
* - Tag names should be alphanumeric (including underscores) and all tags should have an opening tag
* and a closing tag.
* The [*] tag is an exception because it was already a standard
* bbcode tag. Technecially tags don't *have* to be alphanumeric, but since
* regular expressions are used to parse the text, if you use a non-alphanumeric
* tag names, just make sure the tag name gets escaped properly (if needed).
* --------------------------------------------------------------------------- */
// tags to add:
// chrissy (wtf)
// kissy (wtf)
// flash (not supported)
/* Allowed Color Codes:
* red
* green
* blue
* orange
* yellow
* pink
* black
* white
* beige
* brown
* grey
* gray
* silver
* purple
* maroon
* lime
* limegreen
* olive
* navy
* teal
* aqua
*/
/* hard coded colors:
* Black
* Blue
* Green
* Red
* White
*/
tags = {
"anchor": {
openTag: function(params, content) {
var id = params || '';
id = id.substr(1) || '';
if ( !anchorPattern.test(id) ) { id = ''; }
return '<span id="post_' + id + '">';
},
closeTag: function(params, content) { return '</span>'; }
},
"abbr": {
openTag: function(params,content) {
var title = params || '';
title = title.substr(1) || '';
title = title.replace(/<.*?>/g,'');
return '<abbr title="' + title + '">';
},
closeTag: function(params,content) {
return '</abbr>';
}
},
"acronym": {
openTag: function(params,content) {
var title = params || '';
title = title.substr(1) || '';
title = title.replace(/<.*?>/g,'');
return '<acronym title="' + title + '">';
},
closeTag: function(params,content) {
return '</acronym>';
}
},
"b": {
openTag: function(params,content) { return '<b>'; },
closeTag: function(params,content) { return '</b>'; }
},
/*
This tag does nothing and is here mostly to be used as a classification for
the bbcode input when evaluating parent-child tag relationships
*/
"bbcode": {
openTag: function(params,content) { return ''; },
closeTag: function(params,content) { return ''; }
},
"black": {
openTag: function(params,content) {
return '<span class="bbcode-color-black" style="color: black;">';
},
closeTag: function(params,content) {
return '</span>';
}
},
"blue": {
openTag: function(params,content) {
return '<span class="bbcode-color-blue" style="color: blue;">';
},
closeTag: function(params,content) {
return '</span>';
}
},
"br": {
openTag: function(params,content) { return '<br />'; },
closeTag: function(params,content) { return ''; }
},
"btc": {
openTag: function(params,content) {
return '<span class="BTC">BTC</span>';
},
closeTag: function(params,content) {
return '';
}
},
"center": {
openTag: function(params,content) {
return '<div align="center">';
},
closeTag: function(params,content) {
return '</div>';
}
},
"code": {
openTag: function(params,content) {
return '<code>';
},
closeTag: function(params,content) {
return '</code>';
},
noParse: true
},
"color": {
openTag: function(params,content) {
var simpleColor = '';
var colorCode = params || '=black';
colorCode = colorCode.substr(1) || "black";
colorCode = colorCode.toLowerCase();
colorCode = colorCode.trim();
colorNamePattern.lastIndex = 0;
colorCodePattern.lastIndex = 0;
if ( !colorNamePattern.test( colorCode ) ) {
if ( !colorCodePattern.test( colorCode ) ) {
colorCode = 'black';
simpleColor = 'black';
}
else {
if (colorCode.substr(0,1) !== "#") {
simpleColor = '_' + colorCode;
colorCode = '#' + colorCode;
}
else {
simpleColor = '_' + colorCode.substr(1);
}
}
}
else { simpleColor = colorCode; }
return '<span class="bbcode-color-' + simpleColor + '" style="color:' + colorCode + '">';
},
closeTag: function(params,content) {
return '</span>';
}
},
"email": {
openTag: function(params,content) {
var myEmail;
if (!params) {
myEmail = content.replace(/<.*?>/g,'');
}
else {
myEmail = params.substr(1);
}
myEmail = myEmail.trim();
emailPattern.lastIndex = 0;
if ( !emailPattern.test( myEmail ) ) {
return '<a>';
}
return '<a href="mailto:' + myEmail + '" target="_blank">';
},
closeTag: function(params,content) {
return '</a>';
}
},
"ftp": {
openTag: function(params,content) {
var thisFtp = "";
if (!params) {
thisFtp = content.replace(/<.*?>/g,'');
}
else {
thisFtp = params.substr(1);
}
thisFtp = thisFtp.trim();
ftpPattern.lastIndex = 0;
if ( !ftpPattern.test( thisFtp ) ) {
return '<a target="_blank">';
}
return '<a href="' + thisFtp + '" target="_blank">';
},
closeTag: function(params,content) {
return '</a>';
}
},
// "font": {
// openTag: function(params,content) {
// var faceCode = params || '=inherit';
// faceCode = params.substr(1);
// faceCode = faceCode.trim();
// fontFacePattern.lastIndex = 0;
// if ( !fontFacePattern.test( faceCode ) ) {
// faceCode = "inherit";
// }
// return '<span style="font-family:' + faceCode + '">';
// },
// closeTag: function(params,content) { return '</span>'; }
// },
"glow": {
openTag: function(params, content) {
var options = params || '';
options = params.substr(1) || '';
options = options.replace(/<.*?>/g,'');
var simpleColor = '';
var color = options.split(',')[0];
color = color.trim();
color = color.toLowerCase();
colorNamePattern.lastIndex = 0;
colorCodePattern.lastIndex = 0;
if ( !colorNamePattern.test( color ) ) {
if ( !colorCodePattern.test( color ) ) {
color = 'inherit';
simpleColor = 'inherit';
}
else {
if (color.substr(0,1) !== "#") {
simpleColor = '_' + color;
color = '#' + color;
}
else {
simpleColor = '_' + color.substr(1);
}
}
}
else { simpleColor = color; }
return '<span class="bbcode-bgcolor-' + simpleColor +'" style="background-color: ' + color + '">';
},
closeTag: function(params, content) { return '</span>'; }
},
"green": {
openTag: function(params,content) {
return '<span class="bbcode-color-green" style="color: green;">';
},
closeTag: function(params,content) {
return '</span>';
}
},
"hr": {
openTag: function(params,content) { return '<hr />'; },
closeTag: function(params,content) { return ''; }
},
"html": {
openTag: function(params,content) { return ''; },
closeTag: function(params,content) { return ''; }
},
"i": {
openTag: function(params,content) { return '<i>'; },
closeTag: function(params,content) { return '</i>'; }
},
"img": {
openTag: function(params,content) {
// url
var myUrl = content;
myUrl = myUrl.trim();
urlPattern.lastIndex = 0;
if ( !urlPattern.test( myUrl ) ) { myUrl = ""; }
if (!params) { return '<img src="' + myUrl + '" />'; }
// params
var options = params;
options = options.trim();
options = options.replace(/<.*?>/g,'');
var alt, width, height;
// validate params counts
if ((options.match(/alt=/gi) || []).length > 1) {
return '<img src="' + myUrl + '" />';
}
if ((options.match(/width=/gi) || []).length > 1) {
return '<img src="' + myUrl + '" />';
}
if ((options.match(/height=/gi) || []).length > 1) {
return '<img src="' + myUrl + '" />';
}
// pull out parameters through regex
var regexArray = /^(alt=.+?|width=[0-9]+?|height=[0-9]+?)?\s*(alt=.+?|width=[0-9]+?|height=[0-9]+?)?\s*(alt=.+?|width=[0-9]+?|height=[0-9]+?)$/i.exec(options);
// check that array exists
if (regexArray) {
// parse author link date
// the regex array output is weird:
// [0] = the matched output as a whole so skip that
for (var i = 1; i < regexArray.length; i++) {
var value = regexArray[i] || '';
value = value.trim();
if (/alt=/i.test(value)) {
alt = value.substr(4);
}
else if (/width=/i.test(value)) {
width = value.substr(6);
}
else if (/height=/i.test(value)) {
height = value.substr(7);
}
}
}
var tag = '<img src="' + myUrl + '" ';
if (alt) { tag += 'alt="' + alt + '" '; }
if (width) { tag += 'width="' + width + '" '; }
if (height) { tag += 'height="' + height + '" '; }
tag += '/>';
return tag;
},
closeTag: function(params,content) { return ''; },
displayContent: false
},
"iurl": {
openTag: function(params,content) {
var myUrl;
if (!params) {
myUrl = content.replace(/<.*?>/g,"");
}
else {
myUrl = params.substr(1);
}
myUrl = myUrl.trim();
urlPattern.lastIndex = 0;
if ( !urlPattern.test( myUrl ) ) {
myUrl = "#";
}
return '<a href="' + myUrl + '" />';
},
closeTag: function(params,content) {
return '';
}
},
"left": {
openTag: function(params,content) {
return '<div class="bbcode-text-left" style="text-align: left;">';
},
closeTag: function(params,content) {
return '</div>';
}
},
"li": {
openTag: function(params,content) { return "<li>"; },
closeTag: function(params,content) { return "</li>"; },
restrictParentsTo: ["list","ul","ol"]
},
"list": {
openTag: function(params,content) {
var tag = '<ul>';
// clean input
var type = params || '';
type = type.trim();
type = type.replace(/<.*?>/g,'');
// check for type= param
if (type.indexOf('type=') === 0) {
// parse type
type = type.replace('type=', '');
type = type.trim();
// check type validity
if (/(none|disc|circle|square|decimal|decimal-leading-zero|lower-roman|upper-roman|lower-alpha|upper-alpha|lower-greek|lower-latin|upper-latin|hebrew|armenian|georgian|cjk-ideographic|hiragana|katakana|hiragana-iroha|katakana-iroha)/i.test(type)) {
tag = '<ul class="bbcode-list-' + type + '" style="list-style-type: ' + type + '">';
}
}
return tag;
},
closeTag: function(params,content) { return '</ul>'; },
restrictChildrenTo: ["*", "li"]
},
"ltr": {
openTag: function(params,content) { return '<div dir="ltr">'; },
closeTag: function(params,content) { return '</div>'; }
},
"me": {
openTag: function(params, content) {
var name = params || '';
name = name.substr(1);
name = name.replace(/<.*?>/g,"");
name = name.trim();
if (content) { name = name + " "; }
return '<div class="bbcode-color-red" style="color: red;">* ' + name + ' ';
},
closeTag: function(params, content) { return '</div>'; }
},
"move": {
openTag: function(params, content) { return '<div>'; },
closeTag: function(params, content) { return '</div>'; }
},
"noparse": {
openTag: function(params,content) { return ''; },
closeTag: function(params,content) { return ''; },
noParse: true
},
"nobbc": {
openTag: function(params,content) { return ''; },
closeTag: function(params,content) { return ''; },
noParse: true
},
"ol": {
openTag: function(params,content) { return '<ol>'; },
closeTag: function(params,content) { return '</ol>'; },
restrictChildrenTo: ["*", "li"]
},
"pre": {
openTag: function(params,content) { return '<pre>'; },
closeTag: function(params,content) { return '</pre>'; }
},
"php": {
openTag: function(params,content) { return '<pre>'; },
closeTag: function(params,content) { return '</pre>'; },
noParse: true
},
"quote": {
openTag: function(params,content) {
// default: no author, link or date
var quoteHeader = '<div class="quoteHeader">Quote</div>';
var quote = '<div class="quote">';
// clean params
var options = params || '';
options = options.trim();
options = options.replace(/<.*?>/g,'');
// case 1: no params
if (options === '') { return quoteHeader + quote; }
// case 2: author from =params
if (options.indexOf('=') === 0) {
var equalsAuthor = options.substr(1);
quoteHeader = '<div class="quoteHeader">';
quoteHeader += 'Quote From: ' + equalsAuthor;
quoteHeader += '</div>';
return quoteHeader + quote;
}
// case 3: author="author"
if (/author="/i.test(options)) {
var refString = options.toLowerCase();
var quoteAuthor = options.substr(refString.indexOf('author="')+7);
quoteAuthor = quoteAuthor.replace(/"/g, '');
quoteHeader = '<div class="quoteHeader">';
quoteHeader += 'Quote From: ' + quoteAuthor;
quoteHeader += '</div>';
return quoteHeader + quote;
}
// case 4 & 5: parameters = author || author, link, and date
var author, link, date;
// validate that there is only one author, and optional link and date
var authorCount = options.match(/author=/gi) || [];
if (authorCount.length > 1 || authorCount.length === 0) {
return quoteHeader + quote;
}
if ((options.match(/link=/gi) || []).length > 1) {
return quoteHeader + quote;
}
if ((options.match(/date=/gi) || []).length > 1) {
return quoteHeader + quote;
}
// pull out parameters through regex
var regexArray = /^(author=.+?|link=.+?|date=[0-9]+?)?\s*(author=.+?|link=.+?|date=[0-9]+?)?\s*(author=.+?|link=.+?|date=[0-9]+?)$/i.exec(options);
// check that array exists
if (regexArray) {
// parse author link date
// the regex array output is weird:
// [0] = the matched output as a whole so skip that
for (var i = 1; i < regexArray.length; i++) {
var value = regexArray[i] || '';
value = value.trim();
if (/author=/i.test(value)) {
author = value.substr(7);
}
else if (/link=/i.test(value)) {
link = value.substr(5);
if (!smfQuotePattern.test(link) &&
!eptQuotePattern.test(link)) {
link = undefined;
}
}
else if (/date=/i.test(value)) {
date = Number(value.substr(5));
}
}
}
// author
if (author) {
quoteHeader = '<div class="quoteHeader">';
quoteHeader += 'Quote From: ' + author;
quoteHeader += '</div>';
}
// author link date
if (author && link && date) {
quoteHeader = '<div class="quoteHeader">';
quoteHeader += '<a href="' + link + '">';
quoteHeader += 'Quote from: ' + author + ' on ept-date=' + date;
quoteHeader += '</a>';
quoteHeader += '</div>';
}
return quoteHeader + quote;
},
closeTag: function(params,content) { return '</div>'; }
},
"right": {
openTag: function(params,content) {
return '<div class="bbcode-text-right" style="text-align: right;">';
},
closeTag: function(params,content) {
return '</div>';
}
},
"red": {
openTag: function(params,content) {
return '<span class="bbcode-color-red" style="color: red;">';
},
closeTag: function(params,content) {
return '</span>';
}
},
"rtl": {
openTag: function(params,content) { return '<div dir="rtl">'; },
closeTag: function(params,content) { return '</div>'; }
},
"s": {
openTag: function(params,content) { return '<del>'; },
closeTag: function(params,content) { return '</del>'; }
},
"shadow": {
openTag: function(params, content) {
var shadow = '';
var color = '';
var direction = '';
var blur = '';
var dirPattern = /^(?:left|right|top|bottom)$/;
var numberPattern = /^[0-9]\d{0,2}$/;
// params = color, direction, blur
var options = params || '';
options = params.substr(1) || '';
options = options.replace(/<.*?>/g,'');
if (options.indexOf(',') < 0) { return '<span>'; }
// color
color = options.split(',')[0];
color = color.trim();
color = color.toLowerCase();
colorNamePattern.lastIndex = 0;
colorCodePattern.lastIndex = 0;
if ( !colorNamePattern.test( color ) ) {
if ( !colorCodePattern.test( color ) ) {
color = "black";
}
else {
if (color.substr(0,1) !== "#") {
color = "#" + color;
}
}
}
// direction
direction = options.split(',')[1] || '';
direction = direction.trim();
// check if direction or angle
if (!direction) { direction = ' 0 0'; }
// direction
else if (dirPattern.test(direction)) {
// direction
if (direction === 'left') {
direction = ' -2px 2px';
}
else if (direction === 'right') {
direction = ' 2px 2px';
}
else if (direction === 'top') {
direction = ' 0 -2px';
}
else if (direction === 'bottom') {
direction = ' 0 2px';
}
else {
direction = ' 0 0';
}
}
// angle
else if (numberPattern.test(direction)) {
var angle = direction;
var radian = angle * 0.0174532925;
direction = ' ' + Math.round(4*Math.cos(radian)) + 'px';
direction += ' ' + Math.round(-4*Math.sin(radian)) + 'px';
}
else { direction = ' 0 0'; }
// blur
blur = options.split(',')[2] || '';
blur = blur.trim();
if (!blur) { blur = ' 0'; }
else if (numberPattern.test(blur)) { blur = ' ' + blur + 'px'; }
else { blur = ' 0'; }
// final output
shadow = color + direction + blur;
var simpleShadow = shadow.replace(/\s/gi, '_');
simpleShadow = simpleShadow.replace(/#/gi, '_');
return '<span class="bbcode-shadow-' + simpleShadow + '" style="text-shadow: ' + shadow + '">';
},
closeTag: function(params, content) { return '</span>'; }
},
"size": {
openTag: function(params,content) {
var size = params || '';
size = size.substr(1) || "inherit";
size = size.trim();
sizePattern.lastIndex = 0;
if (!sizePattern.test(size)) {
size = "inherit";
}
return '<span class="bbcode-size-' + size + '" style="font-size: ' + size + ' !important; line-height: 1.3em;">';
},
closeTag: function(params,content) { return '</span>'; }
},
"spoiler": {
openTag: function(params,content) { return '<span class="spoiler">'; },
closeTag: function(params,content) { return '</span>'; }
},
"sub": {
openTag: function(params,content) { return '<sub>'; },
closeTag: function(params,content) { return '</sub>'; }
},
"sup": {
openTag: function(params,content) { return '<sup>'; },
closeTag: function(params,content) { return '</sup>'; }
},
"tt": {
openTag: function(params,content) { return '<tt>'; },
closeTag: function(params,content) { return '</tt>'; }
},
"time": {
openTag: function(params, content) {
var date = content || '';
if (Number(date)) {
date = 'ept-date=' + date;
}
else {
date = new Date(date).getTime();
date = 'ept-date=' + date;
}
return date;
},
closeTag: function(params, content) { return ''; },
displayContent: false
},
"table": {
openTag: function(params,content) { return '<table>'; },
closeTag: function(params,content) { return '</table>'; },
restrictChildrenTo: ["tbody","thead", "tfoot", "tr"]
},
"tbody": {
openTag: function(params,content) { return '<tbody>'; },
closeTag: function(params,content) { return '</tbody>'; },
restrictChildrenTo: ["tr"],
restrictParentsTo: ["table"]
},
"tfoot": {
openTag: function(params,content) { return '<tfoot>'; },
closeTag: function(params,content) { return '</tfoot>'; },
restrictChildrenTo: ["tr"],
restrictParentsTo: ["table"]
},
"thead": {
openTag: function(params,content) { return '<thead>'; },
closeTag: function(params,content) { return '</thead>'; },
restrictChildrenTo: ["tr"],
restrictParentsTo: ["table"]
},
"td": {
openTag: function(params,content) { return '<td valign="top">'; },
closeTag: function(params,content) { return '</td>'; },
restrictParentsTo: ["tr"]
},
"th": {
openTag: function(params,content) { return '<th>'; },
closeTag: function(params,content) { return '</th>'; },
restrictParentsTo: ["tr"]
},
"tr": {
openTag: function(params,content) { return '<tr>'; },
closeTag: function(params,content) { return '</tr>'; },
restrictChildrenTo: ["td","th"],
restrictParentsTo: ["table","tbody","tfoot","thead"]
},
"u": {
openTag: function(params,content) {
return '<u>';
},
closeTag: function(params,content) {
return '</u>';
}
},
"ul": {
openTag: function(params,content) { return '<ul>'; },
closeTag: function(params,content) { return '</ul>'; },
restrictChildrenTo: ["*", "li"]
},
"url": {
openTag: function(params,content) {
var myUrl;
if (!params) { myUrl = content.replace(/<.*?>/g,''); }
else { myUrl = params.substr(1); }
myUrl = myUrl.trim();
urlPattern.lastIndex = 0;
if ( !urlPattern.test( myUrl ) ) {
myUrl = "#";
}
return '<a href="' + myUrl + '" target="_blank">';
},
closeTag: function(params,content) {
return '</a>';
}
},
"white": {
openTag: function(params,content) {
return '<span class="bbcode-color-white" style="color: white;">';
},
closeTag: function(params,content) {
return '</span>';
}
},
/*
The [*] tag is special since the user does not define a closing [/*] tag when writing their bbcode.
Instead this module parses the code and adds the closing [/*] tag in for them. None of the tags you
add will act like this and this tag is an exception to the others.
*/
"*": {
openTag: function(params,content) { return "<li>"; },
closeTag: function(params,content) { return "</li>"; },
restrictParentsTo: ["list","ul","ol"]
}
};
// create tag list and lookup fields
function initTags() {
tagList = [];
var prop,
ii,
len;
for (prop in tags) {
if (tags.hasOwnProperty(prop)) {
if (prop === "*") {
tagList.push("\\" + prop);
}
else {
tagList.push(prop);
if ( tags[prop].noParse ) {
tagsNoParseList.push(prop);
}
}
tags[prop].validChildLookup = {};
tags[prop].validParentLookup = {};
tags[prop].restrictParentsTo = tags[prop].restrictParentsTo || [];
tags[prop].restrictChildrenTo = tags[prop].restrictChildrenTo || [];
len = tags[prop].restrictChildrenTo.length;
for (ii = 0; ii < len; ii++) {
tags[prop].validChildLookup[ tags[prop].restrictChildrenTo[ii] ] = true;
}
len = tags[prop].restrictParentsTo.length;
for (ii = 0; ii < len; ii++) {
tags[prop].validParentLookup[ tags[prop].restrictParentsTo[ii] ] = true;
}
}
}
bbRegExp = new RegExp("<bbcl=([0-9]+) (" + tagList.join("|") + ")([ =][^>]*?)?>((?:.|[\\r\\n])*?)<bbcl=\\1 /\\2>", "gi");
pbbRegExp = new RegExp("\\[(" + tagList.join("|") + ")([ =][^\\]]*?)?\\]([^\\[]*?)\\[/\\1\\]", "gi");
pbbRegExp2 = new RegExp("\\[(" + tagsNoParseList.join("|") + ")([ =][^\\]]*?)?\\]([\\s\\S]*?)\\[/\\1\\]", "gi");
// create the regex for escaping ['s that aren't apart of tags
(function() {
var closeTagList = [];
for (var ii = 0; ii < tagList.length; ii++) {
if ( tagList[ii] !== "\\*" ) { // the * tag doesn't have an offical closing tag
closeTagList.push ( "/" + tagList[ii] );
}
}
openTags = new RegExp("(\\[)((?:" + tagList.join("|") + ")(?:[ =][^\\]]*?)?)(\\])", "gi");
closeTags = new RegExp("(\\[)(" + closeTagList.join("|") + ")(\\])", "gi");
})();
}
initTags();
// -----------------------------------------------------------------------------
// private functions
// -----------------------------------------------------------------------------
function checkParentChildRestrictions(parentTag, bbcode, bbcodeLevel, tagName, tagParams, tagContents, errQueue) {
errQueue = errQueue || [];
bbcodeLevel++;
// get a list of all of the child tags to this tag
var reTagNames = new RegExp("(<bbcl=" + bbcodeLevel + " )(" + tagList.join("|") + ")([ =>])","gi"),
reTagNamesParts = new RegExp("(<bbcl=" + bbcodeLevel + " )(" + tagList.join("|") + ")([ =>])","i"),
matchingTags = tagContents.match(reTagNames) || [],
cInfo,
errStr,
ii,
childTag,
pInfo = tags[parentTag] || {};
reTagNames.lastIndex = 0;
if (!matchingTags) { tagContents = ""; }
for (ii = 0; ii < matchingTags.length; ii++) {
reTagNamesParts.lastIndex = 0;
childTag = (matchingTags[ii].match(reTagNamesParts))[2].toLowerCase();
if ( pInfo && pInfo.restrictChildrenTo && pInfo.restrictChildrenTo.length > 0 ) {
if ( !pInfo.validChildLookup[childTag] ) {
errStr = "The tag \"" + childTag + "\" is not allowed as a child of the tag \"" + parentTag + "\".";
errQueue.push(errStr);
}
}
cInfo = tags[childTag] || {};
if ( cInfo.restrictParentsTo.length > 0 ) {
if ( !cInfo.validParentLookup[parentTag] ) {
errStr = "The tag \"" + parentTag + "\" is not allowed as a parent of the tag \"" + childTag + "\".";
errQueue.push(errStr);
}
}
}
tagContents = tagContents.replace(bbRegExp, function(matchStr, bbcodeLevel, tagName, tagParams, tagContents ) {
errQueue = checkParentChildRestrictions(tagName, matchStr, bbcodeLevel, tagName, tagParams, tagContents, errQueue);
return matchStr;
});
return errQueue;
}
/*
This function updates or adds a piece of metadata to each tag called "bbcl" which
indicates how deeply nested a particular tag was in the bbcode. This property is removed
from the HTML code tags at the end of the processing.
*/
function updateTagDepths(tagContents) {
tagContents = tagContents.replace(/\<([^\>][^\>]*?)\>/gi, function(matchStr, subMatchStr) {
var bbCodeLevel = subMatchStr.match(/^bbcl=([0-9]+) /);
if (bbCodeLevel === null) {
return "<bbcl=0 " + subMatchStr + ">";
}
else {
return "<" + subMatchStr.replace(/^(bbcl=)([0-9]+)/, function(matchStr, m1, m2) {
return m1 + (parseInt(m2, 10) + 1);
}) + ">";
}
});
return tagContents;
}
/*
This function removes the metadata added by the updateTagDepths function
*/
function unprocess(tagContent) {
return tagContent.replace(/<bbcl=[0-9]+ \/\*>/gi,"").replace(/<bbcl=[0-9]+ /gi,"[").replace(/>/gi,"]");
}