-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdhiliter.py
105 lines (92 loc) · 4.07 KB
/
mdhiliter.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
#############################################################################
# Copyright (C) 2024 CrowdWare
#
# This file is part of EbookCreator.
#
# EbookCreator is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# EbookCreator is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with EbookCreator. If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
from PySide6.QtGui import QFont, QFontMetrics, QImage, QSyntaxHighlighter, QTextCharFormat, QColor
from PySide6.QtWidgets import QWidget
TITLE = 0
BOLD = 1
ITALIC = 2
BOLD_ITALIC = 3
LAST_CONSTRUCT = BOLD_ITALIC
class MdHiLiter(QSyntaxHighlighter):
def __init__(self, parent=None):
super(MdHiLiter, self).__init__(parent)
self.formats = [0] * (LAST_CONSTRUCT + 1)
titleFormat = QTextCharFormat()
titleFormat.setForeground(QWidget().palette().highlight().color())
titleFormat.setFontWeight(QFont.Bold)
self.setFormatFor(TITLE, titleFormat)
boldFormat = QTextCharFormat()
boldFormat.setForeground(QColor("#abb471"))
boldFormat.setFontWeight(QFont.Bold)
self.setFormatFor(BOLD, boldFormat)
italicFormat = QTextCharFormat()
italicFormat.setForeground(QColor("#bf8369"))
italicFormat.setFontItalic(True)
self.setFormatFor(ITALIC, italicFormat)
boldItalicFormat = QTextCharFormat()
boldItalicFormat.setForeground(QColor("#bf8369"))
boldItalicFormat.setFontItalic(True)
boldItalicFormat.setFontWeight(QFont.Bold)
self.setFormatFor(BOLD_ITALIC, boldItalicFormat)
def setFormatFor(self, construct, format):
self.formats[construct] = format
self.rehighlight()
def formatFor(self, construct):
return self.formats[construct]
def highlightBlock(self, text):
length = len(text)
start = 0
pos = 0
while pos < length:
ch = text[pos]
if ch == '#' and pos == 0 or text[pos - 1] == "\n":
start = pos
while pos < length and text[pos] != "\n":
pos = pos + 1
self.setFormat(start, pos - start, self.formats[TITLE])
elif ch == "*":
start = pos
cr = text.find("\n", pos + 1)
tripl_star = self.find("***", text, pos + 4)
dbl_star = self.find("**", text, pos + 3)
star = self.find("*", text, pos + 2)
if (pos + 3) < length and text[pos + 1] == "*" and text[pos + 2] == "*" and text[pos + 3] != " " and tripl_star > 0 and (tripl_star < cr or cr < 0):
self.setFormat(start, tripl_star + 3 - start, self.formats[BOLD_ITALIC])
pos = dbl_star + 3
elif (pos + 2) < length and text[pos + 1] == "*" and text[pos + 2] != " " and dbl_star > 0 and (dbl_star < cr or cr < 0):
self.setFormat(start, dbl_star + 2 - start, self.formats[BOLD])
pos = dbl_star + 2
elif (pos + 1) < length and text[pos + 1] != " " and star > 0 and (star < cr or cr < 0):
self.setFormat(start, star + 1 - start, self.formats[ITALIC])
pos = star + 1
pos += 1
else:
pos += 1
def find(self, needle, haystack, pos):
while True:
found = haystack.find(needle, pos)
if found < 0:
return -1
else:
if haystack[found - 1] == " ":
pos = found + 1
continue
else:
return found