From 1b70878e9e0a40b506940fb0de9cc9249a871835 Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Fri, 20 Jun 2014 00:05:17 -0700 Subject: [PATCH] syntax highlight --- .../core/static/shell/opencloud_shell.js | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/planetstack/core/static/shell/opencloud_shell.js b/planetstack/core/static/shell/opencloud_shell.js index 861cd10..9091353 100644 --- a/planetstack/core/static/shell/opencloud_shell.js +++ b/planetstack/core/static/shell/opencloud_shell.js @@ -354,8 +354,39 @@ MongoHandler.prototype = { } }; +function replaceAll(find, replace, str) { + return str.replace(new RegExp(find, 'g'), replace); +} + +/* 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') { + json = JSON.stringify(json, undefined, "\t"); + } + json = json.replace(/&/g, '&').replace(//g, '>'); + return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) { + var cls = 'terminal_number'; + if (/^"/.test(match)) { + if (/:$/.test(match)) { + cls = 'terminal_key'; + } else { + cls = 'terminal_string'; + } + } else if (/true|false/.test(match)) { + cls = 'terminal_boolean'; + } else if (/null/.test(match)) { + cls = 'terminal_null'; + } + return '' + match + ''; + }); +} + $htmlFormat = function(obj) { - result=tojson(obj, ' ', ' ', true); + //JSON.stringify(obj,undefined,2) + result=replaceAll("\t"," ",replaceAll("\n","
",syntaxHighlight(obj))); //tojson(obj, ' ', ' ', true); return result; } -- 2.45.2