python-3.x - Markdown 文本突出显示性能问题 - Tkinter
问题描述
概述
我正在尝试在我的项目的文本编辑器中添加 markdown 语法突出显示,但我在使其用户证明可以这么说时遇到了一些问题,同时对性能友好
基本上,我在这之后——来自 Visual Studio Code 的降价:
我说的是粗体、斜体、列表等的简单突出显示,以指示用户预览其降价文件时将应用的样式。
我的解决方案
我最初为我的项目设置了此方法(针对问题进行了简化并使用颜色使样式更清晰以进行调试)
import re
import tkinter
root = tkinter.Tk()
root.title("Markdown Text Editor")
editor = tkinter.Text(root)
editor.pack()
# bind each key Release to the markdown checker function
editor.bind("<KeyRelease>", lambda event : check_markdown(editor.index('insert').split(".")[0]))
# configure markdown styles
editor.tag_config("bold", foreground = "#FF0000") # red for debugging clarity
editor.tag_config("italic", foreground = "#00FF00") # green for debugging clarity
editor.tag_config("bold-italic", foreground = "#0000FF") # blue for debugging clarity
# regex expressions and empty tag legnth
search_expressions = {
# <tag name> <regex expression> <empty tag size>
"italic" : ["\*(.*?)\*", 2],
"bold" : ["\*\*(.*?)\*\*", 4],
"bold-italic" : ["\*\*\*(.*?)\*\*\*", 6],
}
def check_markdown(current_line):
# loop through each tag with the matching regex expression
for tag, expression in search_expressions.items():
# start and end indices for the seach area
start_index, end_index = f"{current_line}.0", f"{current_line}.end"
# remove all tag instances
editor.tag_remove(tag, start_index, end_index)
# while there is still text to search
while 1:
length = tkinter.IntVar()
# get the index of 'tag' that matches 'expression' on the 'current_line'
index = editor.search(expression[0], start_index, count = length, stopindex = end_index, regexp = True)
# break if the expression was not met on the current line
if not index:
break
# else is this tag empty ('**' <- empty italic)
elif length.get() != expression[1]:
# apply the tag to the markdown syntax
editor.tag_add(tag, index, f"{index}+{length.get()}c")
# continue searching after the markdown
start_index = index + f"+{length.get()}c"
# update the display - stops program freezing
root.update_idletasks()
continue
continue
return
root.mainloop()
我推断,通过删除每个 KeyRelease 的所有格式,然后重新扫描当前行,它减少了被误解的语法数量,如粗体斜体为粗体或斜体,以及标签堆叠在一起。这适用于一行上的几个句子,但如果用户在一行上输入大量文本,性能会迅速下降,需要等待很长时间才能应用样式——尤其是当涉及到许多不同的 markdown 语法时。
我使用 Visual Studio Code 的 markdown 语言突出显示作为比较,它可以在一行上处理更多的语法,然后再出于“性能原因”删除突出显示。
我知道这是对每个 keyReleaee 进行的大量循环,但我发现替代方案要复杂得多,同时并没有真正提高性能。
替代解决方案
我想,让我们减少负载。每次用户键入诸如星号和 m-dashes 之类的降价语法时,我都测试过检查,并对已编辑的任何标签(标签范围内的键释放)进行验证。但是用户输入需要考虑很多变量——比如将文本粘贴到编辑器中时,因为很难确定某些语法组合对周围文档降价的影响——这些都需要检查和验证。
有没有更好更直观的方法来突出我还没有想到的降价?有没有办法大大加快我最初的想法?还是 python 和 Tkinter 根本无法足够快地完成我想做的事情。
提前致谢。
解决方案
我不知道这个解决方案是否提高了性能,但至少它提高了语法突出显示。
这个想法是让pygments (这里的官方文档)为我们完成这项工作,pygments.lex(text, lexer)
用于解析文本,其中 lexer 是 pygments 用于 Markdown 语法的词法分析器。这个函数返回一个(token,text)对的列表,所以我用str(token)
它作为标签名称,例如标签“Token.Generic.Strong”对应于粗体文本。为避免一一配置标签,我使用了随load_style()
函数加载的预定义 pygments 样式之一。
不幸的是,pygments 的降价词法分析器不能识别粗斜体,所以我定义了一个自定义Lexer
类来扩展 pygments 的类。
import tkinter
from pygments import lex
from pygments.lexers.markup import MarkdownLexer
from pygments.token import Generic
from pygments.lexer import bygroups
from pygments.styles import get_style_by_name
# add markup for bold-italic
class Lexer(MarkdownLexer):
tokens = {key: val.copy() for key, val in MarkdownLexer.tokens.items()}
# # bold-italic fenced by '***'
tokens['inline'].insert(2, (r'(\*\*\*[^* \n][^*\n]*\*\*\*)',
bygroups(Generic.StrongEmph)))
# # bold-italic fenced by '___'
tokens['inline'].insert(2, (r'(\_\_\_[^_ \n][^_\n]*\_\_\_)',
bygroups(Generic.StrongEmph)))
def load_style(stylename):
style = get_style_by_name(stylename)
syntax_highlighting_tags = []
for token, opts in style.list_styles():
kwargs = {}
fg = opts['color']
bg = opts['bgcolor']
if fg:
kwargs['foreground'] = '#' + fg
if bg:
kwargs['background'] = '#' + bg
font = ('Monospace', 10) + tuple(key for key in ('bold', 'italic') if opts[key])
kwargs['font'] = font
kwargs['underline'] = opts['underline']
editor.tag_configure(str(token), **kwargs)
syntax_highlighting_tags.append(str(token))
editor.configure(bg=style.background_color,
fg=editor.tag_cget("Token.Text", "foreground"),
selectbackground=style.highlight_color)
editor.tag_configure(str(Generic.StrongEmph), font=('Monospace', 10, 'bold', 'italic'))
syntax_highlighting_tags.append(str(Generic.StrongEmph))
return syntax_highlighting_tags
def check_markdown(start='insert linestart', end='insert lineend'):
data = editor.get(start, end)
while data and data[0] == '\n':
start = editor.index('%s+1c' % start)
data = data[1:]
editor.mark_set('range_start', start)
# clear tags
for t in syntax_highlighting_tags:
editor.tag_remove(t, start, "range_start +%ic" % len(data))
# parse text
for token, content in lex(data, lexer):
editor.mark_set("range_end", "range_start + %ic" % len(content))
for t in token.split():
editor.tag_add(str(t), "range_start", "range_end")
editor.mark_set("range_start", "range_end")
root = tkinter.Tk()
root.title("Markdown Text Editor")
editor = tkinter.Text(root, font="Monospace 10")
editor.pack()
lexer = Lexer()
syntax_highlighting_tags = load_style("monokai")
# bind each key Release to the markdown checker function
editor.bind("<KeyRelease>", lambda event: check_markdown())
root.mainloop()
为了提高性能,您可以仅绑定check_markdown()
到某些键或选择仅在用户更改行时应用语法突出显示。
推荐阅读
- kubernetes - nginx 入口控制器 pod 是否应该能够访问其他 pod?
- javascript - 如果项目相同,则自动添加数量
- reactjs - React/Next js,根据索引在按钮上设置活动类
- python - 添加 。打印(时间) - python
- wireshark - 使用 Wireshark 跟踪 OSI 数据包
- javascript - 一页上的多个 Swiper 实例
- laravel - laravel中的delete方法在null中接收id参数
- android - 左包存储位置在哪里?
- c++ - 左键单击 - 检测单击时循环
- javascript - 我在 Github 页面中部署我的 React 应用程序时遇到问题,它显示了一个空白页面