-
Notifications
You must be signed in to change notification settings - Fork 7
/
search.js
196 lines (159 loc) · 7.8 KB
/
search.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
// search in all files - and offer a replace
var eejs = require('ep_etherpad-lite/node/eejs');
var padManager = require('ep_etherpad-lite/node/db/PadManager');
var padMessageHandler = require("ep_etherpad-lite/node/handler/PadMessageHandler");
var exec = require("child_process").exec;
var settings = require('ep_etherpad-lite/node/utils/Settings');
// abs - absolute part of the files path - the project path
var abs = '/tmp/';
// theme from settings
var theme = 'Default';
var cif = '';
if (settings.ep_codepad) {
if (settings.ep_codepad.project_path) {
abs = settings.ep_codepad.project_path;
}
if (settings.ep_codepad.theme) {
theme = settings.ep_codepad.theme;
}
if (settings.ep_codepad.installation_folder) {
cif = settings.ep_codepad.installation_folder;
}
}
function zeroPad(num, places) {
var zero = places - num.toString().length + 1;
return Array(+(zero > 0 && zero)).join(" ") + '#' + num;
}
exports.expressCreateSearchServer = function(hook_name, args, cb) {
args.app.get('/sr', function(req, res) {
var search_term = req.query.search;
//check params
if (search_term === '' || typeof search_term === 'undefined') {
res.send("Error. No search term.");
return;
}
var offer_term = '';
var hasOffer = false;
if (req.query.offer !== '' && typeof req.query.offer !== 'undefined') {
hasOffer = true;
offer_term = req.query.offer;
}
var doReplace = false;
var replace_file = '';
var replace_term = '';
if (req.query.replace !== '' && typeof req.query.replace !== 'undefined' && req.query.file !== '' && typeof req.query.file !== 'undefined') {
replace_file = req.query.file;
replace_term = req.query.replace;
offer_term = req.query.replace;
doReplace = true;
hasOffer = true;
}
// do some things in the pads
if (doReplace)
padManager.getPad(replace_file, null, function(err, value) {
if (err) {
//console.log('sr-err: ', err);
}
var text = value.atext.text;
var rex = new RegExp(search_term, 'g');
var matches = text.match(rex);
//console.log("[REPLACE IN PAD]: " + text);
value.setText(text.replace(rex, replace_term));
padMessageHandler.updatePadClients(value, cb);
var msg = {
type: 'COLLABROOM',
data: {
type: "CUSTOM",
payload: {
padId: replace_file,
from: "codepad",
errors: "Replaced " + search_term + " to " + replace_term
}
}
};
padMessageHandler.handleCustomObjectMessage(msg, undefined, cb);
});
var execterm = '';
// replace in files
if (doReplace) execterm = "sed -i 's/" + search_term + "/" + replace_term + "/g' " + abs + "/" + replace_file + " && ";
execterm += "cd " + abs + " && find . | grep -irnF '" + search_term + "' *";
console.log("CODEPAD SEARCH " + execterm);
exec(execterm, function(err, data, stderr) {
//exec(' grep -rnw ' + abs + ' -e "' + search_term + '"', function(err, data, stderr) {
if (err) {
console.log('CODEPAD SEARCH sr-err: ', err);
}
//Print stdout/stderr to console
//console.log(stdout);
//console.log(stderr);
//res.write(stdout);
//res.end();
// we assemble the html here and send it directly.
var res_send = ''; //'
res_send += '<!DOCTYPE html><head><title>log</title>';
res_send += '<script src="' + cif + '/static/plugins/ep_codepad/static/js/cookies.js" type="text/javascript"></script>';
res_send += '<link href="' + cif + '/static/plugins/ep_codepad/static/css/logcolors.css" rel="stylesheet" type="text/css" media="screen" />';
res_send += '<script type="text/javascript">';
res_send += 'window.onload=toBottom;';
res_send += 'function toBottom(){window.scrollTo(0, document.body.scrollHeight);}';
res_send += 'var theme = "' + theme + '";';
res_send += 'if (getCookie("codepad_theme") != "") theme = getCookie("codepad_theme");';
res_send += 'document.write(\'<link rel="stylesheet" type="text/css" href="' + cif + '/static/plugins/ep_codepad/static/css/theme/\'+theme+\'.css">\');';
res_send += '</script></head><body>';
var lines = data.split('\n');
if (lines.length === '0') res_send += "<b>" + search_term + " not found.</b>";
var i_s = 0;
if (lines.length > 350) {
i_s = lines.length - 250;
res_send += lines.length + " matches. Truncated ...<br />";
}
// current folder+file
var c = '';
var link = '';
for (var i = i_s; i < lines.length; i++) {
// current line
var l = lines[i];
if (l.length < 2) continue;
// index of folder+file|#+text seperation
var v = l.indexOf(':');
// index of line-counter
var w = l.substring(v + 1).indexOf(':');
// index of folder|filename seperaion
var g = l.substring(0, v).lastIndexOf('/');
// skip binary files (where line number is NaN)
if (isNaN(parseInt(l.substring(v + 1, v + w + 2)))) continue;
// actual folder+file
var ff = l.substring(0, v);
// actual linenumber
var no = parseInt(l.substring(v + 1, v + w + 2));
// line indicator
link = cif + '/p/' + ff + '?line=' + no;
var tt = '<b><a href="' + link + '" onclick="JavaScript:top.document.getElementById(\'fileView\').src=\'' + link + '\'" style="text-decoration: none">' + zeroPad(no, 5) + '</a> </b>';
//current text
var re = new RegExp(search_term, 'g');
var sw = '<b><span class="term">' + search_term + '</span></b>';
var t = l.substring(v + w + 2, 250).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(re, sw);
if (c !== ff) {
link = cif + '/v/' + ff;
res_send += '<br /><a href="' + link + '" onclick="JavaScript:top.document.getElementById(\'fileView\').src=\'' + link + '\'">' + ff.substring(0, g) + '<b>' + ff.substring(g) + '</b></a> - ' + sw;
// offer_term the replace action
if (hasOffer) res_send += ' - <a href="' + cif + '/sr?&search=' + search_term + '&replace=' + offer_term + '&file=' + ff + '">REPLACE ALL TO:</a> <span class="term">' + offer_term + '</span>';
res_send += '<br /><br />' + tt + t + '<br />';
c = ff;
} else {
res_send += tt + t + '<br />';
}
}
res_send += "<br /><br /> " + lines.length + " matches.";
if (lines.length > 350) {
res_send += " Truncated.";
}
// ITT TARTUNK EZ A JÓ ... erre kell cserélni a sima herefeket
//res_send += '<a href="/p/test.html" onclick="JavaScript:top.document.getElementById(\'fileView\').src=\'/p/test.html\'">TEST</a>';
res_send += '</div></body></html>';
res.send(res_send);
//res.end();
});
//res.send("req.query: " + search_term);
});
};