-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMarkdown-Table.md.js
77 lines (68 loc) · 1.74 KB
/
Markdown-Table.md.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Data extractor to Markdown Table
*/
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", "");
str = str.replaceAll("([\\[\\]\\|])", "\\$1");
return str;
}
var NEWLINE = "\n";
function output() {
for (var i = 0; i < arguments.length; i++) {
OUT.append(arguments[i]);
}
}
function outputln() {
for (var i = 0; i < arguments.length; i++) {
OUT.append(arguments[i].toString());
}
OUT.append("\n");
}
function outputRow(items) {
output("| ");
for (var i = 0; i < items.length; i++) output(escape(items[i]), " |");
output("", NEWLINE);
}
if (TRANSPOSED) {
var values = mapEach(COLUMNS, function (col) {
return [col.name()];
});
var heading = [];
eachWithIdx(ROWS, function (row) {
heading.push("---");
eachWithIdx(COLUMNS, function (col, i) {
values[i].push(FORMATTER.format(row, col));
});
});
heading.push("---");
outputRow(heading);
eachWithIdx(COLUMNS, function (_, i) {
outputRow(values[i]);
});
} else {
outputRow(mapEach(COLUMNS, function (col) {
return col.name();
}));
outputRow(mapEach(COLUMNS, function (col) {
return "---";
}));
eachWithIdx(ROWS, function (row) {
outputRow(mapEach(COLUMNS, function (col) {
return FORMATTER.format(row, col);
}));
});
}