mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
improve Header and CodeBlock
This commit is contained in:
parent
9d764697ca
commit
5afea80133
@ -23,14 +23,12 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
'Link': re.compile(r'(?u)(?<![!\\]])\[.*?(?<!\\)\](\[.+?(?<!\\)\]|\(.+?(?<!\\)\))'),
|
'Link': re.compile(r'(?u)(?<![!\\]])\[.*?(?<!\\)\](\[.+?(?<!\\)\]|\(.+?(?<!\\)\))'),
|
||||||
'Image': re.compile(r'(?u)(?<!\\)!\[.*?(?<!\\)\](\[.+?(?<!\\)\]|\(.+?(?<!\\)\))'),
|
'Image': re.compile(r'(?u)(?<!\\)!\[.*?(?<!\\)\](\[.+?(?<!\\)\]|\(.+?(?<!\\)\))'),
|
||||||
'LinkRef': re.compile(r'(?u)^ *\[.*?\]:[ \t]*.*$'),
|
'LinkRef': re.compile(r'(?u)^ *\[.*?\]:[ \t]*.*$'),
|
||||||
'HeaderAtx': re.compile(r'(?u)^\#{1,6}(.*?)\#*(''\n|$)'),
|
'Header': re.compile(r'(?u)^#{1,6}(.*?)$'),
|
||||||
'Header': re.compile('^(.+)[ \t]*\n(=+|-+)[ \t]*\n+'),
|
|
||||||
'CodeBlock': re.compile('^([ ]{4,}|\t).*'),
|
'CodeBlock': re.compile('^([ ]{4,}|\t).*'),
|
||||||
'UnorderedList': re.compile(r'(?u)^\s*(\* |\+ |- )+\s*'),
|
'UnorderedList': re.compile(r'(?u)^\s*(\* |\+ |- )+\s*'),
|
||||||
'UnorderedListStar': re.compile(r'^\s*(\* )+\s*'),
|
'UnorderedListStar': re.compile(r'^\s*(\* )+\s*'),
|
||||||
'OrderedList': re.compile(r'(?u)^\s*(\d+\. )\s*'),
|
'OrderedList': re.compile(r'(?u)^\s*(\d+\. )\s*'),
|
||||||
'BlockQuote': re.compile(r'(?u)^([ ]{0,3}>)+\s*'),
|
'BlockQuote': re.compile(r'(?u)^[ ]{0,3}>+[ \t]?'),
|
||||||
'BlockQuoteCount': re.compile(r'^[ ]{0,3}>[ \t]?'),
|
|
||||||
'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,}$'),
|
||||||
@ -47,7 +45,6 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
'Link': "link",
|
'Link': "link",
|
||||||
'Image': "image",
|
'Image': "image",
|
||||||
'LinkRef': "link",
|
'LinkRef': "link",
|
||||||
'HeaderAtx': "header",
|
|
||||||
'Header': "header",
|
'Header': "header",
|
||||||
'HeaderLine': "header",
|
'HeaderLine': "header",
|
||||||
'CodeBlock': "codeblock",
|
'CodeBlock': "codeblock",
|
||||||
@ -55,7 +52,6 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
'UnorderedListStar': "unorderedlist",
|
'UnorderedListStar': "unorderedlist",
|
||||||
'OrderedList': "orderedlist",
|
'OrderedList': "orderedlist",
|
||||||
'BlockQuote': "blockquote",
|
'BlockQuote': "blockquote",
|
||||||
'BlockQuoteCount': "blockquote",
|
|
||||||
'CodeSpan': "codespan",
|
'CodeSpan': "codespan",
|
||||||
'HR': "line",
|
'HR': "line",
|
||||||
'Html': "html",
|
'Html': "html",
|
||||||
@ -132,7 +128,7 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
if self.highlightHorizontalLine(text, cursor, bf, strt):
|
if self.highlightHorizontalLine(text, cursor, bf, strt):
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.highlightAtxHeader(text, cursor, bf, strt):
|
if self.highlightHeader(text, cursor, bf, strt):
|
||||||
return
|
return
|
||||||
|
|
||||||
self.highlightList(text, cursor, bf, strt)
|
self.highlightList(text, cursor, bf, strt)
|
||||||
@ -156,11 +152,8 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
mo = re.search(self.MARKDOWN_KEYS_REGEX['BlockQuote'],text)
|
mo = re.search(self.MARKDOWN_KEYS_REGEX['BlockQuote'],text)
|
||||||
if mo:
|
if mo:
|
||||||
self.setFormat(mo.start(), mo.end() - mo.start(), self.MARKDOWN_KWS_FORMAT['BlockQuote'])
|
self.setFormat(mo.start(), mo.end() - mo.start(), self.MARKDOWN_KWS_FORMAT['BlockQuote'])
|
||||||
unquote = re.sub(self.MARKDOWN_KEYS_REGEX['BlockQuoteCount'],'',text)
|
spcslen = mo.end()
|
||||||
spcs = re.match(self.MARKDOWN_KEYS_REGEX['BlockQuoteCount'],text)
|
unquote = text[spcslen:]
|
||||||
spcslen = 0
|
|
||||||
if spcs:
|
|
||||||
spcslen = len(spcs.group(0))
|
|
||||||
self.highlightMarkdown(unquote,spcslen)
|
self.highlightMarkdown(unquote,spcslen)
|
||||||
found = True
|
found = True
|
||||||
return found
|
return found
|
||||||
@ -180,7 +173,7 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
prevCursor = QTextCursor(prevBlock)
|
prevCursor = QTextCursor(prevBlock)
|
||||||
prev = prevBlock.text()
|
prev = prevBlock.text()
|
||||||
prevAscii = str(prev.replace('\u2029','\n'))
|
prevAscii = str(prev.replace('\u2029','\n'))
|
||||||
if prevAscii.strip():
|
if strt == 0 and prevAscii.strip():
|
||||||
#print "Its a header"
|
#print "Its a header"
|
||||||
prevCursor.select(QTextCursor.SelectionType.LineUnderCursor)
|
prevCursor.select(QTextCursor.SelectionType.LineUnderCursor)
|
||||||
#prevCursor.setCharFormat(self.MARKDOWN_KWS_FORMAT['Header'])
|
#prevCursor.setCharFormat(self.MARKDOWN_KWS_FORMAT['Header'])
|
||||||
@ -197,13 +190,13 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
found = True
|
found = True
|
||||||
return found
|
return found
|
||||||
|
|
||||||
def highlightAtxHeader(self, text, cursor, bf, strt):
|
def highlightHeader(self, text, cursor, bf, strt):
|
||||||
found = False
|
found = False
|
||||||
for mo in re.finditer(self.MARKDOWN_KEYS_REGEX['HeaderAtx'],text):
|
for mo in re.finditer(self.MARKDOWN_KEYS_REGEX['Header'],text):
|
||||||
#bf.setBackground(QBrush(QColor(7,54,65)))
|
#bf.setBackground(QBrush(QColor(7,54,65)))
|
||||||
#cursor.movePosition(QTextCursor.End)
|
#cursor.movePosition(QTextCursor.End)
|
||||||
#cursor.mergeBlockFormat(bf)
|
#cursor.mergeBlockFormat(bf)
|
||||||
self.setFormat(mo.start()+strt, mo.end() - mo.start(), self.MARKDOWN_KWS_FORMAT['HeaderAtx'])
|
self.setFormat(mo.start()+strt, mo.end() - mo.start(), self.MARKDOWN_KWS_FORMAT['Header'])
|
||||||
found = True
|
found = True
|
||||||
return found
|
return found
|
||||||
|
|
||||||
@ -284,7 +277,7 @@ class MarkdownHighlighter(QSyntaxHighlighter):
|
|||||||
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):
|
||||||
stripped = text.lstrip()
|
stripped = text.lstrip()
|
||||||
if stripped[0] not in ('*','-','+','>'):
|
if stripped[0] not in ('*','-','+','>') and not re.match('\d+\.', stripped):
|
||||||
self.setFormat(mo.start()+strt, mo.end() - mo.start(), self.MARKDOWN_KWS_FORMAT['CodeBlock'])
|
self.setFormat(mo.start()+strt, mo.end() - mo.start(), self.MARKDOWN_KWS_FORMAT['CodeBlock'])
|
||||||
found = True
|
found = True
|
||||||
return found
|
return found
|
||||||
|
Loading…
x
Reference in New Issue
Block a user