mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
DRYer
This commit is contained in:
parent
0bdaf44cfb
commit
447f366839
@ -7,3 +7,23 @@ def get_current_book_data(set_val=False):
|
|||||||
if set_val is not False:
|
if set_val is not False:
|
||||||
setattr(get_current_book_data, 'ans', set_val)
|
setattr(get_current_book_data, 'ans', set_val)
|
||||||
return getattr(get_current_book_data, 'ans', {})
|
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
|
||||||
|
@ -23,7 +23,7 @@ from calibre.gui2.dialogs.confirm_delete import confirm
|
|||||||
from calibre.gui2.library.annotations import (
|
from calibre.gui2.library.annotations import (
|
||||||
Details, Export as ExportBase, render_highlight_as_text, render_notes
|
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.config import vprefs
|
||||||
from calibre.gui2.viewer.search import SearchInput
|
from calibre.gui2.viewer.search import SearchInput
|
||||||
from calibre.gui2.viewer.shortcuts import get_shortcut_for, index_to_key_sequence
|
from calibre.gui2.viewer.shortcuts import get_shortcut_for, index_to_key_sequence
|
||||||
@ -131,20 +131,6 @@ class Export(ExportBase):
|
|||||||
return _('highlights')
|
return _('highlights')
|
||||||
|
|
||||||
def exported_data(self):
|
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()
|
fmt = self.export_format.currentData()
|
||||||
if fmt == 'calibre_highlights':
|
if fmt == 'calibre_highlights':
|
||||||
return json.dumps({
|
return json.dumps({
|
||||||
@ -154,6 +140,7 @@ class Export(ExportBase):
|
|||||||
}, ensure_ascii=False, sort_keys=True, indent=2)
|
}, ensure_ascii=False, sort_keys=True, indent=2)
|
||||||
lines = []
|
lines = []
|
||||||
as_markdown = fmt == 'md'
|
as_markdown = fmt == 'md'
|
||||||
|
link_prefix = link_prefix_for_location_links()
|
||||||
for hl in self.annotations:
|
for hl in self.annotations:
|
||||||
render_highlight_as_text(hl, lines, as_markdown=as_markdown, link_prefix=link_prefix)
|
render_highlight_as_text(hl, lines, as_markdown=as_markdown, link_prefix=link_prefix)
|
||||||
return '\n'.join(lines).strip()
|
return '\n'.join(lines).strip()
|
||||||
|
@ -8,12 +8,16 @@ import shutil
|
|||||||
import sys
|
import sys
|
||||||
from itertools import count
|
from itertools import count
|
||||||
from PyQt5.Qt import (
|
from PyQt5.Qt import (
|
||||||
QT_VERSION, QApplication, QBuffer, QByteArray, QFontDatabase, QFontInfo, QPalette, QEvent,
|
QT_VERSION, QApplication, QBuffer, QByteArray, QEvent, QFontDatabase, QFontInfo,
|
||||||
QHBoxLayout, QMimeData, QSize, Qt, QTimer, QUrl, QWidget, pyqtSignal, QIODevice, QLocale
|
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 (
|
from PyQt5.QtWebEngineWidgets import (
|
||||||
QWebEnginePage, QWebEngineProfile, QWebEngineScript, QWebEngineView, QWebEngineSettings
|
QWebEnginePage, QWebEngineProfile, QWebEngineScript, QWebEngineSettings,
|
||||||
|
QWebEngineView
|
||||||
)
|
)
|
||||||
|
|
||||||
from calibre import as_unicode, prints
|
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.metadata.book.base import field_metadata
|
||||||
from calibre.ebooks.oeb.polish.utils import guess_type
|
from calibre.ebooks.oeb.polish.utils import guess_type
|
||||||
from calibre.gui2 import choose_images, error_dialog, safe_open_url
|
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.config import viewer_config_dir, vprefs
|
||||||
from calibre.gui2.viewer.tts import TTS
|
from calibre.gui2.viewer.tts import TTS
|
||||||
from calibre.gui2.webengine import (
|
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):
|
def start_book_load(self, initial_position=None, highlights=None, current_book_data=None):
|
||||||
key = (set_book_path.path,)
|
key = (set_book_path.path,)
|
||||||
cbd = current_book_data or {}
|
book_url = link_prefix_for_location_links(add_open_at=False)
|
||||||
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"]}'
|
|
||||||
self.execute_when_ready('start_book_load', key, initial_position, set_book_path.pathtoebook, highlights or [], book_url)
|
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):
|
def execute_when_ready(self, action, *args):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user