xonecas.io
818
Du

dustymarkdown / files

dustymarkdown / node_modules / node-markdown / lib / vendor / showdown / src / showdown.js

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