Skip to content

Commit

Permalink
Insert bold and italic buttons, create special syntax variables for b…
Browse files Browse the repository at this point in the history
…old, italic and bold-italic and update the function/calls to use them.
  • Loading branch information
hreikin committed Apr 7, 2022
1 parent 02b4eb5 commit a5f3bdc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
4 changes: 3 additions & 1 deletion tkintermd/tkintermd_constants.py
Expand Up @@ -13,6 +13,7 @@
"####", "#####", "######",
)
"""tuple: Markdown syntax to ignore for bold highlighting."""
bold_md_special = ("*","***", "_", "___")
italic_md_syntax = ("*", "_")
"""tuple: Markdown syntax for italic highlighting."""
italic_md_ignore = (
Expand All @@ -22,7 +23,7 @@
"####", "#####", "######",
)
"""tuple: Markdown syntax to ignore for italic highlighting."""
# Needs adjusting.
italic_md_special = ("**","***", "__", "___")
bold_italic_md_syntax = ("***", "___")
"""tuple: Markdown syntax for bold-italic highlighting."""
bold_italic_md_ignore = (
Expand All @@ -32,6 +33,7 @@
"####", "#####", "######",
)
"""tuple: Markdown syntax to ignore for bold-italic highlighting."""
bold_italic_md_special = ("*","**", "_", "__")
strikethrough_md_syntax = ("~~", "~~")
"""tuple: Markdown syntax for strikethrough highlighting."""
strikethrough_md_ignore = (
Expand Down
25 changes: 13 additions & 12 deletions tkintermd/tkintermd_frame.py
Expand Up @@ -59,12 +59,12 @@ def __init__(self, master, **kwargs):
self.paste_btn.pack(side="left", padx=0, pady=0)
self.find_btn = tk.Button(self.top_bar, text="Find", command=self.find)
self.find_btn.pack(side="left", padx=0, pady=0)
# self.bold_btn = tk.Button(self.top_bar, text="Bold")
# self.bold_btn.pack(side="left", padx=0, pady=0)
# self.italic_btn = tk.Button(self.top_bar, text="Italic")
# self.italic_btn.pack(side="left", padx=0, pady=0)
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))
self.bold_btn.pack(side="left", padx=0, pady=0)
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))
self.italic_btn.pack(side="left", padx=0, pady=0)
# # Currently has issues, needs constants adjusting.
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))
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))
self.bold_italic_btn.pack(side="left", padx=0, pady=0)
# self.heading_btn = tk.Button(self.top_bar, text="Heading")
# self.heading_btn.pack(side="left", padx=0, pady=0)
Expand Down Expand Up @@ -375,18 +375,19 @@ def remove_markdown_both_sides(self, selection, md_syntax):
self.text_area.insert(INSERT, self.remove_md)
return

def check_bold_italic(self, md_syntax, md_ignore):
def check_bold_italic(self, md_syntax, md_ignore, md_special):
self.md_syntax = md_syntax
self.md_ignore = md_ignore
self.md_special = md_special
self.cur_selection = self.text_area.selection_get()
# Ignore items in the md_ignore variable and then deal with bold and
# italic syntax individually. If string starts with anything in
# md_ignore do nothing and return from the function.
# Ignore items in the md_ignore variable and then deal with special
# syntax individually. If string starts with anything in md_ignore do
# nothing and return from the function.
if str(self.cur_selection).startswith(self.md_ignore):
return
# If the string already has bold or italic formatting and doesnt start
# with bold-italic formatting do nothing and return from the function.
elif str(self.cur_selection).startswith(("*","**", "_", "__")) and str(self.cur_selection).endswith(("*","**", "_", "__")) and not str(self.cur_selection).startswith(self.md_syntax) and not str(self.cur_selection).startswith(self.md_syntax):
# If the formatting requires special items which can't go in md_ignore
# because they cause issues with markdown being applied incorrectly.
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):
return
# Apply or remove the markdown once we reach this stage.
elif str(self.cur_selection).startswith(self.md_syntax) and str(self.cur_selection).endswith(self.md_syntax):
Expand Down

0 comments on commit a5f3bdc

Please sign in to comment.