1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2008 - 2010 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the handler class for handling a highlighting styles XML file. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtGui import QColor, QFont |
|
11 |
|
12 from .Config import highlightingStylesFileFormatVersion |
|
13 from .XMLHandlerBase import XMLHandlerBase |
|
14 |
|
15 class HighlightingStylesHandler(XMLHandlerBase): |
|
16 """ |
|
17 Class implementing a sax handler to read a highlighting styles file. |
|
18 """ |
|
19 def __init__(self, lexers): |
|
20 """ |
|
21 Constructor |
|
22 |
|
23 @param lexers dictionary of lexer objects for which to import the styles |
|
24 """ |
|
25 XMLHandlerBase.__init__(self) |
|
26 |
|
27 self.lexers = lexers |
|
28 self.lexer = None |
|
29 |
|
30 self.startDocumentSpecific = self.startDocumentHighlightingStyles |
|
31 |
|
32 self.elements.update({ |
|
33 'HighlightingStyles' : (self.startHighlightingStyles, self.defaultEndElement), |
|
34 'Lexer' : (self.startLexer, self.defaultEndElement), |
|
35 'Style' : (self.startStyle, self.defaultEndElement), |
|
36 }) |
|
37 |
|
38 def startDocumentHighlightingStyles(self): |
|
39 """ |
|
40 Handler called, when the document parsing is started. |
|
41 """ |
|
42 self.version = '' |
|
43 |
|
44 ################################################### |
|
45 ## below follow the individual handler functions |
|
46 ################################################### |
|
47 |
|
48 def startHighlightingStyles(self, attrs): |
|
49 """ |
|
50 Handler method for the "HighlightingStyles" start tag. |
|
51 |
|
52 @param attrs list of tag attributes |
|
53 """ |
|
54 self.version = attrs.get('version', highlightingStylesFileFormatVersion) |
|
55 |
|
56 def startLexer(self, attrs): |
|
57 """ |
|
58 Handler method for the "Lexer" start tag. |
|
59 |
|
60 @param attrs list of tag attributes |
|
61 """ |
|
62 language = attrs.get("name", "") |
|
63 if language and language in self.lexers: |
|
64 self.lexer = self.lexers[language] |
|
65 else: |
|
66 self.lexer = None |
|
67 |
|
68 def startStyle(self, attrs): |
|
69 """ |
|
70 Handler method for the "Style" start tag. |
|
71 |
|
72 @param attrs list of tag attributes |
|
73 """ |
|
74 self.buffer = "" |
|
75 |
|
76 if self.lexer is not None: |
|
77 style = attrs.get("style") |
|
78 if style is not None: |
|
79 style = int(style) |
|
80 |
|
81 color = attrs.get("color") |
|
82 if color is None: |
|
83 color = self.lexer.defaultColor(style) |
|
84 else: |
|
85 color = QColor(color) |
|
86 self.lexer.setColor(color, style) |
|
87 |
|
88 paper = attrs.get("paper") |
|
89 if paper is None: |
|
90 paper = self.lexer.defaultPaper(style) |
|
91 else: |
|
92 paper = QColor(paper) |
|
93 self.lexer.setPaper(paper, style) |
|
94 |
|
95 fontStr = attrs.get("font") |
|
96 if fontStr is None: |
|
97 font = self.lexer.defaultFont(style) |
|
98 else: |
|
99 font = QFont() |
|
100 font.fromString(fontStr) |
|
101 self.lexer.setFont(font, style) |
|
102 |
|
103 eolfill = attrs.get("eolfill") |
|
104 if eolfill is None: |
|
105 eolfill = self.lexer.defaulEolFill(style) |
|
106 else: |
|
107 eolfill = int(eolfill) |
|
108 self.lexer.setEolFill(eolfill, style) |
|
109 |
|
110 def getVersion(self): |
|
111 """ |
|
112 Public method to retrieve the version of the shortcuts. |
|
113 |
|
114 @return String containing the version number. |
|
115 """ |
|
116 return self.version |
|