ux/Showdown.js
[roojs1] / ux / Showdown.js
index 02ea74b..3af364f 100644 (file)
 
 //
 // Showdown usage:
-//
-//   var text = "Markdown *rocks*.";
-//
-//   var html = Roo.ux.Showdown.makeHtml(text);
-//
-//   alert(html);
-//
+// 
+//   alert( Roo.ux.Showdown.toHtml("Markdown *rocks*.") );
+// 
 // Note: move the sample code to the bottom of this
 // file before uncommenting it.
 //
@@ -65,7 +61,7 @@
 //
 Roo.namespace('Roo.ux'); 
 Roo.ux.Showdown = {};
-Roo.ux.Showdown.makeHTML = function(text) {
+Roo.ux.Showdown.toHtml = function(text) {
     var c = new Roo.ux.Showdown.converter();
     return c.makeHtml(text);
 };
@@ -91,14 +87,14 @@ Roo.ux.Showdown.converter = function() {
     var g_list_level = 0;
     
     
-    this.makeHtml = function(text) {
+    this.makeHtml = function(_text) {
     //
     // Main function. The order in which other subs are called here is
     // essential. Link and image substitutions need to happen before
     // _EscapeSpecialCharsWithinTagAttributes(), so that any *'s or _'s in the <a>
     // and <img> tags get encoded.
-    //
-    
+    //v
+            var text = '' + _text;
             // Clear the global hashes. If we don't clear these, you get conflicts
             // from other articles when generating a page which contains more than
             // one article (e.g. an index page that shows the N most recent
@@ -134,6 +130,8 @@ Roo.ux.Showdown.converter = function() {
             // contorted like /[ \t]*\n+/ .
             text = text.replace(/^[ \t]+$/mg,"");
     
+            text = _DoCodeBlocks(text);
+    
             // Turn block-level HTML blocks into hash entries
             text = _HashHTMLBlocks(text);
     
@@ -349,6 +347,9 @@ Roo.ux.Showdown.converter = function() {
     // These are all the transformations that form block-level
     // tags like paragraphs, headers, and list items.
     //
+            // code blocks first... so content does not get translated..
+            text = _DoCodeBlocks(text);
+            
             text = _DoHeaders(text);
     
             // Do Horizontal Rules:
@@ -358,16 +359,14 @@ Roo.ux.Showdown.converter = function() {
             text = text.replace(/^[ ]{0,2}([ ]?\_[ ]?){3,}[ \t]*$/gm,key);
     
             text = _DoLists(text);
-            text = _DoCodeBlocks(text);
-            text = _DoBlockQuotes(text);
-    
+     
             // We already ran _HashHTMLBlocks() before, in Markdown(), but that
             // was to escape raw HTML in the original Markdown source. This time,
             // we're escaping the markup we've just created, so that we don't wrap
             // <p> tags around block-level tags.
-            text = _HashHTMLBlocks(text);
+           
             text = _FormParagraphs(text);
-    
+            text = _HashHTMLBlocks(text);
             return text;
     }
     
@@ -868,6 +867,7 @@ Roo.ux.Showdown.converter = function() {
             */
     
             // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
+            text = text.replace(/~0/,"");
             text += "~0";
             
             text = text.replace(/(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g,
@@ -889,6 +889,28 @@ Roo.ux.Showdown.converter = function() {
             // attacklab: strip sentinel
             text = text.replace(/~0/,"");
     
+    
+      
+            text += '~0';
+          
+            text = text.replace(/(?:^|\n)```(.*)\n([\s\S]*?)\n```/g, function (wholeMatch, language, codeblock) {
+                    var end =  '\n';
+                
+                    // First parse the github code block
+                    codeblock =  _EncodeCode( codeblock); 
+                    codeblock =  _Detab(codeblock);
+                    codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
+                    codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing whitespace
+                
+                    codeblock = '<pre><code' + (language ? ' class="' + language + ' language-' + language + '"' : '') + '>' + codeblock + end + '</code></pre>';
+                
+                    return hashBlock(codeblock) ;
+            });
+          
+            // attacklab: strip sentinel
+            text = text.replace(/~0/, '');
+
+          
             return text;
     }
     
@@ -955,14 +977,19 @@ Roo.ux.Showdown.converter = function() {
     // Encode/escape certain characters inside Markdown code runs.
     // The point is that in code, these characters are literals,
     // and lose their special Markdown meanings.
+    
+    // REMOVED - Data going into markdown should be encoded before it enters..
+    
     //
             // Encode all ampersands; HTML entities are not
             // entities within a Markdown code span.
-            text = text.replace(/&/g,"&amp;");
+            
+            
+            //text = text.replace(/&/g,"&amp;");
     
             // Do the angle bracket song and dance:
-            text = text.replace(/</g,"&lt;");
-            text = text.replace(/>/g,"&gt;");
+            //text = text.replace(/</g,"&lt;");
+            //text = text.replace(/>/g,"&gt;");
     
             // Now, escape characters that are magic in Markdown:
             text = escapeCharacters(text,"\*_{}[]\\",false);
@@ -1236,7 +1263,7 @@ Roo.ux.Showdown.converter = function() {
             text = text.replace(/^(\t|[ ]{1,4})/gm,"~0"); // attacklab: g_tab_width
     
             // attacklab: clean up hack
-            text = text.replace(/~0/g,"")
+            text = text.replace(/~0/g,"");
     
             return text;
     }