Markdown 文本突出显示性能问题 - Tkinter

Markdown Text Highlighting Performance Issues - Tkinter

概述

我正在尝试在我的项目的文本编辑器中添加 Markdown 语法高亮显示,但是我在制作它时遇到了一些问题 用户证明 可以这么说,同时提高性能友好

基本上,我在追求这个——来自Visual Studio代码的降价:

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

我的解决方案

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

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 等降价语法时检查,并对任何已编辑的标签(标签范围内的密钥发布)进行验证。但是用户输入有很多变量需要考虑——比如当文本被粘贴到编辑器中时,因为很难确定某些语法组合可能对周围文档降价产生什么影响——这些需要检查和已验证。

有没有更好更直观的markdown高亮方法我还没想到?有没有办法大大加快我最初的想法?或者 python 和 Tkinter 根本无法足够快地完成我想做的事情。

提前致谢。

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

想法是让pygments (official documentation here)为我们完成工作,使用pygments.lex(text, lexer)解析文本,其中lexer是pygments的Markdown语法的lexer。这个函数 returns (token, text) 对的列表,所以我使用 str(token) 作为标签名称,例如标签“Token.Generic.Strong”对应于粗体文本。为了避免一个一个地配置标签,我使用了一种预定义的 pygments 样式,我使用 load_style() 函数加载它。

不幸的是,pygments 的 markdown 词法分析器无法识别粗斜体,所以我定义了一个自定义 Lexer class 来扩展 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() 绑定到仅某些键或选择仅在用户更改行时应用语法突出显示。

如果您不想使用外部库并保持代码简单,使用 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 在同一行时有效,否则您需要拆分文本并逐行搜索。