[PyKDE] QSyntaxHighlighter issues

David Boddie david at boddie.org.uk
Wed Sep 29 14:29:07 BST 2004


On Tue, 28 Sep 2004 17:54:09, Eli Yukelzon wrote: 
 
> On Mon, 27 Sep 2004 12:02:22 +0200, Eli Yukelzon <reflog at gmail.com> wrote: 
 
> > First of all, in case anyone interested, i've attached my small HTML 
> > highliter (maybe somebody needs one). 
 
An interesting example to choose. 
 
> > Now, what I wanted to ask - can anyone explain to me, how can use the 
> > Paragraph highlighting? For example, i have <!-- --> comment marking 
> > which works ok when all the comment is on one line, but when it spans 
> > a few lines, i can a problem. I know I have to use the second 
> > parameter of highlightParagraph, I'm just completly clueless about HOW 
> > :) 
 
You just need to decide on a code to return when you find the start of a 
paragraph spanning element (like <!-- in a multi line comment), and check 
for this code at the beginning of highlightParagraph so that you know that 
you need to look for the end of the element (-->). 
 
> And to keep somebody motivated, i'm offering a GMail invite for a solution 
> :)  
 
I don't need the GMail invite, but maybe you can register with the PyQt 
wiki and add the following code as one of the sample code snippets: 
 
    http://www.diotavelli.net/PyQtWiki/SampleCode 
 
David 
 
Sorry about the style of the class. I went with what you initially wrote 
and mixed it up quite a bit. 
 
Here goes (hope that the reformatting works): 
 
""" HTML tags syntax highlighting """ 
 
from qt import * 
import re 
 
class HTMLSyntax(QSyntaxHighlighter): 
 
    def __init__(self,parent): 
        """ sets up the highliting regex's and their funcs """ 
        QSyntaxHighlighter.__init__(self,parent) 
        # order is important! 
        self.modes = [ 
        {#tag 
            'color':QColor("blue"), 
            
'regex':re.compile(r'(<\s*([A-Z][A-Z0-9]*)[^>]*>)(.*?)(<\s*/\2\s*>)',re.MULTILINE|re.IGNORECASE), 
            'func' :self.tagre, 
            'complete': 1 
        }, 
        {#string inside tag 
            'color':QColor("red"), 
            'regex':re.compile(r'<(?:.*?)=\s*"(.*?)"',re.MULTILINE), 
            'func' :self.strre, 
            'complete': 1 
        }, 
        {#comments 
            'color':QColor("gray"), 
            'regex':re.compile(r'<!--'), 
            'ending':re.compile(r'-->',re.MULTILINE), 
            'func' :self.comre, 
            'complete': 0, 
            'code': 3 
        } 
        ] 
 
    def tagre(self,match,color): 
        self.setFormat(match.start(1),match.end(1)-match.start(1),color) 
        self.setFormat(match.start(4),match.end(4)-match.start(1),color) 
 
    def strre(self,match,color): 
            self.setFormat(match.start(1),match.end(1)-match.start(1),color) 
 
    def comre(self,start,end,color): 
            self.setFormat(start,end-start,color) 
 
    def match_ending(self, mode, start, text): 
        func = mode['func'] 
        match = mode['ending'].search(text, start) 
        if match: 
            end = match.end() 
            func(start, end, mode['color']) 
            return end, 0 
        else: 
            end = len(text) 
            func(start, end, mode["color"]) 
            return end, mode["code"] 
 
    def highlightParagraph (self, text, endStateOfLastPara): 
        t = str(text) 
        i = 0 
        if endStateOfLastPara > 0 and \ 
            endStateOfLastPara <= len(self.modes): 
 
            mode = self.modes[endStateOfLastPara-1] 
            i, code = self.match_ending(mode, i, t) 
            if code != 0: 
                return code 
 
        while i < len(t): 
 
            matches = [] 
            for mode in self.modes: 
                match = mode['regex'].search(t, i) 
                if match: 
                    matches.append( (match.start(), match, mode) ) 
 
            if matches == []: 
                return 0 
 
            # Find the first match. 
            matches.sort() 
            start, match, mode = matches[0] 
            if mode["complete"]: 
                func = mode["func"] 
                func(match, mode["color"]) 
                i = match.end() 
            else: 
                i, code = self.match_ending(mode, match.start(), t) 
 
                if code != 0: 
                    return code 
 
        return 0 
 


___________________________________________________________
$0 Web Hosting with up to 120MB web space, 1000 MB Transfer
10 Personalized POP and Web E-mail Accounts, and much more.
Signup at www.doteasy.com




More information about the PyQt mailing list