Skip to content

Instantly share code, notes, and snippets.

@mgk
Created December 2, 2019 17:31
Show Gist options
  • Save mgk/c415599e9b180a16d35db0b716707975 to your computer and use it in GitHub Desktop.
Save mgk/c415599e9b180a16d35db0b716707975 to your computer and use it in GitHub Desktop.
DataGrip Extractor for tables with JSON (uses <td><pre></pre></td> instead of just <td></td>)
function eachWithIdx(iterable, f) { var i = iterable.iterator(); var idx = 0; while (i.hasNext()) f(i.next(), idx++); }
function mapEach(iterable, f) { var vs = []; eachWithIdx(iterable, function (i) { vs.push(f(i));}); return vs; }
function escape(str) {
str = str.replaceAll("\t|\b|\\f", "");
str = com.intellij.openapi.util.text.StringUtil.escapeXml(str);
str = str.replaceAll("\\r|\\n|\\r\\n", "<br/>");
return str;
}
var NEWLINE = "\n";
function output() { for (var i = 0; i < arguments.length; i++) { OUT.append(arguments[i]); } }
function outputRow(items, tag) {
output("<tr>");
for (var i = 0; i < items.length; i++)
output("<", tag, "><pre>", escape(items[i]), "</pre></", tag, ">");
output("</tr>", NEWLINE);
}
output("<!DOCTYPE html>", NEWLINE,
"<html>", NEWLINE,
"<head>", NEWLINE,
"<title></title>", NEWLINE,
"</head>", NEWLINE,
"<body>", NEWLINE,
"<table border=\"1\" style=\"border-collapse:collapse\">", NEWLINE);
if (TRANSPOSED) {
var values = mapEach(COLUMNS, function(col) { return [col.name()]; });
eachWithIdx(ROWS, function (row) {
eachWithIdx(COLUMNS, function (col, i) {
values[i].push(FORMATTER.format(row, col));
});
});
eachWithIdx(COLUMNS, function (_, i) { outputRow(values[i], "td"); });
}
else {
outputRow(mapEach(COLUMNS, function (col) { return col.name(); }), "th");
eachWithIdx(ROWS, function (row) {
outputRow(mapEach(COLUMNS, function (col) { return FORMATTER.format(row, col); }), "td")
});
}
output("</table>", NEWLINE,
"</body>", NEWLINE,
"</html>", NEWLINE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment