This commit is contained in:
Kovid Goyal 2021-01-18 10:49:50 +05:30
parent 0bdaf44cfb
commit 447f366839
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 32 additions and 24 deletions

View File

@ -7,3 +7,23 @@ def get_current_book_data(set_val=False):
if set_val is not False:
setattr(get_current_book_data, 'ans', set_val)
return getattr(get_current_book_data, 'ans', {})
def link_prefix_for_location_links(add_open_at=True):
cbd = get_current_book_data()
link_prefix = library_id = None
if 'calibre_library_id' in cbd:
library_id = cbd['calibre_library_id']
book_id = cbd['calibre_book_id']
book_fmt = cbd['calibre_book_fmt']
elif cbd.get('book_library_details'):
bld = cbd['book_library_details']
book_id = bld['book_id']
book_fmt = bld['fmt'].upper()
library_id = bld['library_id']
if library_id:
library_id = '_hex_-' + library_id.encode('utf-8').hex()
link_prefix = f'calibre://view-book/{library_id}/{book_id}/{book_fmt}'
if add_open_at:
link_prefix += '?open_at='
return link_prefix

View File

@ -23,7 +23,7 @@ from calibre.gui2.dialogs.confirm_delete import confirm
from calibre.gui2.library.annotations import (
Details, Export as ExportBase, render_highlight_as_text, render_notes
)
from calibre.gui2.viewer import get_current_book_data
from calibre.gui2.viewer import link_prefix_for_location_links
from calibre.gui2.viewer.config import vprefs
from calibre.gui2.viewer.search import SearchInput
from calibre.gui2.viewer.shortcuts import get_shortcut_for, index_to_key_sequence
@ -131,20 +131,6 @@ class Export(ExportBase):
return _('highlights')
def exported_data(self):
cbd = get_current_book_data()
link_prefix = library_id = None
if 'calibre_library_id' in cbd:
library_id = cbd['calibre_library_id']
book_id = cbd['calibre_book_id']
book_fmt = cbd['calibre_book_fmt']
elif cbd.get('book_library_details'):
bld = cbd['book_library_details']
book_id = bld['book_id']
book_fmt = bld['fmt'].upper()
library_id = bld['library_id']
if library_id:
library_id = '_hex_-' + library_id.encode('utf-8').hex()
link_prefix = f'calibre://view-book/{library_id}/{book_id}/{book_fmt}?open_at='
fmt = self.export_format.currentData()
if fmt == 'calibre_highlights':
return json.dumps({
@ -154,6 +140,7 @@ class Export(ExportBase):
}, ensure_ascii=False, sort_keys=True, indent=2)
lines = []
as_markdown = fmt == 'md'
link_prefix = link_prefix_for_location_links()
for hl in self.annotations:
render_highlight_as_text(hl, lines, as_markdown=as_markdown, link_prefix=link_prefix)
return '\n'.join(lines).strip()

View File

@ -8,12 +8,16 @@ import shutil
import sys
from itertools import count
from PyQt5.Qt import (
QT_VERSION, QApplication, QBuffer, QByteArray, QFontDatabase, QFontInfo, QPalette, QEvent,
QHBoxLayout, QMimeData, QSize, Qt, QTimer, QUrl, QWidget, pyqtSignal, QIODevice, QLocale
QT_VERSION, QApplication, QBuffer, QByteArray, QEvent, QFontDatabase, QFontInfo,
QHBoxLayout, QIODevice, QLocale, QMimeData, QPalette, QSize, Qt, QTimer, QUrl,
QWidget, pyqtSignal
)
from PyQt5.QtWebEngineCore import (
QWebEngineUrlRequestInfo, QWebEngineUrlRequestJob, QWebEngineUrlSchemeHandler
)
from PyQt5.QtWebEngineCore import QWebEngineUrlSchemeHandler, QWebEngineUrlRequestJob, QWebEngineUrlRequestInfo
from PyQt5.QtWebEngineWidgets import (
QWebEnginePage, QWebEngineProfile, QWebEngineScript, QWebEngineView, QWebEngineSettings
QWebEnginePage, QWebEngineProfile, QWebEngineScript, QWebEngineSettings,
QWebEngineView
)
from calibre import as_unicode, prints
@ -24,6 +28,7 @@ from calibre.constants import (
from calibre.ebooks.metadata.book.base import field_metadata
from calibre.ebooks.oeb.polish.utils import guess_type
from calibre.gui2 import choose_images, error_dialog, safe_open_url
from calibre.gui2.viewer import link_prefix_for_location_links
from calibre.gui2.viewer.config import viewer_config_dir, vprefs
from calibre.gui2.viewer.tts import TTS
from calibre.gui2.webengine import (
@ -621,11 +626,7 @@ class WebView(RestartingWebEngineView):
def start_book_load(self, initial_position=None, highlights=None, current_book_data=None):
key = (set_book_path.path,)
cbd = current_book_data or {}
book_url = None
if 'calibre_library_id' in cbd:
lid = cbd['calibre_library_id'].encode('utf-8').hex()
book_url = f'calibre://view-book/_hex_-{lid}/{cbd["calibre_book_id"]}/{cbd["calibre_book_fmt"]}'
book_url = link_prefix_for_location_links(add_open_at=False)
self.execute_when_ready('start_book_load', key, initial_position, set_book_path.pathtoebook, highlights or [], book_url)
def execute_when_ready(self, action, *args):