-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHTMLGenerator.ts
More file actions
214 lines (198 loc) · 7.91 KB
/
HTMLGenerator.ts
File metadata and controls
214 lines (198 loc) · 7.91 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
function escapeHTML(s: any) {
if (s !== undefined && s !== null) {
return s.toString().replace(/[^0-9A-Za-z ]/g, (c: string) => "&#" + c.charCodeAt(0) + ";");
} else {
return s;
}
}
function toID(s: any) {
if (s !== undefined && s !== null) {
return Array.from(s.toString()).filter((c) => {
return c !== "\"" && c !== "\t" && c !== "\n" && c !== "\f" && c !== "\r" && c !== " ";
}).join("");
} else {
return s;
}
}
function keyToString(key: any) {
if (key.type !== undefined) {
return key.type === 'ref' ? '' : (key.value ? key.value : key);
} else {
return key.toString();
}
}
export class HTMLGenerator {
uniqueId: number = -1;
constructor() {
}
generateHTML(traceElement: BackendTraceElem): FrontendTraceElem {
this.uniqueId = -1;
const frameItems = `
<div class="column" id="frameItems">
${traceElement.stack.map((stackElem, index) => this.frameItem(index, stackElem)).join('')}
</div>
`;
const keys = Array.from(Object.keys(traceElement.heap));
const values = Array.from(Object.values(traceElement.heap));
const objectItems = `
<div class="column" id="objectItems">
${keys.map((name, index) => this.objectItem(name, values[index])).join('')}
<div>
`;
let output = traceElement.stdout;
if (traceElement.traceback !== undefined) {
output += `<span class="traceback-text">${traceElement.traceback}</span>`;
}
return {
lineNumber: traceElement.line,
stackHTML: frameItems,
heapHTML: objectItems,
filename: traceElement.filePath,
outputState: output
};
}
private objectItem(name: string, value: HeapValue): string {
let headline: string;
switch (value.type) {
case 'instance':
headline = value.name;
break;
default:
headline = value.type;
}
return `
<div class="column object-item" id="objectItem${name}">
<div>${escapeHTML(headline)}</div>
<div>${this.heapValue(name, value)}</div>
</div>
`;
}
private heapValue(name: string, heapValue: HeapValue): string {
let result = '';
let keyLen = 0;
switch (heapValue.type) {
case 'dict':
const metaKeys = Array.from(Object.keys(heapValue.keys));
// keys and value are not really Maps but rather objects
// -> Conversion required
const keyMap = new Map(Object.entries(heapValue.keys));
const valueMap = new Map(Object.entries(heapValue.value));
keyLen = 0;
metaKeys.forEach(metaKey => {
let key = keyMap.get(metaKey)!;
keyLen = Math.max(keyToString(key).length, keyLen);
});
result = `
<div class="column" id="heapEndPointer${name}">
${metaKeys.map(metaKey => this.dictValue(keyLen, keyMap.get(metaKey)!, valueMap.get(metaKey)!)).join('')}
</div>
`;
break;
case 'instance':
const instanceKeys = Array.from(Object.keys(heapValue.value));
const instanceValues = Array.from(Object.values(heapValue.value)); // maybe endpointer look for if its exist and if add a second number or key or smth
keyLen = 0;
instanceKeys.forEach(key => {
keyLen = Math.max(keyToString(key).length, keyLen);
});
result = `
<div class="column" id="heapEndPointer${name}">
${instanceKeys.map((key, index) => this.dictValue(keyLen, key, instanceValues[index])).join('')}
</div>
`;
break;
case 'set':
result = `
<div class="row" id="heapEndPointer${name}">
${heapValue.value.map((v, i) => this.setValue(v)).join('')}
</div>
`;
break;
break;
/* tuple, list, int[], int[][], ...*/
default:
result = `
<div class="row" id="heapEndPointer${name}">
${heapValue.value.map((v, i) => this.listValue(v, i)).join('')}
</div>
`;
break;
}
return result;
}
private dictValue(maxKeyLen: number, key: any, value: Value): string {
this.uniqueId++;
const keyWidth = Math.min(100, maxKeyLen * 10 + 10);
const valWidth = 200 - keyWidth;
return `
<div class="row">
<div class="box box-content-dict box-content-dict-key" ${key.type === 'ref' ? `id="${this.uniqueId}startPointer${key.value}"` : ''}
style="width: ${keyWidth}px">
${escapeHTML(keyToString(key))}
</div>
<div class="box box-content-dict box-content-dict-value" ${value.type === 'ref' ? `id="${this.uniqueId}startPointer${value.value}"` : ''}
style="width: ${valWidth}px">
${value.type === 'ref' ? '' : this.getCorrectValueOf(value)}
</div>
</div>
`;
}
private listValue(value: Value, index: number): string {
this.uniqueId++;
return `
<div class="box list column">
<div class="row box-content-top">
${index}
</div>
<div class="row box-content-bottom" ${value.type === 'ref' ? `id="${this.uniqueId}startPointer${value.value}"` : ''}>
${value.type === 'ref' ? '' : this.getCorrectValueOf(value)}
</div>
</div>
`;
}
private setValue(value: Value): string {
this.uniqueId++;
return `
<div class="box box-set column">
<div class="row box-content-bottom" ${value.type === 'ref' ? `id="${this.uniqueId}startPointer${value.value}"` : ''}>
${value.type === 'ref' ? '' : this.getCorrectValueOf(value)}
</div>
</div>
`;
}
private frameItem(index: number, stackElem: StackElem): string {
return `
<div class="column frame-item" id="frameItem?">
<div class="row subtitle" id="frameItemTitle">
${stackElem.frameName === '<module>' ? 'Global' : escapeHTML(stackElem.frameName)}
</div>
<div class="column ${index === 0 ? 'current-frame' : 'frame'}" id="frameItemSubItems">
${stackElem.locals.map(namedValue => this.frameSubItem(stackElem.frameName, namedValue)).join('')}
</div>
</div>
`;
}
private frameSubItem(frameName: string, namedValue: NamedValue): string {
const isReturn = namedValue.name === 'return';
return `
<div class="row frame-item" id="subItem${toID(namedValue.name)}">
<div class="name-border${isReturn ? ' return-value' : ''}">
${escapeHTML(namedValue.name)}
</div>
<div class="value-border" ${namedValue.type === 'ref' ? `id="${toID(frameName)}${toID(namedValue.name)}Pointer${toID(namedValue.value)}"` : ''}>
${this.getCorrectValueOf(namedValue)}
</div>
</div>
`;
}
private getCorrectValueOf(value: Value): string {
switch (value.type) {
case 'ref':
return '';
case 'none':
return 'None';
default:
return escapeHTML(`${value.value}`);
}
}
}