From 2411298bb5b491b92fe723a0d2f4c1d05a6cc519 Mon Sep 17 00:00:00 2001 From: robert Date: Tue, 12 Feb 2019 14:00:10 +0100 Subject: [PATCH] owcorpusviewer: move text makring to python --- orangecontrib/text/widgets/owcorpusviewer.py | 51 ++++++++++--------- .../text/widgets/resources/highlighter.js | 17 ------- 2 files changed, 28 insertions(+), 40 deletions(-) delete mode 100644 orangecontrib/text/widgets/resources/highlighter.js diff --git a/orangecontrib/text/widgets/owcorpusviewer.py b/orangecontrib/text/widgets/owcorpusviewer.py index a17bb4aa3..36857be24 100644 --- a/orangecontrib/text/widgets/owcorpusviewer.py +++ b/orangecontrib/text/widgets/owcorpusviewer.py @@ -3,8 +3,7 @@ from itertools import chain from AnyQt.QtCore import ( - Qt, QUrl, QItemSelection, QItemSelectionModel, QItemSelectionRange, - pyqtSlot as Slot + Qt, QUrl, QItemSelection, QItemSelectionModel, QItemSelectionRange ) from AnyQt.QtGui import QStandardItemModel, QStandardItem @@ -118,7 +117,6 @@ def __init__(self): # Document contents self.doc_webview = gui.WebviewWidget(self.splitter, debug=False) - self.doc_webview.loadFinished.connect(self.highlight_docs) self.mainArea.layout().addWidget(self.splitter) @@ -321,14 +319,16 @@ def show_docs(self): row_ind = index.data(Qt.UserRole).row_index for ind in self.display_indices: feature = self.display_features[ind] - mark = ' mark-area' if feature in marked_search_features else '' - value = str(index.data(Qt.UserRole)[feature.name]).replace('\n', '
') + value = str(index.data(Qt.UserRole)[feature.name]) + if feature in marked_search_features: + value = self.__mark_text(value) + value = value.replace('\n', '
') is_image = feature.attributes.get('type', '') == 'image' if is_image and value != '?': value = ''.format(value) html += '{}:' \ - '{}'.format( - feature.name, mark, value) + '{}'.format( + feature.name, value) if self.show_tokens: html += 'Tokens & Tags:' \ @@ -339,6 +339,27 @@ def show_docs(self): base = QUrl.fromLocalFile(__file__) self.doc_webview.setHtml(HTML.format(html), base) + def __mark_text(self, text): + search_keyword = self.regexp_filter.strip('|') + if not search_keyword: + return text + + try: + reg = re.compile(search_keyword, re.IGNORECASE | re.MULTILINE) + except sre_constants.error: + return text + + matches = list(reg.finditer(text)) + if not matches: + return text + + text = list(text) + for m in matches[::-1]: + text[m.start():m.end()] = list('{}'\ + .format("".join(text[m.start():m.end()]))) + + return "".join(text) + def search_features_changed(self): self.regenerate_docs() self.refresh_search() @@ -359,22 +380,6 @@ def refresh_search(self): self.update_info() self.commit() - @Slot() - def highlight_docs(self): - search_keyword = self.regexp_filter.\ - strip('|').replace('\\', '\\\\') # escape one \ to two for mark.js - - if search_keyword: - # mark is undefined when clearing the view (`setHtml('')`). Maybe - # set and template html with all the scripts, ... but no contents? - self.doc_webview.runJavaScript( - ''' - if (typeof mark !== "undefined") {{ - mark("{}"); - }} - '''.format(search_keyword) - ) - def update_info(self): if self.corpus is not None: self.n_documents = len(self.corpus) diff --git a/orangecontrib/text/widgets/resources/highlighter.js b/orangecontrib/text/widgets/resources/highlighter.js deleted file mode 100644 index 31e3d624e..000000000 --- a/orangecontrib/text/widgets/resources/highlighter.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Adopted from https://jsfiddle.net/julmot/ova17daa/ - */ -var DEBUG = false; - -var mark = function(pattern) { - var flags = 'gmi'; - try { - var regex = new RegExp(pattern, flags); - - // Mark regex inside the context if regex is valid - $(".mark-area").unmark(); - $(".mark-area").markRegExp(regex); - } catch(e) { // skip invalid regexes - if (DEBUG) {console.log('[INFO] Skipping marking for invalid regex pattern: ' + pattern)} - } -};