Cleanup previous PR

This commit is contained in:
Kovid Goyal 2023-04-26 12:35:21 +05:30
parent 83fa5e1647
commit ef13a51774
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 62 additions and 23 deletions

View File

@ -481,6 +481,12 @@ class Markdown(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 super().initialize(book_id)
def setter(self, val): def setter(self, val):
self._tb.markdown = str(val or '').strip() self._tb.markdown = str(val or '').strip()

View File

@ -1,34 +1,58 @@
#!/usr/bin/env python #!/usr/bin/env python
# License: GPLv3 Copyright: 2023, un_pogaz <un.pogaz@gmail.com>
import os
__license__ = 'GPL v3'
__copyright__ = '2023, un_pogaz <un.pogaz@gmail.com>'
__docformat__ = 'restructuredtext en'
from qt.core import ( from qt.core import (
QPlainTextEdit, Qt, QTabWidget, QVBoxLayout, QWidget, QPlainTextEdit, Qt, QTabWidget, QUrl, QVBoxLayout, QWidget, pyqtSignal,
) )
from calibre.gui2 import safe_open_url
from calibre.gui2.book_details import css from calibre.gui2.book_details import css
from calibre.gui2.widgets2 import HTMLDisplay from calibre.gui2.widgets2 import HTMLDisplay
from calibre.library.comments import markdown from calibre.library.comments import markdown
class Preview(HTMLDisplay):
def __init__(self, parent=None):
super().__init__(parent)
self.setDefaultStyleSheet(css())
self.setTabChangesFocus(True)
self.base_url = None
def loadResource(self, rtype, qurl):
if self.base_url is not None and qurl.isRelative():
qurl = self.base_url.resolved(qurl)
return super().loadResource(rtype, qurl)
class MarkdownEdit(QPlainTextEdit):
smarten_punctuation = pyqtSignal()
def contextMenuEvent(self, ev):
m = self.createStandardContextMenu()
m.addSeparator()
m.addAction(_('Smarten punctuation'), self.smarten_punctuation.emit)
m.exec(ev.globalPos())
class Editor(QWidget): # {{{ class Editor(QWidget): # {{{
def __init__(self, parent=None): def __init__(self, parent=None):
QWidget.__init__(self, parent) QWidget.__init__(self, parent)
self.base_url = None
self._layout = QVBoxLayout(self) self._layout = QVBoxLayout(self)
self.setLayout(self._layout) self.setLayout(self._layout)
self.tabs = QTabWidget(self) self.tabs = QTabWidget(self)
self.tabs.setTabPosition(QTabWidget.TabPosition.South) self.tabs.setTabPosition(QTabWidget.TabPosition.South)
self._layout.addWidget(self.tabs) self._layout.addWidget(self.tabs)
self.editor = QPlainTextEdit(self.tabs) self.editor = MarkdownEdit(self)
self.editor.smarten_punctuation.connect(self.smarten_punctuation)
self.preview = HTMLDisplay(self.tabs) self.preview = Preview(self)
self.preview.setDefaultStyleSheet(css()) self.preview.anchor_clicked.connect(self.link_clicked)
self.preview.setTabChangesFocus(True)
self.tabs.addTab(self.editor, _('&Markdown source')) self.tabs.addTab(self.editor, _('&Markdown source'))
self.tabs.addTab(self.preview, _('&Preview')) self.tabs.addTab(self.preview, _('&Preview'))
@ -36,22 +60,31 @@ class Editor(QWidget): # {{{
self.tabs.currentChanged[int].connect(self.change_tab) self.tabs.currentChanged[int].connect(self.change_tab)
self.layout().setContentsMargins(0, 0, 0, 0) self.layout().setContentsMargins(0, 0, 0, 0)
def link_clicked(self, qurl):
safe_open_url(qurl)
def set_base_url(self, qurl):
self.base_url = qurl
self.preview.base_url = self.base_url
def set_minimum_height_for_editor(self, val): def set_minimum_height_for_editor(self, val):
self.editor.setMinimumHeight(val) self.editor.setMinimumHeight(val)
@property @property
def markdown(self): def markdown(self):
self.tabs.setCurrentIndex(0)
return self.editor.toPlainText().strip() return self.editor.toPlainText().strip()
@markdown.setter @markdown.setter
def markdown(self, v): def markdown(self, v):
self.editor.setPlainText(str(v or '')) self.editor.setPlainText(str(v or ''))
if self.tab == 'preview':
self.update_preview()
def change_tab(self, index): def change_tab(self, index):
if index == 0: # changing to source
pass
if index == 1: # changing to preview if index == 1: # changing to preview
self.update_preview()
def update_preview(self):
html = markdown(self.editor.toPlainText().strip()) html = markdown(self.editor.toPlainText().strip())
val = f'''\ val = f'''\
<html> <html>
@ -82,7 +115,6 @@ class Editor(QWidget): # {{{
newmarkdown = smarten_punctuation(markdown) newmarkdown = smarten_punctuation(markdown)
if markdown != newmarkdown: if markdown != newmarkdown:
self.markdown = newmarkdown self.markdown = newmarkdown
# }}} # }}}
@ -90,6 +122,7 @@ if __name__ == '__main__':
from calibre.gui2 import Application from calibre.gui2 import Application
app = Application([]) app = Application([])
w = Editor() w = Editor()
w.set_base_url(QUrl.fromLocalFile(os.getcwd()))
w.resize(800, 600) w.resize(800, 600)
w.setWindowFlag(Qt.WindowType.Dialog) w.setWindowFlag(Qt.WindowType.Dialog)
w.show() w.show()