Skip to content

Commit 947500b

Browse files
committed
Function that applies formatting to either side of a selection based on 2 tuples passed
into the function when called. Link up bold and italic buttons to function.
1 parent 9a60e80 commit 947500b

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

tkintermd/tkintermd_frame.py

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from markdown import Markdown
1111
from pygments import lex
12+
from pygments.styles import get_all_styles
1213
from pygments.lexers.markup import MarkdownLexer
1314
from pygments.token import Generic
1415
from pygments.lexer import bygroups
@@ -53,10 +54,10 @@ def __init__(self, master, **kwargs):
5354
self.paste_btn.pack(side="left", padx=0, pady=0)
5455
self.find_btn = tk.Button(self.top_bar, text="Find", command=self.find)
5556
self.find_btn.pack(side="left", padx=0, pady=0)
56-
# self.bold_btn = tk.Button(self.top_bar, text="Bold")
57-
# self.bold_btn.pack(side="left", padx=0, pady=0)
58-
# self.italic_btn = tk.Button(self.top_bar, text="Italic")
59-
# self.italic_btn.pack(side="left", padx=0, pady=0)
57+
self.bold_btn = tk.Button(self.top_bar, text="Bold", command=lambda: self.apply_markdown_both_sides(constants.bold_md_syntax, constants.bold_md_ignore))
58+
self.bold_btn.pack(side="left", padx=0, pady=0)
59+
self.italic_btn = tk.Button(self.top_bar, text="Italic", command=lambda: self.apply_markdown_both_sides(constants.italic_md_syntax, constants.italic_md_ignore))
60+
self.italic_btn.pack(side="left", padx=0, pady=0)
6061
# self.bold_italic_btn = tk.Button(self.top_bar, text="Bold Italic")
6162
# self.bold_italic_btn.pack(side="left", padx=0, pady=0)
6263
# self.heading_btn = tk.Button(self.top_bar, text="Heading")
@@ -266,16 +267,46 @@ def check_markdown(self, start='insert linestart', end='insert lineend'):
266267
self.text_area.tag_add(str(t), "range_start", "range_end")
267268
self.text_area.mark_set("range_start", "range_end")
268269

269-
# def bold(self):
270-
# try:
271-
# self.cur_selection = self.text_area.selection_get()
272-
# print(self.cur_selection)
273-
# self.bold_selection = f"**{self.cur_selection}**"
274-
# self.text_area.delete(index1=SEL_FIRST, index2=SEL_LAST)
275-
# self.text_area.insert(INSERT, self.bold_selection)
276-
# except:
277-
# # self.text_area.insert(INSERT, "****")
278-
# pass
270+
def apply_markdown_both_sides(self, md_syntax, md_ignore):
271+
"""
272+
A generic, catch-all attempt to apply and remove markdown from either
273+
side of a selection.
274+
275+
:param md_syntax: Tuple of markdown strings to apply/remove.
276+
:param md_ignore: Tuple of markdown strings to ignore.
277+
"""
278+
self.md_syntax = md_syntax
279+
self.md_ignore = md_ignore
280+
try:
281+
self.cur_selection = self.text_area.selection_get()
282+
if len(self.md_syntax) == 2 and self.md_syntax[0] == "**" and self.md_syntax[1] == "__" and str(self.cur_selection)[1] != "*" and str(self.cur_selection)[1] != "_":
283+
if str(self.cur_selection).startswith(self.md_syntax) == False and str(self.cur_selection).startswith(self.md_ignore) == False and str(self.cur_selection)[0] != "*" and str(self.cur_selection)[0] != "_":
284+
self.with_md_selection = f"{self.md_syntax[0]}{self.cur_selection}{self.md_syntax[0]}"
285+
self.text_area.delete(index1=SEL_FIRST, index2=SEL_LAST)
286+
self.text_area.insert(INSERT, self.with_md_selection)
287+
return
288+
else:
289+
return
290+
if str(self.cur_selection).startswith(self.md_syntax) == True and str(self.cur_selection).endswith(self.md_syntax) == True and str(self.cur_selection).startswith(self.md_ignore) == False:
291+
self.without_md_selection = str(self.cur_selection).replace(self.md_syntax[0], "").replace(self.md_syntax[1], "")
292+
self.text_area.delete(index1=SEL_FIRST, index2=SEL_LAST)
293+
self.text_area.insert(INSERT, self.without_md_selection)
294+
return
295+
elif str(self.cur_selection).startswith(self.md_syntax) == True and str(self.cur_selection).startswith(self.md_ignore) == True:
296+
return
297+
elif str(self.cur_selection).startswith(self.md_syntax) == True and str(self.cur_selection).startswith(self.md_ignore) == False:
298+
self.without_md_selection = str(self.cur_selection).replace(self.md_syntax[0], "").replace(self.md_syntax[1], "")
299+
self.text_area.delete(index1=SEL_FIRST, index2=SEL_LAST)
300+
self.text_area.insert(INSERT, self.without_md_selection)
301+
return
302+
elif str(self.cur_selection).startswith(self.md_syntax) == False and str(self.cur_selection).startswith(self.md_ignore) == False:
303+
self.with_md_selection = f"{self.md_syntax[0]}{self.cur_selection}{self.md_syntax[0]}"
304+
self.text_area.delete(index1=SEL_FIRST, index2=SEL_LAST)
305+
self.text_area.insert(INSERT, self.with_md_selection)
306+
return
307+
except:
308+
print("EXCEPTION: Application/removal of markdown formatting failed.")
309+
pass
279310

280311
class Lexer(MarkdownLexer):
281312
"""Extend MarkdownLexer to add markup for bold-italic. This needs extending

0 commit comments

Comments
 (0)