mirror of
https://github.com/olivierkes/manuskript.git
synced 2024-06-14 00:44:34 +12:00
Basic MMD highlighting
This commit is contained in:
parent
c5feb0fb6f
commit
c92daf32d1
|
@ -1,16 +1,65 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf8 -*-
|
||||
import re
|
||||
|
||||
from PyQt5.QtCore import Qt
|
||||
from PyQt5.QtGui import QTextCharFormat, QFont, QTextCursor, QFontMetrics
|
||||
|
||||
from manuskript.ui.editors.basicHighlighter import basicHighlighter
|
||||
|
||||
|
||||
class MMDHighlighter(basicHighlighter):
|
||||
|
||||
MARKDOWN_REGEX = {
|
||||
'Bold': '(\*\*)(.+?)(\*\*)',
|
||||
'Bold2': '(__)(.+?)(__)',
|
||||
'Italic': '(\*)([^\*].+?[^\*])(\*)',
|
||||
'Italic2': '(_)([^_].+?[^_])(_)',
|
||||
'Title': '^(#+)(\s*)(.*)(#*)',
|
||||
'HTML': '<.+?>',
|
||||
}
|
||||
|
||||
def __init__(self, editor, style="Default"):
|
||||
basicHighlighter.__init__(self, editor)
|
||||
|
||||
self.editor = editor
|
||||
|
||||
def highlightBlock(self, text):
|
||||
basicHighlighter.highlightBlockBefore(self, text)
|
||||
|
||||
# FIXME
|
||||
self.doHighlightBlock(text)
|
||||
|
||||
basicHighlighter.highlightBlockAfter(self, text)
|
||||
|
||||
def doHighlightBlock(self, text):
|
||||
|
||||
fop = QTextCharFormat()
|
||||
fop.setForeground(Qt.lightGray)
|
||||
fb = QTextCharFormat()
|
||||
fb.setFontWeight(QFont.Bold)
|
||||
fi = QTextCharFormat()
|
||||
fi.setFontItalic(True)
|
||||
|
||||
for name, style in [
|
||||
("Italic", fi),
|
||||
("Italic2", fi),
|
||||
("Bold", fb),
|
||||
("Bold2", fb),
|
||||
]:
|
||||
r = re.compile(self.MARKDOWN_REGEX[name])
|
||||
|
||||
for m in r.finditer(text):
|
||||
self.setFormat(m.start(1), len(m.group(1)), fop)
|
||||
self.setFormat(m.start(2), len(m.group(2)), style)
|
||||
self.setFormat(m.start(3), len(m.group(3)), fop)
|
||||
|
||||
fps = self._defaultCharFormat.font().pointSize()
|
||||
|
||||
r = re.compile(self.MARKDOWN_REGEX["Title"])
|
||||
for m in r.finditer(text):
|
||||
fop.setFontPointSize(fps + 12 - 2 * len(m.group(1)))
|
||||
fb.setFontPointSize(fps + 12 - 2 * len(m.group(1)))
|
||||
|
||||
self.setFormat(m.start(1), len(m.group(1)), fop)
|
||||
self.setFormat(m.start(3), len(m.group(3)), fb)
|
||||
self.setFormat(m.start(4), len(m.group(4)), fop)
|
Loading…
Reference in a new issue