|
9 | 9 |
|
10 | 10 | from markdown import Markdown
|
11 | 11 | from pygments import lex
|
| 12 | +from pygments.styles import get_all_styles |
12 | 13 | from pygments.lexers.markup import MarkdownLexer
|
13 | 14 | from pygments.token import Generic
|
14 | 15 | from pygments.lexer import bygroups
|
@@ -53,10 +54,10 @@ def __init__(self, master, **kwargs):
|
53 | 54 | self.paste_btn.pack(side="left", padx=0, pady=0)
|
54 | 55 | self.find_btn = tk.Button(self.top_bar, text="Find", command=self.find)
|
55 | 56 | 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) |
60 | 61 | # self.bold_italic_btn = tk.Button(self.top_bar, text="Bold Italic")
|
61 | 62 | # self.bold_italic_btn.pack(side="left", padx=0, pady=0)
|
62 | 63 | # self.heading_btn = tk.Button(self.top_bar, text="Heading")
|
@@ -266,16 +267,46 @@ def check_markdown(self, start='insert linestart', end='insert lineend'):
|
266 | 267 | self.text_area.tag_add(str(t), "range_start", "range_end")
|
267 | 268 | self.text_area.mark_set("range_start", "range_end")
|
268 | 269 |
|
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 |
279 | 310 |
|
280 | 311 | class Lexer(MarkdownLexer):
|
281 | 312 | """Extend MarkdownLexer to add markup for bold-italic. This needs extending
|
|
0 commit comments