UI/Previewer.py

Sat, 14 Dec 2013 23:44:25 +0100

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Sat, 14 Dec 2013 23:44:25 +0100
branch
Py2 comp.
changeset 3145
a9de05d4a22f
parent 3060
5883ce99ee12
child 3161
06f57a834adf
permissions
-rw-r--r--

# __IGNORE_WARNING__ added/ removed.

2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de>
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a previewer widget for HTML, Markdown and ReST files.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
3145
a9de05d4a22f # __IGNORE_WARNING__ added/ removed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3060
diff changeset
10 from __future__ import unicode_literals
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2500
diff changeset
11
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import os
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 import threading
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 import re
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 from PyQt4.QtCore import pyqtSlot, pyqtSignal, Qt, QTimer, QSize, QUrl, QThread
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 from PyQt4.QtGui import QWidget
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 from PyQt4.QtWebKit import QWebPage
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 from E5Gui.E5Application import e5App
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 from .Ui_Previewer import Ui_Previewer
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 import Preferences
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 import Utilities
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 class Previewer(QWidget, Ui_Previewer):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 Class implementing a previewer widget for HTML, Markdown and ReST files.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 def __init__(self, viewmanager, splitter, parent=None):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 Constructor
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 @param viewmanager reference to the viewmanager object (ViewManager)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 @param splitter reference to the embedding splitter (QSplitter)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 @param parent reference to the parent widget (QWidget)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 """
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2500
diff changeset
40 super(Previewer, self).__init__(parent)
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 self.setupUi(self)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 self.__vm = viewmanager
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 self.__splitter = splitter
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 self.__firstShow = True
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
48 self.previewView.page().setLinkDelegationPolicy(
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
49 QWebPage.DelegateAllLinks)
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 # Don't update too often because the UI might become sluggish
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 self.__typingTimer = QTimer()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 self.__typingTimer.setInterval(500) # 500ms
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 self.__typingTimer.timeout.connect(self.__runProcessingThread)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 self.__scrollBarPositions = {}
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 self.__vScrollBarAtEnd = {}
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 self.__hScrollBarAtEnd = {}
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 self.__processingThread = PreviewProcessingThread()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 self.__processingThread.htmlReady.connect(self.__setHtml)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 self.__previewedPath = None
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 self.__vm.editorChangedEd.connect(self.__editorChanged)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 self.__vm.editorLanguageChanged.connect(self.__editorLanguageChanged)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 self.__vm.editorTextChanged.connect(self.__editorTextChanged)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 self.__vm.previewStateChanged.connect(self.__previewStateChanged)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 self.__splitter.splitterMoved.connect(self.__splitterMoved)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 self.hide()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 def show(self):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 Public method to show the preview widget.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 """
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2500
diff changeset
79 super(Previewer, self).show()
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 if self.__firstShow:
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
81 self.__splitter.restoreState(
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
82 Preferences.getUI("PreviewSplitterState"))
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
83 self.jsCheckBox.setChecked(
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
84 Preferences.getUI("ShowFilePreviewJS"))
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
85 self.ssiCheckBox.setChecked(
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
86 Preferences.getUI("ShowFilePreviewSSI"))
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 self.__firstShow = False
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 self.__typingTimer.start()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 def hide(self):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 Public method to hide the preview widget.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 """
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2500
diff changeset
94 super(Previewer, self).hide()
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 self.__typingTimer.stop()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 def shutdown(self):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 Public method to perform shutdown actions.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 self.__typingTimer.stop()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 self.__processingThread.wait()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 def __splitterMoved(self):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 Private slot to handle the movement of the embedding splitter's handle.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 state = self.__splitter.saveState()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 Preferences.setUI("PreviewSplitterState", state)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 @pyqtSlot(bool)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 def on_jsCheckBox_clicked(self, checked):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 Private slot to enable/disable JavaScript.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 @param checked state of the checkbox (boolean)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 Preferences.setUI("ShowFilePreviewJS", checked)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 self.__setJavaScriptEnabled(checked)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 def __setJavaScriptEnabled(self, enable):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 Private method to enable/disable JavaScript.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 @param enable flag indicating the enable state (boolean)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 self.jsCheckBox.setChecked(enable)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 settings = self.previewView.settings()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 settings.setAttribute(settings.JavascriptEnabled, enable)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 self.__runProcessingThread()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 @pyqtSlot(bool)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 def on_ssiCheckBox_clicked(self, checked):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 Private slot to enable/disable SSI.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 @param checked state of the checkbox (boolean)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 Preferences.setUI("ShowFilePreviewSSI", checked)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 self.__runProcessingThread()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 def __editorChanged(self, editor):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 Private slot to handle a change of the current editor.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 @param editor reference to the editor (Editor)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 if editor is None:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 self.hide()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 return
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
154 if Preferences.getUI("ShowFilePreview") and \
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
155 self.__isPreviewable(editor):
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 self.show()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 self.__runProcessingThread()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 else:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 self.hide()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 def __editorLanguageChanged(self, editor):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 Private slot to handle a change of the current editor's language.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 @param editor reference to the editor (Editor)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 self.__editorChanged(editor)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 def __editorTextChanged(self, editor):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 Private slot to handle changes of an editor's text.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 @param editor reference to the editor (Editor)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 if self.isVisible():
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 self.__typingTimer.stop()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 self.__typingTimer.start()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 def __previewStateChanged(self, on):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 Public slot to toggle the display of the preview.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 @param on flag indicating to show a preview (boolean)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 editor = self.__vm.activeWindow()
2720
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
186 if on and editor and self.__isPreviewable(editor):
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 self.show()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 else:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 self.hide()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 def __isPreviewable(self, editor):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 """
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
193 Private method to check, if a preview can be shown for the given
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
194 editor.
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 @param editor reference to an editor (Editor)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 @return flag indicating if a preview can be shown (boolean)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 """
2720
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
199 if editor:
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
200 if editor.getFileName() is not None:
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
201 extension = os.path.normcase(
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
202 os.path.splitext(editor.getFileName())[1][1:])
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
203 return extension in \
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
204 Preferences.getEditor("PreviewHtmlFileNameExtensions") + \
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
205 Preferences.getEditor(
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
206 "PreviewMarkdownFileNameExtensions") + \
2720
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
207 Preferences.getEditor("PreviewRestFileNameExtensions")
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
208 elif editor.getLanguage() == "HTML":
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
209 return True
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
210
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
211 return False
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 def __runProcessingThread(self):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 Private slot to schedule the processing of the current editor's text.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 self.__typingTimer.stop()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 editor = self.__vm.activeWindow()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 if editor is not None:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 fn = editor.getFileName()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 if fn:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 extension = os.path.normcase(os.path.splitext(fn)[1][1:])
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 else:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 extension = ""
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 if extension in \
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 Preferences.getEditor("PreviewHtmlFileNameExtensions") or \
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 editor.getLanguage() == "HTML":
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 language = "HTML"
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 elif extension in \
3039
8dd0165d805d Fixed a bunch of indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3034
diff changeset
232 Preferences.getEditor("PreviewMarkdownFileNameExtensions"):
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 language = "Markdown"
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 elif extension in \
3039
8dd0165d805d Fixed a bunch of indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3034
diff changeset
235 Preferences.getEditor("PreviewRestFileNameExtensions"):
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 language = "ReST"
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 else:
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
238 self.__setHtml(fn, self.trUtf8(
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
239 "<p>No preview available for this type of file.</p>"))
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 return
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 if fn:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 project = e5App().getObject("Project")
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 if project.isProjectFile(fn):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 rootPath = project.getProjectPath()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 else:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 rootPath = os.path.dirname(os.path.abspath(fn))
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 else:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 rootPath = ""
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 self.__processingThread.process(
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 fn, language, editor.text(),
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 self.ssiCheckBox.isChecked(), rootPath)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 def __setHtml(self, filePath, html):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 """
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
257 Private method to set the HTML to the view and restore the scroll bars
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
258 positions.
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 @param filePath file path of the previewed editor (string)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 @param html processed HTML text ready to be shown (string)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 self.__saveScrollBarPositions()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 self.__previewedPath = Utilities.normcasepath(
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 Utilities.fromNativeSeparators(filePath))
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 self.previewView.page().mainFrame().contentsSizeChanged.connect(
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 self.__restoreScrollBarPositions)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 self.previewView.setHtml(html, baseUrl=QUrl.fromLocalFile(filePath))
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 @pyqtSlot(str)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 def on_previewView_titleChanged(self, title):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 Private slot to handle a change of the title.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 @param title new title (string)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 if title:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 self.titleLabel.setText(self.trUtf8("Preview - {0}").format(title))
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 else:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 self.titleLabel.setText(self.trUtf8("Preview"))
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 def __saveScrollBarPositions(self):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 Private method to save scroll bar positions for a previewed editor.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 frame = self.previewView.page().mainFrame()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 if frame.contentsSize() == QSize(0, 0):
2537
0ff250733d45 Fixed a few PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2500
diff changeset
288 return # no valid data, nothing to save
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 pos = frame.scrollPosition()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 self.__scrollBarPositions[self.__previewedPath] = pos
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 self.__hScrollBarAtEnd[self.__previewedPath] = \
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 frame.scrollBarMaximum(Qt.Horizontal) == pos.x()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 self.__vScrollBarAtEnd[self.__previewedPath] = \
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 frame.scrollBarMaximum(Qt.Vertical) == pos.y()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 def __restoreScrollBarPositions(self):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 Private method to restore scroll bar positions for a previewed editor.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 try:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 self.previewView.page().mainFrame().contentsSizeChanged.disconnect(
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 self.__restoreScrollBarPositions)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 except TypeError:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 # not connected, simply ignore it
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 pass
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 if self.__previewedPath not in self.__scrollBarPositions:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 return
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 frame = self.previewView.page().mainFrame()
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
312 frame.setScrollPosition(
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
313 self.__scrollBarPositions[self.__previewedPath])
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 if self.__hScrollBarAtEnd[self.__previewedPath]:
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
316 frame.setScrollBarValue(
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
317 Qt.Horizontal, frame.scrollBarMaximum(Qt.Horizontal))
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 if self.__vScrollBarAtEnd[self.__previewedPath]:
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
320 frame.setScrollBarValue(
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
321 Qt.Vertical, frame.scrollBarMaximum(Qt.Vertical))
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
322
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 @pyqtSlot(QUrl)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 def on_previewView_linkClicked(self, url):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 Private slot handling the clicking of a link.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 @param url url of the clicked link (QUrl)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330 e5App().getObject("UserInterface").launchHelpViewer(url.toString())
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 class PreviewProcessingThread(QThread):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334 """
2537
0ff250733d45 Fixed a few PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2500
diff changeset
335 Class implementing a thread to process some text into HTML usable by the
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336 previewer view.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
338 @signal htmlReady(str,str) emitted with the file name and processed HTML
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
339 to signal the availability of the processed HTML
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 htmlReady = pyqtSignal(str, str)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 def __init__(self, parent=None):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 Constructor
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 @param parent reference to the parent object (QObject)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 """
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2500
diff changeset
349 super(PreviewProcessingThread, self).__init__()
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 self.__lock = threading.Lock()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 def process(self, filePath, language, text, ssiEnabled, rootPath):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 Convert the given text to HTML.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 @param filePath file path of the text (string)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 @param language language of the text (string)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 @param text text to be processed (string)
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
360 @param ssiEnabled flag indicating to do some (limited) SSI processing
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
361 (boolean)
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 @param rootPath root path to be used for SSI processing (str)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 with self.__lock:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 self.__filePath = filePath
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 self.__language = language
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367 self.__text = text
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 self.__ssiEnabled = ssiEnabled
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 self.__rootPath = rootPath
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 self.__haveData = True
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 if not self.isRunning():
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 self.start(QThread.LowPriority)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 def run(self):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 Thread function to convert the stored data.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 while True:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 # exits with break
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 with self.__lock:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 filePath = self.__filePath
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 language = self.__language
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383 text = self.__text
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 ssiEnabled = self.__ssiEnabled
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 rootPath = self.__rootPath
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
386 self.__haveData = False
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
388 html = self.__getHtml(language, text, ssiEnabled, filePath,
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
389 rootPath)
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 with self.__lock:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 if not self.__haveData:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 self.htmlReady.emit(filePath, html)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394 break
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 # else - next iteration
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 def __getHtml(self, language, text, ssiEnabled, filePath, rootPath):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398 """
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2720
diff changeset
399 Private method to process the given text depending upon the given
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2720
diff changeset
400 language.
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 @param language language of the text (string)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403 @param text to be processed (string)
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2720
diff changeset
404 @param ssiEnabled flag indicating to do some (limited) SSI processing
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2720
diff changeset
405 (boolean)
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406 @param filePath file path of the text (string)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
407 @param rootPath root path to be used for SSI processing (str)
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2720
diff changeset
408 @return processed HTML text (string)
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 if language == "HTML":
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 if ssiEnabled:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412 return self.__processSSI(text, filePath, rootPath)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 else:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 return text
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415 elif language == "Markdown":
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 return self.__convertMarkdown(text)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 elif language == "ReST":
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 return self.__convertReST(text)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 else:
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
420 return self.trUtf8(
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
421 "<p>No preview available for this type of file.</p>")
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423 def __processSSI(self, txt, filename, root):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 Private method to process the given text for SSI statements.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 Note: Only a limited subset of SSI statements are supported.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 @param txt text to be processed (string)
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
430 @param filename name of the file associated with the given text
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
431 (string)
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 @param root directory of the document root (string)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 @return processed HTML (string)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435 if not filename:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 return txt
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 # SSI include
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 incRe = re.compile(
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 r"""<!--#include[ \t]+(virtual|file)=[\"']([^\"']+)[\"']\s*-->""",
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 re.IGNORECASE)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442 baseDir = os.path.dirname(os.path.abspath(filename))
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 docRoot = root if root != "" else baseDir
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 while True:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
445 incMatch = incRe.search(txt)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
446 if incMatch is None:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
447 break
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 if incMatch.group(1) == "virtual":
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 incFile = Utilities.normjoinpath(docRoot, incMatch.group(2))
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 elif incMatch.group(1) == "file":
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452 incFile = Utilities.normjoinpath(baseDir, incMatch.group(2))
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453 else:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454 incFile = ""
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 if os.path.exists(incFile):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 try:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
457 f = open(incFile, "r")
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
458 incTxt = f.read()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
459 f.close()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 except (IOError, OSError):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 # remove SSI include
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 incTxt = ""
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463 else:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464 # remove SSI include
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 incTxt = ""
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466 txt = txt[:incMatch.start(0)] + incTxt + txt[incMatch.end(0):]
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468 return txt
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470 def __convertReST(self, text):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
471 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
472 Private method to convert ReST text into HTML.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 @param text text to be processed (string)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475 @return processed HTML (string)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 try:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
478 import docutils.core # __IGNORE_EXCEPTION__ __IGNORE_WARNING__
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
479 except ImportError:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
480 return self.trUtf8(
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
481 """<p>ReStructuredText preview requires the"""
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
482 """ <b>python-docutils</b> package.<br/>Install it with"""
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
483 """ your package manager or see"""
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
484 """ <a href="http://pypi.python.org/pypi/docutils">"""
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
485 """this page.</a></p>""")
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
486
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
487 return docutils.core.publish_string(text, writer_name='html')\
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
488 .decode("utf-8")
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490 def __convertMarkdown(self, text):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
492 Private method to convert Markdown text into HTML.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
493
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
494 @param text text to be processed (string)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 @return processed HTML (string)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
496 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497 try:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498 import markdown # __IGNORE_EXCEPTION__ __IGNORE_WARNING__
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499 except ImportError:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
500 return self.trUtf8(
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
501 """<p>Markdown preview requires the <b>python-markdown</b> """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
502 """package.<br/>Install it with your package manager or see """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
503 """<a href="http://pythonhosted.org/Markdown/install.html">"""
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
504 """installation instructions.</a></p>""")
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
506 try:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
507 import mdx_mathjax # __IGNORE_EXCEPTION__ __IGNORE_WARNING__
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 except ImportError:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 #mathjax doesn't require import statement if installed as extension
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510 pass
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511
2500
a06292298f9a Extended the previewer for Markdown by enabling the 'extra' extension.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2424
diff changeset
512 extensions = ['fenced_code', 'nl2br', 'extra']
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
514 # version 2.0 supports only extension names, not instances
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
515 if markdown.version_info[0] > 2 or \
3034
7ce719013078 Fixed various coding style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3012
diff changeset
516 (markdown.version_info[0] == 2 and
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
517 markdown.version_info[1] > 0):
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 class _StrikeThroughExtension(markdown.Extension):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520 Class is placed here, because it depends on imported markdown,
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521 and markdown import is lazy.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
522
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
523 (see http://achinghead.com/
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
524 python-markdown-adding-insert-delete.html this page for
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
525 details)
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
527 DEL_RE = r'(~~)(.*?)~~'
2537
0ff250733d45 Fixed a few PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2500
diff changeset
528
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
529 def extendMarkdown(self, md, md_globals):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
530 # Create the del pattern
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
531 del_tag = markdown.inlinepatterns.SimpleTagPattern(
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
532 self.DEL_RE, 'del')
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
533 # Insert del pattern into markdown parser
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
534 md.inlinePatterns.add('del', del_tag, '>not_strong')
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
535
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
536 extensions.append(_StrikeThroughExtension())
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
538 try:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
539 return markdown.markdown(text, extensions + ['mathjax'])
2537
0ff250733d45 Fixed a few PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2500
diff changeset
540 except (ImportError, ValueError):
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
541 # markdown raises ValueError or ImportError, depends on version
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
542 # It is not clear, how to distinguish missing mathjax from other
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
543 # errors. So keep going without mathjax.
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
544 return markdown.markdown(text, extensions)

eric ide

mercurial