def highlight_block(self, text):
        """Implement specific highlight for Python."""
        text = to_text_string(text)
        prev_state = tbh.get_state(self.currentBlock().previous())
        if prev_state == self.INSIDE_DQ3STRING:
            offset = -4
            text = r'""" '+text
        elif prev_state == self.INSIDE_SQ3STRING:
            offset = -4
            text = r"''' "+text
        elif prev_state == self.INSIDE_DQSTRING:
            offset = -2
            text = r'" '+text
        elif prev_state == self.INSIDE_SQSTRING:
            offset = -2
            text = r"' "+text
        else:
            offset = 0
            prev_state = self.NORMAL
        
        oedata = None
        import_stmt = None

        self.setFormat(0, len(text), self.formats["normal"])
        
        state = self.NORMAL
        match = self.PROG.search(text)
        while match:
            for key, value in list(match.groupdict().items()):
                if value:
                    start, end = match.span(key)
                    start = max([0, start+offset])
                    end = max([0, end+offset])
                    if key == "uf_sq3string":
                        self.setFormat(start, end-start,
                                       self.formats["string"])
                        state = self.INSIDE_SQ3STRING
                    elif key == "uf_dq3string":
                        self.setFormat(start, end-start,
                                       self.formats["string"])
                        state = self.INSIDE_DQ3STRING
                    elif key == "uf_sqstring":
                        self.setFormat(start, end-start,
                                       self.formats["string"])
                        state = self.INSIDE_SQSTRING
                    elif key == "uf_dqstring":
                        self.setFormat(start, end-start,
                                       self.formats["string"])
                        state = self.INSIDE_DQSTRING
                    else:
                        self.setFormat(start, end-start, self.formats[key])
                        if key == "comment":
                            if text.lstrip().startswith(self.cell_separators):
                                self.found_cell_separators = True
                                oedata = OutlineExplorerData()
                                oedata.text = to_text_string(text).strip()
                                # cell_head: string contaning the first group
                                # of '%'s in the cell header
                                cell_head = re.search(r"%+|$",
                                                      text.lstrip()).group()
                                if cell_head == '':
                                    oedata.cell_level = 0
                                else:
                                    oedata.cell_level = len(cell_head) - 2
                                oedata.fold_level = start
                                oedata.def_type = OutlineExplorerData.CELL
                                oedata.def_name = get_code_cell_name(text)
                            elif self.OECOMMENT.match(text.lstrip()):
                                oedata = OutlineExplorerData()
                                oedata.text = to_text_string(text).strip()
                                oedata.fold_level = start
                                oedata.def_type = OutlineExplorerData.COMMENT
                                oedata.def_name = text.strip()
                        elif key == "keyword":
                            if value in ("def", "class"):
                                match1 = self.IDPROG.match(text, end)
                                if match1:
                                    start1, end1 = match1.span(1)
                                    self.setFormat(start1, end1-start1,
                                                   self.formats["definition"])
                                    oedata = OutlineExplorerData()
                                    oedata.text = to_text_string(text)
                                    oedata.fold_level = (len(text)
                                                         - len(text.lstrip()))
                                    oedata.def_type = self.DEF_TYPES[
                                                        to_text_string(value)]
                                    oedata.def_name = text[start1:end1]
                                    oedata.color = self.formats["definition"]
                            elif value in ("elif", "else", "except", "finally",
                                           "for", "if", "try", "while",
                                           "with"):
                                if text.lstrip().startswith(value):
                                    oedata = OutlineExplorerData()
                                    oedata.text = to_text_string(text).strip()
                                    oedata.fold_level = start
                                    oedata.def_type = \
                                        OutlineExplorerData.STATEMENT
                                    oedata.def_name = text.strip()
                            elif value == "import":
                                import_stmt = text.strip()
                                # color all the "as" words on same line, except
                                # if in a comment; cheap approximation to the
                                # truth
                                if '#' in text:
                                    endpos = text.index('#')
                                else:
                                    endpos = len(text)
                                while True:
                                    match1 = self.ASPROG.match(text, end,
                                                               endpos)
                                    if not match1:
                                        break
                                    start, end = match1.span(1)
                                    self.setFormat(start, end-start,
                                                   self.formats["keyword"])
                    
            match = self.PROG.search(text, match.end())
        
        tbh.set_state(self.currentBlock(), state)
        
        # Use normal format for indentation and trailing spaces.
        self.formats['leading'] = self.formats['normal']
        self.formats['trailing'] = self.formats['normal']
        self.highlight_spaces(text, offset)
        
        if oedata is not None:
            block_nb = self.currentBlock().blockNumber()
            self.outlineexplorer_data[block_nb] = oedata
            self.outlineexplorer_data['found_cell_separators'] = self.found_cell_separators
        if import_stmt is not None:
            block_nb = self.currentBlock().blockNumber()
            self.import_statements[block_nb] = import_stmt
