Viewer: The --open-at command line argument now allows matching on ToC hrefs as well as titles

This commit is contained in:
Kovid Goyal 2019-11-21 10:27:36 +05:30
parent c1d6b7b85c
commit ab57033fcb
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 20 additions and 3 deletions

View File

@ -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

View File

@ -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']):

View File

@ -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):