Package node_manager_fkie :: Module yaml_highlighter
[frames] | no frames]

Source Code for Module node_manager_fkie.yaml_highlighter

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
  4  # based on code of Timo Roehling 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions 
  9  # are met: 
 10  # 
 11  #  * Redistributions of source code must retain the above copyright 
 12  #    notice, this list of conditions and the following disclaimer. 
 13  #  * Redistributions in binary form must reproduce the above 
 14  #    copyright notice, this list of conditions and the following 
 15  #    disclaimer in the documentation and/or other materials provided 
 16  #    with the distribution. 
 17  #  * Neither the name of Fraunhofer nor the names of its 
 18  #    contributors may be used to endorse or promote products derived 
 19  #    from this software without specific prior written permission. 
 20  # 
 21  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 22  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 23  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 24  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 25  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 26  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 27  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 28  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 29  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 30  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 31  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 32  # POSSIBILITY OF SUCH DAMAGE. 
 33   
 34  from python_qt_binding import QtGui 
 35  from python_qt_binding import QtCore 
 36   
37 -class YamlHighlighter(QtGui.QSyntaxHighlighter):
38 ''' 39 Enabled the syntax highlightning for the yaml files. 40 ''' 41
42 - def __init__(self, parent=None):
43 QtGui.QSyntaxHighlighter.__init__(self, parent) 44 self.rules = [] 45 self.commentStart = QtCore.QRegExp("#") 46 self.commentEnd = QtCore.QRegExp("\n|\r") 47 self.commentFormat = QtGui.QTextCharFormat() 48 self.commentFormat.setFontItalic(True) 49 self.commentFormat.setForeground(QtCore.Qt.darkGray) 50 51 f = QtGui.QTextCharFormat() 52 r = QtCore.QRegExp() 53 r.setMinimal(True) 54 f.setFontWeight(QtGui.QFont.Normal) 55 f.setForeground (QtCore.Qt.blue) 56 tagList = ["\\btrue\\b", "\\bfalse\\b"] 57 for tag in tagList: 58 r.setPattern(tag) 59 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 60 61 f.setForeground(QtGui.QColor(127,64,127)) 62 r.setPattern ("\\d+") 63 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 64 65 f.setForeground(QtCore.Qt.darkBlue) 66 r.setPattern("^\s*[_.\w]*\s*:") 67 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 68 69 f.setForeground(QtCore.Qt.darkBlue) 70 r.setPattern(":\s*:[_\.\w]*$|:\s*\@[_\.\w]*$") 71 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 72 73 f.setFontWeight(QtGui.QFont.Bold) 74 f.setForeground(QtCore.Qt.darkRed) 75 r.setPattern("^\s*-") 76 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 77 78 f.setForeground(QtCore.Qt.darkRed) 79 r.setPattern("^---$") 80 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 81 82 f.setForeground(QtCore.Qt.darkGreen) 83 r.setPattern("[\[\]\{\}\,]") 84 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 85 86 f.setFontWeight(QtGui.QFont.Normal) 87 f.setForeground(QtCore.Qt.magenta) 88 r.setPattern("\".*\"|\'.*\'") 89 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 90 91 f.setForeground(QtGui.QColor(127,64,127)) 92 r.setPattern ("\\$\\(.*\\)") 93 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 94 95 f.setForeground (QtCore.Qt.lightGray) 96 r.setPattern ("<!DOCTYPE.*>") 97 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 98 r.setPattern ("<\\?xml.*\\?>") 99 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f)))
100 101
102 - def highlightBlock(self, text):
103 for pattern, format in self.rules: 104 index = pattern.indexIn(text) 105 while index >= 0: 106 length = pattern.matchedLength() 107 self.setFormat(index, length, format) 108 index = pattern.indexIn(text, index + length) 109 110 # mark comment blocks 111 self.setCurrentBlockState(0) 112 startIndex = 0 113 if self.previousBlockState() != 1: 114 startIndex = self.commentStart.indexIn(text) 115 if startIndex >= 0: 116 commentLength = len(text) - startIndex 117 self.setFormat(startIndex, commentLength, self.commentFormat)
118