mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
Merge branch 'markdown-highlighter-entity' of https://github.com/un-pogaz/calibre
This commit is contained in:
commit
f06b4cea64
@ -183,7 +183,7 @@ if __name__ == '__main__':
|
|||||||
w.setWindowFlag(Qt.WindowType.Dialog)
|
w.setWindowFlag(Qt.WindowType.Dialog)
|
||||||
w.show()
|
w.show()
|
||||||
w.markdown = '''\
|
w.markdown = '''\
|
||||||
test *italic* **bold** ***bold-italic*** `code` [link](https://calibre-ebook.com) <span style="font-weight: bold; color:red">span</span>
|
normal& *italic&* **bold&** ***bold-italic*** `code` [link](https://calibre-ebook.com) <span style="font-weight: bold; color:red">span</span>
|
||||||
|
|
||||||
> Blockquotes
|
> Blockquotes
|
||||||
|
|
||||||
|
@ -32,7 +32,8 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
'CodeSpan': re.compile(r'(?<!\\)(?P<delim>`+).+?(?P=delim)'),
|
'CodeSpan': re.compile(r'(?<!\\)(?P<delim>`+).+?(?P=delim)'),
|
||||||
'HeaderLine': re.compile(r'(?u)^(-|=)+\s*$'),
|
'HeaderLine': re.compile(r'(?u)^(-|=)+\s*$'),
|
||||||
'HR': re.compile(r'(?u)^(\s*(\*|-|_)\s*){3,}$'),
|
'HR': re.compile(r'(?u)^(\s*(\*|-|_)\s*){3,}$'),
|
||||||
'Html': re.compile(r'(?u)</?[^/\s].*?(?<!\\)>')
|
'Html': re.compile(r'(?u)</?[^/\s].*?(?<!\\)>'),
|
||||||
|
'Entity': re.compile(r'&([A-z]{2,7}|#\d{1,7}|#x[\dA-Fa-f]{1,6});'),
|
||||||
}
|
}
|
||||||
|
|
||||||
key_theme_maps = {
|
key_theme_maps = {
|
||||||
@ -55,6 +56,7 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
'CodeSpan': "codespan",
|
'CodeSpan': "codespan",
|
||||||
'HR': "line",
|
'HR': "line",
|
||||||
'Html': "html",
|
'Html': "html",
|
||||||
|
'Entity': "entity",
|
||||||
}
|
}
|
||||||
|
|
||||||
light_theme = {
|
light_theme = {
|
||||||
@ -70,7 +72,8 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
"codespan": {"color":"#ff5800", "font-weight":"normal", "font-style":"normal"},
|
"codespan": {"color":"#ff5800", "font-weight":"normal", "font-style":"normal"},
|
||||||
"codeblock": {"color":"#ff5800", "font-weight":"normal", "font-style":"normal"},
|
"codeblock": {"color":"#ff5800", "font-weight":"normal", "font-style":"normal"},
|
||||||
"line": {"color":"#2aa198", "font-weight":"normal", "font-style":"normal"},
|
"line": {"color":"#2aa198", "font-weight":"normal", "font-style":"normal"},
|
||||||
"html": {"color":"#c000c0", "font-weight":"normal", "font-style":"normal"}
|
"html": {"color":"#c000c0", "font-weight":"normal", "font-style":"normal"},
|
||||||
|
"entity": {"color":"#006496"},
|
||||||
}
|
}
|
||||||
|
|
||||||
dark_theme = {
|
dark_theme = {
|
||||||
@ -86,7 +89,8 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
"codespan": {"color":"#90ee90", "font-weight":"normal", "font-style":"normal"},
|
"codespan": {"color":"#90ee90", "font-weight":"normal", "font-style":"normal"},
|
||||||
"codeblock": {"color":"#ff9900", "font-weight":"normal", "font-style":"normal"},
|
"codeblock": {"color":"#ff9900", "font-weight":"normal", "font-style":"normal"},
|
||||||
"line": {"color":"#2aa198", "font-weight":"normal", "font-style":"normal"},
|
"line": {"color":"#2aa198", "font-weight":"normal", "font-style":"normal"},
|
||||||
"html": {"color":"#F653A6", "font-weight":"normal", "font-style":"normal"}
|
"html": {"color":"#f653a6", "font-weight":"normal", "font-style":"normal"},
|
||||||
|
"entity": {"color":"#ff82ac"},
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
@ -98,6 +102,16 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
self.theme = theme
|
self.theme = theme
|
||||||
self.MARKDOWN_KWS_FORMAT = {}
|
self.MARKDOWN_KWS_FORMAT = {}
|
||||||
|
|
||||||
|
for k in ['Bold', 'Italic','BoldItalic']:
|
||||||
|
# generate dynamically keys and theme for EntityBold, EntityItalic, EntityBoldItalic
|
||||||
|
t = self.key_theme_maps[k]
|
||||||
|
newtheme = theme['entity'].copy()
|
||||||
|
newtheme.update(theme[t])
|
||||||
|
newthemekey = 'entity'+t
|
||||||
|
newmapkey = 'Entity'+k
|
||||||
|
theme[newthemekey] = newtheme
|
||||||
|
self.key_theme_maps[newmapkey] = newthemekey
|
||||||
|
|
||||||
for k,t in self.key_theme_maps.items():
|
for k,t in self.key_theme_maps.items():
|
||||||
subtheme = theme[t]
|
subtheme = theme[t]
|
||||||
format = QTextCharFormat()
|
format = QTextCharFormat()
|
||||||
@ -141,6 +155,8 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
|
|
||||||
self.highlightImage(text, cursor, bf)
|
self.highlightImage(text, cursor, bf)
|
||||||
|
|
||||||
|
self.highlightEntity(text, cursor, bf)
|
||||||
|
|
||||||
self.highlightCodeSpan(text, cursor, bf)
|
self.highlightCodeSpan(text, cursor, bf)
|
||||||
|
|
||||||
self.highlightCodeBlock(text, cursor, bf)
|
self.highlightCodeBlock(text, cursor, bf)
|
||||||
@ -295,6 +311,24 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
found = True
|
found = True
|
||||||
return found
|
return found
|
||||||
|
|
||||||
|
def highlightEntity(self, text, cursor, bf):
|
||||||
|
found = False
|
||||||
|
for mo in re.finditer(self.MARKDOWN_KEYS_REGEX['Entity'],text):
|
||||||
|
charformat = self.format(self.offset+ mo.start())
|
||||||
|
charbold = charformat.fontWeight() == QFont.Weight.Bold
|
||||||
|
charitalic = charformat.fontItalic()
|
||||||
|
if charbold and charitalic:
|
||||||
|
format = self.MARKDOWN_KWS_FORMAT['EntityBoldItalic']
|
||||||
|
elif charbold and not charitalic:
|
||||||
|
format = self.MARKDOWN_KWS_FORMAT['EntityBold']
|
||||||
|
elif not charbold and charitalic:
|
||||||
|
format = self.MARKDOWN_KWS_FORMAT['EntityItalic']
|
||||||
|
else:
|
||||||
|
format = self.MARKDOWN_KWS_FORMAT['Entity']
|
||||||
|
self.setFormat(self.offset+ mo.start(), mo.end() - mo.start(), format)
|
||||||
|
found = True
|
||||||
|
return found
|
||||||
|
|
||||||
def highlightHtml(self, text):
|
def highlightHtml(self, text):
|
||||||
for mo in re.finditer(self.MARKDOWN_KEYS_REGEX['Html'], text):
|
for mo in re.finditer(self.MARKDOWN_KEYS_REGEX['Html'], text):
|
||||||
self.setFormat(mo.start(), mo.end() - mo.start(), self.MARKDOWN_KWS_FORMAT['Html'])
|
self.setFormat(mo.start(), mo.end() - mo.start(), self.MARKDOWN_KWS_FORMAT['Html'])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user