From ab57033fcb4646bbba553bada0431b66c78a4864 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 21 Nov 2019 10:27:36 +0530 Subject: [PATCH] Viewer: The --open-at command line argument now allows matching on ToC hrefs as well as titles --- src/calibre/gui2/viewer/main.py | 10 +++++++--- src/calibre/gui2/viewer/toc.py | 9 +++++++++ src/calibre/gui2/viewer/ui.py | 4 ++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/calibre/gui2/viewer/main.py b/src/calibre/gui2/viewer/main.py index 133e991829..e660c56b97 100644 --- a/src/calibre/gui2/viewer/main.py +++ b/src/calibre/gui2/viewer/main.py @@ -166,7 +166,9 @@ View an e-book. 'a location or position you can get by using the Go to->Location action in the viewer controls. ' 'Alternately, you can use the form toc:something and it will open ' 'at the location of the first Table of Contents entry that contains ' - 'the string "something".')) + 'the string "something". The form toc-href:something will match the ' + 'href (internal link destination) of toc nodes. The matching is exact, ' + 'If you want to match a substring, use the form toc-href-contains:something. ')) a('--continue', default=False, action='store_true', dest='continue_reading', help=_('Continue reading at the previously opened book')) @@ -187,8 +189,10 @@ def main(args=sys.argv): parser = option_parser() opts, args = parser.parse_args(args) - - if opts.open_at and not (opts.open_at.startswith('toc:') or opts.open_at.startswith('epubcfi(/') or is_float(opts.open_at)): + oat = opts.open_at + if oat and not ( + oat.startswith('toc:') or oat.startswith('toc-href:') or oat.startswith('toc-href-contains:') or + oat.startswith('epubcfi(/') or is_float(oat)): raise SystemExit('Not a valid --open-at value: {}'.format(opts.open_at)) listener = None diff --git a/src/calibre/gui2/viewer/toc.py b/src/calibre/gui2/viewer/toc.py index b7bae85d7d..b67ec8e5ad 100644 --- a/src/calibre/gui2/viewer/toc.py +++ b/src/calibre/gui2/viewer/toc.py @@ -144,6 +144,9 @@ class TOCItem(QStandardItem): def __init__(self, toc, depth, all_items, normal_font, emphasis_font, parent=None): text = toc.get('title') or '' + self.href = (toc.get('dest') or '') + if toc.get('frag'): + self.href += '#' + toc['frag'] if text: text = re.sub(r'\s', ' ', text) self.title = text @@ -214,6 +217,12 @@ class TOC(QStandardItemModel): for item in self.find_items(query): return item.node_id + def node_id_for_href(self, query, exact=False): + for item in self.all_items: + href = item.href + if (exact and query == href) or (not exact and query in href): + return item.node_id + def search(self, query): cq = self.current_query if cq['items'] and -1 < cq['index'] < len(cq['items']): diff --git a/src/calibre/gui2/viewer/ui.py b/src/calibre/gui2/viewer/ui.py index 868fe6a7f5..5a7c953795 100644 --- a/src/calibre/gui2/viewer/ui.py +++ b/src/calibre/gui2/viewer/ui.py @@ -407,6 +407,10 @@ class EbookViewer(MainWindow): if open_at: if open_at.startswith('toc:'): initial_toc_node = self.toc_model.node_id_for_text(open_at[len('toc:'):]) + elif open_at.startswith('toc-href:'): + initial_toc_node = self.toc_model.node_id_for_href(open_at[len('toc-href:'):], exact=True) + elif open_at.startswith('toc-href-contains:'): + initial_toc_node = self.toc_model.node_id_for_href(open_at[len('toc-href-contains:'):], exact=False) elif open_at.startswith('epubcfi(/'): initial_cfi = open_at elif is_float(open_at):