Allow using relative URLs in comments type metadata. The relative URLs are interpreted relative to the book folder in the calibre library

This commit is contained in:
Kovid Goyal 2018-03-08 10:54:27 +05:30
parent aed9a856b3
commit a9500f08da
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 38 additions and 6 deletions

View File

@ -3,6 +3,7 @@
# License: GPLv3 Copyright: 2010, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2010, Kovid Goyal <kovid at kovidgoyal.net>
import cPickle import cPickle
import os
import re import re
from binascii import unhexlify from binascii import unhexlify
from collections import namedtuple from collections import namedtuple
@ -38,6 +39,18 @@ _css = None
InternetSearch = namedtuple('InternetSearch', 'author where') InternetSearch = namedtuple('InternetSearch', 'author where')
def set_html(mi, html, web_view):
from calibre.gui2.ui import get_gui
gui = get_gui()
book_id = getattr(mi, 'id', None)
if gui and book_id is not None:
path = gui.current_db.abspath(book_id, index_is_id=True)
if path:
web_view.setHtml(html, QUrl.fromLocalFile(os.path.join(path, 'metadata.html')))
return
web_view.setHtml(html)
def css(): def css():
global _css global _css
if _css is None: if _css is None:
@ -603,7 +616,7 @@ class BookInfo(QWebView):
def show_data(self, mi): def show_data(self, mi):
html = render_html(mi, css(), self.vertical, self.parent()) html = render_html(mi, css(), self.vertical, self.parent())
self.setHtml(html) set_html(mi, html, self)
def mouseDoubleClickEvent(self, ev): def mouseDoubleClickEvent(self, ev):
swidth = self.page().mainFrame().scrollBarGeometry(Qt.Vertical).width() swidth = self.page().mainFrame().scrollBarGeometry(Qt.Vertical).width()

View File

@ -72,6 +72,7 @@ class EditorWidget(QWebView): # {{{
def __init__(self, parent=None): def __init__(self, parent=None):
QWebView.__init__(self, parent) QWebView.__init__(self, parent)
self.base_url = None
self._parent = weakref.ref(parent) self._parent = weakref.ref(parent)
self.readonly = False self.readonly = False
@ -370,10 +371,17 @@ class EditorWidget(QWebView): # {{{
return ans return ans
def fset(self, val): def fset(self, val):
self.setHtml(val) if self.base_url is None:
self.setHtml(val)
else:
self.setHtml(val, self.base_url)
self.set_font_style() self.set_font_style()
return property(fget=fget, fset=fset) return property(fget=fget, fset=fset)
def set_base_url(self, qurl):
self.base_url = qurl
self.setHtml('', self.base_url)
def set_html(self, val, allow_undo=True): def set_html(self, val, allow_undo=True):
if not allow_undo or self.readonly: if not allow_undo or self.readonly:
self.html = val self.html = val
@ -650,6 +658,7 @@ class Editor(QWidget): # {{{
t = getattr(self, 'toolbar%d'%i) t = getattr(self, 'toolbar%d'%i)
t.setIconSize(QSize(18, 18)) t.setIconSize(QSize(18, 18))
self.editor = EditorWidget(self) self.editor = EditorWidget(self)
self.set_base_url = self.editor.set_base_url
self.set_html = self.editor.set_html self.set_html = self.editor.set_html
self.tabs = QTabWidget(self) self.tabs = QTabWidget(self)
self.tabs.setTabPosition(self.tabs.South) self.tabs.setTabPosition(self.tabs.South)

View File

