-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterminal.js
279 lines (252 loc) · 7.71 KB
/
terminal.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
@projectName TerminalJs
@author Vipin Chaudhary
@source https://github.com/thebrokenloop/terminalJs
*/
/**
* Terminal Base Class
* @param target Dom in html document
* @param language that has to be shown in terminal
* @param theme
* @constructor Terminal
*/
function Terminal(target, language, theme) {
this.target = document.getElementById(target);
this.language = language;
this.theme = theme;
this.linesQueue = [];
}
/**
* Extending prototype functions in Terminal base class
* @type {{constructor: Terminal, init: Terminal.init, populateIntoLineQueue: Terminal.populateIntoLineQueue}}
*/
Terminal.prototype = {
constructor: Terminal,
/**
* Function to initiate the terminal
*/
init: function () {
// alert("Creating terminal " + this.language + " with " + this.theme);
this.target.className += " terminal "+this.theme + " " + this.language;
this.populateIntoLineQueue();
},
/**
* Divides text into lines using '\n' character
*/
populateIntoLineQueue: function () {
var html = String(this.target.innerHTML).trim();
var end = html.search(/\w/);
while (end != -1) {
end = html.indexOf("\n");
this.linesQueue.push(html.slice(0, end));
html = html.substr(end + 1, html.length).trim();
}
console.log(this.linesQueue);
var temp = '';
var processLine = new ProcessLine(this.language);
for (var i = 0; i < this.linesQueue.length; i++) {
temp += processLine.process(this.linesQueue[i]);
}
this.target.innerHTML = temp;
}
};
function initTerminal() {
var terminal1 = new Terminal("terminal1", "python", "dark");
terminal1.init();
var terminal2 = new Terminal("terminal2", "python", "light");
terminal2.init();
var terminal3 = new Terminal("terminal3", "cpp", "dark");
terminal3.init();
var terminal4 = new Terminal("terminal4", "cpp", "light");
terminal4.init();
}
function ProcessLine(language){
this.language = language;
}
ProcessLine.prototype = {
constructor: ProcessLine,
process: function(line){
/// Some preliminary operations on line
line = line.trim().replace(" ", "");
line = line.replace(/(["'][^,]*["'])/g, stringReplacer) // Check for methods
.replace(/(\.[a-zA-Z^(]+)/g, attributeReplacer) // Check for attributes
.replace(/([\s(\[])(\d*\.?\d*)([\s)\]:])/g, numberReplacer) // Check for numbers
.replace(/(\.[a-zA-Z]+\()(.*)(\))/g, methodReplacer) // Check for attribute methods
.replace(/([a-zA-Z]+\()(.*)(\))/g, methodReplacer); // Check for normal methods
/// Generic language Operations
switch (this.language) {
case 'python':
line = this.python(line);
break;
case 'cpp':
line = this.cpp(line);
break;
case 'java':
line = this.java(line);
break;
default:
}
return line;
},
python: function(line){
/// Python Specific Operations
line = line.replace(/(#.*)/, commentReplacer) // Check for comments
/// python's reserved characters Regex Patterns matching
.replace(/(\s?import|from|for|all|\sin\s|^break|^continue|^pass|^print\s|^if|^el[is][fe])/g, keywordReplacer);
// console.log("Returning = " + line);
return line;
},
cpp: function(line){
/// Cpp or C Specific Operations
line = line.replace(/(\/\/.*)/, commentReplacer) // Check for comments
/// python's reserved characters Regex Patterns matching
.replace(/(^#include|\s?\(?int\s|\s?\(?long\s|^float\s|^char\s|^break;|^if|^else|^else\sif)/g, keywordReplacer);
// console.log("Returning = " + line);
return line;
}
};
/**
* Process lines through available theme
* @param line
* @return {*}
*/
function processPythonLine(line) {
/// Some preliminary operations on line
line.trim().replace(" ", "").replace(" ", "");
/// python's reserved characters Regex Patterns matching
line = line.replace(/(["'][^,]*["'])/g, stringReplacer) // Check for methods
.replace(/(\.[a-zA-Z^(]+)/g, attributeReplacer) // Check for attributes
.replace(/([\s(\[])(\d*\.?\d*)([\s)\]:])/g, numberReplacer) // Check for numbers
.replace(/(\.[a-zA-Z]+\()(.*)(\))/g, methodReplacer) // Check for attribute methods
.replace(/([a-zA-Z]+\()(.*)(\))/g, methodReplacer) // Check for normal methods
.replace(/(#.*)/, commentReplacer) // Check for comments
/// Check for "import" statement
.replace(/(\s?import|from|for|all|\sin\s|^break|^continue|^pass|^print\s|^if|^el[is][fe])/g, keywordReplacer);
console.log("Returning = " + line);
return line;
}
/******************* Regex Pattern Formatter Functions \*****************/
/**
* Regex pattern formatter function
* Take 3 parameter , method name , method arguments and method closing parenthesis and return with attached method class
* @param match
* @param p1
* @param p2
* @param p3
* @param offset
* @param string
* @return {string}
*/
function methodReplacer(match, p1, p2, p3, offset, string) {
return getMethodHtml(p1) + p2 + getMethodHtml(p3);
}
/**
* Regex pattern formatter function
* Take 1 parameter , attribute name and return with attached attribute class
* @param match
* @param p1
* @param offset
* @param string
* @return {string}
*/
function attributeReplacer(match, p1, offset, string) {
return getAttributeHtml(p1);
}
/**
* Regex pattern formatter function
* Take 1 parameter , string and return with attached string class
* @param match
* @param p1
* @param offset
* @param string
* @return {string}
*/
function stringReplacer(match, p1, offset, string) {
return getStringHtml(p1);
}
/**
* Regex pattern formatter function
* Take 1 parameter , # type comments and return with attached comment class
* @param match
* @param p1
* @param offset
* @param string
* @return {string}
*/
function commentReplacer(match, p1, offset, string) {
return getCommentHtml(p1);
}
/**
* Regex pattern formatter function
* Take 1 parameter , language keywords and return with attached keyword class
* @param match
* @param p1
* @param offset
* @param string
* @return {string}
*/
function keywordReplacer(match, p1, offset, string) {
return getKeywordHtml(p1);
}
/**
* Regex pattern formatter function for numbers
* @param match
* @param p1
* @param p2
* @param p3
* @param offset
* @param string
* @return {*}
*/
function numberReplacer(match, p1, p2, p3, offset, string) {
return p1 + getNumberHtml(p2) + p3;
}
/******************* Class Attach Functions \*****************/
/**
* Take raw line as input and return line with attached keyword class
* @param text
* @return {string}
*/
function getKeywordHtml(text) {
return '<span class="keyword">' + text + '</span>';
}
/**
* Take raw line as input and return line with attached method class
* @param text
* @return {string}
*/
function getMethodHtml(text) {
return '<span class="method">' + text + '</span>';
}
/**
* Take raw line as input and return line with attached string class
* @param text
* @return {string}
*/
function getStringHtml(text) {
return '<span class="string">' + text + '</span>';
}
/**
* Take raw line as input and return line with attached attribute class
* @param text
* @return {string}
*/
function getAttributeHtml(text) {
return '<span class="attribute">' + text + '</span>';
}
/**
* Take raw line as input and return line with attached comment class
* @param text
* @return {string}
*/
function getCommentHtml(text) {
return '<span class="comment">' + text + '</span>';
}
/**
* Take raw line as input and return line with attached number class
* @param text
* @return {string}
*/
function getNumberHtml(text) {
return '<span class="number">' + text + '</span>';
}