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