-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmarkdown_diary.py
executable file
·1003 lines (795 loc) · 36.7 KB
/
markdown_diary.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""This module contains the main markdown-diary app code.
Markdown diary is a simple note taking app build on PyQt5. Its diaries are
stored as plain text markdown files. The app has support for several features
not included in plain markdown, such as tables, mathjax math rendering and
syntax highlighting.
"""
import os
import sys
import uuid
import re
import datetime
from PyQt5 import QtGui, QtCore
from PyQt5 import QtWidgets
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
from PyQt5.QtWebEngineWidgets import QWebEngineSettings
# from PyQt5.QtCore import pyqtRemoveInputHook # enable for debugging
from markdownhighlighter import MarkdownHighlighter
import markdown_math
import style
import diary
class DummyItemDelegate(QtWidgets.QItemDelegate): # pylint: disable=too-few-public-methods
"""A class used to modify behavior and appearance of the QtTreeWidget.
This class is used to disable editing for specific columns as well as
increase row height.
"""
def createEditor(self, parent, option, index):
"""Do nothing to disable editing."""
pass
def sizeHint(self, _option, _index): # pylint: disable=no-self-use
"""Increase row size.
This is used in the QTreeWidget.
"""
return QtCore.QSize(1, 20)
class MyQTextEdit(QtWidgets.QTextEdit): # pylint: disable=too-few-public-methods
"""Modified QTextEdit that highlights all search matches."""
def highlightSearch(self, pattern):
"""Highlight all search occurences.
The search is case insensitive.
Args:
pattern (str): The text to be highlighted
"""
self.moveCursor(QtGui.QTextCursor.Start)
color = QtGui.QColor("yellow")
extraSelections = []
while self.find(pattern):
extra = QtWidgets.QTextEdit.ExtraSelection()
extra.format.setBackground(color)
extra.cursor = self.textCursor()
extraSelections.append(extra)
self.setExtraSelections(extraSelections)
self.moveCursor(QtGui.QTextCursor.Start)
self.find(pattern)
def insertFromMimeData(self, source):
"""Insert supported formats in a specific way (i.e., Markdown-like)."""
if source.hasUrls():
for url in source.urls():
path = url.path()
if self.isWebImage(path):
fileName = os.path.basename(path)
diaryPath = self.parent().parent().parent().parent().diary.fname
relPath = os.path.relpath(
path, start=os.path.dirname(diaryPath))
imgMarkdown = '\n'.format(
relPath, fileName)
self.insertPlainText(imgMarkdown)
else:
super(MyQTextEdit, self).insertFromMimeData(source)
else:
super(MyQTextEdit, self).insertFromMimeData(source)
@staticmethod
def isWebImage(path):
"""Check if path leads to an image that can by displayed by browser."""
if path.lower().endswith(('.gif', '.png', '.jpg', '.jpeg')):
return True
return False
class MyWebEnginePage(QWebEnginePage):
"""Modified QWebEnginePage that opens external links in the default system browser."""
def __init__(self, parent=None):
"""Initialize the parent class."""
super().__init__(parent)
self.diaryPath = ""
def acceptNavigationRequest(self, qurl, navtype, mainframe):
"""Open external links in the system browser, other links in this one.
For some reason all links that don't have 'http://' or similar
prepended, have 'file://' and the diary dir automatically prepended.
I believe the reason is the following line in
displayHTMLRenderedMarkdown():
self.web.setHtml(html, baseUrl=QtCore.QUrl.fromLocalFile(mainPath))
We need this line in order to show images and stylesheets, which are
resolved relative to the baseUrl. So here we fix the link and open it
in the system browser.
"""
# print("Navigation Request intercepted:", qurl)
if qurl.isLocalFile(): # delegate link to default browser
diaryDirPath = os.path.dirname(self.diaryPath)
url = qurl.toString().replace('file://', 'http://').replace(diaryDirPath + '/', '')
QtGui.QDesktopServices.openUrl(QtCore.QUrl(url))
return False
else:
# When setting QWebEngineView's content manually, the URL starts with 'data:text/html;'
if qurl.toString().startswith("data"):
# open in QWebEngineView
return True
else:
# delegate link to default browser
QtGui.QDesktopServices.openUrl(qurl)
return False
class DiaryApp(QtWidgets.QMainWindow): # pylint: disable=too-many-public-methods,too-many-instance-attributes
"""Diary application class inheriting from QMainWindow."""
def __init__(self, parent=None):
"""Initialize member variables and GUI."""
self.maxRecentItems = 10
self.markdownAction = None
self.newNoteAction = None
self.saveNoteAction = None
self.deleteNoteAction = None
self.exportToHTMLAction = None
self.exportToPDFAction = None
self.newDiaryAction = None
self.openDiaryAction = None
self.searchLineAction = None
self.recentDiariesActions = None
self.clearRecentDiariesAction = None
self.quitAction = None
self.searchLine = None
self.toolbar = None
self.fileMenu = None
self.noteMenu = None
self.noteDate = None
self.noteId = None
self.recentDiaries = None
self.recentNotes = None
self.diary = None
QtWidgets.QMainWindow.__init__(self, parent)
renderer = markdown_math.HighlightRenderer()
self.toMarkdown = markdown_math.MarkdownWithMath(renderer=renderer)
self.tempFiles = []
self.initUI()
self.settings = QtCore.QSettings(
"markdown-diary", application="settings")
self.loadSettings()
if self.recentDiaries and os.path.isfile(self.recentDiaries[0]):
self.loadDiary(self.recentDiaries[0])
else:
self.text.setDisabled(True)
self.saveNoteAction.setDisabled(True)
self.newNoteAction.setDisabled(True)
self.deleteNoteAction.setDisabled(True)
self.exportToHTMLAction.setDisabled(True)
self.exportToPDFAction.setDisabled(True)
self.markdownAction.setDisabled(True)
self.searchLineAction.setDisabled(True)
def closeEvent(self, event):
"""Check if there are unsaved changes and display dialog if there are.
This redefines the basic close event to give the user a chance to save
his work. It also saves the current settings.
Args:
event (QEvent):
"""
if self.text.document().isModified():
reply = self.promptToSaveOrDiscard()
if reply == QtWidgets.QMessageBox.Cancel:
event.ignore()
return
elif reply == QtWidgets.QMessageBox.Save:
self.saveNote()
self.writeSettings()
def initUI(self):
"""Initialize the UI - create widgets, set their pars, etc."""
self.window = QtWidgets.QWidget(self)
self.splitter = QtWidgets.QSplitter()
self.initToolbar()
self.initMenu()
self.shortcutFindNext = QtWidgets.QShortcut(QtGui.QKeySequence('F3'), self)
self.shortcutFindNext.activated.connect(self.searchNext)
self.text = MyQTextEdit(self)
self.text.setAcceptRichText(False)
self.text.setFont(QtGui.QFont(
QtGui.QFontDatabase.systemFont(QtGui.QFontDatabase.FixedFont)))
self.text.textChanged.connect(self.setTitle)
self.web = QWebEngineView(self)
self.web.settings().setAttribute(QWebEngineSettings.FocusOnNavigationEnabled, False)
self.page = MyWebEnginePage()
self.web.setPage(self.page)
self.web.loadFinished.connect(self.webLoadFinished)
self.highlighter = MarkdownHighlighter(self.text)
self.setCentralWidget(self.window)
self.setWindowTitle("Markdown Diary")
self.stack = QtWidgets.QStackedWidget()
self.stack.addWidget(self.text)
self.stack.addWidget(self.web)
self.tree = QtWidgets.QTreeWidget()
self.tree.setUniformRowHeights(True)
self.tree.setColumnCount(3)
self.tree.setHeaderLabels(["Id", "Date", "Title"])
self.tree.setColumnHidden(0, True)
self.tree.setSortingEnabled(True)
self.tree.sortByColumn(1, QtCore.Qt.DescendingOrder)
self.tree.itemSelectionChanged.connect(self.itemSelectionChanged)
self.tree.itemChanged.connect(self.itemChanged)
self.tree.itemDoubleClicked.connect(self.itemDoubleClicked)
# Disable editing for the 'title' column
self.tree.setItemDelegateForColumn(2, DummyItemDelegate())
self.splitter.addWidget(self.stack)
self.splitter.addWidget(self.tree)
layout = QtWidgets.QHBoxLayout()
layout.addWidget(self.splitter)
self.window.setLayout(layout)
def initToolbar(self):
"""Initialize toolbar - create QActions and bind to functions, etc."""
self.markdownAction = QtWidgets.QAction(
QtGui.QIcon.fromTheme("down"), "Toggle Markdown", self)
self.markdownAction.setShortcut("Ctrl+M")
self.markdownAction.setStatusTip("Toggle markdown rendering")
self.markdownAction.triggered.connect(self.markdownToggle)
self.newNoteAction = QtWidgets.QAction(
QtGui.QIcon.fromTheme("document-new"), "New note", self)
self.newNoteAction.setShortcut("Ctrl+N")
self.newNoteAction.setStatusTip("Create a new note")
self.newNoteAction.triggered.connect(self.newNote)
self.saveNoteAction = QtWidgets.QAction(
QtGui.QIcon.fromTheme("document-save"), "Save note", self)
self.saveNoteAction.setShortcut("Ctrl+S")
self.saveNoteAction.setStatusTip("Save note")
self.saveNoteAction.triggered.connect(self.saveNote)
self.newDiaryAction = QtWidgets.QAction(
QtGui.QIcon.fromTheme("folder-new"), "New diary", self)
self.newDiaryAction.setStatusTip("New diary")
self.newDiaryAction.triggered.connect(self.newDiary)
self.openDiaryAction = QtWidgets.QAction(
QtGui.QIcon.fromTheme("document-open"), "Open diary", self)
self.openDiaryAction.setShortcut("Ctrl+O")
self.openDiaryAction.setStatusTip("Open diary")
self.openDiaryAction.triggered.connect(self.openDiary)
self.clearRecentDiariesAction = QtWidgets.QAction("Clear list", self)
self.clearRecentDiariesAction.setStatusTip("Clear list")
self.clearRecentDiariesAction.triggered.connect(
lambda: self.clearRecentDiaries()) # pylint: disable=unnecessary-lambda
self.quitAction = QtWidgets.QAction("Quit", self)
self.quitAction.setStatusTip("Quit the application")
self.quitAction.setMenuRole(QtWidgets.QAction.QuitRole)
self.quitAction.setShortcut(QtGui.QKeySequence.Quit)
self.quitAction.triggered.connect(
lambda: self.close()) # pylint: disable=unnecessary-lambda
self.deleteNoteAction = QtWidgets.QAction(
QtGui.QIcon.fromTheme("remove"), "Delete Note", self)
self.deleteNoteAction.setShortcut("Del")
self.deleteNoteAction.setStatusTip("Delete note")
self.deleteNoteAction.triggered.connect(
lambda: self.deleteNote()) # pylint: disable=unnecessary-lambda
self.exportToHTMLAction = QtWidgets.QAction(
QtGui.QIcon.fromTheme("document-export"), "Export to HTML", self)
self.exportToHTMLAction.setStatusTip("Export to HTML")
self.exportToHTMLAction.triggered.connect(self.exportToHTML)
self.exportToPDFAction = QtWidgets.QAction(
QtGui.QIcon.fromTheme("document-export"), "Export to PDF", self)
self.exportToPDFAction.setStatusTip("Export to PDF")
self.exportToPDFAction.triggered.connect(self.exportToPDF)
self.searchLine = QtWidgets.QLineEdit(self)
self.searchLine.setFixedWidth(200)
self.searchLine.setPlaceholderText("Search...")
self.searchLine.setClearButtonEnabled(True)
self.searchLineAction = QtWidgets.QWidgetAction(self)
self.searchLineAction.setDefaultWidget(self.searchLine)
self.searchLineAction.setShortcut(QtGui.QKeySequence.Find)
self.searchLineAction.triggered.connect(self.selectSearch)
self.searchLine.textChanged.connect(self.search)
self.searchLine.returnPressed.connect(self.searchNext)
self.toolbar = self.addToolBar("Main toolbar")
self.toolbar.setFloatable(False)
self.toolbar.setAllowedAreas(
QtCore.Qt.TopToolBarArea | QtCore.Qt.BottomToolBarArea)
self.toolbar.addAction(self.markdownAction)
self.toolbar.addSeparator()
self.toolbar.addAction(self.newNoteAction)
self.toolbar.addAction(self.saveNoteAction)
self.toolbar.addAction(self.deleteNoteAction)
self.toolbar.addSeparator()
self.toolbar.addAction(self.openDiaryAction)
self.toolbar.addSeparator()
self.toolbar.addAction(self.searchLineAction)
def initMenu(self):
"""Create the main application menu - File, etc."""
self.fileMenu = self.menuBar().addMenu("&File")
self.fileMenu.addAction(self.newDiaryAction)
self.fileMenu.addAction(self.openDiaryAction)
self.fileMenu.addSeparator()
self.recentDiariesActions = []
for _ in range(self.maxRecentItems):
action = QtWidgets.QAction(self)
action.setVisible(False)
self.recentDiariesActions.append(action)
self.fileMenu.addAction(action)
self.fileMenu.addSeparator()
self.fileMenu.addAction(self.clearRecentDiariesAction)
self.fileMenu.addSeparator()
self.fileMenu.addAction(self.quitAction)
self.noteMenu = self.menuBar().addMenu("&Note")
self.noteMenu.addAction(self.newNoteAction)
self.noteMenu.addAction(self.saveNoteAction)
self.noteMenu.addAction(self.deleteNoteAction)
self.noteMenu.addSeparator()
self.noteMenu.addAction(self.exportToHTMLAction)
self.noteMenu.addAction(self.exportToPDFAction)
def loadTree(self, metadata):
"""Load notes tree from diary metadata.
Load notes tree from diary metadata and populate the QTreeWidget
with it.
"""
entries = []
for note in metadata:
entries.append(QtWidgets.QTreeWidgetItem(
[note["note_id"], note["date"], note["title"]]))
for entry in entries:
entry.setFlags(entry.flags() | QtCore.Qt.ItemIsEditable)
self.tree.clear()
self.tree.addTopLevelItems(entries)
def loadSettings(self):
"""Load settings via self.settings QSettings object."""
self.recentDiaries = self.settings.value("diary/recent_diaries", [])
self.updateRecentDiaries()
self.recentNotes = self.settings.value("diary/recent_notes", [])
self.resize(self.settings.value(
"window/size", QtCore.QSize(600, 400)))
self.move(self.settings.value(
"window/position", QtCore.QPoint(200, 200)))
self.splitter.setSizes(
[int(val) for val in self.settings.value(
"window/splitter", [70, 30])])
toolBarArea = int(self.settings.value("window/toolbar_area",
QtCore.Qt.TopToolBarArea))
# addToolBar() actually just moves the specified toolbar if it
# was already added, which is what we want
self.addToolBar(QtCore.Qt.ToolBarArea(toolBarArea), self.toolbar)
self.mathjax = self.settings.value(
"mathjax/location",
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js")
def writeSettings(self):
"""Save settings via self.settings QSettings object."""
self.settings.setValue("window/size", self.size())
self.settings.setValue("window/position", self.pos())
self.settings.setValue("window/splitter", self.splitter.sizes())
self.settings.setValue("window/toolbar_area", self.toolBarArea(
self.toolbar))
if self.recentDiaries:
self.settings.setValue("diary/recent_diaries", self.recentDiaries)
if self.recentNotes:
self.settings.setValue("diary/recent_notes", self.recentNotes)
def markdownToggle(self):
"""Switch between displaying Markdown source and rendered HTML."""
if self.stack.currentIndex() == 1:
self.stack.setCurrentIndex(0)
else:
self.stack.setCurrentIndex(1)
if (self.text.document().isModified()):
self.displayHTMLRenderedMarkdown(self.text.toPlainText())
def createHTML(self, markdownText):
"""Create full, valid HTML from Markdown source.
Args:
markdownText (str): Markdown source to convert to HTML.
Returns:
Full HTML page text.
"""
html = style.header
# We load MathJax only when there is a good chance there is
# math in the note. We first perform inline math search as
# as that should be faster then the re.DOTALL multiline
# block math search, which gets executed only if we don't
# find inline math.
mathInline = re.compile(r"\$(.+?)\$")
mathBlock = re.compile(r"^\$\$(.+?)^\$\$",
re.DOTALL | re.MULTILINE)
if mathInline.search(markdownText) or mathBlock.search(markdownText):
html += style.mathjax
mathjaxScript = (
'<script type="text/javascript" src="{}?config='
'TeX-AMS-MML_HTMLorMML"></script>\n').format(self.mathjax)
html += mathjaxScript
html += self.toMarkdown(markdownText) # pylint: disable=not-callable
html += style.footer
return html
def displayHTMLRenderedMarkdown(self, markdownText):
"""Display HTML rendered Markdown."""
html = self.createHTML(markdownText)
# QWebEngineView resolves relative links (like images and stylesheets)
# with respect to the baseUrl
mainPath = self.diary.fname
self.web.setHtml(html, baseUrl=QtCore.QUrl.fromLocalFile(mainPath))
def newNote(self):
"""Create an empty note and add it to the QTreeWidget.
The note is not added to the diary until it is saved.
"""
self.noteDate = datetime.date.today().isoformat()
self.noteId = str(uuid.uuid1())
self.text.clear()
self.stack.setCurrentIndex(0)
self.text.setFocus()
self.text.setText("# <Untitled note>")
self.saveNote()
self.loadTree(self.diary.data)
self.selectItemWithoutReload(self.noteId)
# Select the '<Untitled note>' part of the new note for convenient
# renaming
cursor = self.text.textCursor()
cursor.setPosition(2)
cursor.setPosition(17, QtGui.QTextCursor.KeepAnchor)
self.text.setTextCursor(cursor)
def saveNote(self):
"""Save the displayed note.
Either updates an existing note or adds a new one to a diary.
"""
if self.text.toPlainText().lstrip() == "":
QtWidgets.QMessageBox.information(
self, 'Message', "You can't save an empty note!")
return
# Notes should begin with a title, so strip any whitespace,
# including newlines from the beggining
self.diary.saveNote(
self.text.toPlainText().lstrip(), self.noteId, self.noteDate)
self.text.document().setModified(False)
self.setTitle()
# Rerender the HTML since we removed the modified flag from
# self.text.document()
self.displayHTMLRenderedMarkdown(self.text.toPlainText())
# Change the title in the tree, without reloading the tree (that would
# cause the filtered results when searching to be lost)
self.tree.blockSignals(True)
# When there are no items in the tree (new diary) must add the item
# first and select it
if self.tree.topLevelItemCount() == 0:
newItem = QtWidgets.QTreeWidgetItem(
[self.noteId, self.noteDate, ""])
self.tree.addTopLevelItem(newItem)
self.tree.setCurrentItem(newItem)
self.tree.currentItem().setText(2, self.diary.getNoteMetadata(
self.noteId)["title"])
self.tree.blockSignals(False)
def deleteNote(self, noteId=None):
"""Delete a specified note.
If there are unsaved changes, prompt the user. Refresh note tree
after deletion.
Args:
noteId (str, optional): UUID of the note to delete
"""
if noteId is None:
noteId = self.noteId
noteTitle = self.diary.getNoteMetadata(self.noteId)["title"]
deleteMsg = "Do you really want to delete the note '" + noteTitle + "'?"
reply = QtWidgets.QMessageBox.question(
self, 'Message', deleteMsg,
QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No)
if reply == QtWidgets.QMessageBox.No:
return
nextNoteId = self.tree.itemBelow(self.tree.currentItem()).text(0)
self.diary.deleteNote(noteId)
self.text.document().setModified(False)
self.loadTree(self.diary.data)
self.tree.setCurrentItem(
self.tree.findItems(nextNoteId, QtCore.Qt.MatchExactly)[0])
def newDiary(self):
"""Display a file save dialog and create diary at specified path.
Enable relevant toolbar items (new note, save note, etc.), in case
no diary was open before and they were disabled.
"""
if self.text.document().isModified():
reply = self.promptToSaveOrDiscard()
if reply == QtWidgets.QMessageBox.Cancel:
return
elif reply == QtWidgets.QMessageBox.Discard:
self.text.document().setModified(False)
elif reply == QtWidgets.QMessageBox.Save:
self.saveNote()
fname = QtWidgets.QFileDialog.getSaveFileName(
caption="Create a New Diary",
filter="Markdown Files (*.md);;All Files (*)")[0]
if fname:
with open(fname, 'w'):
os.utime(fname)
self.loadDiary(fname)
self.newNote()
def openDiary(self):
"""Display a file open dialog and load the selected diary.
Enable relevant toolbar items (new note, save note, etc.), in case
no diary was open before and they were disabled.
"""
if self.text.document().isModified():
reply = self.promptToSaveOrDiscard()
if reply == QtWidgets.QMessageBox.Cancel:
return
elif reply == QtWidgets.QMessageBox.Discard:
self.text.document().setModified(False)
elif reply == QtWidgets.QMessageBox.Save:
self.saveNote()
fname = QtWidgets.QFileDialog.getOpenFileName(
caption="Open Diary",
filter="Markdown Files (*.md);;All Files (*)")[0]
if fname:
if self.isValidDiary(fname):
self.loadDiary(fname)
self.text.setDisabled(False)
self.saveNoteAction.setDisabled(False)
self.newNoteAction.setDisabled(False)
self.deleteNoteAction.setDisabled(False)
self.exportToHTMLAction.setDisabled(False)
self.exportToPDFAction.setDisabled(False)
self.markdownAction.setDisabled(False)
self.searchLineAction.setDisabled(False)
else:
print("ERROR:" + fname + "is not a valid diary file!")
@staticmethod
def isValidDiary(fname):
"""Check if a file path leads to a valid diary.
Args:
fname (str): Path to a diary file to be validated.
Returns:
bool: True for valid, False for invalid diary.
"""
# TODO Implement checks
return True
def loadDiary(self, fname):
"""Load diary from file.
Display last note from the diary if it exists.
Args:
fname (str): Path to a file containing a diary.
"""
if self.text.document().isModified():
reply = self.promptToSaveOrDiscard()
if reply == QtWidgets.QMessageBox.Cancel:
return
elif reply == QtWidgets.QMessageBox.Discard:
self.text.document().setModified(False)
elif reply == QtWidgets.QMessageBox.Save:
self.saveNote()
self.updateRecentDiaries(fname)
self.diary = diary.Diary(fname)
# Save the diary path to QWebEnginePage, so we can fix external links,
# which (for some reason) look like file://DIARY_PATH/EXTERNAL_LINK
self.page.diaryPath = fname
self.loadTree(self.diary.data)
# Display empty editor if the diary has no notes (e.g., new diary)
if not self.diary.data:
self.text.clear()
self.stack.setCurrentIndex(0)
return
# Check if we saved a recent noteId for this diary and open it if we
# did, otherwise open the newest note
lastNoteId = ""
for recentNote in self.recentNotes:
if recentNote in (metaDict["note_id"] for metaDict in self.diary.data):
lastNoteId = recentNote
break
if lastNoteId == "":
lastNoteId = self.diary.data[-1]["note_id"]
self.tree.setCurrentItem(
self.tree.findItems(lastNoteId, QtCore.Qt.MatchExactly)[0])
self.stack.setCurrentIndex(1)
def updateRecentDiaries(self, fname=""):
"""Update list of recently opened diaries.
When fname is specified, adds/moves the specified diary to the
beggining of a list. Otherwise just populates the list in the file
menu.
Args:
fname (str, optional): The most recent diary to be added/moved
to the top of the list.
"""
if fname != "":
if fname in self.recentDiaries:
self.recentDiaries.remove(fname)
self.recentDiaries.insert(0, fname)
if len(self.recentDiaries) > self.maxRecentItems:
del self.recentDiaries[self.maxRecentItems:]
for recent in self.recentDiariesActions:
recent.setVisible(False)
for i, recent in enumerate(self.recentDiaries):
self.recentDiariesActions[i].setText(os.path.basename(recent))
self.recentDiariesActions[i].setData(recent)
self.recentDiariesActions[i].setVisible(True)
# Multiple signals can be connected, so to avoid old signals we
# disconnect them
self.recentDiariesActions[i].triggered.disconnect()
self.recentDiariesActions[i].triggered.connect(
lambda dummy=False, recent=recent: self.loadDiary(recent))
def clearRecentDiaries(self):
"""Clear the list of recent diaries."""
self.recentDiaries = []
self.updateRecentDiaries()
def updateRecentNotes(self, noteId):
"""Update list of recently viewed notes.
Adds/moves the specified noteId to the beggining of the list.
Args:
noteId (str): The most recent note to be added/moved to the top
of the list.
"""
if noteId in self.recentNotes:
self.recentNotes.remove(noteId)
self.recentNotes.insert(0, noteId)
if len(self.recentNotes) > self.maxRecentItems:
del self.recentNotes[self.maxRecentItems:]
@staticmethod
def promptToSaveOrDiscard():
"""Display a message box asking whether to save or discard changes.
Returns:
One of the three options:
QtWidgets.QMessageBox.Save
QtWidgets.QMessageBox.Discard
QtWidgets.QMessageBox.Cancel
"""
msgBox = QtWidgets.QMessageBox()
msgBox.setWindowTitle("Save or Discard")
msgBox.setIcon(QtWidgets.QMessageBox.Question)
msgBox.setText("Save changes before closing note?")
msgBox.setStandardButtons(
QtWidgets.QMessageBox.Save |
QtWidgets.QMessageBox.Discard |
QtWidgets.QMessageBox.Cancel)
msgBox.setDefaultButton(QtWidgets.QMessageBox.Save)
return msgBox.exec()
def itemSelectionChanged(self):
"""Display a new selected note.
Prompts the user if there is unsaved work. If there is an active
search, reruns it on the new note.
"""
if len(self.tree.selectedItems()) == 0: # pylint: disable=len-as-condition
return
newNoteId = self.tree.selectedItems()[0].text(0)
if self.text.document().isModified():
# Keep the cursor on the note in question while the dialog is
# displayed
self.tree.blockSignals(True)
self.tree.setCurrentItem(self.tree.findItems(
self.noteId, QtCore.Qt.MatchExactly)[0])
self.tree.blockSignals(False)
reply = self.promptToSaveOrDiscard()
# We just save note/flag it as unmodified and recursively call
# this method again
if reply == QtWidgets.QMessageBox.Save:
self.saveNote()
self.tree.setCurrentItem(self.tree.findItems(
newNoteId, QtCore.Qt.MatchExactly)[0])
elif reply == QtWidgets.QMessageBox.Discard:
self.text.document().setModified(False)
self.tree.setCurrentItem(self.tree.findItems(
newNoteId, QtCore.Qt.MatchExactly)[0])
return
self.displayNote(newNoteId)
if self.searchLine.text() != "":
# Search in the editor (searching in WebView happens
# asynchronously, once the page is loaded)
self.text.highlightSearch(self.searchLine.text())
def displayNote(self, noteId):
"""Display a specified note."""
self.text.setText(self.diary.getNote(noteId))
self.setTitle()
self.noteId = noteId
self.updateRecentNotes(noteId)
self.noteDate = self.diary.getNoteMetadata(noteId)["date"]
self.displayHTMLRenderedMarkdown(self.text.toPlainText())
def selectSearch(self):
"""Focus the search widget and select its contents."""
self.searchLine.setFocus()
self.searchLine.selectAll()
def search(self):
"""Search and highlight text in all notes.
Highlights text occurrences in the editor and web view. Searches all
notes for the text and removes non-matching from the note tree. The
text to search for is taken from the searchLine widget.
"""
if self.searchLine.text() == "":
self.loadTree(self.diary.data)
self.selectItemWithoutReload(self.noteId)
self.text.highlightSearch("")
self.web.findText("")
return
# Search in the editor
self.text.highlightSearch(self.searchLine.text())
# Search in the WebView
self.web.findText(self.searchLine.text())
# Search for matching notes
entries = self.diary.searchNotes(self.searchLine.text())
self.loadTree(entries)
if entries:
# Select the matching item in the tree. Either the current one, if it
# is among the matching items, or the last matching one.
if self.noteId in (entry["note_id"] for entry in entries):
self.selectItemWithoutReload(self.noteId)
else:
self.tree.setCurrentItem(self.tree.findItems(
entries[-1]["note_id"], QtCore.Qt.MatchExactly)[0])
self.searchLine.setFocus()
def searchNext(self):
"""Move main highlight (and scroll) to the next search match."""
self.web.findText(self.searchLine.text())
if self.text.extraSelections():
if not self.text.find(self.searchLine.text()):
self.text.moveCursor(QtGui.QTextCursor.Start)
self.text.find(self.searchLine.text())
def setTitle(self):
"""Set the application title; add '*' if editor in dirty state."""
if self.text.document().isModified():
self.setWindowTitle("*Markdown Diary")
else:
self.setWindowTitle("Markdown Diary")
if hasattr(self, 'diary') and self.diary is not None:
self.setWindowTitle(self.windowTitle() + " - " +
os.path.basename(self.diary.fname))
def itemDoubleClicked(self, _item, column):
"""Decide action based on which column the user clicked.
If the user clicked the title, toggle Markdown.
"""
if column == 2:
self.markdownToggle()
def itemChanged(self, item, _column):
"""Update note when some of its metadata are changed in the TreeWidget.
Currently only the date can be changed. The date is first validated,
otherwise no action is taken.
"""
noteId = item.text(0)
noteDate = item.text(1)
if self.diary.isValidDate(noteDate):
self.diary.changeNoteDate(noteId, noteDate)
self.noteDate = noteDate
self.loadTree(self.diary.data)
self.selectItemWithoutReload(noteId)
else:
print("Invalid date")
self.loadTree(self.diary.data)
self.selectItemWithoutReload(noteId)
def selectItemWithoutReload(self, noteId):
"""Select an item in the QtTreeWidget without reloading the note."""
self.tree.blockSignals(True)
self.tree.setCurrentItem(
self.tree.findItems(noteId, QtCore.Qt.MatchExactly)[0])
self.tree.blockSignals(False)
def exportToHTML(self):
"""Export the displayed note to HTML."""
markdownText = self.diary.getNote(self.noteId)
html = self.createHTML(markdownText)
# To be able to load the CSS during normal operation correctly, we have
# to use an absolute path. This is not desirable when exporting to
# HTML, so we change it to a relative path.
newhtml = ""
stillInHead = True
for line in html.splitlines():
if stillInHead:
if "github-markdown.css" in line:
newhtml += '<link rel="stylesheet" href="css/github-markdown.css">\n'
elif "github-pygments.css" in line:
newhtml += '<link rel="stylesheet" href="css/github-pygments.css">\n'
else:
newhtml += line + '\n'
if "</head>" in line:
stillInHead = False
else:
newhtml += line + '\n'
fname = QtWidgets.QFileDialog.getSaveFileName(
caption="Export Note to HTML",
filter="HTML Files (*.html);;All Files (*)")[0]
if fname:
with open(fname, 'w') as f:
os.utime(fname)
f.write(newhtml)
def exportToPDF(self):
"""Export the displayed note to PDF."""
fname = QtWidgets.QFileDialog.getSaveFileName(
caption="Export Note to PDF",
filter="PDF Files (*.pdf);;All Files (*)")[0]
if fname:
# Make sure we export the current version of the text
if self.stack.currentIndex() == 0:
self.displayHTMLRenderedMarkdown(self.text.toPlainText())
pageLayout = QtGui.QPageLayout(QtGui.QPageSize(
QtGui.QPageSize.A4), QtGui.QPageLayout.Landscape, QtCore.QMarginsF(0, 0, 0, 0))
self.web.page().printToPdf(fname, pageLayout)
def webLoadFinished(self):
if self.searchLine.text() != "":
# Search in the WebView
self.web.findText(self.searchLine.text())
def __del__(self):
"""Clean up temporary files on exit."""
# Delete temporary files
# We put it into __del__ deliberately, so if one wants, one can avoid
# the temporary files being deleted by killing the process. This might
# be useful, e.g., in case of accidental over-write.
for f in self.tempFiles:
os.unlink(f.name)
def main():
"""Run the whole QApplication."""
app = QtWidgets.QApplication(sys.argv)
# pyqtRemoveInputHook() # enable for debugging
diaryApp = DiaryApp()
diaryApp.show()
sys.exit(app.exec_())