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>
import cPickle
import os
import re
from binascii import unhexlify
from collections import namedtuple
@ -38,6 +39,18 @@ _css = None
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():
global _css
if _css is None:
@ -603,7 +616,7 @@ class BookInfo(QWebView):
def show_data(self, mi):
html = render_html(mi, css(), self.vertical, self.parent())
self.setHtml(html)
set_html(mi, html, self)
def mouseDoubleClickEvent(self, ev):
swidth = self.page().mainFrame().scrollBarGeometry(Qt.Vertical).width()

View File

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

View File

@ -5,10 +5,11 @@ __license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import os
from functools import partial
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,
QPushButton, QMessageBox, QToolButton, Qt, QPlainTextEdit)
@ -315,6 +316,12 @@ class Comments(Base):
self._box.setLayout(self._layout)
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):
if not val or not val.strip():
val = ''

View File

@ -14,7 +14,7 @@ from PyQt5.QtWebKitWidgets import QWebView
from calibre import fit_image
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.widgets import CoverView
from calibre.gui2.widgets2 import Dialog
@ -275,7 +275,7 @@ class BookInfo(QDialog):
self.cover_pixmap.setDevicePixelRatio(dpr)
self.resize_cover()
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.cover.setBackgroundBrush(self.marked_brush if mi.marked else self.normal_brush)
self.update_cover_tooltip()

View File

@ -15,7 +15,7 @@ from PyQt5.Qt import (
QLabel, QGridLayout, QApplication, QDoubleSpinBox, QListWidgetItem, QSize,
QPixmap, QDialog, QMenu, QLineEdit, QSizePolicy, QKeySequence,
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.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)
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.original_val = self.current_val