diff --git a/magpie/handler/base.py b/magpie/handler/base.py index 4935326..e2ea926 100644 --- a/magpie/handler/base.py +++ b/magpie/handler/base.py @@ -26,7 +26,8 @@ def _notebooks_list(self, hide_notebooks=False): return [] return sorted([self.decode_name(nb) for nb in \ - listdir(self.settings.repo) if nb != '']) + listdir(self.settings.repo) \ + if (nb != '' and path.isdir(path.join(self.settings.repo, nb)) ) ]) def _notes_list(self, notebook_name): notes_path = path.join(self.settings.repo, @@ -47,9 +48,9 @@ def _notes_list(self, notebook_name): return sorted(starred) + sorted(unstarred) def highlight(self, text, highlight): - return text.replace(highlight.encode('utf8'), - "%s" % \ - highlight.encode('utf8')) + return re.sub( highlight.encode('utf8'), + lambda m: "" + m.group() \ + + "", text, flags=re.IGNORECASE) def get_current_user(self): if self.settings.username is None and self.settings.pwdhash is None \ diff --git a/magpie/handler/search.py b/magpie/handler/search.py index a35c203..460aed8 100644 --- a/magpie/handler/search.py +++ b/magpie/handler/search.py @@ -14,31 +14,35 @@ def get(self): if query == '': self.redirect('/') try: - results = str(grep('-R', '--exclude-dir', '.git', query, + results = str(grep('-Rli', '--exclude-dir', '.git', query, self.settings.repo)) except ErrorReturnCode_1 as e: results = '' try: - results += str(find(self.settings.repo, '-type', 'f', '-name', + results += str(find(self.settings.repo, '-type', 'f', '-iname', '*' + query + '*', '-not', '(', '-path', - '%s/%s/*' % (self.settings.repo, '.git') )) + '%s/%s/*' % (self.settings.repo, '.git'), ')')) except ErrorReturnCode_1 as e: pass results = results.replace(self.settings.repo, '').split('\n')[:-1] formatted_results = [] - for result in results: - if 'Binary file' in result or result == '': + for filename in results: + if 'Binary file' in filename or filename == '': continue - # TODO this doesn't play well with colons in filenames - stuff = result.split(':') - filename = stuff[0] if path.basename(filename).startswith('.'): filename = path.join(path.dirname(filename), path.basename(filename)[1:]) - string = ''.join(stuff[1:]) - string = self.highlight(string, query) - formatted_results.append({'filename': filename, 'string': string}) + + resultpath = path.join(self.settings.repo, filename[1:]) + if path.exists(resultpath): + try: + string = str(grep('-i', query, resultpath )) + except ErrorReturnCode_1 as e: + string = '' + + string = self.highlight(string, query) + formatted_results.append({'filename': filename, 'string': string}) self.render('search.html', query=query, results=formatted_results) diff --git a/todo.md b/todo.md index 67c3504..29f0c85 100644 --- a/todo.md +++ b/todo.md @@ -1,8 +1,4 @@ ## To Do -### ``magpie/handler/search.py`` -(line 35) this doesn't play well with colons in filenames - - ### ``utils/email_notes.py`` (line 43) there seems to be a bug where other notes in other notebooks can be deleted