mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #5414 (regexp tester used by structure detection does not show multi-line matches)
This commit is contained in:
commit
7f60b191f0
@ -8,40 +8,14 @@ import re
|
|||||||
|
|
||||||
from PyQt4.QtCore import SIGNAL, Qt
|
from PyQt4.QtCore import SIGNAL, Qt
|
||||||
from PyQt4.QtGui import QDialog, QWidget, QDialogButtonBox, QFileDialog, \
|
from PyQt4.QtGui import QDialog, QWidget, QDialogButtonBox, QFileDialog, \
|
||||||
QBrush, QSyntaxHighlighter, QTextCharFormat
|
QBrush, QTextCursor, QTextEdit
|
||||||
|
|
||||||
from calibre.gui2.convert.regex_builder_ui import Ui_RegexBuilder
|
from calibre.gui2.convert.regex_builder_ui import Ui_RegexBuilder
|
||||||
from calibre.gui2.convert.xexp_edit_ui import Ui_Form as Ui_Edit
|
from calibre.gui2.convert.xexp_edit_ui import Ui_Form as Ui_Edit
|
||||||
from calibre.gui2 import qstring_to_unicode
|
|
||||||
from calibre.gui2 import error_dialog
|
from calibre.gui2 import error_dialog
|
||||||
from calibre.ebooks.oeb.iterator import EbookIterator
|
from calibre.ebooks.oeb.iterator import EbookIterator
|
||||||
from calibre.gui2.dialogs.choose_format import ChooseFormatDialog
|
from calibre.gui2.dialogs.choose_format import ChooseFormatDialog
|
||||||
|
|
||||||
class RegexHighlighter(QSyntaxHighlighter):
|
|
||||||
|
|
||||||
def __init__(self, *args):
|
|
||||||
QSyntaxHighlighter.__init__(self, *args)
|
|
||||||
|
|
||||||
self.regex = u''
|
|
||||||
|
|
||||||
def update_regex(self, regex):
|
|
||||||
self.regex = regex
|
|
||||||
self.rehighlight()
|
|
||||||
|
|
||||||
def highlightBlock(self, text):
|
|
||||||
valid_regex = True
|
|
||||||
text = qstring_to_unicode(text)
|
|
||||||
format = QTextCharFormat()
|
|
||||||
format.setBackground(QBrush(Qt.yellow))
|
|
||||||
|
|
||||||
if self.regex:
|
|
||||||
try:
|
|
||||||
for mo in re.finditer(self.regex, text):
|
|
||||||
self.setFormat(mo.start(), mo.end() - mo.start(), format)
|
|
||||||
except:
|
|
||||||
valid_regex = False
|
|
||||||
self.emit(SIGNAL('regex_valid(PyQt_PyObject)'), valid_regex)
|
|
||||||
|
|
||||||
class RegexBuilder(QDialog, Ui_RegexBuilder):
|
class RegexBuilder(QDialog, Ui_RegexBuilder):
|
||||||
|
|
||||||
def __init__(self, db, book_id, regex, *args):
|
def __init__(self, db, book_id, regex, *args):
|
||||||
@ -49,9 +23,7 @@ class RegexBuilder(QDialog, Ui_RegexBuilder):
|
|||||||
self.setupUi(self)
|
self.setupUi(self)
|
||||||
|
|
||||||
self.regex.setText(regex)
|
self.regex.setText(regex)
|
||||||
self.regex_valid(True)
|
self.regex_valid()
|
||||||
self.highlighter = RegexHighlighter(self.preview.document())
|
|
||||||
self.highlighter.update_regex(regex)
|
|
||||||
|
|
||||||
if not db or not book_id:
|
if not db or not book_id:
|
||||||
self.button_box.addButton(QDialogButtonBox.Open)
|
self.button_box.addButton(QDialogButtonBox.Open)
|
||||||
@ -62,19 +34,37 @@ class RegexBuilder(QDialog, Ui_RegexBuilder):
|
|||||||
self.connect(self.regex, SIGNAL('textChanged(QString)'), self.regex_valid)
|
self.connect(self.regex, SIGNAL('textChanged(QString)'), self.regex_valid)
|
||||||
self.connect(self.test, SIGNAL('clicked()'), self.do_test)
|
self.connect(self.test, SIGNAL('clicked()'), self.do_test)
|
||||||
|
|
||||||
def regex_valid(self, valid):
|
def regex_valid(self):
|
||||||
regex = qstring_to_unicode(self.regex.text())
|
regex = unicode(self.regex.text())
|
||||||
if regex:
|
if regex:
|
||||||
try:
|
try:
|
||||||
re.compile(regex)
|
re.compile(regex)
|
||||||
self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgba(0,255,0,20%); }')
|
self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgba(0,255,0,20%); }')
|
||||||
except:
|
except:
|
||||||
self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgb(255,0,0,20%); }')
|
self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgb(255,0,0,20%); }')
|
||||||
|
return False
|
||||||
else:
|
else:
|
||||||
self.regex.setStyleSheet('QLineEdit { color: black; background-color: white; }')
|
self.regex.setStyleSheet('QLineEdit { color: black; background-color: white; }')
|
||||||
|
return True
|
||||||
|
|
||||||
def do_test(self):
|
def do_test(self):
|
||||||
self.highlighter.update_regex(qstring_to_unicode(self.regex.text()))
|
selections = []
|
||||||
|
if self.regex_valid():
|
||||||
|
text = unicode(self.preview.toPlainText())
|
||||||
|
regex = unicode(self.regex.text())
|
||||||
|
cursor = QTextCursor(self.preview.document())
|
||||||
|
extsel = QTextEdit.ExtraSelection()
|
||||||
|
extsel.cursor = cursor
|
||||||
|
extsel.format.setBackground(QBrush(Qt.yellow))
|
||||||
|
try:
|
||||||
|
for match in re.finditer(regex, text):
|
||||||
|
es = QTextEdit.ExtraSelection(extsel)
|
||||||
|
es.cursor.setPosition(match.start(), QTextCursor.MoveAnchor)
|
||||||
|
es.cursor.setPosition(match.end(), QTextCursor.KeepAnchor)
|
||||||
|
selections.append(es)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
self.preview.setExtraSelections(selections)
|
||||||
|
|
||||||
def select_format(self, db, book_id):
|
def select_format(self, db, book_id):
|
||||||
format = None
|
format = None
|
||||||
@ -106,7 +96,7 @@ class RegexBuilder(QDialog, Ui_RegexBuilder):
|
|||||||
if button == self.button_box.button(QDialogButtonBox.Open):
|
if button == self.button_box.button(QDialogButtonBox.Open):
|
||||||
name = QFileDialog.getOpenFileName(self, _('Open book'), _('~'))
|
name = QFileDialog.getOpenFileName(self, _('Open book'), _('~'))
|
||||||
if name:
|
if name:
|
||||||
self.open_book(qstring_to_unicode(name))
|
self.open_book(unicode(name))
|
||||||
if button == self.button_box.button(QDialogButtonBox.Ok):
|
if button == self.button_box.button(QDialogButtonBox.Ok):
|
||||||
self.accept()
|
self.accept()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user