E-book viewer: Allow specifying ToC hrefs via the --open-at command line parameter. Fixes #1656573 [[Enhancement] viewer command-line parameter to go to ePub TOC entry/bookmark](https://bugs.launchpad.net/calibre/+bug/1656573)

This commit is contained in:
Kovid Goyal 2019-04-18 10:39:28 +05:30
parent c1333ea71b
commit 4cdf76309b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 24 additions and 6 deletions

View File

@ -1083,13 +1083,22 @@ class EbookViewer(MainWindow):
self.pending_goto_page = open_at self.pending_goto_page = open_at
else: else:
self.goto_page(open_at, loaded_check=False) self.goto_page(open_at, loaded_check=False)
elif open_at.startswith('toc:'): else:
index = self.toc_model.search(open_at[4:]) target_index = None
if index.isValid(): if open_at.startswith('toc:'):
index = self.toc_model.search(open_at[4:])
if index.isValid():
target_index = index
elif open_at.startswith('toc-href:'):
for index in self.toc_model.find_indices_by_href(open_at[len('toc-href:'):]):
if index.isValid():
target_index = index
break
if target_index is not None:
if self.resize_in_progress: if self.resize_in_progress:
self.pending_toc_click = index self.pending_toc_click = target_index
else: else:
self.toc_clicked(index, force=True) self.toc_clicked(target_index, force=True)
def set_vscrollbar_value(self, pagenum): def set_vscrollbar_value(self, pagenum):
self.vertical_scrollbar.blockSignals(True) self.vertical_scrollbar.blockSignals(True)
@ -1306,7 +1315,7 @@ def main(args=sys.argv):
opts, args = parser.parse_args(args) opts, args = parser.parse_args(args)
open_at = None open_at = None
if opts.open_at is not None: if opts.open_at is not None:
if opts.open_at.startswith('toc:'): if ':' in opts.open_at:
open_at = opts.open_at open_at = opts.open_at
else: else:
open_at = float(opts.open_at.replace(',', '.')) open_at = float(opts.open_at.replace(',', '.'))

View File

@ -150,6 +150,7 @@ class TOCItem(QStandardItem):
text = re.sub(r'\s', ' ', text) text = re.sub(r'\s', ' ', text)
self.title = text self.title = text
self.parent = parent self.parent = parent
self.href = toc.href
QStandardItem.__init__(self, text if text else '') QStandardItem.__init__(self, text if text else '')
self.abspath = toc.abspath if toc.href else None self.abspath = toc.abspath if toc.href else None
self.fragment = toc.fragment self.fragment = toc.fragment
@ -380,6 +381,14 @@ class TOC(QStandardItemModel):
if primary_contains(query, item.text()): if primary_contains(query, item.text()):
yield item yield item
def find_indices_by_href(self, query):
for item in self.all_items:
q = (item.href or '')
if item.fragment:
q += '#' + item.fragment
if primary_contains(query, q):
yield self.indexFromItem(item)
def search(self, query): def search(self, query):
cq = self.current_query cq = self.current_query
if cq['items'] and -1 < cq['index'] < len(cq['items']): if cq['items'] and -1 < cq['index'] < len(cq['items']):