@catlab/requirejs-plugins 0.0.0-stage → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1353 @@
1
+ //>>excludeStart('excludeMdown', pragmas.excludeMdown)
2
+ //
3
+ // edited original source code to convert API and wrap into
4
+ // an AMD module, changes are before/after the wrapped code.
5
+ //
6
+ // you can replace the markdown converter as long as it
7
+ // implements the `makeHtml()` method
8
+ //
9
+ define(['require', 'exports'], function(require, exports){
10
+ // ======= START WRAP
11
+
12
+
13
+ var Markdown;
14
+
15
+ if (typeof exports === "object" && typeof require === "function") // we're in a CommonJS (e.g. Node.js) module
16
+ Markdown = exports;
17
+ else
18
+ Markdown = {};
19
+
20
+ // The following text is included for historical reasons, but should
21
+ // be taken with a pinch of salt; it's not all true anymore.
22
+
23
+ //
24
+ // Wherever possible, Showdown is a straight, line-by-line port
25
+ // of the Perl version of Markdown.
26
+ //
27
+ // This is not a normal parser design; it's basically just a
28
+ // series of string substitutions. It's hard to read and
29
+ // maintain this way, but keeping Showdown close to the original
30
+ // design makes it easier to port new features.
31
+ //
32
+ // More importantly, Showdown behaves like markdown.pl in most
33
+ // edge cases. So web applications can do client-side preview
34
+ // in Javascript, and then build identical HTML on the server.
35
+ //
36
+ // This port needs the new RegExp functionality of ECMA 262,
37
+ // 3rd Edition (i.e. Javascript 1.5). Most modern web browsers
38
+ // should do fine. Even with the new regular expression features,
39
+ // We do a lot of work to emulate Perl's regex functionality.
40
+ // The tricky changes in this file mostly have the "attacklab:"
41
+ // label. Major or self-explanatory changes don't.
42
+ //
43
+ // Smart diff tools like Araxis Merge will be able to match up
44
+ // this file with markdown.pl in a useful way. A little tweaking
45
+ // helps: in a copy of markdown.pl, replace "#" with "//" and
46
+ // replace "$text" with "text". Be sure to ignore whitespace
47
+ // and line endings.
48
+ //
49
+
50
+
51
+ //
52
+ // Usage:
53
+ //
54
+ // var text = "Markdown *rocks*.";
55
+ //
56
+ // var converter = new Markdown.Converter();
57
+ // var html = converter.makeHtml(text);
58
+ //
59
+ // alert(html);
60
+ //
61
+ // Note: move the sample code to the bottom of this
62
+ // file before uncommenting it.
63
+ //
64
+
65
+ (function () {
66
+
67
+ function identity(x) { return x; }
68
+ function returnFalse(x) { return false; }
69
+
70
+ function HookCollection() { }
71
+
72
+ HookCollection.prototype = {
73
+
74
+ chain: function (hookname, func) {
75
+ var original = this[hookname];
76
+ if (!original)
77
+ throw new Error("unknown hook " + hookname);
78
+
79
+ if (original === identity)
80
+ this[hookname] = func;
81
+ else
82
+ this[hookname] = function (x) { return func(original(x)); }
83
+ },
84
+ set: function (hookname, func) {
85
+ if (!this[hookname])
86
+ throw new Error("unknown hook " + hookname);
87
+ this[hookname] = func;
88
+ },
89
+ addNoop: function (hookname) {
90
+ this[hookname] = identity;
91
+ },
92
+ addFalse: function (hookname) {
93
+ this[hookname] = returnFalse;
94
+ }
95
+ };
96
+
97
+ Markdown.HookCollection = HookCollection;
98
+
99
+ // g_urls and g_titles allow arbitrary user-entered strings as keys. This
100
+ // caused an exception (and hence stopped the rendering) when the user entered
101
+ // e.g. [push] or [__proto__]. Adding a prefix to the actual key prevents this
102
+ // (since no builtin property starts with "s_"). See
103
+ // http://meta.stackoverflow.com/questions/64655/strange-wmd-bug
104
+ // (granted, switching from Array() to Object() alone would have left only __proto__
105
+ // to be a problem)
106
+ function SaveHash() { }
107
+ SaveHash.prototype = {
108
+ set: function (key, value) {
109
+ this["s_" + key] = value;
110
+ },
111
+ get: function (key) {
112
+ return this["s_" + key];
113
+ }
114
+ };
115
+
116
+ Markdown.Converter = function () {
117
+ var pluginHooks = this.hooks = new HookCollection();
118
+ pluginHooks.addNoop("plainLinkText"); // given a URL that was encountered by itself (without markup), should return the link text that's to be given to this link
119
+ pluginHooks.addNoop("preConversion"); // called with the orignal text as given to makeHtml. The result of this plugin hook is the actual markdown source that will be cooked
120
+ pluginHooks.addNoop("postConversion"); // called with the final cooked HTML code. The result of this plugin hook is the actual output of makeHtml
121
+
122
+ //
123
+ // Private state of the converter instance:
124
+ //
125
+
126
+ // Global hashes, used by various utility routines
127
+ var g_urls;
128
+ var g_titles;
129
+ var g_html_blocks;
130
+
131
+ // Used to track when we're inside an ordered or unordered list
132
+ // (see _ProcessListItems() for details):
133
+ var g_list_level;
134
+
135
+ this.makeHtml = function (text) {
136
+
137
+ //
138
+ // Main function. The order in which other subs are called here is
139
+ // essential. Link and image substitutions need to happen before
140
+ // _EscapeSpecialCharsWithinTagAttributes(), so that any *'s or _'s in the <a>
141
+ // and <img> tags get encoded.
142
+ //
143
+
144
+ // This will only happen if makeHtml on the same converter instance is called from a plugin hook.
145
+ // Don't do that.
146
+ if (g_urls)
147
+ throw new Error("Recursive call to converter.makeHtml");
148
+
149
+ // Create the private state objects.
150
+ g_urls = new SaveHash();
151
+ g_titles = new SaveHash();
152
+ g_html_blocks = [];
153
+ g_list_level = 0;
154
+
155
+ text = pluginHooks.preConversion(text);
156
+
157
+ // attacklab: Replace ~ with ~T
158
+ // This lets us use tilde as an escape char to avoid md5 hashes
159
+ // The choice of character is arbitray; anything that isn't
160
+ // magic in Markdown will work.
161
+ text = text.replace(/~/g, "~T");
162
+
163
+ // attacklab: Replace $ with ~D
164
+ // RegExp interprets $ as a special character
165
+ // when it's in a replacement string
166
+ text = text.replace(/\$/g, "~D");
167
+
168
+ // Standardize line endings
169
+ text = text.replace(/\r\n/g, "\n"); // DOS to Unix
170
+ text = text.replace(/\r/g, "\n"); // Mac to Unix
171
+
172
+ // Make sure text begins and ends with a couple of newlines:
173
+ text = "\n\n" + text + "\n\n";
174
+
175
+ // Convert all tabs to spaces.
176
+ text = _Detab(text);
177
+
178
+ // Strip any lines consisting only of spaces and tabs.
179
+ // This makes subsequent regexen easier to write, because we can
180
+ // match consecutive blank lines with /\n+/ instead of something
181
+ // contorted like /[ \t]*\n+/ .
182
+ text = text.replace(/^[ \t]+$/mg, "");
183
+
184
+ // Turn block-level HTML blocks into hash entries
185
+ text = _HashHTMLBlocks(text);
186
+
187
+ // Strip link definitions, store in hashes.
188
+ text = _StripLinkDefinitions(text);
189
+
190
+ text = _RunBlockGamut(text);
191
+
192
+ text = _UnescapeSpecialChars(text);
193
+
194
+ // attacklab: Restore dollar signs
195
+ text = text.replace(/~D/g, "$$");
196
+
197
+ // attacklab: Restore tildes
198
+ text = text.replace(/~T/g, "~");
199
+
200
+ text = pluginHooks.postConversion(text);
201
+
202
+ g_html_blocks = g_titles = g_urls = null;
203
+
204
+ return text;
205
+ };
206
+
207
+ function _StripLinkDefinitions(text) {
208
+ //
209
+ // Strips link definitions from text, stores the URLs and titles in
210
+ // hash references.
211
+ //
212
+
213
+ // Link defs are in the form: ^[id]: url "optional title"
214
+
215
+ /*
216
+ text = text.replace(/
217
+ ^[ ]{0,3}\[(.+)\]: // id = $1 attacklab: g_tab_width - 1
218
+ [ \t]*
219
+ \n? // maybe *one* newline
220
+ [ \t]*
221
+ <?(\S+?)>? // url = $2
222
+ (?=\s|$) // lookahead for whitespace instead of the lookbehind removed below
223
+ [ \t]*
224
+ \n? // maybe one newline
225
+ [ \t]*
226
+ ( // (potential) title = $3
227
+ (\n*) // any lines skipped = $4 attacklab: lookbehind removed
228
+ [ \t]+
229
+ ["(]
230
+ (.+?) // title = $5
231
+ [")]
232
+ [ \t]*
233
+ )? // title is optional
234
+ (?:\n+|$)
235
+ /gm, function(){...});
236
+ */
237
+
238
+ text = text.replace(/^[ ]{0,3}\[(.+)\]:[ \t]*\n?[ \t]*<?(\S+?)>?(?=\s|$)[ \t]*\n?[ \t]*((\n*)["(](.+?)[")][ \t]*)?(?:\n+)/gm,
239
+ function (wholeMatch, m1, m2, m3, m4, m5) {
240
+ m1 = m1.toLowerCase();
241
+ g_urls.set(m1, _EncodeAmpsAndAngles(m2)); // Link IDs are case-insensitive
242
+ if (m4) {
243
+ // Oops, found blank lines, so it's not a title.
244
+ // Put back the parenthetical statement we stole.
245
+ return m3;
246
+ } else if (m5) {
247
+ g_titles.set(m1, m5.replace(/"/g, "&quot;"));
248
+ }
249
+
250
+ // Completely remove the definition from the text
251
+ return "";
252
+ }
253
+ );
254
+
255
+ return text;
256
+ }
257
+
258
+ function _HashHTMLBlocks(text) {
259
+
260
+ // Hashify HTML blocks:
261
+ // We only want to do this for block-level HTML tags, such as headers,
262
+ // lists, and tables. That's because we still want to wrap <p>s around
263
+ // "paragraphs" that are wrapped in non-block-level tags, such as anchors,
264
+ // phrase emphasis, and spans. The list of tags we're looking for is
265
+ // hard-coded:
266
+ var block_tags_a = "p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del"
267
+ var block_tags_b = "p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math"
268
+
269
+ // First, look for nested blocks, e.g.:
270
+ // <div>
271
+ // <div>
272
+ // tags for inner block must be indented.
273
+ // </div>
274
+ // </div>
275
+ //
276
+ // The outermost tags must start at the left margin for this to match, and
277
+ // the inner nested divs must be indented.
278
+ // We need to do this before the next, more liberal match, because the next
279
+ // match will start at the first `<div>` and stop at the first `</div>`.
280
+
281
+ // attacklab: This regex can be expensive when it fails.
282
+
283
+ /*
284
+ text = text.replace(/
285
+ ( // save in $1
286
+ ^ // start of line (with /m)
287
+ <($block_tags_a) // start tag = $2
288
+ \b // word break
289
+ // attacklab: hack around khtml/pcre bug...
290
+ [^\r]*?\n // any number of lines, minimally matching
291
+ </\2> // the matching end tag
292
+ [ \t]* // trailing spaces/tabs
293
+ (?=\n+) // followed by a newline
294
+ ) // attacklab: there are sentinel newlines at end of document
295
+ /gm,function(){...}};
296
+ */
297
+ text = text.replace(/^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del)\b[^\r]*?\n<\/\2>[ \t]*(?=\n+))/gm, hashElement);
298
+
299
+ //
300
+ // Now match more liberally, simply from `\n<tag>` to `</tag>\n`
301
+ //
302
+
303
+ /*
304
+ text = text.replace(/
305
+ ( // save in $1
306
+ ^ // start of line (with /m)
307
+ <($block_tags_b) // start tag = $2
308
+ \b // word break
309
+ // attacklab: hack around khtml/pcre bug...
310
+ [^\r]*? // any number of lines, minimally matching
311
+ .*</\2> // the matching end tag
312
+ [ \t]* // trailing spaces/tabs
313
+ (?=\n+) // followed by a newline
314
+ ) // attacklab: there are sentinel newlines at end of document
315
+ /gm,function(){...}};
316
+ */
317
+ text = text.replace(/^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math)\b[^\r]*?.*<\/\2>[ \t]*(?=\n+)\n)/gm, hashElement);
318
+
319
+ // Special case just for <hr />. It was easier to make a special case than
320
+ // to make the other regex more complicated.
321
+
322
+ /*
323
+ text = text.replace(/
324
+ \n // Starting after a blank line
325
+ [ ]{0,3}
326
+ ( // save in $1
327
+ (<(hr) // start tag = $2
328
+ \b // word break
329
+ ([^<>])*?
330
+ \/?>) // the matching end tag
331
+ [ \t]*
332
+ (?=\n{2,}) // followed by a blank line
333
+ )
334
+ /g,hashElement);
335
+ */
336
+ text = text.replace(/\n[ ]{0,3}((<(hr)\b([^<>])*?\/?>)[ \t]*(?=\n{2,}))/g, hashElement);
337
+
338
+ // Special case for standalone HTML comments:
339
+
340
+ /*
341
+ text = text.replace(/
342
+ \n\n // Starting after a blank line
343
+ [ ]{0,3} // attacklab: g_tab_width - 1
344
+ ( // save in $1
345
+ <!
346
+ (--(?:|(?:[^>-]|-[^>])(?:[^-]|-[^-])*)--) // see http://www.w3.org/TR/html-markup/syntax.html#comments and http://meta.stackoverflow.com/q/95256
347
+ >
348
+ [ \t]*
349
+ (?=\n{2,}) // followed by a blank line
350
+ )
351
+ /g,hashElement);
352
+ */
353
+ text = text.replace(/\n\n[ ]{0,3}(<!(--(?:|(?:[^>-]|-[^>])(?:[^-]|-[^-])*)--)>[ \t]*(?=\n{2,}))/g, hashElement);
354
+
355
+ // PHP and ASP-style processor instructions (<?...?> and <%...%>)
356
+
357
+ /*
358
+ text = text.replace(/
359
+ (?:
360
+ \n\n // Starting after a blank line
361
+ )
362
+ ( // save in $1
363
+ [ ]{0,3} // attacklab: g_tab_width - 1
364
+ (?:
365
+ <([?%]) // $2
366
+ [^\r]*?
367
+ \2>
368
+ )
369
+ [ \t]*
370
+ (?=\n{2,}) // followed by a blank line
371
+ )
372
+ /g,hashElement);
373
+ */
374
+ text = text.replace(/(?:\n\n)([ ]{0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g, hashElement);
375
+
376
+ return text;
377
+ }
378
+
379
+ function hashElement(wholeMatch, m1) {
380
+ var blockText = m1;
381
+
382
+ // Undo double lines
383
+ blockText = blockText.replace(/^\n+/, "");
384
+
385
+ // strip trailing blank lines
386
+ blockText = blockText.replace(/\n+$/g, "");
387
+
388
+ // Replace the element text with a marker ("~KxK" where x is its key)
389
+ blockText = "\n\n~K" + (g_html_blocks.push(blockText) - 1) + "K\n\n";
390
+
391
+ return blockText;
392
+ }
393
+
394
+ function _RunBlockGamut(text, doNotUnhash) {
395
+ //
396
+ // These are all the transformations that form block-level
397
+ // tags like paragraphs, headers, and list items.
398
+ //
399
+ text = _DoHeaders(text);
400
+
401
+ // Do Horizontal Rules:
402
+ var replacement = "<hr />\n";
403
+ text = text.replace(/^[ ]{0,2}([ ]?\*[ ]?){3,}[ \t]*$/gm, replacement);
404
+ text = text.replace(/^[ ]{0,2}([ ]?-[ ]?){3,}[ \t]*$/gm, replacement);
405
+ text = text.replace(/^[ ]{0,2}([ ]?_[ ]?){3,}[ \t]*$/gm, replacement);
406
+
407
+ text = _DoLists(text);
408
+ text = _DoCodeBlocks(text);
409
+ text = _DoBlockQuotes(text);
410
+
411
+ // We already ran _HashHTMLBlocks() before, in Markdown(), but that
412
+ // was to escape raw HTML in the original Markdown source. This time,
413
+ // we're escaping the markup we've just created, so that we don't wrap
414
+ // <p> tags around block-level tags.
415
+ text = _HashHTMLBlocks(text);
416
+ text = _FormParagraphs(text, doNotUnhash);
417
+
418
+ return text;
419
+ }
420
+
421
+ function _RunSpanGamut(text) {
422
+ //
423
+ // These are all the transformations that occur *within* block-level
424
+ // tags like paragraphs, headers, and list items.
425
+ //
426
+
427
+ text = _DoCodeSpans(text);
428
+ text = _EscapeSpecialCharsWithinTagAttributes(text);
429
+ text = _EncodeBackslashEscapes(text);
430
+
431
+ // Process anchor and image tags. Images must come first,
432
+ // because ![foo][f] looks like an anchor.
433
+ text = _DoImages(text);
434
+ text = _DoAnchors(text);
435
+
436
+ // Make links out of things like `<http://example.com/>`
437
+ // Must come after _DoAnchors(), because you can use < and >
438
+ // delimiters in inline links like [this](<url>).
439
+ text = _DoAutoLinks(text);
440
+
441
+ text = text.replace(/~P/g, "://"); // put in place to prevent autolinking; reset now
442
+
443
+ text = _EncodeAmpsAndAngles(text);
444
+ text = _DoItalicsAndBold(text);
445
+
446
+ // Do hard breaks:
447
+ text = text.replace(/ +\n/g, " <br>\n");
448
+
449
+ return text;
450
+ }
451
+
452
+ function _EscapeSpecialCharsWithinTagAttributes(text) {
453
+ //
454
+ // Within tags -- meaning between < and > -- encode [\ ` * _] so they
455
+ // don't conflict with their use in Markdown for code, italics and strong.
456
+ //
457
+
458
+ // Build a regex to find HTML tags and comments. See Friedl's
459
+ // "Mastering Regular Expressions", 2nd Ed., pp. 200-201.
460
+
461
+ // SE: changed the comment part of the regex
462
+
463
+ var regex = /(<[a-z\/!$]("[^"]*"|'[^']*'|[^'">])*>|<!(--(?:|(?:[^>-]|-[^>])(?:[^-]|-[^-])*)--)>)/gi;
464
+
465
+ text = text.replace(regex, function (wholeMatch) {
466
+ var tag = wholeMatch.replace(/(.)<\/?code>(?=.)/g, "$1`");
467
+ tag = escapeCharacters(tag, wholeMatch.charAt(1) == "!" ? "\\`*_/" : "\\`*_"); // also escape slashes in comments to prevent autolinking there -- http://meta.stackoverflow.com/questions/95987
468
+ return tag;
469
+ });
470
+
471
+ return text;
472
+ }
473
+
474
+ function _DoAnchors(text) {
475
+ //
476
+ // Turn Markdown link shortcuts into XHTML <a> tags.
477
+ //
478
+ //
479
+ // First, handle reference-style links: [link text] [id]
480
+ //
481
+
482
+ /*
483
+ text = text.replace(/
484
+ ( // wrap whole match in $1
485
+ \[
486
+ (
487
+ (?:
488
+ \[[^\]]*\] // allow brackets nested one level
489
+ |
490
+ [^\[] // or anything else
491
+ )*
492
+ )
493
+ \]
494
+
495
+ [ ]? // one optional space
496
+ (?:\n[ ]*)? // one optional newline followed by spaces
497
+
498
+ \[
499
+ (.*?) // id = $3
500
+ \]
501
+ )
502
+ ()()()() // pad remaining backreferences
503
+ /g, writeAnchorTag);
504
+ */
505
+ text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeAnchorTag);
506
+
507
+ //
508
+ // Next, inline-style links: [link text](url "optional title")
509
+ //
510
+
511
+ /*
512
+ text = text.replace(/
513
+ ( // wrap whole match in $1
514
+ \[
515
+ (
516
+ (?:
517
+ \[[^\]]*\] // allow brackets nested one level
518
+ |
519
+ [^\[\]] // or anything else
520
+ )*
521
+ )
522
+ \]
523
+ \( // literal paren
524
+ [ \t]*
525
+ () // no id, so leave $3 empty
526
+ <?( // href = $4
527
+ (?:
528
+ \([^)]*\) // allow one level of (correctly nested) parens (think MSDN)
529
+ |
530
+ [^()]
531
+ )*?
532
+ )>?
533
+ [ \t]*
534
+ ( // $5
535
+ (['"]) // quote char = $6
536
+ (.*?) // Title = $7
537
+ \6 // matching quote
538
+ [ \t]* // ignore any spaces/tabs between closing quote and )
539
+ )? // title is optional
540
+ \)
541
+ )
542
+ /g, writeAnchorTag);
543
+ */
544
+
545
+ text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\]\([ \t]*()<?((?:\([^)]*\)|[^()])*?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, writeAnchorTag);
546
+
547
+ //
548
+ // Last, handle reference-style shortcuts: [link text]
549
+ // These must come last in case you've also got [link test][1]
550
+ // or [link test](/foo)
551
+ //
552
+
553
+ /*
554
+ text = text.replace(/
555
+ ( // wrap whole match in $1
556
+ \[
557
+ ([^\[\]]+) // link text = $2; can't contain '[' or ']'
558
+ \]
559
+ )
560
+ ()()()()() // pad rest of backreferences
561
+ /g, writeAnchorTag);
562
+ */
563
+ text = text.replace(/(\[([^\[\]]+)\])()()()()()/g, writeAnchorTag);
564
+
565
+ return text;
566
+ }
567
+
568
+ function writeAnchorTag(wholeMatch, m1, m2, m3, m4, m5, m6, m7) {
569
+ if (m7 == undefined) m7 = "";
570
+ var whole_match = m1;
571
+ var link_text = m2.replace(/:\/\//g, "~P"); // to prevent auto-linking withing the link. will be converted back after the auto-linker runs
572
+ var link_id = m3.toLowerCase();
573
+ var url = m4;
574
+ var title = m7;
575
+
576
+ if (url == "") {
577
+ if (link_id == "") {
578
+ // lower-case and turn embedded newlines into spaces
579
+ link_id = link_text.toLowerCase().replace(/ ?\n/g, " ");
580
+ }
581
+ url = "#" + link_id;
582
+
583
+ if (g_urls.get(link_id) != undefined) {
584
+ url = g_urls.get(link_id);
585
+ if (g_titles.get(link_id) != undefined) {
586
+ title = g_titles.get(link_id);
587
+ }
588
+ }
589
+ else {
590
+ if (whole_match.search(/\(\s*\)$/m) > -1) {
591
+ // Special case for explicit empty url
592
+ url = "";
593
+ } else {
594
+ return whole_match;
595
+ }
596
+ }
597
+ }
598
+ url = encodeProblemUrlChars(url);
599
+ url = escapeCharacters(url, "*_");
600
+ var result = "<a href=\"" + url + "\"";
601
+
602
+ if (title != "") {
603
+ title = attributeEncode(title);
604
+ title = escapeCharacters(title, "*_");
605
+ result += " title=\"" + title + "\"";
606
+ }
607
+
608
+ result += ">" + link_text + "</a>";
609
+
610
+ return result;
611
+ }
612
+
613
+ function _DoImages(text) {
614
+ //
615
+ // Turn Markdown image shortcuts into <img> tags.
616
+ //
617
+
618
+ //
619
+ // First, handle reference-style labeled images: ![alt text][id]
620
+ //
621
+
622
+ /*
623
+ text = text.replace(/
624
+ ( // wrap whole match in $1
625
+ !\[
626
+ (.*?) // alt text = $2
627
+ \]
628
+
629
+ [ ]? // one optional space
630
+ (?:\n[ ]*)? // one optional newline followed by spaces
631
+
632
+ \[
633
+ (.*?) // id = $3
634
+ \]
635
+ )
636
+ ()()()() // pad rest of backreferences
637
+ /g, writeImageTag);
638
+ */
639
+ text = text.replace(/(!\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeImageTag);
640
+
641
+ //
642
+ // Next, handle inline images: ![alt text](url "optional title")
643
+ // Don't forget: encode * and _
644
+
645
+ /*
646
+ text = text.replace(/
647
+ ( // wrap whole match in $1
648
+ !\[
649
+ (.*?) // alt text = $2
650
+ \]
651
+ \s? // One optional whitespace character
652
+ \( // literal paren
653
+ [ \t]*
654
+ () // no id, so leave $3 empty
655
+ <?(\S+?)>? // src url = $4
656
+ [ \t]*
657
+ ( // $5
658
+ (['"]) // quote char = $6
659
+ (.*?) // title = $7
660
+ \6 // matching quote
661
+ [ \t]*
662
+ )? // title is optional
663
+ \)
664
+ )
665
+ /g, writeImageTag);
666
+ */
667
+ text = text.replace(/(!\[(.*?)\]\s?\([ \t]*()<?(\S+?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, writeImageTag);
668
+
669
+ return text;
670
+ }
671
+
672
+ function attributeEncode(text) {
673
+ // unconditionally replace angle brackets here -- what ends up in an attribute (e.g. alt or title)
674
+ // never makes sense to have verbatim HTML in it (and the sanitizer would totally break it)
675
+ return text.replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");
676
+ }
677
+
678
+ function writeImageTag(wholeMatch, m1, m2, m3, m4, m5, m6, m7) {
679
+ var whole_match = m1;
680
+ var alt_text = m2;
681
+ var link_id = m3.toLowerCase();
682
+ var url = m4;
683
+ var title = m7;
684
+
685
+ if (!title) title = "";
686
+
687
+ if (url == "") {
688
+ if (link_id == "") {
689
+ // lower-case and turn embedded newlines into spaces
690
+ link_id = alt_text.toLowerCase().replace(/ ?\n/g, " ");
691
+ }
692
+ url = "#" + link_id;
693
+
694
+ if (g_urls.get(link_id) != undefined) {
695
+ url = g_urls.get(link_id);
696
+ if (g_titles.get(link_id) != undefined) {
697
+ title = g_titles.get(link_id);
698
+ }
699
+ }
700
+ else {
701
+ return whole_match;
702
+ }
703
+ }
704
+
705
+ alt_text = escapeCharacters(attributeEncode(alt_text), "*_[]()");
706
+ url = escapeCharacters(url, "*_");
707
+ var result = "<img src=\"" + url + "\" alt=\"" + alt_text + "\"";
708
+
709
+ // attacklab: Markdown.pl adds empty title attributes to images.
710
+ // Replicate this bug.
711
+
712
+ //if (title != "") {
713
+ title = attributeEncode(title);
714
+ title = escapeCharacters(title, "*_");
715
+ result += " title=\"" + title + "\"";
716
+ //}
717
+
718
+ result += " />";
719
+
720
+ return result;
721
+ }
722
+
723
+ function _DoHeaders(text) {
724
+
725
+ // Setext-style headers:
726
+ // Header 1
727
+ // ========
728
+ //
729
+ // Header 2
730
+ // --------
731
+ //
732
+ text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm,
733
+ function (wholeMatch, m1) { return "<h1>" + _RunSpanGamut(m1) + "</h1>\n\n"; }
734
+ );
735
+
736
+ text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm,
737
+ function (matchFound, m1) { return "<h2>" + _RunSpanGamut(m1) + "</h2>\n\n"; }
738
+ );
739
+
740
+ // atx-style headers:
741
+ // # Header 1
742
+ // ## Header 2
743
+ // ## Header 2 with closing hashes ##
744
+ // ...
745
+ // ###### Header 6
746
+ //
747
+
748
+ /*
749
+ text = text.replace(/
750
+ ^(\#{1,6}) // $1 = string of #'s
751
+ [ \t]*
752
+ (.+?) // $2 = Header text
753
+ [ \t]*
754
+ \#* // optional closing #'s (not counted)
755
+ \n+
756
+ /gm, function() {...});
757
+ */
758
+
759
+ text = text.replace(/^(\#{1,6})[ \t]*(.+?)[ \t]*\#*\n+/gm,
760
+ function (wholeMatch, m1, m2) {
761
+ var h_level = m1.length;
762
+ return "<h" + h_level + ">" + _RunSpanGamut(m2) + "</h" + h_level + ">\n\n";
763
+ }
764
+ );
765
+
766
+ return text;
767
+ }
768
+
769
+ function _DoLists(text) {
770
+ //
771
+ // Form HTML ordered (numbered) and unordered (bulleted) lists.
772
+ //
773
+
774
+ // attacklab: add sentinel to hack around khtml/safari bug:
775
+ // http://bugs.webkit.org/show_bug.cgi?id=11231
776
+ text += "~0";
777
+
778
+ // Re-usable pattern to match any entirel ul or ol list:
779
+
780
+ /*
781
+ var whole_list = /
782
+ ( // $1 = whole list
783
+ ( // $2
784
+ [ ]{0,3} // attacklab: g_tab_width - 1
785
+ ([*+-]|\d+[.]) // $3 = first list item marker
786
+ [ \t]+
787
+ )
788
+ [^\r]+?
789
+ ( // $4
790
+ ~0 // sentinel for workaround; should be $
791
+ |
792
+ \n{2,}
793
+ (?=\S)
794
+ (?! // Negative lookahead for another list item marker
795
+ [ \t]*
796
+ (?:[*+-]|\d+[.])[ \t]+
797
+ )
798
+ )
799
+ )
800
+ /g
801
+ */
802
+ var whole_list = /^(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
803
+
804
+ if (g_list_level) {
805
+ text = text.replace(whole_list, function (wholeMatch, m1, m2) {
806
+ var list = m1;
807
+ var list_type = (m2.search(/[*+-]/g) > -1) ? "ul" : "ol";
808
+
809
+ var result = _ProcessListItems(list, list_type);
810
+
811
+ // Trim any trailing whitespace, to put the closing `</$list_type>`
812
+ // up on the preceding line, to get it past the current stupid
813
+ // HTML block parser. This is a hack to work around the terrible
814
+ // hack that is the HTML block parser.
815
+ result = result.replace(/\s+$/, "");
816
+ result = "<" + list_type + ">" + result + "</" + list_type + ">\n";
817
+ return result;
818
+ });
819
+ } else {
820
+ whole_list = /(\n\n|^\n?)(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/g;
821
+ text = text.replace(whole_list, function (wholeMatch, m1, m2, m3) {
822
+ var runup = m1;
823
+ var list = m2;
824
+
825
+ var list_type = (m3.search(/[*+-]/g) > -1) ? "ul" : "ol";
826
+ var result = _ProcessListItems(list, list_type);
827
+ result = runup + "<" + list_type + ">\n" + result + "</" + list_type + ">\n";
828
+ return result;
829
+ });
830
+ }
831
+
832
+ // attacklab: strip sentinel
833
+ text = text.replace(/~0/, "");
834
+
835
+ return text;
836
+ }
837
+
838
+ var _listItemMarkers = { ol: "\\d+[.]", ul: "[*+-]" };
839
+
840
+ function _ProcessListItems(list_str, list_type) {
841
+ //
842
+ // Process the contents of a single ordered or unordered list, splitting it
843
+ // into individual list items.
844
+ //
845
+ // list_type is either "ul" or "ol".
846
+
847
+ // The $g_list_level global keeps track of when we're inside a list.
848
+ // Each time we enter a list, we increment it; when we leave a list,
849
+ // we decrement. If it's zero, we're not in a list anymore.
850
+ //
851
+ // We do this because when we're not inside a list, we want to treat
852
+ // something like this:
853
+ //
854
+ // I recommend upgrading to version
855
+ // 8. Oops, now this line is treated
856
+ // as a sub-list.
857
+ //
858
+ // As a single paragraph, despite the fact that the second line starts
859
+ // with a digit-period-space sequence.
860
+ //
861
+ // Whereas when we're inside a list (or sub-list), that line will be
862
+ // treated as the start of a sub-list. What a kludge, huh? This is
863
+ // an aspect of Markdown's syntax that's hard to parse perfectly
864
+ // without resorting to mind-reading. Perhaps the solution is to
865
+ // change the syntax rules such that sub-lists must start with a
866
+ // starting cardinal number; e.g. "1." or "a.".
867
+
868
+ g_list_level++;
869
+
870
+ // trim trailing blank lines:
871
+ list_str = list_str.replace(/\n{2,}$/, "\n");
872
+
873
+ // attacklab: add sentinel to emulate \z
874
+ list_str += "~0";
875
+
876
+ // In the original attacklab showdown, list_type was not given to this function, and anything
877
+ // that matched /[*+-]|\d+[.]/ would just create the next <li>, causing this mismatch:
878
+ //
879
+ // Markdown rendered by WMD rendered by MarkdownSharp
880
+ // ------------------------------------------------------------------
881
+ // 1. first 1. first 1. first
882
+ // 2. second 2. second 2. second
883
+ // - third 3. third * third
884
+ //
885
+ // We changed this to behave identical to MarkdownSharp. This is the constructed RegEx,
886
+ // with {MARKER} being one of \d+[.] or [*+-], depending on list_type:
887
+
888
+ /*
889
+ list_str = list_str.replace(/
890
+ (^[ \t]*) // leading whitespace = $1
891
+ ({MARKER}) [ \t]+ // list marker = $2
892
+ ([^\r]+? // list item text = $3
893
+ (\n+)
894
+ )
895
+ (?=
896
+ (~0 | \2 ({MARKER}) [ \t]+)
897
+ )
898
+ /gm, function(){...});
899
+ */
900
+
901
+ var marker = _listItemMarkers[list_type];
902
+ var re = new RegExp("(^[ \\t]*)(" + marker + ")[ \\t]+([^\\r]+?(\\n+))(?=(~0|\\1(" + marker + ")[ \\t]+))", "gm");
903
+ var last_item_had_a_double_newline = false;
904
+ list_str = list_str.replace(re,
905
+ function (wholeMatch, m1, m2, m3) {
906
+ var item = m3;
907
+ var leading_space = m1;
908
+ var ends_with_double_newline = /\n\n$/.test(item);
909
+ var contains_double_newline = ends_with_double_newline || item.search(/\n{2,}/) > -1;
910
+
911
+ if (contains_double_newline || last_item_had_a_double_newline) {
912
+ item = _RunBlockGamut(_Outdent(item), /* doNotUnhash = */true);
913
+ }
914
+ else {
915
+ // Recursion for sub-lists:
916
+ item = _DoLists(_Outdent(item));
917
+ item = item.replace(/\n$/, ""); // chomp(item)
918
+ item = _RunSpanGamut(item);
919
+ }
920
+ last_item_had_a_double_newline = ends_with_double_newline;
921
+ return "<li>" + item + "</li>\n";
922
+ }
923
+ );
924
+
925
+ // attacklab: strip sentinel
926
+ list_str = list_str.replace(/~0/g, "");
927
+
928
+ g_list_level--;
929
+ return list_str;
930
+ }
931
+
932
+ function _DoCodeBlocks(text) {
933
+ //
934
+ // Process Markdown `<pre><code>` blocks.
935
+ //
936
+
937
+ /*
938
+ text = text.replace(/
939
+ (?:\n\n|^)
940
+ ( // $1 = the code block -- one or more lines, starting with a space/tab
941
+ (?:
942
+ (?:[ ]{4}|\t) // Lines must start with a tab or a tab-width of spaces - attacklab: g_tab_width
943
+ .*\n+
944
+ )+
945
+ )
946
+ (\n*[ ]{0,3}[^ \t\n]|(?=~0)) // attacklab: g_tab_width
947
+ /g ,function(){...});
948
+ */
949
+
950
+ // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
951
+ text += "~0";
952
+
953
+ text = text.replace(/(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g,
954
+ function (wholeMatch, m1, m2) {
955
+ var codeblock = m1;
956
+ var nextChar = m2;
957
+
958
+ codeblock = _EncodeCode(_Outdent(codeblock));
959
+ codeblock = _Detab(codeblock);
960
+ codeblock = codeblock.replace(/^\n+/g, ""); // trim leading newlines
961
+ codeblock = codeblock.replace(/\n+$/g, ""); // trim trailing whitespace
962
+
963
+ codeblock = "<pre><code>" + codeblock + "\n</code></pre>";
964
+
965
+ return "\n\n" + codeblock + "\n\n" + nextChar;
966
+ }
967
+ );
968
+
969
+ // attacklab: strip sentinel
970
+ text = text.replace(/~0/, "");
971
+
972
+ return text;
973
+ }
974
+
975
+ function hashBlock(text) {
976
+ text = text.replace(/(^\n+|\n+$)/g, "");
977
+ return "\n\n~K" + (g_html_blocks.push(text) - 1) + "K\n\n";
978
+ }
979
+
980
+ function _DoCodeSpans(text) {
981
+ //
982
+ // * Backtick quotes are used for <code></code> spans.
983
+ //
984
+ // * You can use multiple backticks as the delimiters if you want to
985
+ // include literal backticks in the code span. So, this input:
986
+ //
987
+ // Just type ``foo `bar` baz`` at the prompt.
988
+ //
989
+ // Will translate to:
990
+ //
991
+ // <p>Just type <code>foo `bar` baz</code> at the prompt.</p>
992
+ //
993
+ // There's no arbitrary limit to the number of backticks you
994
+ // can use as delimters. If you need three consecutive backticks
995
+ // in your code, use four for delimiters, etc.
996
+ //
997
+ // * You can use spaces to get literal backticks at the edges:
998
+ //
999
+ // ... type `` `bar` `` ...
1000
+ //
1001
+ // Turns to:
1002
+ //
1003
+ // ... type <code>`bar`</code> ...
1004
+ //
1005
+
1006
+ /*
1007
+ text = text.replace(/
1008
+ (^|[^\\]) // Character before opening ` can't be a backslash
1009
+ (`+) // $2 = Opening run of `
1010
+ ( // $3 = The code block
1011
+ [^\r]*?
1012
+ [^`] // attacklab: work around lack of lookbehind
1013
+ )
1014
+ \2 // Matching closer
1015
+ (?!`)
1016
+ /gm, function(){...});
1017
+ */
1018
+
1019
+ text = text.replace(/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm,
1020
+ function (wholeMatch, m1, m2, m3, m4) {
1021
+ var c = m3;
1022
+ c = c.replace(/^([ \t]*)/g, ""); // leading whitespace
1023
+ c = c.replace(/[ \t]*$/g, ""); // trailing whitespace
1024
+ c = _EncodeCode(c);
1025
+ c = c.replace(/:\/\//g, "~P"); // to prevent auto-linking. Not necessary in code *blocks*, but in code spans. Will be converted back after the auto-linker runs.
1026
+ return m1 + "<code>" + c + "</code>";
1027
+ }
1028
+ );
1029
+
1030
+ return text;
1031
+ }
1032
+
1033
+ function _EncodeCode(text) {
1034
+ //
1035
+ // Encode/escape certain characters inside Markdown code runs.
1036
+ // The point is that in code, these characters are literals,
1037
+ // and lose their special Markdown meanings.
1038
+ //
1039
+ // Encode all ampersands; HTML entities are not
1040
+ // entities within a Markdown code span.
1041
+ text = text.replace(/&/g, "&amp;");
1042
+
1043
+ // Do the angle bracket song and dance:
1044
+ text = text.replace(/</g, "&lt;");
1045
+ text = text.replace(/>/g, "&gt;");
1046
+
1047
+ // Now, escape characters that are magic in Markdown:
1048
+ text = escapeCharacters(text, "\*_{}[]\\", false);
1049
+
1050
+ // jj the line above breaks this:
1051
+ //---
1052
+
1053
+ //* Item
1054
+
1055
+ // 1. Subitem
1056
+
1057
+ // special char: *
1058
+ //---
1059
+
1060
+ return text;
1061
+ }
1062
+
1063
+ function _DoItalicsAndBold(text) {
1064
+
1065
+ // <strong> must go first:
1066
+ text = text.replace(/([\W_]|^)(\*\*|__)(?=\S)([^\r]*?\S[\*_]*)\2([\W_]|$)/g,
1067
+ "$1<strong>$3</strong>$4");
1068
+
1069
+ text = text.replace(/([\W_]|^)(\*|_)(?=\S)([^\r\*_]*?\S)\2([\W_]|$)/g,
1070
+ "$1<em>$3</em>$4");
1071
+
1072
+ return text;
1073
+ }
1074
+
1075
+ function _DoBlockQuotes(text) {
1076
+
1077
+ /*
1078
+ text = text.replace(/
1079
+ ( // Wrap whole match in $1
1080
+ (
1081
+ ^[ \t]*>[ \t]? // '>' at the start of a line
1082
+ .+\n // rest of the first line
1083
+ (.+\n)* // subsequent consecutive lines
1084
+ \n* // blanks
1085
+ )+
1086
+ )
1087
+ /gm, function(){...});
1088
+ */
1089
+
1090
+ text = text.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm,
1091
+ function (wholeMatch, m1) {
1092
+ var bq = m1;
1093
+
1094
+ // attacklab: hack around Konqueror 3.5.4 bug:
1095
+ // "----------bug".replace(/^-/g,"") == "bug"
1096
+
1097
+ bq = bq.replace(/^[ \t]*>[ \t]?/gm, "~0"); // trim one level of quoting
1098
+
1099
+ // attacklab: clean up hack
1100
+ bq = bq.replace(/~0/g, "");
1101
+
1102
+ bq = bq.replace(/^[ \t]+$/gm, ""); // trim whitespace-only lines
1103
+ bq = _RunBlockGamut(bq); // recurse
1104
+
1105
+ bq = bq.replace(/(^|\n)/g, "$1 ");
1106
+ // These leading spaces screw with <pre> content, so we need to fix that:
1107
+ bq = bq.replace(
1108
+ /(\s*<pre>[^\r]+?<\/pre>)/gm,
1109
+ function (wholeMatch, m1) {
1110
+ var pre = m1;
1111
+ // attacklab: hack around Konqueror 3.5.4 bug:
1112
+ pre = pre.replace(/^ /mg, "~0");
1113
+ pre = pre.replace(/~0/g, "");
1114
+ return pre;
1115
+ });
1116
+
1117
+ return hashBlock("<blockquote>\n" + bq + "\n</blockquote>");
1118
+ }
1119
+ );
1120
+ return text;
1121
+ }
1122
+
1123
+ function _FormParagraphs(text, doNotUnhash) {
1124
+ //
1125
+ // Params:
1126
+ // $text - string to process with html <p> tags
1127
+ //
1128
+
1129
+ // Strip leading and trailing lines:
1130
+ text = text.replace(/^\n+/g, "");
1131
+ text = text.replace(/\n+$/g, "");
1132
+
1133
+ var grafs = text.split(/\n{2,}/g);
1134
+ var grafsOut = [];
1135
+
1136
+ var markerRe = /~K(\d+)K/;
1137
+
1138
+ //
1139
+ // Wrap <p> tags.
1140
+ //
1141
+ var end = grafs.length;
1142
+ for (var i = 0; i < end; i++) {
1143
+ var str = grafs[i];
1144
+
1145
+ // if this is an HTML marker, copy it
1146
+ if (markerRe.test(str)) {
1147
+ grafsOut.push(str);
1148
+ }
1149
+ else if (/\S/.test(str)) {
1150
+ str = _RunSpanGamut(str);
1151
+ str = str.replace(/^([ \t]*)/g, "<p>");
1152
+ str += "</p>"
1153
+ grafsOut.push(str);
1154
+ }
1155
+
1156
+ }
1157
+ //
1158
+ // Unhashify HTML blocks
1159
+ //
1160
+ if (!doNotUnhash) {
1161
+ end = grafsOut.length;
1162
+ for (var i = 0; i < end; i++) {
1163
+ var foundAny = true;
1164
+ while (foundAny) { // we may need several runs, since the data may be nested
1165
+ foundAny = false;
1166
+ grafsOut[i] = grafsOut[i].replace(/~K(\d+)K/g, function (wholeMatch, id) {
1167
+ foundAny = true;
1168
+ return g_html_blocks[id];
1169
+ });
1170
+ }
1171
+ }
1172
+ }
1173
+ return grafsOut.join("\n\n");
1174
+ }
1175
+
1176
+ function _EncodeAmpsAndAngles(text) {
1177
+ // Smart processing for ampersands and angle brackets that need to be encoded.
1178
+
1179
+ // Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin:
1180
+ // http://bumppo.net/projects/amputator/
1181
+ text = text.replace(/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/g, "&amp;");
1182
+
1183
+ // Encode naked <'s
1184
+ text = text.replace(/<(?![a-z\/?\$!])/gi, "&lt;");
1185
+
1186
+ return text;
1187
+ }
1188
+
1189
+ function _EncodeBackslashEscapes(text) {
1190
+ //
1191
+ // Parameter: String.
1192
+ // Returns: The string, with after processing the following backslash
1193
+ // escape sequences.
1194
+ //
1195
+
1196
+ // attacklab: The polite way to do this is with the new
1197
+ // escapeCharacters() function:
1198
+ //
1199
+ // text = escapeCharacters(text,"\\",true);
1200
+ // text = escapeCharacters(text,"`*_{}[]()>#+-.!",true);
1201
+ //
1202
+ // ...but we're sidestepping its use of the (slow) RegExp constructor
1203
+ // as an optimization for Firefox. This function gets called a LOT.
1204
+
1205
+ text = text.replace(/\\(\\)/g, escapeCharacters_callback);
1206
+ text = text.replace(/\\([`*_{}\[\]()>#+-.!])/g, escapeCharacters_callback);
1207
+ return text;
1208
+ }
1209
+
1210
+ function _DoAutoLinks(text) {
1211
+
1212
+ // note that at this point, all other URL in the text are already hyperlinked as <a href=""></a>
1213
+ // *except* for the <http://www.foo.com> case
1214
+
1215
+ // automatically add < and > around unadorned raw hyperlinks
1216
+ // must be preceded by space/BOF and followed by non-word/EOF character
1217
+ text = text.replace(/(^|\s)(https?|ftp)(:\/\/[-A-Z0-9+&@#\/%?=~_|\[\]\(\)!:,\.;]*[-A-Z0-9+&@#\/%=~_|\[\]])($|\W)/gi, "$1<$2$3>$4");
1218
+
1219
+ // autolink anything like <http://example.com>
1220
+
1221
+ var replacer = function (wholematch, m1) { return "<a href=\"" + m1 + "\">" + pluginHooks.plainLinkText(m1) + "</a>"; }
1222
+ text = text.replace(/<((https?|ftp):[^'">\s]+)>/gi, replacer);
1223
+
1224
+ // Email addresses: <address@domain.foo>
1225
+ /*
1226
+ text = text.replace(/
1227
+ <
1228
+ (?:mailto:)?
1229
+ (
1230
+ [-.\w]+
1231
+ \@
1232
+ [-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+
1233
+ )
1234
+ >
1235
+ /gi, _DoAutoLinks_callback());
1236
+ */
1237
+
1238
+ /* disabling email autolinking, since we don't do that on the server, either
1239
+ text = text.replace(/<(?:mailto:)?([-.\w]+\@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi,
1240
+ function(wholeMatch,m1) {
1241
+ return _EncodeEmailAddress( _UnescapeSpecialChars(m1) );
1242
+ }
1243
+ );
1244
+ */
1245
+ return text;
1246
+ }
1247
+
1248
+ function _UnescapeSpecialChars(text) {
1249
+ //
1250
+ // Swap back in all the special characters we've hidden.
1251
+ //
1252
+ text = text.replace(/~E(\d+)E/g,
1253
+ function (wholeMatch, m1) {
1254
+ var charCodeToReplace = parseInt(m1);
1255
+ return String.fromCharCode(charCodeToReplace);
1256
+ }
1257
+ );
1258
+ return text;
1259
+ }
1260
+
1261
+ function _Outdent(text) {
1262
+ //
1263
+ // Remove one level of line-leading tabs or spaces
1264
+ //
1265
+
1266
+ // attacklab: hack around Konqueror 3.5.4 bug:
1267
+ // "----------bug".replace(/^-/g,"") == "bug"
1268
+
1269
+ text = text.replace(/^(\t|[ ]{1,4})/gm, "~0"); // attacklab: g_tab_width
1270
+
1271
+ // attacklab: clean up hack
1272
+ text = text.replace(/~0/g, "")
1273
+
1274
+ return text;
1275
+ }
1276
+
1277
+ function _Detab(text) {
1278
+ if (!/\t/.test(text))
1279
+ return text;
1280
+
1281
+ var spaces = [" ", " ", " ", " "],
1282
+ skew = 0,
1283
+ v;
1284
+
1285
+ return text.replace(/[\n\t]/g, function (match, offset) {
1286
+ if (match === "\n") {
1287
+ skew = offset + 1;
1288
+ return match;
1289
+ }
1290
+ v = (offset - skew) % 4;
1291
+ skew = offset + 1;
1292
+ return spaces[v];
1293
+ });
1294
+ }
1295
+
1296
+ //
1297
+ // attacklab: Utility functions
1298
+ //
1299
+
1300
+ var _problemUrlChars = /(?:["'*()[\]:]|~D)/g;
1301
+
1302
+ // hex-encodes some unusual "problem" chars in URLs to avoid URL detection problems
1303
+ function encodeProblemUrlChars(url) {
1304
+ if (!url)
1305
+ return "";
1306
+
1307
+ var len = url.length;
1308
+
1309
+ return url.replace(_problemUrlChars, function (match, offset) {
1310
+ if (match == "~D") // escape for dollar
1311
+ return "%24";
1312
+ if (match == ":") {
1313
+ if (offset == len - 1 || /[0-9\/]/.test(url.charAt(offset + 1)))
1314
+ return ":"
1315
+ }
1316
+ return "%" + match.charCodeAt(0).toString(16);
1317
+ });
1318
+ }
1319
+
1320
+
1321
+ function escapeCharacters(text, charsToEscape, afterBackslash) {
1322
+ // First we have to escape the escape characters so that
1323
+ // we can build a character class out of them
1324
+ var regexString = "([" + charsToEscape.replace(/([\[\]\\])/g, "\\$1") + "])";
1325
+
1326
+ if (afterBackslash) {
1327
+ regexString = "\\\\" + regexString;
1328
+ }
1329
+
1330
+ var regex = new RegExp(regexString, "g");
1331
+ text = text.replace(regex, escapeCharacters_callback);
1332
+
1333
+ return text;
1334
+ }
1335
+
1336
+
1337
+ function escapeCharacters_callback(wholeMatch, m1) {
1338
+ var charCodeToEscape = m1.charCodeAt(0);
1339
+ return "~E" + charCodeToEscape + "E";
1340
+ }
1341
+
1342
+ }; // end of the Markdown.Converter constructor
1343
+
1344
+ })();
1345
+
1346
+
1347
+ // ======= END WRAP
1348
+
1349
+ // no reason for multiple instances,
1350
+ // just call `makeHtml`
1351
+ return new Markdown.Converter();
1352
+ });
1353
+ //>>excludeEnd('excludeMdown')