syntax highlight
authorScott Baker <smbaker@gmail.com>
Fri, 20 Jun 2014 07:05:17 +0000 (00:05 -0700)
committerScott Baker <smbaker@gmail.com>
Fri, 20 Jun 2014 07:05:17 +0000 (00:05 -0700)
planetstack/core/static/shell/opencloud_shell.js

index 861cd10..9091353 100644 (file)
@@ -354,8 +354,39 @@ MongoHandler.prototype = {
   }
 };
 
+function replaceAll(find, replace, str) {
+  return str.replace(new RegExp(find, 'g'), replace);\r
+}
+
+/* stackoverflow: http://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript */
+function syntaxHighlight(json) {
+    if ( json.hasOwnProperty("__str__")) {
+        return syntaxHighlight(json.__str__());
+    }
+    if (typeof json != 'string') {\r
+         json = JSON.stringify(json, undefined, "\t");\r
+    }\r
+    json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');\r
+    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {\r
+        var cls = 'terminal_number';\r
+        if (/^"/.test(match)) {\r
+            if (/:$/.test(match)) {\r
+                cls = 'terminal_key';\r
+            } else {\r
+                cls = 'terminal_string';\r
+            }\r
+        } else if (/true|false/.test(match)) {\r
+            cls = 'terminal_boolean';\r
+        } else if (/null/.test(match)) {\r
+            cls = 'terminal_null';\r
+        }\r
+        return '<span class="' + cls + '">' + match + '</span>';\r
+    });\r
+}
+
 $htmlFormat = function(obj) {
-  result=tojson(obj, ' ', ' ', true);
+  //JSON.stringify(obj,undefined,2)
+  result=replaceAll("\t","&nbsp;",replaceAll("\n","<br>",syntaxHighlight(obj))); //tojson(obj, ' ', ' ', true);
   return result;
 }