【问题标题】:Markdown Text Highlighting Performance Issues - TkinterMarkdown 文本突出显示性能问题 - Tkinter
【发布时间】:2024-04-25 06:15:02
【问题描述】:

概述

我正在尝试在我的项目的文本编辑器中添加 markdown 语法突出显示,但我在使其用户证明可以这么说时遇到了一些问题,同时对性能友好

基本上,我追求的是这个——来自 Visual Studio Code 的降价:

我说的是粗体、斜体、列表等的简单突出显示,以指示用户预览其 Markdown 文件时将应用的样式。

我的解决方案

我最初为我的项目设置了这种方法(针对问题进行了简化并使用颜色使样式更清晰以进行调试)

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 的所有格式,然后重新扫描当前行,它可以减少语法被误解的数量,例如粗体斜体被误解为粗体或斜体,以及标签堆叠在一起。这适用于一行中的几个句子,但如果用户在一行中输入大量文本,性能会迅速下降,需要等待很长时间才能应用样式——尤其是在涉及许多不同的降价语法时。

我使用 Visual Studio Code 的 markdown 语言突出显示作为比较,它可以在一行上处理更多的语法,然后再出于“性能原因”删除突出显示。

我知道每个 keyReleaee 都需要大量循环,但我发现替代方案要复杂得多,同时并不能真正提高性能。

替代解决方案

我想,让我们减少负载。我已经测试了每次用户键入诸如星号和 m-dashes 之类的降价语法时的检查,并对任何已编辑的标签(标签范围内的键释放)进行验证。但是用户输入需要考虑很多变量——比如将文本粘贴到编辑器中时,因为很难确定某些语法组合对周围文档降价的影响——这些都需要检查和验证。

有没有更好,更直观的方法来突出我还没有想到的 Markdown?有没有办法大大加快我最初的想法?还是 python 和 Tkinter 根本无法足够快地完成我想做的事情。

提前致谢。

【问题讨论】:

    标签: python-3.x regex tkinter markdown tkinter-text


    【解决方案1】:

    如果您不想使用外部库并保持代码简单,使用re.finditer() 似乎比Text.search() 更快。

    您可以使用单个正则表达式来匹配所有情况:

    regexp = re.compile(r"((?P<delimiter>\*{1,3})[^*]+?(?P=delimiter)|(?P<delimiter2>\_{1,3})[^_]+?(?P=delimiter2))")
    

    “分隔符”组的长度为您提供标记,匹配的范围为您提​​供应用标记的位置。

    代码如下:

    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())
    
    # 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
    
    regexp = re.compile(r"((?P<delimiter>\*{1,3})[^*]+?(?P=delimiter)|(?P<delimiter2>\_{1,3})[^_]+?(?P=delimiter2))")
    tags = {1: "italic", 2: "bold", 3: "bold-italic"}  # the length of the delimiter gives the tag
    
    
    def check_markdown(start_index="insert linestart", end_index="insert lineend"):
        text = editor.get(start_index, end_index)
        # remove all tag instances
        for tag in tags.values():
            editor.tag_remove(tag, start_index, end_index)
        # loop through each match and add the corresponding tag
        for match in regexp.finditer(text):
            groupdict = match.groupdict()
            delim = groupdict["delimiter"] # * delimiter
            if delim is None:
                delim = groupdict["delimiter2"]  # _ delimiter
            start, end = match.span()
            editor.tag_add(tags[len(delim)], f"{start_index}+{start}c", f"{start_index}+{end}c")
        return
    
    root.mainloop()
    

    请注意,check_markdown() 仅在 start_indexend_index 在同一行时有效,否则您需要拆分文本并逐行进行搜索。

    【讨论】:

    • 效果很好,非常感谢!我最初想用 Text.get() 将文本从编辑器中取出并使用 re 模块在字符串上运行正则表达式,但不确定如何将找到的索引转换回 Tkinters line.character 格式,我现在明白了,我可以在开头使用 +*characters*c,我真傻!
    【解决方案2】:

    我不知道这个解决方案是否提高了性能,但至少它提高了语法高亮。

    这个想法是让pygments(官方文档here)为我们完成这项工作,使用pygments.lex(text, lexer)来解析文本,其中lexer是pygments的Markdown语法词法分析器。此函数返回(令牌,文本)对的列表,因此我使用 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() 仅绑定到某些键,或者选择仅在用户换行时应用语法突出显示。

    【讨论】:

    • 如果pygments 有一个很好的文档就更好了。