mirror of
https://github.com/jupyter/notebook.git
synced 2024-12-21 04:10:17 +08:00
draft CaseSensitifvity and non-regexmatches.
This commit is contained in:
parent
40ee54fffe
commit
e58992094e
@ -343,14 +343,61 @@ define(function(require){
|
|||||||
handler: function(env){
|
handler: function(env){
|
||||||
var search = $("<input/>")
|
var search = $("<input/>")
|
||||||
.addClass('form-control')
|
.addClass('form-control')
|
||||||
|
.css('width','86%')
|
||||||
.attr('placeholder','Search');
|
.attr('placeholder','Search');
|
||||||
|
var isRegExpButton = $('<button/>')
|
||||||
|
.attr('type', 'button')
|
||||||
|
.attr('id', 'isreg')
|
||||||
|
.addClass("btn btn-default")
|
||||||
|
.attr('data-toggle','button')
|
||||||
|
//.attr('aria-pressed', "false")
|
||||||
|
.attr('title', 'use regular expression (now you have N+1 problems)')
|
||||||
|
.attr('value', '.*')
|
||||||
|
.css('border-radius', '0')
|
||||||
|
.css('border-left', 'none')
|
||||||
|
.click(function(){search.focus();})
|
||||||
|
.text('.*');
|
||||||
|
|
||||||
|
var isCaseSensitiveButton = $('<button/>')
|
||||||
|
.attr('type', 'button')
|
||||||
|
.addClass("btn btn-default")
|
||||||
|
.attr('data-toggle','button')
|
||||||
|
//.attr('aria-pressed', "false")
|
||||||
|
.attr('title', 'is search case sensitive')
|
||||||
|
.attr('value', 'a≠A')
|
||||||
|
.css('border-top-left-radius', '0')
|
||||||
|
.css('border-bottom-left-radius', '0')
|
||||||
|
.css('border-left', 'none')
|
||||||
|
.click(function(){search.focus();})
|
||||||
|
.text('a≠A');
|
||||||
|
|
||||||
|
var isCaseSensitive = function(){
|
||||||
|
var value = isCaseSensitiveButton.attr('aria-pressed') == 'true';
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
|
||||||
|
var RegExpOrNot = function(str, flags){
|
||||||
|
if (isRegExpButton.attr('aria-pressed') === 'true'){
|
||||||
|
return new RegExp(str, flags);
|
||||||
|
} else {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
var repl = $("<input/>")
|
var repl = $("<input/>")
|
||||||
.addClass('form-control')
|
.addClass('form-control')
|
||||||
.attr('placeholder','replace');
|
.attr('placeholder','replace');
|
||||||
//var submit = $("<input/>").attr('type', 'submit');
|
//var submit = $("<input/>").attr('type', 'submit');
|
||||||
var body = $('<div/>').css('max-height','60vh').css('overflow','auto');
|
var body = $('<div/>').css('max-height','60vh').css('overflow','auto');
|
||||||
var form = $('<form/>')
|
var form = $('<form/>')
|
||||||
.append($('<div/>').addClass('form-group').append(search))
|
.append($('<div/>').addClass('form-group')
|
||||||
|
.append(
|
||||||
|
$('<div/>').addClass('input-group').css("width","100%")
|
||||||
|
.append(search)
|
||||||
|
.append(isRegExpButton)
|
||||||
|
.append(isCaseSensitiveButton)
|
||||||
|
)
|
||||||
|
)
|
||||||
.append($('<div/>').addClass('form-group').append(repl))
|
.append($('<div/>').addClass('form-group').append(repl))
|
||||||
//.append(submit)
|
//.append(submit)
|
||||||
.append(body);
|
.append(body);
|
||||||
@ -364,7 +411,6 @@ define(function(require){
|
|||||||
var cells = env.notebook.get_cells();
|
var cells = env.notebook.get_cells();
|
||||||
var arr = [];
|
var arr = [];
|
||||||
for(var c=0; c < cells.length; c++){
|
for(var c=0; c < cells.length; c++){
|
||||||
//console.log("looping through cell", c);
|
|
||||||
var oldvalue = cells[c].code_mirror.getValue();
|
var oldvalue = cells[c].code_mirror.getValue();
|
||||||
var newvalue = oldvalue.replace(new RegExp(sre, 'g'), replace);
|
var newvalue = oldvalue.replace(new RegExp(sre, 'g'), replace);
|
||||||
cells[c].code_mirror.setValue(newvalue);
|
cells[c].code_mirror.setValue(newvalue);
|
||||||
@ -388,7 +434,7 @@ define(function(require){
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new RegExp(sre);
|
new RegExpOrNot(sre);
|
||||||
} catch (e){
|
} catch (e){
|
||||||
body.empty();
|
body.empty();
|
||||||
body.append($('<p/>').text('No matches, invalid or empty regular expression'));
|
body.append($('<p/>').text('No matches, invalid or empty regular expression'));
|
||||||
@ -404,15 +450,15 @@ define(function(require){
|
|||||||
|
|
||||||
var html = [];
|
var html = [];
|
||||||
for(var r=0; r < arr.length; r++){
|
for(var r=0; r < arr.length; r++){
|
||||||
var matches = getMatches(sre, arr[r]);
|
var matches = getMatches(sre, arr[r], isCaseSensitive());
|
||||||
//console.log("looping through line", r, "matches", matches);
|
//console.log("looping through line", r, "matches", matches);
|
||||||
for(var mindex=0; mindex < matches.length ; mindex++){
|
for(var mindex=0; mindex < matches.length ; mindex++){
|
||||||
var start = matches[mindex][0];
|
var start = matches[mindex][0];
|
||||||
var stop = matches[mindex][1];
|
var stop = matches[mindex][1];
|
||||||
//console.log(matches[mindex], arr[r].slice(start, stop));
|
//console.log(matches[mindex], arr[r].slice(start, stop));
|
||||||
var init = arr[r].slice(start, stop);
|
var init = arr[r].slice(start, stop);
|
||||||
var replaced = init.replace( new RegExp(sre), replace);
|
var replaced = init.replace( new RegExpOrNot(sre), replace);
|
||||||
html.push([cutBefore(arr[r].slice(0, start)), arr[r].slice(start, stop), replaced, cutAfter(arr[r].slice(stop))]);
|
html.push([cutBefore(arr[r].slice(0, start)), arr[r].slice(start, stop), replaced, cutAfter(arr[r].slice(stop), 30-(stop-start))]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
body.empty();
|
body.empty();
|
||||||
@ -465,9 +511,13 @@ define(function(require){
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var cutAfter = function(string){
|
var cutAfter = function(string, n){
|
||||||
if(string.length > 13){
|
n=n||10;
|
||||||
return string.slice(0, 10)+'...';
|
while(n<10){
|
||||||
|
n+=15;
|
||||||
|
}
|
||||||
|
if(string.length > n+3){
|
||||||
|
return string.slice(0, n)+'...';
|
||||||
}
|
}
|
||||||
return string;
|
return string;
|
||||||
};
|
};
|
||||||
@ -479,9 +529,11 @@ define(function(require){
|
|||||||
return string;
|
return string;
|
||||||
};
|
};
|
||||||
|
|
||||||
var getMatches = function(re, string){
|
var getMatches = function(re, string, caseSensitive, R){
|
||||||
|
R = R || function(re, extra){ return new RegExp(re, extra);};
|
||||||
|
var extra = caseSensitive ? '':'i';
|
||||||
try {
|
try {
|
||||||
re = new RegExp(re, 'g');// have to global or infinite loop
|
re = new R(re, 'g'+extra);// have to global or infinite loop
|
||||||
} catch (e){
|
} catch (e){
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@ -493,8 +545,8 @@ define(function(require){
|
|||||||
while((match = re.exec(string)) !== null) {
|
while((match = re.exec(string)) !== null) {
|
||||||
res.push([match.index, match.index+match[0].length]);
|
res.push([match.index, match.index+match[0].length]);
|
||||||
escape_hatch++;
|
escape_hatch++;
|
||||||
if(escape_hatch > 1000){
|
if(escape_hatch > 300){
|
||||||
console.warn("More than 1000 matches, aborting");
|
console.warn("More than 300 matches, aborting");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user