1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 from python_qt_binding.QtCore import QRegExp, Qt
35 from python_qt_binding.QtGui import QColor, QFont, QSyntaxHighlighter, QTextCharFormat
36
37
39 '''
40 Enabled the syntax highlightning for the yaml files.
41 '''
42
44 QSyntaxHighlighter.__init__(self, parent)
45 self.rules = []
46 self.commentStart = QRegExp("#")
47 self.commentEnd = QRegExp("\n|\r")
48 self.default_format = QTextCharFormat()
49 self.default_format.setForeground(QColor(24, 24, 24))
50 self.commentFormat = QTextCharFormat()
51 self.commentFormat.setFontItalic(True)
52 self.commentFormat.setForeground(Qt.darkGray)
53
54 tagList = ["\\btrue\\b", "\\bfalse\\b"]
55
56 for tag in tagList:
57 self.rules.append((self._create_regexp(tag), self._create_format(Qt.blue)))
58
59
60 self.rules.append((self._create_regexp("\\d+"), self._create_format(QColor(127, 64, 127))))
61
62
63 self.rules.append((self._create_regexp("^\s*[_.\w]*\s*:"), self._create_format(Qt.darkBlue)))
64
65
66 self.rules.append((self._create_regexp(":\s*:[_\.\w]*$|:\s*\@[_\.\w]*$"), self._create_format(Qt.darkBlue)))
67
68
69 self.rules.append((self._create_regexp("^\s*-"), self._create_format(Qt.darkRed, 'bold')))
70
71
72 self.rules.append((self._create_regexp("^---$"), self._create_format(Qt.darkRed)))
73
74
75 self.rules.append((self._create_regexp("[\[\]\{\}\,]"), self._create_format(Qt.darkGreen)))
76
77
78 self.rules.append((self._create_regexp("\".*\"|\'.*\'"), self._create_format(Qt.blue)))
79
80
81 self.rules.append((self._create_regexp("\\$\\(.*\\)"), self._create_format(QColor(127, 64, 127))))
82
83
84 self.rules.append((self._create_regexp("<!DOCTYPE.*>"), self._create_format(Qt.lightGray)))
85 self.rules.append((self._create_regexp("<\\?xml.*\\?>"), self._create_format(Qt.lightGray)))
86
88 self.setFormat(0, len(text), self.default_format)
89 for pattern, form in self.rules:
90 index = pattern.indexIn(text)
91 while index >= 0:
92 length = pattern.matchedLength()
93 self.setFormat(index, length, form)
94 index = pattern.indexIn(text, index + length)
95
96
97 self.setCurrentBlockState(0)
98 startIndex = 0
99 if self.previousBlockState() != 1:
100 startIndex = self.commentStart.indexIn(text)
101 if startIndex >= 0:
102 commentLength = len(text) - startIndex
103 self.setFormat(startIndex, commentLength, self.commentFormat)
104
106 _regexp = QRegExp()
107 _regexp.setMinimal(True)
108 _regexp.setPattern(pattern)
109 return _regexp
110
121