mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Cleanup previous PR
This commit is contained in:
parent
83fa5e1647
commit
ef13a51774
@ -481,6 +481,12 @@ class Markdown(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 super().initialize(book_id)
|
||||
|
||||
def setter(self, val):
|
||||
self._tb.markdown = str(val or '').strip()
|
||||
|
||||
|
@ -1,34 +1,58 @@
|
||||
#!/usr/bin/env python
|
||||
# License: GPLv3 Copyright: 2023, un_pogaz <un.pogaz@gmail.com>
|
||||
|
||||
|
||||
__license__ = 'GPL v3'
|
||||
__copyright__ = '2023, un_pogaz <un.pogaz@gmail.com>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import os
|
||||
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.widgets2 import HTMLDisplay
|
||||
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): # {{{
|
||||
|
||||
def __init__(self, parent=None):
|
||||
QWidget.__init__(self, parent)
|
||||
self.base_url = None
|
||||
self._layout = QVBoxLayout(self)
|
||||
self.setLayout(self._layout)
|
||||
self.tabs = QTabWidget(self)
|
||||
self.tabs.setTabPosition(QTabWidget.TabPosition.South)
|
||||
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.setDefaultStyleSheet(css())
|
||||
self.preview.setTabChangesFocus(True)
|
||||
self.preview = Preview(self)
|
||||
self.preview.anchor_clicked.connect(self.link_clicked)
|
||||
|
||||
self.tabs.addTab(self.editor, _('&Markdown source'))
|
||||
self.tabs.addTab(self.preview, _('&Preview'))
|
||||
@ -36,22 +60,31 @@ class Editor(QWidget): # {{{
|
||||
self.tabs.currentChanged[int].connect(self.change_tab)
|
||||
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):
|
||||
self.editor.setMinimumHeight(val)
|
||||
|
||||
@property
|
||||
def markdown(self):
|
||||
self.tabs.setCurrentIndex(0)
|
||||
return self.editor.toPlainText().strip()
|
||||
|
||||
@markdown.setter
|
||||
def markdown(self, v):
|
||||
self.editor.setPlainText(str(v or ''))
|
||||
if self.tab == 'preview':
|
||||
self.update_preview()
|
||||
|
||||
def change_tab(self, index):
|
||||
if index == 0: # changing to source
|
||||
pass
|
||||
if index == 1: # changing to preview
|
||||
self.update_preview()
|
||||
|
||||
def update_preview(self):
|
||||
html = markdown(self.editor.toPlainText().strip())
|
||||
val = f'''\
|
||||
<html>
|
||||
@ -82,7 +115,6 @@ class Editor(QWidget): # {{{
|
||||
newmarkdown = smarten_punctuation(markdown)
|
||||
if markdown != newmarkdown:
|
||||
self.markdown = newmarkdown
|
||||
|
||||
# }}}
|
||||
|
||||
|
||||
@ -90,6 +122,7 @@ if __name__ == '__main__':
|
||||
from calibre.gui2 import Application
|
||||
app = Application([])
|
||||
w = Editor()
|
||||
w.set_base_url(QUrl.fromLocalFile(os.getcwd()))
|
||||
w.resize(800, 600)
|
||||
w.setWindowFlag(Qt.WindowType.Dialog)
|
||||
w.show()
|
||||
|
Loading…
x
Reference in New Issue
Block a user