-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsnuownd.js
3950 lines (3202 loc) · 104 KB
/
snuownd.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
/**
@preserve snuownd.js - javascript port of reddit's "snudown" markdown parser
https://github.com/gamefreak/snuownd
*/
/**
* @license Copyright (c) 2009, Natacha Porté
* Copyright (c) 2011, Vicent Marti
* Copyright (c) 2012, Scott McClaugherty
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
// up to date with commit e518f5077b072599dea87596f4ce3c1d03e443c0
/**
@module SnuOwnd
*/
(function(exports){
function _isspace(c) {return c == ' ' || c == '\n';}
function isspace(c) {return /[\x09-\x0d ]/.test(c);}
function isalnum(c) { return /[A-Za-z0-9]/.test(c); }
function isalpha(c) { return /[A-Za-z]/.test(c); }
function isdigit(c) { return /[0-9]/.test(c); }
function isxdigit(c) { return /[0-9a-fA-F]/.test(c); }
function ispunct(c) {return /[\x20-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]/.test(c); }
function urlHexCode(number) {
var hex_str = '0123456789ABCDEF';
return '%'+hex_str[(number&0xf0)>>4]+hex_str[(number&0x0f)>>0];
}
function escapeUTF8Char(char) {
var code = char.charCodeAt(0);
if (code < 0x80) {
return urlHexCode(code);
} else if((code > 0x7f) && (code < 0x0800)) {
var seq = urlHexCode(code >> 6 & 0xff | 0xc0);
seq += urlHexCode(code >> 0 & 0x3f | 0x80);
return seq;
} else {
var seq = urlHexCode(code >> 12 & 0xff | 0xe0);
seq += urlHexCode(code >> 6 & 0x3f | 0x80);
seq += urlHexCode(code >> 0 & 0x3f | 0x80);
return seq;
}
}
function find_block_tag (str) {
var wordList = [
'p', 'dl', 'div', 'math',
'table', 'ul', 'del', 'form',
'blockquote', 'figure', 'ol', 'fieldset',
'h1', 'h6', 'pre', 'script',
'h5', 'noscript', 'style', 'iframe',
'h4', 'ins', 'h3', 'h2'
];
if (wordList.indexOf(str.toLowerCase()) != -1) {
return str.toLowerCase();
}
return '';
}
function sdhtml_is_tag(tag_data, tagname) {
var i;
var closed = 0;
var tag_size = tag_data.length;
if (tag_size < 3 || tag_data[0] != '<') return HTML_TAG_NONE;
i = 1;
if (tag_data[i] == '/') {
closed = 1;
i++;
}
var tagname_c = 0;
for (; i < tag_size; ++i, ++tagname_c) {
if (tagname_c >= tagname.length) break;
if (tag_data[i] != tagname[tagname_c]) return HTML_TAG_NONE;
}
if (i == tag_size) return HTML_TAG_NONE;
if (isspace(tag_data[i]) || tag_data[i] == '>')
return closed ? HTML_TAG_CLOSE : HTML_TAG_OPEN;
return HTML_TAG_NONE;
}
function unscape_text(out, src) {
var i = 0, org;
while (i < src.s.length) {
org = i;
while (i < src.s.length && src.s[i] != '\\') i++;
if (i > org) out.s += src.s.slice(org, i);
if (i + 1 >= src.s.length) break;
out.s += src.s[i + 1];
i += 2;
}
}
// Parsers tend to choke on entities with values greater than this
var MAX_NUM_ENTITY_VAL = 0x10ffff;
function is_valid_numeric_entity(entity_val) {
/* Some XML parsers will choke on entities with certain
* values (mostly control characters.)
*
* According to lxml these are all problematic:
*
* [xrange(0, 8),
* xrange(11, 12),
* xrange(14, 31),
* xrange(55296, 57343),
* xrange(65534, 65535)]
*/
return (entity_val > 8
&& (entity_val !== 11 && entity_val > 12)
&& (entity_val < 14 || entity_val > 31)
&& (entity_val < 55296 || entity_val > 57343)
&& (entity_val !== 65534 && entity_val !== 65535)
&& entity_val <= MAX_NUM_ENTITY_VAL);
}
// Any numeric entity longer than this is obviously above MAX_ENTITY_CHAR
// used to avoid dealing with overflows.
var MAX_NUM_ENTITY_LEN = 7;
var VALID_ENTITIES = [
'Æ', 'Á', 'Â', 'À', 'Α', 'Å',
'Ã', 'Ä', 'Β', 'Ç', 'Χ', '‡', 'Δ',
'Ð', 'É', 'Ê', 'È', 'Ε', 'Η', 'Ë',
'Γ', 'Í', 'Î', 'Ì', 'Ι', 'Ï', 'Κ',
'Λ', 'Μ', 'Ñ', 'Ν', 'Œ', 'Ó', 'Ô',
'Ò', 'Ω', 'Ο', 'Ø', 'Õ', 'Ö', 'Φ',
'Π', '″', 'Ψ', 'Ρ', 'Š', 'Σ', 'Þ', 'Τ',
'Θ', 'Ú', 'Û', 'Ù', 'Υ', 'Ü', 'Ξ',
'Ý', 'Ÿ', 'Ζ', 'á', 'â', '´', 'æ',
'à', 'ℵ', 'α', '&', '∧', '∠', ''',
'å', '≈', 'ã', 'ä', '„', 'β', '¦',
'•', '∩', 'ç', '¸', '¢', 'χ', 'ˆ',
'♣', '≅', '©', '↵', '∪', '¤', '⇓',
'†', '↓', '°', 'δ', '♦', '÷',
'é', 'ê', 'è', '∅', ' ', ' ',
'ε', '≡', 'η', 'ð', 'ë', '€', '∃',
'ƒ', '∀', '½', '¼', '¾', '⁄',
'γ','≥', '>', '⇔', '↔', '♥', '…',
'í', 'î', '¡', 'ì', 'ℑ',
'∞',
'∫', 'ι', '¿', '∈', 'ï', 'κ', '⇐',
'λ', '⟨', '«', '←', '⌈', '“', '≤',
'⌊', '∗', '◊', '‎', '‹', '‘', '<',
'¯', '—', 'µ', '·', '−', 'μ', '∇',
' ', '–', '≠', '∋', '¬', '∉', '⊄', 'ñ',
'ν', 'ó', 'ô', 'œ', 'ò', '‾', 'ω',
'ο', '⊕', '∨', 'ª', 'º', 'ø', 'õ',
'⊗', 'ö', '¶', '∂', '‰', '⊥', 'φ',
'π', 'ϖ', '±', '£', '′', '∏', '∝',
'ψ', '"', '⇒', '√', '⟩', '»', '→',
'⌉', '”', 'ℜ', '®', '⌋', 'ρ', '‏',
'›', '’', '‚', 'š', '⋅', '§', '­',
'σ', 'ς', '∼', '♠', '⊂', '⊆', '∑',
'¹', '²', '³', '⊃', '⊇', 'ß', 'τ',
'∴', 'θ', 'ϑ', ' ', 'þ', '˜',
'×', '™', '⇑', 'ú', '↑', 'û', 'ù',
'¨', 'ϒ', 'υ', 'ü', '℘', 'ξ', 'ý',
'¥', 'ÿ', 'ζ', '‍', '‌',
];
/**
* According to the OWASP rules:
*
* & --> &
* < --> <
* > --> >
* " --> "
* ' --> ' ' is not recommended
* / --> / forward slash is included as it helps end an HTML entity
*
*/
var HTML_ESCAPE_TABLE = [
7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 7, 7, 0, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
0, 0, 1, 0, 0, 0, 2, 3, 0, 0, 0, 0, 0, 0, 0, 4,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
];
var HTML_ESCAPES = ["", """, "&", "'", "/", "<", ">", "" /* throw out control characters */ ];
function escape_html(out, src, secure) {
var i = 0, org, esc = 0;
while (i < src.length) {
org = i;
while (i < src.length && !(esc = HTML_ESCAPE_TABLE[src.charCodeAt(i)]))
i++;
if (i > org) out.s += src.slice(org, i);
/* escaping */
if (i >= src.length) break;
/* The forward slash is only escaped in secure mode */
if (src[i] == '/' && !secure) {
out.s += '/';
} else if (HTML_ESCAPE_TABLE[src.charCodeAt(i)] == 7) {
/* skip control characters */
} else {
out.s += HTML_ESCAPES[esc];
}
i++;
}
}
/*
* The following characters will not be escaped:
*
* -_.+!*'(),%#@?=;:/,+&$ alphanum
*
* Note that this character set is the addition of:
*
* - The characters which are safe to be in an URL
* - The characters which are *not* safe to be in
* an URL because they are RESERVED characters.
*
* We asume (lazily) that any RESERVED char that
* appears inside an URL is actually meant to
* have its native function (i.e. as an URL
* component/separator) and hence needs no escaping.
*
* There are two exceptions: the chacters & (amp)
* and ' (single quote) do not appear in the table.
* They are meant to appear in the URL as components,
* yet they require special HTML-entity escaping
* to generate valid HTML markup.
*
* All other characters will be escaped to %XX.
*
*/
var HREF_SAFE = [
2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 2, 2, 0, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
];
function escape_href(out, src) {
var i = 0, org;
while (i < src.length) {
org = i;
/* Skip by characters that don't need special processing */
while (i < src.length && HREF_SAFE[src.charCodeAt(i)] === 1) i++;
if (i > org) out.s += src.slice(org, i);
/* escaping */
if (i >= src.length) break;
/* throw out control characters */
if (HREF_SAFE[src.charCodeAt(i)] == 2) {
i++;
continue;
}
switch (src[i]) {
/* amp appears all the time in URLs, but needs
* HTML-entity escaping to be inside an href */
case '&':
out.s += '&';
break;
/* the single quote is a valid URL character
* according to the standard; it needs HTML
* entity escaping too */
case '\'':
out.s += ''';
break;
/* the space can be escaped to %20 or a plus
* sign. we're going with the generic escape
* for now. the plus thing is more commonly seen
* when building GET strings */
/*
//This was disabled
case ' ':
out.s += '+'
break;
//*/
/* every other character goes with a %XX escaping */
default:
out.s += escapeUTF8Char(src[i]);
/*
var cc = src.charCodeAt(i);
hex_str[1] = hex_chars[(cc >> 4) & 0xF];
hex_str[2] = hex_chars[cc & 0xF];
out.s += hex_str.join('');
*/
}
i++;
}
}
// function autolink_delim(uint8_t *data, size_t link_end, size_t offset, size_t size)
function autolink_delim(data_, link_end, offset, size) {
var data = data_.slice(offset)
var cclose, copen = 0;
var i;
for (i = 0; i < link_end; ++i)
if (data[i] == '<') {
link_end = i;
break;
}
while (link_end > 0) {
var c = data[link_end - 1];
if (c === "\0") break;
if ("?!.,".indexOf(c) !== -1) {
link_end--;
} else if (c === ';') {
var new_end = link_end - 2;
while (new_end > 0 && isalpha(data[new_end])) new_end--;
if (new_end < link_end - 2 && data[new_end] == '&')
link_end = new_end;
else link_end--;
}
else break;
}
if (link_end == 0) return 0;
cclose = data[link_end - 1];
switch (cclose) {
case '"': copen = '"'; break;
case '\'': copen = '\''; break;
case ')': copen = '('; break;
case ']': copen = '['; break;
case '}': copen = '{'; break;
}
if (copen != 0) {
var closing = 0;
var opening = 0;
var j = 0;
/* Try to close the final punctuation sign in this same line;
* if we managed to close it outside of the URL, that means that it's
* not part of the URL. If it closes inside the URL, that means it
* is part of the URL.
*
* Examples:
*
* foo http://www.pokemon.com/Pikachu_(Electric) bar
* => http://www.pokemon.com/Pikachu_(Electric)
*
* foo (http://www.pokemon.com/Pikachu_(Electric)) bar
* => http://www.pokemon.com/Pikachu_(Electric)
*
* foo http://www.pokemon.com/Pikachu_(Electric)) bar
* => http://www.pokemon.com/Pikachu_(Electric))
*
* (foo http://www.pokemon.com/Pikachu_(Electric)) bar
* => foo http://www.pokemon.com/Pikachu_(Electric)
*/
while (j < link_end) {
if (data[j] == copen) opening++;
else if (data[j] == cclose) closing++;
j++;
}
if (closing != opening) link_end--;
}
return link_end;
}
function check_domain(data, allow_short) {
var i, np = 0;
if (!isalnum(data[0])) return 0;
for (i = 1; i < data.length - 1; ++i) {
if (data[i] == '.') np++;
else if (!isalnum(data[i]) && data[i] != '-') break;
}
/* a valid domain needs to have at least a dot.
* that's as far as we get */
if (allow_short) {
/* We don't need a valid domain in the strict sence (with
* at least one dot; so just make sure it's composed of valid
* domain characters and return the length of the valid
* sequence. */
return i;
} else {
return np ? i : 0;
}
}
function sd_autolink_issafe(link) {
var valid_uris = [
"http://", "https://", "ftp://", "mailto://",
"/", "git://", "steam://", "irc://", "news://", "mumble://",
"ssh://", "ircs://", "ts3server://", "#"];
var i;
for (i = 0; i < valid_uris.length; ++i) {
var len = valid_uris[i].length;
if (link.length > len &&
link.toLowerCase().indexOf(valid_uris[i]) == 0 &&
/[A-Za-z0-9#\/?]/.test(link[len]))
return 1;
}
return 0;
}
function sd_autolink__url(rewind_p, link, data_, offset, size, flags) {
var data = data_.slice(offset);
var link_end, rewind = 0, domain_len;
if (size < 4 || data_[offset+1] != '/' || data_[offset+2] != '/') return 0;
while (rewind < offset && isalpha(data_[offset-rewind - 1])) rewind++;
if (!sd_autolink_issafe(data_.substr(offset-rewind, size+rewind))) return 0;
link_end = "://".length;
domain_len = check_domain(data.slice(link_end), flags & SD_AUTOLINK_SHORT_DOMAINS);
if (domain_len == 0) return 0;
link_end += domain_len;
while (link_end < size && !isspace(data_[offset+link_end])) link_end++;
link_end = autolink_delim(data_, link_end, offset, size);
if (link_end == 0) return 0;
//TODO
link.s += data_.substr(offset-rewind, link_end+rewind);
rewind_p.p = rewind;
return link_end;
}
function sd_autolink__subreddit(rewind_p, link, data_, offset, size) {
var data = data_.slice(offset);
var link_end;
var allMinus = false;
if (size < 3) return 0;
/* make sure this / is part of /r/ */
if (data.indexOf('/r/') != 0) return 0;
link_end = "/r/".length;
if (data.substr(link_end-1, 4).toLowerCase() == "all-") {
allMinus = true;
}
do {
var start = link_end;
var max_length = 24;
/* special case: /r/reddit.com (the only subreddit with a '.') */
if ( size >= link_end+10 && data.substr(link_end, 10).toLowerCase() == 'reddit.com') {
link_end += 10;
max_length = 10;
} else {
/* If not the special case make sure it starts with (t:)?[A-Za-z0-9] */
/* support autolinking to timereddits, /r/t:when (1 April 2012) */
if ( size > link_end+2 && data.substr(link_end, 2) == 't:')
link_end += 2; /* Jump over the 't:' */
/* the first character of a subreddit name must be a letter or digit */
if (!isalnum(data[link_end]))
return 0;
link_end += 1;
}
/* consume valid characters ([A-Za-z0-9_]) until we run out */
while (link_end < size && (isalnum(data[link_end]) ||
data[link_end] == '_'))
link_end++;
/* valid subreddit names are between 3 and 21 characters, with
* some subreddits having 2-character names. Don't bother with
* autolinking for anything outside this length range.
* (chksrname function in reddit/.../validator.py) */
if ( link_end-start < 2 || link_end-start > max_length )
return 0;
/* If we are linking to a multireddit, continue */
} while ( link_end < size && (data[link_end] == '+' || (allMinus && data[link_end] == '-')) && link_end++ );
if (link_end < size && data[link_end] == '/') {
while (link_end < size && (isalnum(data[link_end]) ||
data[link_end] == '_' ||
data[link_end] == '/' ||
data[link_end] == '-')) {
link_end++;
}
}
/* make the link */
link.s += data.slice(0, link_end);
rewind_p.p = 0;
return link_end;
}
function sd_autolink__username(rewind_p, link, data_, offset, size) {
var data = data_.slice(offset);
var link_end;
if (size < 6) return 0;
/* make sure this / is part of /u/ */
if (data.indexOf('/u/') != 0) return 0;
/* the first letter of a username must... well, be valid, we don't care otherwise */
link_end = "/u/".length;
if (!isalnum(data[link_end]) && data[link_end] != '_' && data[link_end] != '-')
return 0;
link_end += 1;
/* consume valid characters ([A-Za-z0-9_-/]) until we run out */
while (link_end < size && (isalnum(data[link_end]) ||
data[link_end] == '_' ||
data[link_end] == '/' ||
data[link_end] == '-'))
link_end++;
/* make the link */
link.s += data.slice(0, link_end);
rewind_p.p = 0;
return link_end;
}
function sd_autolink__email(rewind_p, link, data_, offset, size, flags) {
var data = data_.slice(offset);
var link_end, rewind;
var nb = 0, np = 0;
for (rewind = 0; rewind < offset; ++rewind) {
var c = data_[offset-rewind - 1];
if (c == '\0') break;
if (isalnum(c)) continue;
if (".+-_".indexOf(c) != -1) continue;
break;
}
if (rewind == 0) return 0;
for (link_end = 0; link_end < size; ++link_end) {
var c = data_[offset+link_end];
if (isalnum(c)) continue;
if (c == '@') nb++;
else if (c == '.' && link_end < size - 1) np++;
else if (c != '-' && c != '_') break;
}
if (link_end < 2 || nb != 1 || np == 0) return 0;
//TODO
link_end = autolink_delim(data_, link_end, offset, size);
if (link_end == 0) return 0;
// link.s += data_.slice(offset - rewind, link_end + rewind
link.s += data_.substr(offset - rewind, link_end + rewind);
rewind_p.p = rewind;
return link_end;
}
function sd_autolink__www(rewind_p, link, data_, offset, size, flags) {
var data = data_.slice(offset);
var link_end;
if (offset > 0 && !ispunct(data_[offset-1]) && !isspace(data_[offset-1]))
return 0;
// if (size < 4 || memcmp(data, "www.", strlen("www.")) != 0)
if (size < 4 || (data.slice(0,4) != 'www.')) return 0;
link_end = check_domain(data, 0);
if (link_end == 0)
return 0;
while (link_end < size && !isspace(data[link_end])) link_end++;
link_end = autolink_delim(data_, link_end, offset, size);
if (link_end == 0) return 0;
link.s += data.slice(0, link_end);
rewind_p.p = 0;
return link_end;
}
/**
Initialize a Callbacks object.
@constructor
@param {Object.<string, ?function>} callbacks A set of callbacks to use as the methods on this object.
*/
function Callbacks(callbacks) {
if (callbacks) {
for (var name in callbacks) {
if (name in this) this[name] = callbacks[name];
}
}
}
Callbacks.prototype = {
/**
Renders a code block.
Syntax highlighting specific to lanugage may be performed here.
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The input text.
@param {Buffer} language The name of the code langage.
@param {?Object} context A renderer specific context object.
*/
blockcode: null,
/**
Renders a blockquote.
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The input text.
@param {?Object} context A renderer specific context object.
*/
blockquote: null,
/**
Renders a block of HTML code.
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The input text.
@param {?Object} context A renderer specific context object.
*/
blockhtml: null,
/**
Renders a header.
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The input text.
@param {Number} level The header level.
@param {?Object} context A renderer specific context object.
*/
header: null,
/**
Renders a horizontal rule.
@method
@param {Buffer} out The output string buffer to append to.
@param {?Object} context A renderer specific context object.
*/
hrule: null,
/**
Renders a list.
<p>
This method handles the list wrapper, which in terms of HTML would be <ol> or <ul>.
This method is not responsible for handling list elements, all such processing should
already have occured on text pased to the method . All that it is intended
to do is to wrap the text parameter in anything needed.
</p>
@example
out.s += "<ul>" + text.s + "</ul>"
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The input that goes inside the list.
@param {Number} flags A bitfield holding a portion of the render state. The only bit that this should be concerned with is MKD_LIST_ORDERED
@param {?Object} context A renderer specific context object.
*/
list: null,
/**
Renders a list.
<p>
Wraps the text in a list element.
</p>
@example
out.s += "<li>" + text.s + "</li>"
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The contents of the list element.
@param {Number} flags A bitfield holding a portion of the render state. The only bit that this should be concerned with is MKD_LI_BLOCK.
@param {?Object} context A renderer specific context object.
*/
listitem: null,
/**
Renders a paragraph.
@example
out.s += "<p>" + text.s + "</p>";
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The input text.
@param {?Object} context A renderer specific context object.
*/
paragraph: null,
/**
Renders a table.
@example
out.s += "<table><thead>";
out.s += header.s;
out.s += "</thead><tbody>";
out.s += body.s;
out.s += "</tbody></table>";
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} head The table header.
@param {Buffer} body The table body.
@param {?Object} context A renderer specific context object.
*/
table: null,
/**
Renders a table row.
@example
out.s += "<tr>" + text.s + "</tr>";
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The input text.
@param {?Object} context A renderer specific context object.
*/
table_row: null,
/**
Renders a table cell.
@example
out.s += "<td>" + text.s + "</td>";
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The input text.
@param {Number} flags A bit filed indicating a portion of the output state. Relevant bits are: MKD_TABLE_HEADER, MKD_TABLE_ALIGN_CENTER. MKD_TABLE_ALIGN_L, and MKD_TABLE_ALIGN_R.
@param {?Object} context A renderer specific context object.
*/
table_cell: null,
/**
Renders a link that was autodetected.
@example
out.s += "<a href=\""+ text.s + "\">" + text.s + "</a>";
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The address being linked to.
@param {Number} type Equal to MKDA_NORMAL or MKDA_EMAIL
@param {?Object} context A renderer specific context object.
@returns {Boolean} Whether or not the tag was rendered.
*/
autolink: null,
/**
Renders inline code.
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The text being wrapped.
@param {?Object} context A renderer specific context object.
@returns {Boolean} Whether or not the tag was rendered.
*/
codespan: null,
/**
Renders text with double emphasis. Default is equivalent to the HTML <strong> tag.
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The text being wrapped.
@param {?Object} context A renderer specific context object.
@returns {Boolean} Whether or not the tag was rendered.
*/
double_emphasis: null,
/**
Renders text with single emphasis. Default is equivalent to the HTML <em> tag.
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The text being wrapped.
@param {?Object} context A renderer specific context object.
@returns {Boolean} Whether or not the tag was rendered.
*/
emphasis: null,
/**
Renders an image.
@example
out.s = "<img src=\"" + link.s + "\" title=\"" + title.s + "\" alt=\"" + alt.s + "\"/>";"
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} link The address of the image.
@param {Buffer} title Title text for the image
@param {Buffer} alt Alt text for the image
@param {?Object} context A renderer specific context object.
@returns {Boolean} Whether or not the tag was rendered.
*/
image: null,
/**
Renders line break.
@example
out.s += "<br/>";
@method
@param {Buffer} out The output string buffer to append to.
@param {?Object} context A renderer specific context object.
@returns {Boolean} Whether or not the tag was rendered.
*/
linebreak: null,
/**
Renders a link.
@example
out.s = "<a href=\"" + link.s + "\" title=\"" + title.s + "\">" + content.s + "</a>";
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} link The link address.
@param {Buffer} title Title text for the link.
@param {Buffer} content Link text.
@param {?Object} context A renderer specific context object.
@returns {Boolean} Whether or not the tag was rendered.
*/
link: null,
/**
Copies and potentially escapes some HTML.
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The input text.
@param {?Object} context A renderer specific context object.
@returns {Boolean} Whether or not the tag was rendered.
*/
raw_html_tag: null,
/**
Renders text with triple emphasis. Default is equivalent to both the <em> and <strong> HTML tags.
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The text being wrapped.
@param {?Object} context A renderer specific context object.
@returns {Boolean} Whether or not the tag was rendered.
*/
triple_emphasis: null,
/**
Renders text crossd out.
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The text being wrapped.
@param {?Object} context A renderer specific context object.
@returns {Boolean} Whether or not the tag was rendered.
*/
strikethrough: null,
/**
Renders text as superscript.
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The text being wrapped.
@param {?Object} context A renderer specific context object.
@returns {Boolean} Whether or not the tag was rendered.
*/
superscript: null,
/**
Escapes an HTML entity.
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The text being wrapped.
@param {?Object} context A renderer specific context object.
*/
entity: null,
/**
Renders plain text.
@method
@param {Buffer} out The output string buffer to append to.
@param {Buffer} text The text being rendered.
@param {?Object} context A renderer specific context object.
*/
normal_text: null,
/**
Creates opening boilerplate for a table of contents.
@method
@param {Buffer} out The output string buffer to append to.
@param {?Object} context A renderer specific context object.
*/
doc_header: null,
/**
Creates closing boilerplate for a table of contents.
@method
@param {Buffer} out The output string buffer to append to.
@param {?Object} context A renderer specific context object.
*/
doc_footer: null
};
/**
A renderer object
@constructor
@param {Callbacks} callbacks The callbacks object to use for the renderer.
@param {?Callbacks} context Renderer specific context information.
*/
function Renderer(callbacks, context) {
this.callbacks = callbacks;
this.context = context;
}
/**
Instantiates a custom Renderer object.
@param {Callbacks} callbacks The callbacks object to use for the renderer.
@param {?Callbacks} context Renderer specific context information.