-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
markdown_editor.py
176 lines (138 loc) · 3.84 KB
/
markdown_editor.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
# Imports
from tkinter import *
import ctypes
import re
# Increas Dots Per inch so it looks sharper
ctypes.windll.shcore.SetProcessDpiAwareness(True)
# Setup
root = Tk()
root.title('Markdown Editor')
root.geometry('1000x600')
# Setting the Font globaly
root.option_add('*Font', 'Courier 15')
def changes(event=None):
display['state'] = NORMAL
# Clear the Display Area
display.delete(1.0, END)
# Insert the content of the Edit Area into the Display Area
text = editor.get('1.0', END)
# Save Raw Text for later
textRaw = text
# Remove Unwanted Characters
text = ''.join(text.split('#'))
text = ''.join(text.split('*'))
display.insert(1.0, text)
# Loop through each replacement, unpacking it fully
for pattern, name, fontData, colorData, offset in replacements:
# Get the location indices of the given pattern
locations = search_re(pattern, textRaw, offset)
print(f'{name} at {locations}')
# Add tags where the search_re function found the pattern
for start, end in locations:
display.tag_add(name, start, end)
# Configure the tag to use the specified font and color
# to this every time to delete the previous tags
display.tag_config(name, font=fontData, foreground=colorData)
display['state'] = DISABLED
def search_re(pattern, text, offset):
matches = []
text = text.splitlines()
for i, line in enumerate(text):
for match in re.finditer(pattern, line):
matches.append(
(f"{i + 1}.{match.start()}", f"{i + 1}.{match.end() - offset}")
)
return matches
# Convert an RGB tuple to a HEX string using the % Operator
# 02 means print 2 characters
# x means hexadecimal
def rgbToHex(rgb):
return "#%02x%02x%02x" % rgb
# Style Setup
editorBackground = rgbToHex((40, 40, 40))
editorTextColor = rgbToHex((230, 230, 230))
displayBackground = rgbToHex((60, 60, 60))
displayTextColor = rgbToHex((200, 200, 200))
caretColor = rgbToHex((255, 255, 255))
# Width of the Textareas in characters
width = 10
# Fonts
editorfontName = 'Courier'
displayFontName = 'Calibri'
# Font Sizes
normalSize = 15
h1Size = 40
h2Size = 30
h3Size = 20
# font Colors
h1Color = rgbToHex((240, 240, 240))
h2Color = rgbToHex((200, 200, 200))
h3Color = rgbToHex((160, 160, 160))
# Replacements tell us were to insert tags with the font and colors given
replacements = [
[
'^#[a-zA-Z\s\d\?\!\.]+$',
'Header 1',
f'{displayFontName} {h1Size}',
h1Color,
0
], [
'^##[a-zA-Z\s\d\?\!\.]+$',
'Header 2',
f'{displayFontName} {h2Size}',
h2Color,
0
], [
'^###[a-zA-Z\s\d\?\!\.]+$',
'Header 3',
f'{displayFontName} {h3Size}',
h3Color,
0
], [
'\*.+?\*',
'Bold',
f'{displayFontName} {normalSize} bold',
displayTextColor,
2
],
]
# Making the Editor Area
editor = Text(
root,
height=5,
width=width,
bg=editorBackground,
fg=editorTextColor,
border=30,
relief=FLAT,
insertbackground=caretColor
)
editor.pack(expand=1, fill=BOTH, side=LEFT)
# Bind <KeyReleas> so every change is registered
editor.bind('<KeyRelease>', changes)
editor.focus_set()
# Insert a starting text
editor.insert(INSERT, """#Heading 1
##Heading 2
###Heading 3
This is a *bold* move!
- Markdown Editor -
""")
# Making the Display Area
display = Text(
root,
height=5,
width=width,
bg=displayBackground,
fg=displayTextColor,
border=30,
relief=FLAT,
font=f"{displayFontName} {normalSize}",
)
display.pack(expand=1, fill=BOTH, side=LEFT)
# Disable the Display Area so the user cant write in it
# We will have to toggle it so we can insert text
display['state'] = DISABLED
# Starting the Application
changes()
root.mainloop()