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

Source Code for Module node_manager_fkie.editor.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.QtCore import QRegExp, Qt 
 35  from python_qt_binding.QtGui import QColor, QFont, QSyntaxHighlighter, QTextCharFormat 
 36   
 37   
38 -class YamlHighlighter(QSyntaxHighlighter):
39 ''' 40 Enabled the syntax highlightning for the yaml files. 41 ''' 42
43 - def __init__(self, parent=None):
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 # create patterns for tags 56 for tag in tagList: 57 self.rules.append((self._create_regexp(tag), self._create_format(Qt.blue))) 58 59 # create pattern for digits 60 self.rules.append((self._create_regexp("\\d+"), self._create_format(QColor(127, 64, 127)))) 61 62 # create pattern for params 63 self.rules.append((self._create_regexp("^\s*[_.\w]*\s*:"), self._create_format(Qt.darkBlue))) 64 65 # create pattern for params 66 self.rules.append((self._create_regexp(":\s*:[_\.\w]*$|:\s*\@[_\.\w]*$"), self._create_format(Qt.darkBlue))) 67 68 # create pattern for list signes 69 self.rules.append((self._create_regexp("^\s*-"), self._create_format(Qt.darkRed, 'bold'))) 70 71 # create pattern for ??? 72 self.rules.append((self._create_regexp("^---$"), self._create_format(Qt.darkRed))) 73 74 # create pattern for braces 75 self.rules.append((self._create_regexp("[\[\]\{\}\,]"), self._create_format(Qt.darkGreen))) 76 77 # create patterns for strings 78 self.rules.append((self._create_regexp("\".*\"|\'.*\'"), self._create_format(Qt.blue))) 79 80 # create patterns for substitutions 81 self.rules.append((self._create_regexp("\\$\\(.*\\)"), self._create_format(QColor(127, 64, 127)))) 82 83 # create patterns for DOCTYPE 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
87 - def highlightBlock(self, text):
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 # mark comment blocks 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
105 - def _create_regexp(self, pattern=''):
106 _regexp = QRegExp() 107 _regexp.setMinimal(True) 108 _regexp.setPattern(pattern) 109 return _regexp
110
111 - def _create_format(self, color, style=''):
112 _format = QTextCharFormat() 113 _format.setForeground(color) 114 if 'bold' in style: 115 _format.setFontWeight(QFont.Bold) 116 else: 117 _format.setFontWeight(QFont.Normal) 118 if 'italic' in style: 119 _format.setFontItalic(True) 120 return _format
121