@ -5,10 +5,11 @@ __license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import os
from functools import partial from functools import partial
from PyQt5.Qt import (QComboBox, QLabel, QSpinBox, QDoubleSpinBox, QDateTimeEdit, from PyQt5.Qt import (QComboBox, QLabel, QSpinBox, QDoubleSpinBox, QDateTimeEdit,
QDateTime, QGroupBox, QVBoxLayout, QSizePolicy, QGridLayout, QDateTime, QGroupBox, QVBoxLayout, QSizePolicy, QGridLayout, QUrl,
QSpacerItem, QIcon, QCheckBox, QWidget, QHBoxLayout, QLineEdit, QSpacerItem, QIcon, QCheckBox, QWidget, QHBoxLayout, QLineEdit,
QPushButton, QMessageBox, QToolButton, Qt, QPlainTextEdit) QPushButton, QMessageBox, QToolButton, Qt, QPlainTextEdit)
@ -315,6 +316,12 @@ class Comments(Base):
self._box.setLayout(self._layout) self._box.setLayout(self._layout)
self.widgets = [self._box] self.widgets = [self._box]
def initialize(self, book_id):
path = self.db.abspath(book_id, index_is_id=True)
if path:
self._tb.set_base_url(QUrl.fromLocalFile(os.path.join(path, 'metadata.html')))
return Base.initialize(self, book_id)
def setter(self, val): def setter(self, val):
if not val or not val.strip(): if not val or not val.strip():
val = '' val = ''

View File

@ -14,7 +14,7 @@ from PyQt5.QtWebKitWidgets import QWebView
from calibre import fit_image from calibre import fit_image
from calibre.gui2 import NO_URL_FORMATTING, gprefs from calibre.gui2 import NO_URL_FORMATTING, gprefs
from calibre.gui2.book_details import css, details_context_menu_event, render_html from calibre.gui2.book_details import css, details_context_menu_event, render_html, set_html
from calibre.gui2.ui import get_gui from calibre.gui2.ui import get_gui
from calibre.gui2.widgets import CoverView from calibre.gui2.widgets import CoverView
from calibre.gui2.widgets2 import Dialog from calibre.gui2.widgets2 import Dialog
@ -275,7 +275,7 @@ class BookInfo(QDialog):
self.cover_pixmap.setDevicePixelRatio(dpr) self.cover_pixmap.setDevicePixelRatio(dpr)
self.resize_cover() self.resize_cover()
html = render_html(mi, self.css, True, self, pref_name='popup_book_display_fields') html = render_html(mi, self.css, True, self, pref_name='popup_book_display_fields')
self.details.setHtml(html) set_html(mi, html, self.details)
self.marked = mi.marked self.marked = mi.marked
self.cover.setBackgroundBrush(self.marked_brush if mi.marked else self.normal_brush) self.cover.setBackgroundBrush(self.marked_brush if mi.marked else self.normal_brush)
self.update_cover_tooltip() self.update_cover_tooltip()

View File

@ -15,7 +15,7 @@ from PyQt5.Qt import (
QLabel, QGridLayout, QApplication, QDoubleSpinBox, QListWidgetItem, QSize, QLabel, QGridLayout, QApplication, QDoubleSpinBox, QListWidgetItem, QSize,
QPixmap, QDialog, QMenu, QLineEdit, QSizePolicy, QKeySequence, QPixmap, QDialog, QMenu, QLineEdit, QSizePolicy, QKeySequence,
QDialogButtonBox, QAction, QCalendarWidget, QDate, QDateTime, QUndoCommand, QDialogButtonBox, QAction, QCalendarWidget, QDate, QDateTime, QUndoCommand,
QUndoStack, QVBoxLayout, QPlainTextEdit) QUndoStack, QVBoxLayout, QPlainTextEdit, QUrl)
from calibre.gui2.widgets import EnLineEdit, FormatList as _FormatList, ImageView from calibre.gui2.widgets import EnLineEdit, FormatList as _FormatList, ImageView
from calibre.gui2.widgets2 import access_key, populate_standard_spinbox_context_menu, RightClickButton, Dialog, RatingEditor from calibre.gui2.widgets2 import access_key, populate_standard_spinbox_context_menu, RightClickButton, Dialog, RatingEditor
@ -1247,6 +1247,9 @@ class CommentsEdit(Editor, ToMetadataMixin): # {{{
return property(fget=fget, fset=fset) return property(fget=fget, fset=fset)
def initialize(self, db, id_): def initialize(self, db, id_):
path = db.abspath(id_, index_is_id=True)
if path:
self.set_base_url(QUrl.fromLocalFile(os.path.join(path, 'metadata.html')))
self.current_val = db.comments(id_, index_is_id=True) self.current_val = db.comments(id_, index_is_id=True)
self.original_val = self.current_val self.original_val = self.current_val