src/eric7/UI/Previewers/PreviewerHTML.py

Wed, 13 Jul 2022 14:55:47 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 13 Jul 2022 14:55:47 +0200
branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9268
0d414630a28e
permissions
-rw-r--r--

Reformatted the source code using the 'Black' utility.

3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
8881
54e42bc2437a Updated copyright for 2022.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8674
diff changeset
3 # Copyright (c) 2014 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
3459
275cb00c83e2 Continued adding QSS preview support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3458
diff changeset
7 Module implementing a previewer widget for HTML, Markdown and ReST files.
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import os
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import threading
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import re
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
13 import shutil
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
14 import tempfile
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
15 import sys
7192
a22eee00b052 Started removing runtime support for Python2 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
16 import io
8243
cc717c2ae956 Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 2).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8235
diff changeset
17 import contextlib
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
8318
962bce857696 Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
19 from PyQt6.QtCore import pyqtSlot, pyqtSignal, Qt, QUrl, QThread
962bce857696 Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
20 from PyQt6.QtGui import QCursor
962bce857696 Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
21 from PyQt6.QtWidgets import (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
22 QWidget,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
23 QVBoxLayout,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
24 QLabel,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
25 QCheckBox,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
26 QSizePolicy,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
27 QToolTip,
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
28 )
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29
8358
144a6b854f70 Sorted the eric specific extensions into packages named like the corresponding PyQt packages (i.e. EricCore,EricGui and EricWidgets).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8356
diff changeset
30 from EricWidgets.EricApplication import ericApp
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 import Utilities
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 import Preferences
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35
4615
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
36 class PreviewerHTML(QWidget):
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 Class implementing a previewer widget for HTML, Markdown and ReST files.
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
40
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 def __init__(self, parent=None):
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 Constructor
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
44
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 @param parent reference to the parent widget (QWidget)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8143
diff changeset
47 super().__init__(parent)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
48
4615
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
49 self.__layout = QVBoxLayout(self)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
50
4615
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
51 self.titleLabel = QLabel(self)
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
52 self.titleLabel.setWordWrap(True)
8143
2c730d5fd177 Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7960
diff changeset
53 self.titleLabel.setTextInteractionFlags(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
54 Qt.TextInteractionFlag.NoTextInteraction
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
55 )
4615
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
56 self.__layout.addWidget(self.titleLabel)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
57
6372
ae44c83fccab CodeDocumentationViewer, PreviewerHTML: prepared the code for the non-availability of either QtWebEngine or QtWebKit (e.g. in the Win-32 PyQt5 wheels as of 5.11).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6287
diff changeset
58 self.__previewAvailable = True
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
59
4615
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
60 try:
8318
962bce857696 Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
61 from PyQt6.QtWebEngineWidgets import QWebEngineView
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
62
4625
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
63 self.previewView = QWebEngineView(self)
5845
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
64 self.previewView.page().linkHovered.connect(self.__showLink)
4625
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
65 except ImportError:
7196
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
66 self.__previewAvailable = False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
67 self.titleLabel.setText(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
68 self.tr(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
69 "<b>HTML Preview is not available!<br/>"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
70 "Install PyQt6-WebEngine.</b>"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
71 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
72 )
8143
2c730d5fd177 Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7960
diff changeset
73 self.titleLabel.setAlignment(Qt.AlignmentFlag.AlignHCenter)
7196
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
74 self.__layout.addStretch()
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
75 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
76
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
77 sizePolicy = QSizePolicy(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
78 QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Expanding
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
79 )
4615
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
80 sizePolicy.setHorizontalStretch(0)
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
81 sizePolicy.setVerticalStretch(0)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
82 sizePolicy.setHeightForWidth(self.previewView.sizePolicy().hasHeightForWidth())
4615
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
83 self.previewView.setSizePolicy(sizePolicy)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
84 self.previewView.setContextMenuPolicy(Qt.ContextMenuPolicy.NoContextMenu)
4615
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
85 self.previewView.setUrl(QUrl("about:blank"))
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
86 self.__layout.addWidget(self.previewView)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
87
4615
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
88 self.jsCheckBox = QCheckBox(self.tr("Enable JavaScript"), self)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
89 self.jsCheckBox.setToolTip(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
90 self.tr("Select to enable JavaScript for HTML previews")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
91 )
4615
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
92 self.__layout.addWidget(self.jsCheckBox)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
93
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
94 self.ssiCheckBox = QCheckBox(self.tr("Enable Server Side Includes"), self)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
95 self.ssiCheckBox.setToolTip(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
96 self.tr("Select to enable support for Server Side Includes")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
97 )
4615
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
98 self.__layout.addWidget(self.ssiCheckBox)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
99
4615
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
100 self.jsCheckBox.clicked[bool].connect(self.on_jsCheckBox_clicked)
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
101 self.ssiCheckBox.clicked[bool].connect(self.on_ssiCheckBox_clicked)
7c090c9d389d Changed the HTML Previewer to use a QWebEngineView, if QtWebKit is not available.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
102 self.previewView.titleChanged.connect(self.on_previewView_titleChanged)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
103
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
104 self.jsCheckBox.setChecked(Preferences.getUI("ShowFilePreviewJS"))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
105 self.ssiCheckBox.setChecked(Preferences.getUI("ShowFilePreviewSSI"))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
106
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 self.__scrollBarPositions = {}
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 self.__vScrollBarAtEnd = {}
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 self.__hScrollBarAtEnd = {}
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
110
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 self.__processingThread = PreviewProcessingThread()
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 self.__processingThread.htmlReady.connect(self.__setHtml)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 self.__previewedPath = None
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 self.__previewedEditor = None
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
116
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 def shutdown(self):
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 Public method to perform shutdown actions.
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 """
6372
ae44c83fccab CodeDocumentationViewer, PreviewerHTML: prepared the code for the non-availability of either QtWebEngine or QtWebKit (e.g. in the Win-32 PyQt5 wheels as of 5.11).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6287
diff changeset
121 if self.__previewAvailable:
ae44c83fccab CodeDocumentationViewer, PreviewerHTML: prepared the code for the non-availability of either QtWebEngine or QtWebKit (e.g. in the Win-32 PyQt5 wheels as of 5.11).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6287
diff changeset
122 self.__processingThread.wait()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
123
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 @pyqtSlot(bool)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 def on_jsCheckBox_clicked(self, checked):
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 Private slot to enable/disable JavaScript.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
128
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 @param checked state of the checkbox (boolean)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 Preferences.setUI("ShowFilePreviewJS", checked)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 self.__setJavaScriptEnabled(checked)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
133
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 def __setJavaScriptEnabled(self, enable):
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 Private method to enable/disable JavaScript.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
137
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 @param enable flag indicating the enable state (boolean)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 self.jsCheckBox.setChecked(enable)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
141
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 settings = self.previewView.settings()
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 settings.setAttribute(settings.JavascriptEnabled, enable)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
144
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 self.processEditor()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
146
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 @pyqtSlot(bool)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 def on_ssiCheckBox_clicked(self, checked):
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 Private slot to enable/disable SSI.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
151
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 @param checked state of the checkbox (boolean)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 Preferences.setUI("ShowFilePreviewSSI", checked)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 self.processEditor()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
156
5845
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
157 @pyqtSlot(str)
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
158 def __showLink(self, urlStr):
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
159 """
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
160 Private slot to show the hovered link in a tooltip.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
161
5845
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
162 @param urlStr hovered URL
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
163 @type str
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
164 """
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
165 QToolTip.showText(QCursor.pos(), urlStr, self.previewView)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
166
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 def processEditor(self, editor=None):
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 """
3591
2f2a4a76dd22 Corrected a bunch of source docu issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3539
diff changeset
169 Public slot to process an editor's text.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
170
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 @param editor editor to be processed (Editor)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 """
6372
ae44c83fccab CodeDocumentationViewer, PreviewerHTML: prepared the code for the non-availability of either QtWebEngine or QtWebKit (e.g. in the Win-32 PyQt5 wheels as of 5.11).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6287
diff changeset
173 if not self.__previewAvailable:
ae44c83fccab CodeDocumentationViewer, PreviewerHTML: prepared the code for the non-availability of either QtWebEngine or QtWebKit (e.g. in the Win-32 PyQt5 wheels as of 5.11).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6287
diff changeset
174 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
175
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 if editor is None:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 editor = self.__previewedEditor
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 else:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 self.__previewedEditor = editor
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
180
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 if editor is not None:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 fn = editor.getFileName()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
183
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 if fn:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 extension = os.path.normcase(os.path.splitext(fn)[1][1:])
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 else:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 extension = ""
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
188 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
189 extension in Preferences.getEditor("PreviewHtmlFileNameExtensions")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
190 or editor.getLanguage() == "HTML"
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
191 ):
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 language = "HTML"
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
193 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
194 extension in Preferences.getEditor("PreviewMarkdownFileNameExtensions")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
195 or editor.getLanguage().lower() == "markdown"
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
196 ):
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 language = "Markdown"
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
198 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
199 extension in Preferences.getEditor("PreviewRestFileNameExtensions")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
200 or editor.getLanguage().lower() == "restructuredtext"
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
201 ):
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 language = "ReST"
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 else:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
204 self.__setHtml(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
205 fn, self.tr("<p>No preview available for this type of file.</p>")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
206 )
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
208
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 if fn:
8600
f95586787d90 PreviewerHTML: changed the determination of the root path for the editor to be previewed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8358
diff changeset
210 rootPath = os.path.dirname(os.path.abspath(fn))
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 else:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 rootPath = ""
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
213
5396
8d8940307e44 Little enhancement to the previewers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
214 if bool(editor.text()):
8d8940307e44 Little enhancement to the previewers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
215 self.__processingThread.process(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
216 fn,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
217 language,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
218 editor.text(),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
219 self.ssiCheckBox.isChecked(),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
220 rootPath,
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
221 Preferences.getEditor("PreviewRestUseSphinx"),
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
222 Preferences.getEditor("PreviewMarkdownNLtoBR"),
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
223 Preferences.getEditor("PreviewMarkdownUsePyMdownExtensions"),
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
224 Preferences.getEditor("PreviewMarkdownHTMLFormat"),
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
225 Preferences.getEditor("PreviewRestDocutilsHTMLFormat"),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
226 )
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227
5845
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
228 def __setHtml(self, filePath, html, rootPath):
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 Private method to set the HTML to the view and restore the scroll bars
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 positions.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
232
5845
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
233 @param filePath file path of the previewed editor
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
234 @type str
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
235 @param html processed HTML text ready to be shown
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
236 @type str
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
237 @param rootPath path of the web site root
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
238 @type str
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 self.__previewedPath = Utilities.normcasepath(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
241 Utilities.fromNativeSeparators(filePath)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
242 )
4625
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
243 self.__saveScrollBarPositions()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
244 self.previewView.page().loadFinished.connect(self.__restoreScrollBarPositions)
7196
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
245 if not filePath:
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
246 filePath = "/"
8260
2161475d9639 Applied some more code simplifications suggested by the new Simplify checker (Y108: use ternary operator).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8243
diff changeset
247 baseUrl = (
2161475d9639 Applied some more code simplifications suggested by the new Simplify checker (Y108: use ternary operator).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8243
diff changeset
248 QUrl.fromLocalFile(rootPath + "/index.html")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
249 if rootPath
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
250 else QUrl.fromLocalFile(filePath)
8260
2161475d9639 Applied some more code simplifications suggested by the new Simplify checker (Y108: use ternary operator).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8243
diff changeset
251 )
5845
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
252 self.previewView.setHtml(html, baseUrl=baseUrl)
4929
fcab21c80811 Fixed the focus stealing issue with the QtWebEngine based HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
253 if self.__previewedEditor:
fcab21c80811 Fixed the focus stealing issue with the QtWebEngine based HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
254 self.__previewedEditor.setFocus()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
255
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 @pyqtSlot(str)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 def on_previewView_titleChanged(self, title):
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 Private slot to handle a change of the title.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
260
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 @param title new title (string)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 if title:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 self.titleLabel.setText(self.tr("Preview - {0}").format(title))
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 else:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 self.titleLabel.setText(self.tr("Preview"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
267
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 def __saveScrollBarPositions(self):
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 Private method to save scroll bar positions for a previewed editor.
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 """
8318
962bce857696 Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
272 from PyQt6.QtCore import QPoint
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
273
7196
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
274 try:
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
275 pos = self.previewView.scrollPosition()
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
276 except AttributeError:
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
277 pos = self.__execJavaScript(
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
278 "(function() {"
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
279 "var res = {"
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
280 " x: 0,"
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
281 " y: 0,"
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
282 "};"
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
283 "res.x = window.scrollX;"
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
284 "res.y = window.scrollY;"
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
285 "return res;"
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
286 "})()"
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
287 )
8235
78e6d29eb773 Applied some more code simplifications suggested by the new Simplify checker (Y108: use ternary operator) (batch 3).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8227
diff changeset
288 pos = QPoint(0, 0) if pos is None else QPoint(pos["x"], pos["y"])
7196
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
289 self.__scrollBarPositions[self.__previewedPath] = pos
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
290 self.__hScrollBarAtEnd[self.__previewedPath] = False
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
291 self.__vScrollBarAtEnd[self.__previewedPath] = False
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 def __restoreScrollBarPositions(self):
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 Private method to restore scroll bar positions for a previewed editor.
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 """
7196
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
297 if self.__previewedPath not in self.__scrollBarPositions:
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
298 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
299
7196
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
300 pos = self.__scrollBarPositions[self.__previewedPath]
ab0a91b82b37 Removed support for QtWebKit and the old web rowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7192
diff changeset
301 self.previewView.page().runJavaScript(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
302 "window.scrollTo({0}, {1});".format(pos.x(), pos.y())
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
303 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
304
4625
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
305 def __execJavaScript(self, script):
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
306 """
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
307 Private function to execute a JavaScript function Synchroneously.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
308
4625
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
309 @param script JavaScript script source to be executed
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
310 @type str
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
311 @return result of the script
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
312 @rtype depending upon script result
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
313 """
8318
962bce857696 Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
314 from PyQt6.QtCore import QEventLoop
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
315
4625
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
316 loop = QEventLoop()
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
317 resultDict = {"res": None}
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
318
4625
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
319 def resultCallback(res, resDict=resultDict):
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
320 if loop and loop.isRunning():
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
321 resDict["res"] = res
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
322 loop.quit()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
323
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
324 self.previewView.page().runJavaScript(script, resultCallback)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
325
7759
51aa6c6b66f7 Changed calls to exec_() into exec() (remainder of Python2 elimination).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7724
diff changeset
326 loop.exec()
4625
ac72a3d8f89e Improved the QtWebEngine based HTML previewer variant by including JavaScript to save and restore the current scrollbar positions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4615
diff changeset
327 return resultDict["res"]
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330 class PreviewProcessingThread(QThread):
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 Class implementing a thread to process some text into HTML usable by the
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 previewer view.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
334
5845
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
335 @signal htmlReady(str, str, str) emitted with the file name, the processed
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
336 HTML and the web site root path to signal the availability of the
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
337 processed HTML
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
339
5845
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
340 htmlReady = pyqtSignal(str, str, str)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
341
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 def __init__(self, parent=None):
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 Constructor
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
345
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 @param parent reference to the parent object (QObject)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8143
diff changeset
348 super().__init__()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
349
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 self.__lock = threading.Lock()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
351
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
352 def process(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
353 self,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
354 filePath,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
355 language,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
356 text,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
357 ssiEnabled,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
358 rootPath,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
359 useSphinx,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
360 convertNewLineToBreak,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
361 usePyMdownExtensions,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
362 markdownHtmlFormat,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
363 restDocutilsHtmlFormat,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
364 ):
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 """
3591
2f2a4a76dd22 Corrected a bunch of source docu issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3539
diff changeset
366 Public method to convert the given text to HTML.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
367
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
368 @param filePath file path of the text
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
369 @type str
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
370 @param language language of the text
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
371 @type str
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
372 @param text text to be processed
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
373 @type str
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 @param ssiEnabled flag indicating to do some (limited) SSI processing
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
375 @type bool
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
376 @param rootPath root path to be used for SSI processing
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
377 @type str
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
378 @param useSphinx flag indicating to use Sphinx to generate the
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
379 ReST preview
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
380 @type bool
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
381 @param convertNewLineToBreak flag indicating to convert new lines
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
382 to HTML break (Markdown only)
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
383 @type bool
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
384 @param usePyMdownExtensions flag indicating to enable the PyMdown
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
385 extensions, if they are available
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
386 @type bool
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
387 @param markdownHtmlFormat HTML format to be generated by markdown
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
388 @type str
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
389 @param restDocutilsHtmlFormat HTML format to be generated by docutils
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
390 @type str
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 with self.__lock:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 self.__filePath = filePath
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394 self.__language = language
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 self.__text = text
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 self.__ssiEnabled = ssiEnabled
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 self.__rootPath = rootPath
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398 self.__haveData = True
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
399 self.__useSphinx = useSphinx
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
400 self.__convertNewLineToBreak = convertNewLineToBreak
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
401 self.__usePyMdownExtensions = usePyMdownExtensions
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
402 self.__markdownHtmlFormat = markdownHtmlFormat
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
403 self.__restDocutilsHtmlFormat = restDocutilsHtmlFormat
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 if not self.isRunning():
8143
2c730d5fd177 Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7960
diff changeset
405 self.start(QThread.Priority.LowPriority)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
406
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
407 def run(self):
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408 """
3591
2f2a4a76dd22 Corrected a bunch of source docu issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3539
diff changeset
409 Public thread method to convert the stored data.
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 while True:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412 # exits with break
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 with self.__lock:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 filePath = self.__filePath
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415 language = self.__language
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 text = self.__text
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 ssiEnabled = self.__ssiEnabled
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 rootPath = self.__rootPath
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
419 useSphinx = self.__useSphinx
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
420 convertNewLineToBreak = self.__convertNewLineToBreak
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
421 usePyMdownExtensions = self.__usePyMdownExtensions
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
422 markdownHtmlFormat = self.__markdownHtmlFormat
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
423 restDocutilsHtmlFormat = self.__restDocutilsHtmlFormat
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
424
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
425 self.__haveData = False
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
426
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
427 html = self.__getHtml(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
428 language,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
429 text,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
430 ssiEnabled,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
431 filePath,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
432 rootPath,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
433 useSphinx,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
434 convertNewLineToBreak,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
435 usePyMdownExtensions,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
436 markdownHtmlFormat,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
437 restDocutilsHtmlFormat,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
438 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
439
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 with self.__lock:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 if not self.__haveData:
5845
f3d2172d663e Some slight improvements for the HTML previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5837
diff changeset
442 self.htmlReady.emit(filePath, html, rootPath)
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 break
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 # else - next iteration
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
445
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
446 def __getHtml(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
447 self,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
448 language,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
449 text,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
450 ssiEnabled,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
451 filePath,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
452 rootPath,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
453 useSphinx,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
454 convertNewLineToBreak,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
455 usePyMdownExtensions,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
456 markdownHtmlFormat,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
457 restDocutilsHtmlFormat,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
458 ):
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
459 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 Private method to process the given text depending upon the given
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 language.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
462
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
463 @param language language of the text
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
464 @type str
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
465 @param text to be processed
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
466 @type str
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467 @param ssiEnabled flag indicating to do some (limited) SSI processing
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
468 @type bool
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
469 @param filePath file path of the text
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
470 @type str
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
471 @param rootPath root path to be used for SSI processing
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
472 @type str
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
473 @param useSphinx flag indicating to use Sphinx to generate the
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
474 ReST preview
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
475 @type bool
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
476 @param convertNewLineToBreak flag indicating to convert new lines
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
477 to HTML break (Markdown only)
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
478 @type bool
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
479 @param usePyMdownExtensions flag indicating to enable the PyMdown
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
480 extensions, if they are available
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
481 @type bool
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
482 @param markdownHtmlFormat HTML format to be generated by markdown
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
483 @type str
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
484 @param restDocutilsHtmlFormat HTML format to be generated by docutils
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
485 @type str
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
486 @return processed HTML text
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
487 @rtype str
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
488 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489 if language == "HTML":
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490 if ssiEnabled:
5846
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
491 html = self.__processSSI(text, filePath, rootPath)
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
492 else:
5846
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
493 html = text
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
494 return self.__processRootPath(html, rootPath)
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 elif language == "Markdown":
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
496 return self.__convertMarkdown(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
497 text, convertNewLineToBreak, usePyMdownExtensions, markdownHtmlFormat
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
498 )
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499 elif language == "ReST":
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
500 return self.__convertReST(text, useSphinx, restDocutilsHtmlFormat)
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
501 else:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
502 return self.tr("<p>No preview available for this type of file.</p>")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
503
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
504 def __processSSI(self, txt, filename, root):
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
506 Private method to process the given text for SSI statements.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
507
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 Note: Only a limited subset of SSI statements are supported.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
509
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510 @param txt text to be processed (string)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511 @param filename name of the file associated with the given text
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512 (string)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513 @param root directory of the document root (string)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
514 @return processed HTML (string)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
515 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 if not filename:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517 return txt
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
518
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519 # SSI include
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520 incRe = re.compile(
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521 r"""<!--#include[ \t]+(virtual|file)=[\"']([^\"']+)[\"']\s*-->""",
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
522 re.IGNORECASE,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
523 )
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524 baseDir = os.path.dirname(os.path.abspath(filename))
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525 docRoot = root if root != "" else baseDir
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526 while True:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
527 incMatch = incRe.search(txt)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
528 if incMatch is None:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
529 break
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
530
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
531 if incMatch.group(1) == "virtual":
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
532 incFile = Utilities.normjoinpath(docRoot, incMatch.group(2))
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
533 elif incMatch.group(1) == "file":
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
534 incFile = Utilities.normjoinpath(baseDir, incMatch.group(2))
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
535 else:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
536 incFile = ""
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537 if os.path.exists(incFile):
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
538 try:
7785
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
539 with open(incFile, "r") as f:
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
540 incTxt = f.read()
7836
2f0d208b8137 Changed code to not use the OSError aliases (IOError, EnvironmentError, socket.error and select.error) anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7785
diff changeset
541 except OSError:
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
542 # remove SSI include
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
543 incTxt = ""
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
544 else:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
545 # remove SSI include
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
546 incTxt = ""
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
547 txt = txt[: incMatch.start(0)] + incTxt + txt[incMatch.end(0) :]
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
548
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
549 return txt
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
550
5846
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
551 def __processRootPath(self, txt, root):
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
552 """
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
553 Private method to adjust absolute references to the given root path.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
554
5846
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
555 @param txt text to be processed
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
556 @type str
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
557 @param root directory of the document root
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
558 @type str
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
559 @return processed HTML
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
560 @rtype str
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
561 """
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
562 if not root:
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
563 return txt
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
564
5846
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
565 root = Utilities.fromNativeSeparators(root)
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
566 if not root.endswith("/"):
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
567 root += "/"
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
568 rootLen = len(root)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
569
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
570 refRe = re.compile(r"""(href|src)=[\\"']/([^\\"']+)[\\"']""", re.IGNORECASE)
5846
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
571 pos = 0
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
572 while True:
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
573 refMatch = refRe.search(txt, pos)
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
574 if refMatch is None:
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
575 break
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
576
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
577 txt = (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
578 txt[: refMatch.start(0)]
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
579 + refMatch.group(1)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
580 + '="'
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
581 + root
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
582 + refMatch.group(2)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
583 + '"'
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
584 + txt[refMatch.end(0) :]
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
585 )
5846
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
586 pos = refMatch.end(0) + rootLen
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
587
5846
b3cc692e3bfe Some additional improvements for the HTML previewer to be able to cope with absolute URLs. These get rewritten with respect to the project directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5845
diff changeset
588 return txt
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
589
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
590 def __convertReST(self, text, useSphinx, restDocutilsHtmlFormat):
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
591 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
592 Private method to convert ReST text into HTML.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
593
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
594 @param text text to be processed (string)
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
595 @param useSphinx flag indicating to use Sphinx to generate the
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
596 ReST preview (boolean)
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
597 @param restDocutilsHtmlFormat HTML format to be generated by docutils
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
598 (string)
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
599 @return processed HTML (string)
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
600 """
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
601 if useSphinx:
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
602 return self.__convertReSTSphinx(text)
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
603 else:
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
604 return self.__convertReSTDocutils(text, restDocutilsHtmlFormat)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
605
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
606 def __convertReSTSphinx(self, text):
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
607 """
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
608 Private method to convert ReST text into HTML using 'sphinx'.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
609
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
610 @param text text to be processed (string)
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
611 @return processed HTML (string)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
612 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
613 try:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
614 from sphinx.application import Sphinx # __IGNORE_EXCEPTION__
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
615 except ImportError:
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
616 return self.tr(
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
617 """<p>ReStructuredText preview requires the"""
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
618 """ <b>sphinx</b> package.<br/>Install it with"""
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
619 """ your package manager,'pip install Sphinx' or see"""
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
620 """ <a href="http://pypi.python.org/pypi/Sphinx">"""
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
621 """this page.</a></p>"""
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
622 """<p>Alternatively you may disable Sphinx usage"""
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
623 """ on the Editor, Filehandling configuration page.</p>"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
624 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
625
7675
7d23e502fe85 PreviewerHTML: fixed an issue raised by Sphinx when converting .rst files to .html.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7524
diff changeset
626 srcTempDir = tempfile.mkdtemp(prefix="eric-rest-src-")
7d23e502fe85 PreviewerHTML: fixed an issue raised by Sphinx when converting .rst files to .html.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7524
diff changeset
627 outTempDir = tempfile.mkdtemp(prefix="eric-rest-out-")
7d23e502fe85 PreviewerHTML: fixed an issue raised by Sphinx when converting .rst files to .html.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7524
diff changeset
628 doctreeTempDir = tempfile.mkdtemp(prefix="eric-rest-doctree-")
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
629 try:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
630 filename = "sphinx_preview"
7675
7d23e502fe85 PreviewerHTML: fixed an issue raised by Sphinx when converting .rst files to .html.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7524
diff changeset
631 basePath = os.path.join(srcTempDir, filename)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
632 with open(basePath + ".rst", "w", encoding="utf-8") as fh:
7785
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
633 fh.write(text)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
634
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
635 overrides = {
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
636 "html_add_permalinks": False,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
637 "html_copy_source": False,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
638 "html_title": "Sphinx preview",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
639 "html_use_index": False,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
640 "html_use_modindex": False,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
641 "html_use_smartypants": True,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
642 "master_doc": filename,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
643 }
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
644 app = Sphinx(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
645 srcdir=srcTempDir,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
646 confdir=None,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
647 outdir=outTempDir,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
648 doctreedir=doctreeTempDir,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
649 buildername="html",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
650 confoverrides=overrides,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
651 status=None,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
652 warning=io.StringIO(),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
653 )
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
654 app.build(force_all=True, filenames=None)
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
655
7675
7d23e502fe85 PreviewerHTML: fixed an issue raised by Sphinx when converting .rst files to .html.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7524
diff changeset
656 basePath = os.path.join(outTempDir, filename)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
657 with open(basePath + ".html", "r", encoding="utf-8") as fh:
7785
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
658 html = fh.read()
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
659 finally:
7675
7d23e502fe85 PreviewerHTML: fixed an issue raised by Sphinx when converting .rst files to .html.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7524
diff changeset
660 shutil.rmtree(srcTempDir)
7d23e502fe85 PreviewerHTML: fixed an issue raised by Sphinx when converting .rst files to .html.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7524
diff changeset
661 shutil.rmtree(outTempDir)
7d23e502fe85 PreviewerHTML: fixed an issue raised by Sphinx when converting .rst files to .html.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7524
diff changeset
662 shutil.rmtree(doctreeTempDir)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
663
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
664 return html
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
665
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
666 def __convertReSTDocutils(self, text, htmlFormat):
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
667 """
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
668 Private method to convert ReST text into HTML using 'docutils'.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
669
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
670 @param text text to be processed (string)
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
671 @param htmlFormat HTML format to be generated (string)
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
672 @return processed HTML (string)
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
673 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
674 if "sphinx" in sys.modules:
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
675 # Make sure any Sphinx polution of docutils has been removed.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
676 unloadKeys = [
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
677 k for k in sys.modules.keys() if k.startswith(("docutils", "sphinx"))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
678 ]
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
679 for key in unloadKeys:
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
680 sys.modules.pop(key)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
681
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
682 try:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
683 import docutils.core # __IGNORE_EXCEPTION__
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
684 import docutils.utils # __IGNORE_EXCEPTION__
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
685 except ImportError:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
686 return self.tr(
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
687 """<p>ReStructuredText preview requires the"""
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
688 """ <b>python-docutils</b> package.<br/>Install it with"""
3979
307b09aae43b Added capability to use 'Sphinx' to preview ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3656
diff changeset
689 """ your package manager, 'pip install docutils' or see"""
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
690 """ <a href="http://pypi.python.org/pypi/docutils">"""
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
691 """this page.</a></p>"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
692 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
693
3981
5cd283505cfa A little change to the ReST previewer to discard errors generated by the docutils converter and sent to sys.stderr.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3979
diff changeset
694 # redirect sys.stderr because we are not interested in it here
5cd283505cfa A little change to the ReST previewer to discard errors generated by the docutils converter and sent to sys.stderr.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3979
diff changeset
695 origStderr = sys.stderr
5cd283505cfa A little change to the ReST previewer to discard errors generated by the docutils converter and sent to sys.stderr.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3979
diff changeset
696 sys.stderr = io.StringIO()
6794
10c368c9c02b PreviewerHTML: added a error handler for docutils SystemMessage exceptions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
697 try:
10c368c9c02b PreviewerHTML: added a error handler for docutils SystemMessage exceptions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
698 html = docutils.core.publish_string(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
699 text, writer_name=htmlFormat.lower()
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
700 ).decode("utf-8")
6794
10c368c9c02b PreviewerHTML: added a error handler for docutils SystemMessage exceptions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
701 except docutils.utils.SystemMessage as err:
10c368c9c02b PreviewerHTML: added a error handler for docutils SystemMessage exceptions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
702 errStr = str(err).split(":")[-1].replace("\n", "<br/>")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
703 return self.tr("""<p>Docutils returned an error:</p><p>{0}</p>""").format(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
704 errStr
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
705 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
706
3981
5cd283505cfa A little change to the ReST previewer to discard errors generated by the docutils converter and sent to sys.stderr.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3979
diff changeset
707 sys.stderr = origStderr
5cd283505cfa A little change to the ReST previewer to discard errors generated by the docutils converter and sent to sys.stderr.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3979
diff changeset
708 return html
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
709
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
710 def __convertMarkdown(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
711 self, text, convertNewLineToBreak, usePyMdownExtensions, htmlFormat
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
712 ):
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
713 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
714 Private method to convert Markdown text into HTML.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
715
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
716 @param text text to be processed
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
717 @type str
5837
9ef6a28f1694 - Exporter
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5801
diff changeset
718 @param convertNewLineToBreak flag indicating to convert new lines
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
719 to HTML break (Markdown only)
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
720 @type bool
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
721 @param usePyMdownExtensions flag indicating to enable the PyMdown
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
722 extensions, if they are available
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
723 @type bool
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
724 @param htmlFormat HTML format to be generated by markdown
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
725 @type str
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
726 @return processed HTML
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
727 @rtype str
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
728 """
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
729 try:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
730 import markdown # __IGNORE_EXCEPTION__
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
731 except ImportError:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
732 return self.tr(
5912
b6643d36dddd Added the rich text view to the documentation viewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5905
diff changeset
733 """<p>Markdown preview requires the <b>Markdown</b> """
5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
734 """package.<br/>Install it with your package manager,"""
5912
b6643d36dddd Added the rich text view to the documentation viewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5905
diff changeset
735 """ 'pip install Markdown' or see """
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
736 """<a href="http://pythonhosted.org/Markdown/install.html">"""
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
737 """installation instructions.</a></p>"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
738 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
739
7314
c32c24345ca7 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7309
diff changeset
740 from . import PreviewerHTMLStyles
c32c24345ca7 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7309
diff changeset
741 from . import MarkdownExtensions
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
742
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
743 extensions = []
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
744
7314
c32c24345ca7 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7309
diff changeset
745 mermaidNeeded = False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
746 if Preferences.getEditor(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
747 "PreviewMarkdownMermaid"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
748 ) and MarkdownExtensions.MermaidRegexFullText.search(text):
8227
349308e84eeb Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 2).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
749 extensions.append(MarkdownExtensions.MermaidExtension())
349308e84eeb Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 2).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
750 mermaidNeeded = True
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
751
7309
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
752 if convertNewLineToBreak:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
753 extensions.append("nl2br")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
754
7309
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
755 pyMdown = False
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
756 if usePyMdownExtensions:
8243
cc717c2ae956 Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 2).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8235
diff changeset
757 with contextlib.suppress(ImportError):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
758 import pymdownx # __IGNORE_EXCEPTION__ __IGNORE_WARNING__
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
759
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
760 # PyPI package is 'pymdown-extensions'
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
761
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
762 extensions.extend(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
763 [
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
764 "toc",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
765 "pymdownx.extra",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
766 "pymdownx.caret",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
767 "pymdownx.emoji",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
768 "pymdownx.mark",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
769 "pymdownx.tilde",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
770 "pymdownx.keys",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
771 "pymdownx.tasklist",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
772 "pymdownx.smartsymbols",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
773 ]
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
774 )
7309
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
775 pyMdown = True
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
776
7309
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
777 if not pyMdown:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
778 extensions.extend(["extra", "toc"])
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
779
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
780 # version 2.0 supports only extension names, not instances
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
781 if markdown.version_info[0] > 2 or (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
782 markdown.version_info[0] == 2 and markdown.version_info[1] > 0
7301
6df711503ec0 PreviewerHTML: enhanced the Markdown previewer by using the PyMdown extensions, if they are available and have been enabled.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
783 ):
7314
c32c24345ca7 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7309
diff changeset
784 extensions.append(MarkdownExtensions.SimplePatternExtension())
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
785
7304
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
786 if Preferences.getEditor("PreviewMarkdownMathJax"):
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
787 mathjax = (
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
788 "<script type='text/javascript' id='MathJax-script' async"
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
789 " src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/"
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
790 "tex-chtml.js'>\n"
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
791 "</script>\n"
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
792 )
7309
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
793 # prepare text for mathjax
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
794 text = (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
795 text.replace(r"\(", r"\\(")
7309
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
796 .replace(r"\)", r"\\)")
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
797 .replace(r"\[", r"\\[")
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
798 .replace(r"\]", r"\\]")
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
799 )
7304
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
800 else:
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
801 mathjax = ""
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
802
7314
c32c24345ca7 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7309
diff changeset
803 if mermaidNeeded:
7309
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
804 mermaid = (
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
805 "<script type='text/javascript' id='Mermaid-script'"
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
806 " src='https://unpkg.com/mermaid@8/dist/mermaid.min.js'>\n"
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
807 "</script>\n"
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
808 )
8356
68ec9c3d4de5 Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8318
diff changeset
809 if ericApp().usesDarkPalette():
7524
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
810 mermaid_initialize = (
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
811 "<script>mermaid.initialize({"
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
812 "theme: 'dark', "
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
813 "startOnLoad:true"
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
814 "});</script>"
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
815 )
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
816 else:
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
817 mermaid_initialize = (
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
818 "<script>mermaid.initialize({"
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
819 "theme: 'default', "
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
820 "startOnLoad:true"
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
821 "});</script>"
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
822 )
7309
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
823 else:
5a434813eef3 PreviewerHTML, ExporterHTML:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7304
diff changeset
824 mermaid = ""
7524
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
825 mermaid_initialize = ""
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
826
7304
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
827 htmlFormat = Preferences.getEditor("PreviewMarkdownHTMLFormat").lower()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
828 body = markdown.markdown(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
829 text, extensions=extensions, output_format=htmlFormat.lower()
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
830 )
8260
2161475d9639 Applied some more code simplifications suggested by the new Simplify checker (Y108: use ternary operator).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8243
diff changeset
831 style = (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
832 (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
833 PreviewerHTMLStyles.css_markdown_dark
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
834 + PreviewerHTMLStyles.css_pygments_dark
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
835 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
836 if ericApp().usesDarkPalette()
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
837 else (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
838 PreviewerHTMLStyles.css_markdown_light
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
839 + PreviewerHTMLStyles.css_pygments_light
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
840 )
8260
2161475d9639 Applied some more code simplifications suggested by the new Simplify checker (Y108: use ternary operator).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8243
diff changeset
841 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
842
7304
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
843 if htmlFormat == "xhtml1":
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
844 head = (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
845 """<!DOCTYPE html PUBLIC "-//W3C//DTD"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
846 """ XHTML 1.0 Transitional//EN"\n"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
847 """ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
848 """.dtd">\n"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
849 """<html xmlns="http://www.w3.org/1999/xhtml">\n"""
7304
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
850 )
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
851 elif htmlFormat == "html5":
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
852 head = """<!DOCTYPE html>\n""" """<html lang="EN">\n"""
7304
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
853 else:
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
854 head = '<html lang="EN">\n'
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
855 head += """<head>\n"""
7304
b072a364dd8d PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7301
diff changeset
856 head += (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
857 """<meta name="Generator" content="eric" />\n"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
858 """<meta http-equiv="Content-Type" """
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
859 """content="text/html; charset=utf-8" />\n"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
860 """{0}"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
861 """{1}"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
862 """<style type="text/css">"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
863 """{2}"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
864 """</style>\n"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
865 """</head>\n"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
866 """<body>\n"""
7524
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
867 ).format(mathjax, mermaid, style)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
868
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
869 foot = """\n</body>\n</html>\n"""
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
870
7524
282680dae446 HTML Preview and Exporter: added support for dark color schemes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
871 return head + body + mermaid_initialize + foot

eric ide

mercurial