Merge branch 'fix-markdown-highlighter' of https://github.com/un-pogaz/calibre

This commit is contained in:
Kovid Goyal 2023-04-30 15:19:54 +05:30
commit 0d8a563d2d
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -15,9 +15,11 @@ class MarkdownHighlighter(QSyntaxHighlighter):
MARKDOWN_KEYS_REGEX = { MARKDOWN_KEYS_REGEX = {
'Bold' : re.compile(r'(?P<delim>\*\*)(?P<text>.+)(?P=delim)'), 'Bold' : re.compile(r'(?P<delim>\*\*)(?P<text>.+)(?P=delim)'),
'uBold': re.compile('(?P<delim>__)(?P<text>[^_]{2,})(?P=delim)'), 'uBold': re.compile('(?P<delim>__)(?P<text>.+)(?P=delim)'),
'Italic': re.compile(r'(?P<delim>\*)(?P<text>[^*]{2,})(?P=delim)'), 'Italic': re.compile(r'(?P<delim>\*)(?P<text>([^*]{2,}|[^*]))(?P=delim)'),
'uItalic': re.compile('(?P<delim>_)(?P<text>[^_]+)(?P=delim)'), 'uItalic': re.compile('(?P<delim>_)(?P<text>([^_]{2,}|[^_]))(?P=delim)'),
'BoldItalic': re.compile(r'(?P<delim>\*\*\*)(?P<text>([^*]{2,}|[^*]))(?P=delim)'),
'uBoldItalic': re.compile(r'(?P<delim>___)(?P<text>([^_]{2,}|[^_]))(?P=delim)'),
'Link': re.compile(r'(?u)(^|(?P<pre>[^!]))\[.*?\]:?[ ''\t'r']*\(?[^)]+\)?'), 'Link': re.compile(r'(?u)(^|(?P<pre>[^!]))\[.*?\]:?[ ''\t'r']*\(?[^)]+\)?'),
'Image': re.compile(r'(?u)!\[.*?\]\(.+?\)'), 'Image': re.compile(r'(?u)!\[.*?\]\(.+?\)'),
'HeaderAtx': re.compile(r'(?u)^\#{1,6}(.*?)\#*(''\n|$)'), 'HeaderAtx': re.compile(r'(?u)^\#{1,6}(.*?)\#*(''\n|$)'),
@ -39,6 +41,8 @@ class MarkdownHighlighter(QSyntaxHighlighter):
'uBold': "bold", 'uBold': "bold",
'Italic': "emphasis", 'Italic': "emphasis",
'uItalic': "emphasis", 'uItalic': "emphasis",
'BoldItalic': "boldemphasis",
'uBoldItalic': "boldemphasis",
'Link': "link", 'Link': "link",
'Image': "image", 'Image': "image",
'HeaderAtx': "header", 'HeaderAtx': "header",
@ -58,6 +62,7 @@ class MarkdownHighlighter(QSyntaxHighlighter):
light_theme = { light_theme = {
"bold": {"font-weight":"bold"}, "bold": {"font-weight":"bold"},
"emphasis": {"font-style":"italic"}, "emphasis": {"font-style":"italic"},
"boldemphasis": {"font-weight":"bold", "font-style":"italic"},
"link": {"color":light_link_color.name(), "font-weight":"normal", "font-style":"normal"}, "link": {"color":light_link_color.name(), "font-weight":"normal", "font-style":"normal"},
"image": {"color":"#cb4b16", "font-weight":"normal", "font-style":"normal"}, "image": {"color":"#cb4b16", "font-weight":"normal", "font-style":"normal"},
"header": {"color":"#2aa198", "font-weight":"bold", "font-style":"normal"}, "header": {"color":"#2aa198", "font-weight":"bold", "font-style":"normal"},
@ -73,6 +78,7 @@ class MarkdownHighlighter(QSyntaxHighlighter):
dark_theme = { dark_theme = {
"bold": {"font-weight":"bold"}, "bold": {"font-weight":"bold"},
"emphasis": {"font-style":"italic"}, "emphasis": {"font-style":"italic"},
"boldemphasis": {"font-weight":"bold", "font-style":"italic"},
"link": {"color":dark_link_color.name(), "font-weight":"normal", "font-style":"normal"}, "link": {"color":dark_link_color.name(), "font-weight":"normal", "font-style":"normal"},
"image": {"color":"#cb4b16", "font-weight":"normal", "font-style":"normal"}, "image": {"color":"#cb4b16", "font-weight":"normal", "font-style":"normal"},
"header": {"color":"#2aa198", "font-weight":"bold", "font-style":"normal"}, "header": {"color":"#2aa198", "font-weight":"bold", "font-style":"normal"},
@ -129,16 +135,18 @@ class MarkdownHighlighter(QSyntaxHighlighter):
self.highlightList(text, cursor, bf, strt) self.highlightList(text, cursor, bf, strt)
self.highlightEmphasis(text, cursor, bf, strt)
self.highlightBold(text, cursor, bf, strt)
self.highlightBoldEmphasis(text, cursor, bf, strt)
self.highlightLink(text, cursor, bf, strt) self.highlightLink(text, cursor, bf, strt)
self.highlightImage(text, cursor, bf, strt) self.highlightImage(text, cursor, bf, strt)
self.highlightCodeSpan(text, cursor, bf, strt) self.highlightCodeSpan(text, cursor, bf, strt)
self.highlightEmphasis(text, cursor, bf, strt)
self.highlightBold(text, cursor, bf, strt)
self.highlightCodeBlock(text, cursor, bf, strt) self.highlightCodeBlock(text, cursor, bf, strt)
def highlightBlockQuote(self, text, cursor, bf, strt): def highlightBlockQuote(self, text, cursor, bf, strt):
@ -211,7 +219,9 @@ class MarkdownHighlighter(QSyntaxHighlighter):
def highlightLink(self, text, cursor, bf, strt): def highlightLink(self, text, cursor, bf, strt):
found = False found = False
for mo in re.finditer(self.MARKDOWN_KEYS_REGEX['Link'],text): for mo in re.finditer(self.MARKDOWN_KEYS_REGEX['Link'],text):
self.setFormat(mo.start()+strt, mo.end() - mo.start()-strt, self.MARKDOWN_KWS_FORMAT['Link']) start_bracket = mo.group()[0][0] == '['
self.setFormat(mo.start() + strt + (0 if start_bracket else 1),
mo.end() - mo.start() - strt - (0 if start_bracket else 1), self.MARKDOWN_KWS_FORMAT['Link'])
found = True found = True
return found return found
@ -255,6 +265,17 @@ class MarkdownHighlighter(QSyntaxHighlighter):
found = True found = True
return found return found
def highlightBoldEmphasis(self, text, cursor, bf, strt):
found = False
for mo in re.finditer(self.MARKDOWN_KEYS_REGEX['BoldItalic'],text):
self.setFormat(mo.start()+strt, mo.end() - mo.start()-strt, self.MARKDOWN_KWS_FORMAT['BoldItalic'])
found = True
for mo in re.finditer(self.MARKDOWN_KEYS_REGEX['uBoldItalic'],text):
self.setFormat(mo.start()+strt, mo.end() - mo.start()-strt, self.MARKDOWN_KWS_FORMAT['uBoldItalic'])
found = True
return found
def highlightCodeBlock(self, text, cursor, bf, strt): def highlightCodeBlock(self, text, cursor, bf, strt):
found = False found = False
for mo in re.finditer(self.MARKDOWN_KEYS_REGEX['CodeBlock'],text): for mo in re.finditer(self.MARKDOWN_KEYS_REGEX['CodeBlock'],text):