From 5440e0c3773697db18f7ef9bdf6493c535a94fe0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 18 Feb 2014 15:47:22 +0530 Subject: [PATCH] Add malformed markup check when editing ToC as well --- resources/compiled_coffeescript.zip | Bin 79761 -> 80305 bytes src/calibre/ebooks/oeb/polish/choose.coffee | 17 ++++++++++++- src/calibre/ebooks/oeb/polish/toc.py | 19 +++++++++++++-- src/calibre/gui2/toc/location.py | 25 +++++++------------- src/calibre/gui2/toc/main.py | 2 +- 5 files changed, 42 insertions(+), 21 deletions(-) diff --git a/resources/compiled_coffeescript.zip b/resources/compiled_coffeescript.zip index 257baf3a67e503fde840513b0d7f19cb7c6c4f92..e50a016181f370e2f58b763db26cd71b69d4c043 100644 GIT binary patch delta 179 zcmbREo@L`_mWC~i4orNWH9;;_wUgR)I2jmpIJf&TG5%m>)R}%slTmBB9|vRX^go)6 zIxHm_nZ?_kwHOyLPG8NzC_kN3hf#a`2@b}GT4)AL-)_tp$vm5zarz8SMuq9dCX8B4 z*)r4PO&Il;Yfk(y$hmXv5>kZ6!* VWNemVlsetOlu>+psTrdTBLFu~G$jB4 delta 151 zcmdn^nPuX8mWC~i4orM=6MS66+5=|ruro05uy6NcV*J56J(Ghmb^1ImM)~cpxESwi zp(&bv-IOtsxtf!4`VMYJh3Ua&j9N_IQq$|r81 tags. So force parse it + # with the HTML 5 parser and try again. + raw = container.raw_data(name) + root = container.parse_xhtml(raw, fname=name, force_html5_parse=True) + try: + node = node_from_loc(root, loc, totals=totals) + except MalformedMarkup: + raise MalformedMarkup(_('The file %s has malformed markup. Try running the Fix HTML tool' + ' before editing.') % name) + container.replace(name, root) + node.set('id', node.get('id', uuid_id())) container.commit_item(name, keep_parsed=True) return node.get('id') diff --git a/src/calibre/gui2/toc/location.py b/src/calibre/gui2/toc/location.py index 944acc7c8d..c5f78a3d9c 100644 --- a/src/calibre/gui2/toc/location.py +++ b/src/calibre/gui2/toc/location.py @@ -7,6 +7,7 @@ __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' __docformat__ = 'restructuredtext en' +import json from base64 import b64encode from PyQt4.Qt import (QWidget, QGridLayout, QListWidget, QSize, Qt, QUrl, @@ -20,7 +21,7 @@ from calibre.utils.logging import default_log class Page(QWebPage): # {{{ - elem_clicked = pyqtSignal(object, object, object, object) + elem_clicked = pyqtSignal(object, object, object, object, object) def __init__(self): self.log = default_log @@ -42,21 +43,11 @@ class Page(QWebPage): # {{{ def shouldInterruptJavaScript(self): return True - @pyqtSlot(QWebElement, float) - def onclick(self, elem, frac): + @pyqtSlot(QWebElement, str, str, float) + def onclick(self, elem, loc, totals, frac): elem_id = unicode(elem.attribute('id')) or None tag = unicode(elem.tagName()).lower() - parent = elem - loc = [] - while unicode(parent.tagName()).lower() != 'body': - num = 0 - sibling = parent.previousSibling() - while not sibling.isNull(): - num += 1 - sibling = sibling.previousSibling() - loc.insert(0, num) - parent = parent.parent() - self.elem_clicked.emit(tag, frac, elem_id, tuple(loc)) + self.elem_clicked.emit(tag, frac, elem_id, json.loads(str(loc)), json.loads(str(totals))) def load_js(self): if self.js is None: @@ -69,7 +60,7 @@ class Page(QWebPage): # {{{ class WebView(QWebView): # {{{ - elem_clicked = pyqtSignal(object, object, object, object) + elem_clicked = pyqtSignal(object, object, object, object, object) def __init__(self, parent): QWebView.__init__(self, parent) @@ -294,8 +285,8 @@ class ItemEdit(QWidget): loctext = _('Approximately %d%% from the top')%frac return loctext - def elem_clicked(self, tag, frac, elem_id, loc): - self.current_frag = elem_id or loc + def elem_clicked(self, tag, frac, elem_id, loc, totals): + self.current_frag = elem_id or (loc, totals) base = _('Location: A <%s> tag inside the file')%tag loctext = base + ' [%s]'%self.get_loctext(frac) self.dest_label.setText(self.base_msg + '
' + diff --git a/src/calibre/gui2/toc/main.py b/src/calibre/gui2/toc/main.py index d9fc3ff955..85526778e5 100644 --- a/src/calibre/gui2/toc/main.py +++ b/src/calibre/gui2/toc/main.py @@ -743,7 +743,7 @@ class TOCView(QWidget): # {{{ def update_item(self, item, where, name, frag, title): if isinstance(frag, tuple): - frag = add_id(self.ebook, name, frag) + frag = add_id(self.ebook, name, *frag) child = TOC(title, name, frag) child.dest_exists = True if item is None: