Skip to content

Commit c657875

Browse files
committed
Adapt check_bold_italic function to work with other double-sided markdown formatting.
1 parent e3d6dc1 commit c657875

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

tkintermd/constants.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
bold_italic_md_special = ("*","**", "_", "__")
5151
strikethrough_md_syntax = ("~~", "~~")
5252
strikethrough_md_ignore = (
53-
"*", "_", "- ", "> ", "# ", "`",
54-
"**", "__", "--", ">> ", "## ",
55-
"***", "___", "---", ">>> ", "### ", "```", "===",
56-
"####", "#####", "######",
53+
"#", "##", "###", "####",
54+
"#####", "######", "======",
55+
"------", "- ", "* ", "> ", ">> ",
56+
"```", "`",
5757
)
5858
default_md_string = """# Heading 1
5959
## Heading 2

tkintermd/frame.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ def __init__(self, master, **kwargs):
6464
self.paste_btn.pack(side="left", padx=0, pady=0)
6565
self.find_btn = tk.Button(self.top_bar, text="Find", command=self.find)
6666
self.find_btn.pack(side="left", padx=0, pady=0)
67-
self.bold_btn = tk.Button(self.top_bar, text="Bold", command=lambda: self.check_bold_italic(constants.bold_md_syntax, constants.bold_md_ignore, constants.bold_md_special))
67+
self.bold_btn = tk.Button(self.top_bar, text="Bold", command=lambda: self.check_markdown_both_sides(constants.bold_md_syntax, constants.bold_md_ignore, constants.bold_md_special))
6868
self.bold_btn.pack(side="left", padx=0, pady=0)
69-
self.italic_btn = tk.Button(self.top_bar, text="Italic", command=lambda: self.check_bold_italic(constants.italic_md_syntax, constants.italic_md_ignore, constants.italic_md_special))
69+
self.italic_btn = tk.Button(self.top_bar, text="Italic", command=lambda: self.check_markdown_both_sides(constants.italic_md_syntax, constants.italic_md_ignore, constants.italic_md_special))
7070
self.italic_btn.pack(side="left", padx=0, pady=0)
71-
self.bold_italic_btn = tk.Button(self.top_bar, text="Bold Italic", command=lambda: self.check_bold_italic(constants.bold_italic_md_syntax, constants.bold_italic_md_ignore, constants.bold_italic_md_special))
71+
self.bold_italic_btn = tk.Button(self.top_bar, text="Bold Italic", command=lambda: self.check_markdown_both_sides(constants.bold_italic_md_syntax, constants.bold_italic_md_ignore, constants.bold_italic_md_special))
7272
self.bold_italic_btn.pack(side="left", padx=0, pady=0)
73+
self.strikethrough_btn = tk.Button(self.top_bar, text="Strikethrough", command=lambda: self.check_markdown_both_sides(constants.strikethrough_md_syntax, constants.strikethrough_md_ignore, md_special=None, strikethrough=True))
74+
self.strikethrough_btn.pack(side="left", padx=0, pady=0)
7375
# self.heading_btn = tk.Button(self.top_bar, text="Heading")
7476
# self.heading_btn.pack(side="left", padx=0, pady=0)
75-
# self.strikethrough_btn = tk.Button(self.top_bar, text="Strikethrough")
76-
# self.strikethrough_btn.pack(side="left", padx=0, pady=0)
7777
# self.unordered_list_btn = tk.Button(self.top_bar, text="Unordered List")
7878
# self.unordered_list_btn.pack(side="left", padx=0, pady=0)
7979
# self.ordered_list_btn = tk.Button(self.top_bar, text="Ordered List")
@@ -363,13 +363,13 @@ def load_style(self, stylename):
363363
# print(self.style.background_color or 'white', self.text_area.tag_cget("Token.Text", "foreground") or 'black', stylename)
364364
self.text_area.configure(bg=self.style.background_color or 'white',
365365
fg=self.text_area.tag_cget("Token.Text", "foreground") or 'black',
366-
selectbackground=self.style.highlight_color
366+
selectbackground=self.style.highlight_color,
367367
)
368368
self.text_area.tag_configure(str(Generic.StrongEmph), font=('Monospace', 10, 'bold', 'italic'))
369369
self.syntax_highlighting_tags.append(str(Generic.StrongEmph))
370370
self.css = 'body {background-color: %s; color: %s}' % (
371371
self.style.background_color or 'white',
372-
self.text_area.tag_cget("Token.Text", "foreground") or 'black'
372+
self.text_area.tag_cget("Token.Text", "foreground") or 'black',
373373
)# used string%interpolation here because f'string' interpolation is too annoying with embeded { and }
374374
self.preview_area.add_css(self.css)
375375
return self.syntax_highlighting_tags
@@ -419,7 +419,7 @@ def remove_markdown_both_sides(self, selection, md_syntax):
419419
self.text_area.insert(INSERT, self.remove_md)
420420
return
421421

422-
def check_bold_italic(self, md_syntax, md_ignore, md_special):
422+
def check_markdown_both_sides(self, md_syntax, md_ignore, md_special, strikethrough=None):
423423
"""Specific checks for bold, italic and bold-italic markdown syntax.
424424
425425
This will ignore items in the md_ignore variable and then deal with
@@ -446,6 +446,13 @@ def check_bold_italic(self, md_syntax, md_ignore, md_special):
446446
self.cur_selection = self.text_area.selection_get()
447447
if str(self.cur_selection).startswith(self.md_ignore) or str(self.cur_selection).endswith(self.md_ignore):
448448
return
449+
elif strikethrough == True:
450+
if str(self.cur_selection).startswith(self.md_syntax) and str(self.cur_selection).endswith(self.md_syntax):
451+
self.remove_markdown_both_sides(self.cur_selection, self.md_syntax)
452+
return
453+
else:
454+
self.apply_markdown_both_sides(self.cur_selection, self.md_syntax)
455+
return
449456
elif str(self.cur_selection).startswith(self.md_special) and str(self.cur_selection).endswith(self.md_special) and not str(self.cur_selection).startswith(self.md_syntax) and not str(self.cur_selection).startswith(self.md_syntax):
450457
return
451458
elif str(self.cur_selection).startswith(self.md_syntax) and str(self.cur_selection).endswith(self.md_syntax):

0 commit comments

Comments
 (0)