def highlight_block ( self , text ) : text = to_text_string ( text ) prev_state = tbh . get_state ( self . currentBlock ( ) . previous ( ) ) if prev_state == self . INSIDE_DQ3STRING : offset = - 4 text = r'""" ' + text elif prev_state == self . INSIDE_SQ3STRING : offset = - 4 text = r"''' " + text elif prev_state == self . INSIDE_DQSTRING : offset = - 2 text = r'" ' + text elif prev_state == self . INSIDE_SQSTRING : offset = - 2 text = r"' " + text else : offset = 0 prev_state = self . NORMAL oedata = None import_stmt = None self . setFormat ( 0 , len ( text ) , self . formats [ "normal" ] ) state = self . NORMAL match = self . PROG . search ( text ) while match : for key , value in list ( match . groupdict ( ) . items ( ) ) : if value : start , end = match . span ( key ) start = max ( [ 0 , start + offset ] ) end = max ( [ 0 , end + offset ] ) if key == "uf_sq3string" : self . setFormat ( start , end - start , self . formats [ "string" ] ) state = self . INSIDE_SQ3STRING elif key == "uf_dq3string" : self . setFormat ( start , end - start , self . formats [ "string" ] ) state = self . INSIDE_DQ3STRING elif key == "uf_sqstring" : self . setFormat ( start , end - start , self . formats [ "string" ] ) state = self . INSIDE_SQSTRING elif key == "uf_dqstring" : self . setFormat ( start , end - start , self . formats [ "string" ] ) state = self . INSIDE_DQSTRING else : self . setFormat ( start , end - start , self . formats [ key ] ) if key == "comment" : if text . lstrip ( ) . startswith ( self . cell_separators ) : self . found_cell_separators = True oedata = OutlineExplorerData ( ) oedata . text = to_text_string ( text ) . strip ( ) cell_head = re . search ( r"%+|$" , text . lstrip ( ) ) . group ( ) if cell_head == '' : oedata . cell_level = 0 else : oedata . cell_level = len ( cell_head ) - 2 oedata . fold_level = start oedata . def_type = OutlineExplorerData . CELL oedata . def_name = get_code_cell_name ( text ) elif self . OECOMMENT . match ( text . lstrip ( ) ) : oedata = OutlineExplorerData ( ) oedata . text = to_text_string ( text ) . strip ( ) oedata . fold_level = start oedata . def_type = OutlineExplorerData . COMMENT oedata . def_name = text . strip ( ) elif key == "keyword" : if value in ( "def" , "class" ) : match1 = self . IDPROG . match ( text , end ) if match1 : start1 , end1 = match1 . span ( 1 ) self . setFormat ( start1 , end1 - start1 , self . formats [ "definition" ] ) oedata = OutlineExplorerData ( ) oedata . text = to_text_string ( text ) oedata . fold_level = ( len ( text ) - len ( text . lstrip ( ) ) ) oedata . def_type = self . DEF_TYPES [ to_text_string ( value ) ] oedata . def_name = text [ start1 : end1 ] oedata . color = self . formats [ "definition" ] elif value in ( "elif" , "else" , "except" , "finally" , "for" , "if" , "try" , "while" , "with" ) : if text . lstrip ( ) . startswith ( value ) : oedata = OutlineExplorerData ( ) oedata . text = to_text_string ( text ) . strip ( ) oedata . fold_level = start oedata . def_type = OutlineExplorerData . STATEMENT oedata . def_name = text . strip ( ) elif value == "import" : import_stmt = text . strip ( ) if '#' in text : endpos = text . index ( '#' ) else : endpos = len ( text ) while True : match1 = self . ASPROG . match ( text , end , endpos ) if not match1 : break start , end = match1 . span ( 1 ) self . setFormat ( start , end - start , self . formats [ "keyword" ] ) match = self . PROG . search ( text , match . end ( ) ) tbh . set_state ( self . currentBlock ( ) , state ) self . formats [ 'leading' ] = self . formats [ 'normal' ] self . formats [ 'trailing' ] = self . formats [ 'normal' ] self . highlight_spaces ( text , offset ) if oedata is not None : block_nb = self . currentBlock ( ) . blockNumber ( ) self . outlineexplorer_data [ block_nb ] = oedata self . outlineexplorer_data [ 'found_cell_separators' ] = self . found_cell_separators if import_stmt is not None : block_nb = self . currentBlock ( ) . blockNumber ( ) self . import_statements [ block_nb ] = import_stmt
Implement specific highlight for Python.