-
Notifications
You must be signed in to change notification settings - Fork 3
/
mdviewer.py
405 lines (333 loc) · 14.9 KB
/
mdviewer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python
# coding: utf-8
import sys, os, io, shutil, yaml
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebKit, QtWebKitWidgets, QtPrintSupport
from PyQt5.QtCore import *
from PyQt5.QtGui import QDesktopServices, QIcon, QKeySequence
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QFileDialog
from PyQt5.QtWidgets import QToolBar, QCheckBox, QPushButton, QLineEdit, QAction, QActionGroup, QShortcut
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtWebKitWidgets import QWebPage, QWebView
from PyQt5.QtPrintSupport import QPrintPreviewDialog
VERSION = "0.4"
app_dir = os.path.dirname(os.path.realpath(__file__))
css_dir = os.path.join(app_dir, "stylesheets")
class App(QMainWindow):
@property
def QSETTINGS(self):
return QSettings(QSettings.IniFormat, QSettings.UserScope, "mdviewer", "session")
def __init__(self, parent=None, filename=""):
QMainWindow.__init__(self, parent)
self.filename = filename or os.path.join(app_dir, u"README.md")
# Set environment variables
self.set_env()
# Configure Preview window
self.set_window_title()
self.resize(self.QSETTINGS.value("size", QSize(800,400)))
self.move(self.QSETTINGS.value("pos", QPoint(0,0)))
# Activate WebView
self.web_view = QWebView()
self.setCentralWidget(self.web_view)
self.scroll_pos = {}
# Configure and start file watcher thread
self.thread1 = WatcherThread(self.filename)
self.thread1.update.connect(self.update)
self.watcher = QFileSystemWatcher([self.filename])
self.watcher.fileChanged.connect(self.thread1.run)
self.thread1.run()
self.web_view.loadFinished.connect(self.after_update)
# Get style sheet
self.stylesheet = self.QSETTINGS.value("stylesheet", "default.css")
# Set GUI menus and toolbars
self.set_menus()
self.set_search_bar()
def set_env(self):
path, name = os.path.split(os.path.abspath(self.filename))
ext = os.path.splitext(name)[-1].lower()
os.environ["MDVIEWER_EXT"] = ext[1:]
os.environ["MDVIEWER_FILE"] = name
os.environ["MDVIEWER_ORIGIN"] = path
def set_window_title(self):
_path, name = os.path.split(os.path.abspath(self.filename))
self.setWindowTitle(u"%s – MDviewer" % (name))
def update(self, text, warn):
"Update Preview."
self.web_view.settings().setAttribute(QWebSettings.JavascriptEnabled, True)
self.web_view.settings().setAttribute(QWebSettings.PluginsEnabled, True)
self.web_view.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
# Configure link behavior
self.web_view.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
self.web_view.linkClicked.connect(lambda url: self.handle_link_clicked(url))
# Save scroll position
if not self.web_view.page().currentFrame().scrollPosition() == QPoint(0,0):
self.scroll_pos[self.filename] = self.web_view.page().currentFrame().scrollPosition()
# Update Preview
self.web_view.setHtml(text, baseUrl=QUrl.fromLocalFile(os.path.join(os.getcwd(), self.filename)))
# Load JavaScript and core CSS
scr = os.path.join(app_dir, "mdviewer.js")
css = os.path.join(app_dir, "mdviewer.css")
add_resources = """
(function() {
var head = document.querySelector("head");
var css = document.createElement("link");
css.rel = "stylesheet";
css.href = "%s";
css.id = "coreCSS";
head.appendChild(css);
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "%s";
script.id = "coreJS";
script.setAttribute("defer", "");
head.appendChild(script);
})()
""" % (css, scr)
self.web_view.page().currentFrame().evaluateJavaScript(add_resources)
# Display processor warnings
if warn: QMessageBox.warning(self, "Processor message", warn)
def after_update(self):
"Restore scroll position."
try:
pos = self.scroll_pos[self.filename]
except KeyError:
pass
else:
self.web_view.page().currentFrame().evaluateJavaScript("window.scrollTo(%s, %s)" % (pos.x(), pos.y()))
def open_file(self):
filename, _filter = QFileDialog.getOpenFileName(self, "Open File", os.path.dirname(self.filename))
if filename != "":
self.filename = self.thread1.filename = filename
self.set_env()
self.set_window_title()
self.thread1.run()
else:
pass
def save_html(self):
filename, _filter = QFileDialog.getSaveFileName(self, "Save File", os.path.dirname(self.filename))
if filename != "":
proc = Settings.get("processor_path", "pandoc")
args = Settings.get("processor_args", "")
args = ("%s" % (args)).split() + [self.filename]
caller = QProcess()
caller.start(proc, args)
caller.waitForFinished()
html = str(caller.readAllStandardOutput(), "utf8")
with io.open(filename, "w", encoding="utf8") as f:
f.writelines(html)
f.close()
else:
pass
def find(self, text, btn=""):
page = self.web_view.page()
back = page.FindFlags(1) if btn is self.prev else page.FindFlags(0)
case = page.FindFlags(2) if self.case.isChecked() else page.FindFlags(0)
wrap = page.FindFlags(4) if self.wrap.isChecked() else page.FindFlags(0)
page.findText("", page.FindFlags(8))
page.findText(text, back | wrap | case)
def set_search_bar(self):
self.search_bar = QToolBar()
self.search_bar.setMovable(False)
self.search_bar.setFloatable(False)
self.search_bar.setVisible(False)
self.search_bar.layout().setSpacing(1)
self.addToolBar(0x8, self.search_bar)
self.text = QLineEdit(self)
self.text.setClearButtonEnabled(True)
self.text.setPlaceholderText(u"Search")
self.case = QCheckBox(u"Case sensitive", self)
self.wrap = QCheckBox(u"Wrap", self)
self.next = QPushButton(u"Next", self)
self.next.setToolTip(u"Find next")
self.next.setShortcut(QKeySequence("Return"))
self.next.setDisabled(True)
self.prev = QPushButton(u"Previous", self)
self.prev.setToolTip(u"Find previous")
self.prev.setShortcut(QKeySequence("Shift+Return"))
self.prev.setDisabled(True)
self.done = QPushButton(u"Done", self)
self.done.setToolTip(u"Hide Search bar")
self.done.setShortcut(QKeySequence("Esc"))
def _enable_nav():
if self.text.text() == "":
self.next.setDisabled(True)
self.prev.setDisabled(True)
else:
self.next.setDisabled(False)
self.prev.setDisabled(False)
def _toggle_btn(btn=""):
self.text.setFocus()
self.find(self.text.text(), btn)
def _hide():
if self.search_bar.isVisible():
self.search_bar.hide()
self.search_bar.addWidget(self.done)
self.search_bar.addSeparator()
self.search_bar.addWidget(self.case)
self.search_bar.addWidget(self.wrap)
self.search_bar.addWidget(self.text)
self.search_bar.addSeparator()
self.search_bar.addWidget(self.next)
self.search_bar.addWidget(self.prev)
for btn in (self.prev, self.next):
btn.pressed[()].connect(lambda btn = btn: _toggle_btn(btn))
self.done.pressed.connect(_hide)
self.text.textChanged.connect(self.find)
self.text.textChanged.connect(_enable_nav)
def show_search_bar(self):
self.search_bar.show()
self.text.setFocus()
self.text.selectAll()
def print_doc(self):
dialog = QPrintPreviewDialog()
dialog.paintRequested.connect(self.web_view.print_)
dialog.exec_()
def quit(self, QCloseEvent):
self.QSETTINGS.setValue("size", self.size())
self.QSETTINGS.setValue("pos", self.pos())
self.QSETTINGS.setValue("stylesheet", self.stylesheet)
QtWidgets.qApp.quit()
def zoom_in(self):
self.web_view.setZoomFactor(self.web_view.zoomFactor()+.1)
def zoom_out(self):
self.web_view.setZoomFactor(self.web_view.zoomFactor()-.1)
def zoom_reset(self):
self.web_view.setZoomFactor(1)
def scroll_down(self):
self.web_view.page().currentFrame().scroll(0, +self.web_view.page().viewportSize().height())
def scroll_up(self):
self.web_view.page().currentFrame().scroll(0, -self.web_view.page().viewportSize().height())
def toggle_toc(self):
self.web_view.page().currentFrame().evaluateJavaScript("toggleTOC()")
def handle_link_clicked(self, url):
if url.isLocalFile():
if url.toLocalFile() == os.path.abspath(self.filename) and url.hasFragment():
self.web_view.page().currentFrame().scrollToAnchor(url.fragment())
return
else:
QDesktopServices.openUrl(url)
else:
QDesktopServices.openUrl(url)
@staticmethod
def set_stylesheet(self, stylesheet="default.css"):
path = os.path.join(css_dir, stylesheet)
url = QUrl.fromLocalFile(path)
self.web_view.settings().setUserStyleSheetUrl(url)
self.stylesheet = stylesheet
def about(self):
msg_about = QMessageBox(0, "About MDviewer", u"MDviewer\n\nVersion: %s" % (VERSION), parent=self)
msg_about.show()
def report_issue(self):
url = QUrl.fromUserInput("https://github.com/saf-dmitry/mdviewer/issues")
QDesktopServices.openUrl(url)
def set_menus(self):
menubar = self.menuBar()
file_menu = menubar.addMenu("&File")
for d in (
{"name": u"&Open...", "shct": "Ctrl+O", "func": self.open_file},
{"name": u"&Save HTML...", "shct": "Ctrl+S", "func": self.save_html},
{"name": u"&Find...", "shct": "Ctrl+F", "func": self.show_search_bar},
{"name": u"&Print...", "shct": "Ctrl+P", "func": self.print_doc},
{"name": u"&Quit", "shct": "Ctrl+Q", "func": self.quit}
):
action = QAction(d["name"], self)
action.setShortcut(QKeySequence(d["shct"]))
action.triggered.connect(d["func"])
file_menu.addAction(action)
view_menu = menubar.addMenu("&View")
for d in (
{"name": u"Zoom &In", "shct": "Ctrl++", "func": self.zoom_in},
{"name": u"Zoom &Out", "shct": "Ctrl+-", "func": self.zoom_out},
{"name": u"&Actual Size", "shct": "Ctrl+0", "func": self.zoom_reset}
):
action = QAction(d["name"], self)
action.setShortcut(QKeySequence(d["shct"]))
action.triggered.connect(d["func"])
view_menu.addAction(action)
style_menu = menubar.addMenu("&Style")
style_menu.setStyleSheet("menu-scrollable: 1")
style_menu.setDisabled(True)
if os.path.exists(css_dir):
files = sorted(os.listdir(css_dir))
files = [f for f in files if f.endswith(".css")]
if len(files) > 0:
style_menu.setDisabled(False)
group = QActionGroup(self, exclusive=True)
for i, f in enumerate(files, start=1):
name = os.path.splitext(f)[0].replace("&", "&&")
action = group.addAction(QtWidgets.QAction(name, self))
action.triggered.connect(
lambda x, stylesheet = f: self.set_stylesheet(self, stylesheet))
if i < 10: action.setShortcut(QKeySequence("Ctrl+%d" % i))
action.setCheckable(True)
style_menu.addAction(action)
if f == self.stylesheet: action.trigger()
help_menu = menubar.addMenu("&Help")
for d in (
{"name": u"&About...", "func": self.about},
{"name": u"&Report an Issue", "func": self.report_issue},
):
action = QAction(d["name"], self)
action.triggered.connect(d["func"])
help_menu.addAction(action)
# Redefine reload action
reload_action = self.web_view.page().action(QWebPage.Reload)
reload_action.setShortcut(QKeySequence.Refresh)
reload_action.triggered.connect(self.thread1.run)
self.web_view.addAction(reload_action)
# Define additional shortcuts
QShortcut(QKeySequence("j"), self, activated=self.scroll_down)
QShortcut(QKeySequence("k"), self, activated=self.scroll_up)
QShortcut(QKeySequence("t"), self, activated=self.toggle_toc)
def closeEvent(self, event):
self.quit(event)
event.accept()
class WatcherThread(QThread):
update = pyqtSignal(str, str)
def __init__(self, filename):
QThread.__init__(self)
self.filename = filename
def run(self):
html, warn = self.processor_rules()
self.update.emit(html, warn)
def processor_rules(self):
proc = Settings.get("processor_path", "pandoc")
args = Settings.get("processor_args", "")
args = args.split() + [self.filename]
html = ""; warn = ""
if shutil.which(proc) is not None:
caller = QProcess()
caller.start(proc, args)
caller.waitForFinished()
html = str(caller.readAllStandardOutput(), "utf8")
warn = str(caller.readAllStandardError(), "utf8")
else:
warn = u"Executable not found: %s" % (proc)
return (html, warn)
class Settings:
def __init__(self):
if os.name == "nt":
self.user_source = os.path.join(os.getenv("APPDATA"), "mdviewer", "settings.yml")
else:
self.user_source = os.path.join(os.getenv("HOME"), ".config", "mdviewer", "settings.yml")
self.app_source = os.path.join(app_dir, "settings.yml")
self.settings_file = self.user_source if os.path.exists(self.user_source) else self.app_source
self.load_settings()
def load_settings(self):
with io.open(self.settings_file, "r", encoding="utf8") as f:
self.settings = yaml.safe_load(f)
@classmethod
def get(cls, key, default_value):
return cls().settings.get(key, default_value)
@classmethod
def print_path(cls):
print("Settings: %s" % cls().settings_file)
def main():
app = QApplication(sys.argv)
if len(sys.argv) != 2:
window = App()
else:
window = App(filename=sys.argv[1])
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()