14 |
14 |
15 class HighlightingStylesReader(XMLStreamReaderBase): |
15 class HighlightingStylesReader(XMLStreamReaderBase): |
16 """ |
16 """ |
17 Class for reading a highlighting styles XML file. |
17 Class for reading a highlighting styles XML file. |
18 """ |
18 """ |
|
19 |
19 supportedVersions = ["4.3", "6.0"] |
20 supportedVersions = ["4.3", "6.0"] |
20 |
21 |
21 def __init__(self, device, lexers): |
22 def __init__(self, device, lexers): |
22 """ |
23 """ |
23 Constructor |
24 Constructor |
24 |
25 |
25 @param device reference to the I/O device to read from (QIODevice) |
26 @param device reference to the I/O device to read from (QIODevice) |
26 @param lexers dictionary of lexer objects for which to import the |
27 @param lexers dictionary of lexer objects for which to import the |
27 styles |
28 styles |
28 """ |
29 """ |
29 XMLStreamReaderBase.__init__(self, device) |
30 XMLStreamReaderBase.__init__(self, device) |
30 |
31 |
31 self.lexers = lexers |
32 self.lexers = lexers |
32 |
33 |
33 self.version = "" |
34 self.version = "" |
34 |
35 |
35 def readXML(self): |
36 def readXML(self): |
36 """ |
37 """ |
37 Public method to read and parse the XML document. |
38 Public method to read and parse the XML document. |
38 |
39 |
39 @return list of read lexer style definitions |
40 @return list of read lexer style definitions |
40 @rtype list of dict |
41 @rtype list of dict |
41 """ |
42 """ |
42 self.__lexersList = [] |
43 self.__lexersList = [] |
43 |
44 |
44 while not self.atEnd(): |
45 while not self.atEnd(): |
45 self.readNext() |
46 self.readNext() |
46 if self.isStartElement(): |
47 if self.isStartElement(): |
47 if self.name() == "HighlightingStyles": |
48 if self.name() == "HighlightingStyles": |
48 self.version = self.attribute( |
49 self.version = self.attribute( |
49 "version", |
50 "version", highlightingStylesFileFormatVersion |
50 highlightingStylesFileFormatVersion) |
51 ) |
51 if self.version not in self.supportedVersions: |
52 if self.version not in self.supportedVersions: |
52 self.raiseUnsupportedFormatVersion(self.version) |
53 self.raiseUnsupportedFormatVersion(self.version) |
53 elif self.name() == "Lexer": |
54 elif self.name() == "Lexer": |
54 self.__readLexer() |
55 self.__readLexer() |
55 else: |
56 else: |
56 self.raiseUnexpectedStartTag(self.name()) |
57 self.raiseUnexpectedStartTag(self.name()) |
57 |
58 |
58 self.showErrorMessage() |
59 self.showErrorMessage() |
59 |
60 |
60 return self.__lexersList |
61 return self.__lexersList |
61 |
62 |
62 def __readLexer(self): |
63 def __readLexer(self): |
63 """ |
64 """ |
64 Private method to read the lexer info. |
65 Private method to read the lexer info. |
65 """ |
66 """ |
66 language = self.attribute("name") |
67 language = self.attribute("name") |
67 self.__lexersList.append({ |
68 self.__lexersList.append( |
68 "name": language, |
69 { |
69 "styles": [], |
70 "name": language, |
70 }) |
71 "styles": [], |
71 lexer = ( |
72 } |
72 self.lexers[language] |
|
73 if language and language in self.lexers else |
|
74 None |
|
75 ) |
73 ) |
76 |
74 lexer = self.lexers[language] if language and language in self.lexers else None |
|
75 |
77 while not self.atEnd(): |
76 while not self.atEnd(): |
78 self.readNext() |
77 self.readNext() |
79 if self.isEndElement() and self.name() == "Lexer": |
78 if self.isEndElement() and self.name() == "Lexer": |
80 break |
79 break |
81 |
80 |
82 if self.isStartElement(): |
81 if self.isStartElement(): |
83 if self.name() == "Style": |
82 if self.name() == "Style": |
84 self.__readStyle(lexer) |
83 self.__readStyle(lexer) |
85 else: |
84 else: |
86 self.raiseUnexpectedStartTag(self.name()) |
85 self.raiseUnexpectedStartTag(self.name()) |
87 |
86 |
88 def __readStyle(self, lexer): |
87 def __readStyle(self, lexer): |
89 """ |
88 """ |
90 Private method to read the style info. |
89 Private method to read the style info. |
91 |
90 |
92 @param lexer reference to the lexer object |
91 @param lexer reference to the lexer object |
93 """ |
92 """ |
94 if lexer is not None: |
93 if lexer is not None: |
95 style = self.attribute("style") |
94 style = self.attribute("style") |
96 if style: |
95 if style: |
97 style = int(style) |
96 style = int(style) |
98 substyle = int(self.attribute("substyle", "-1")) |
97 substyle = int(self.attribute("substyle", "-1")) |
99 # -1 is default for base styles |
98 # -1 is default for base styles |
100 |
99 |
101 styleDict = { |
100 styleDict = { |
102 "style": style, |
101 "style": style, |
103 "substyle": substyle, |
102 "substyle": substyle, |
104 } |
103 } |
105 |
104 |
106 color = self.attribute("color") |
105 color = self.attribute("color") |
107 if color: |
106 if color: |
108 styleDict["color"] = color |
107 styleDict["color"] = color |
109 else: |
108 else: |
110 styleDict["color"] = ( |
109 styleDict["color"] = lexer.defaultColor(style, substyle).name() |
111 lexer.defaultColor(style, substyle).name() |
110 |
112 ) |
|
113 |
|
114 paper = self.attribute("paper") |
111 paper = self.attribute("paper") |
115 if paper: |
112 if paper: |
116 styleDict["paper"] = paper |
113 styleDict["paper"] = paper |
117 else: |
114 else: |
118 styleDict["paper"] = ( |
115 styleDict["paper"] = lexer.defaultPaper(style, substyle).name() |
119 lexer.defaultPaper(style, substyle).name() |
116 |
120 ) |
|
121 |
|
122 fontStr = self.attribute("font") |
117 fontStr = self.attribute("font") |
123 if fontStr: |
118 if fontStr: |
124 styleDict["font"] = fontStr |
119 styleDict["font"] = fontStr |
125 else: |
120 else: |
126 styleDict["font"] = ( |
121 styleDict["font"] = lexer.defaultFont(style, substyle).toString() |
127 lexer.defaultFont(style, substyle).toString() |
122 |
128 ) |
|
129 |
|
130 eolfill = self.attribute("eolfill") |
123 eolfill = self.attribute("eolfill") |
131 if eolfill: |
124 if eolfill: |
132 eolfill = self.toBool(eolfill) |
125 eolfill = self.toBool(eolfill) |
133 if eolfill is None: |
126 if eolfill is None: |
134 eolfill = lexer.defaulEolFill(style, substyle) |
127 eolfill = lexer.defaulEolFill(style, substyle) |
135 else: |
128 else: |
136 eolfill = lexer.defaulEolFill(style, substyle) |
129 eolfill = lexer.defaulEolFill(style, substyle) |
137 styleDict["eolfill"] = eolfill |
130 styleDict["eolfill"] = eolfill |
138 |
131 |
139 while not self.atEnd(): |
132 while not self.atEnd(): |
140 self.readNext() |
133 self.readNext() |
141 if self.isStartElement(): |
134 if self.isStartElement(): |
142 if self.name() == "Description": |
135 if self.name() == "Description": |
143 description = self.readElementText().strip() |
136 description = self.readElementText().strip() |
144 if not description: |
137 if not description: |
145 description = lexer.defaultDescription( |
138 description = lexer.defaultDescription(style, substyle) |
146 style, substyle) |
|
147 styleDict["description"] = description |
139 styleDict["description"] = description |
148 elif self.name() == "Words": |
140 elif self.name() == "Words": |
149 words = self.readElementText().strip() |
141 words = self.readElementText().strip() |
150 if not words: |
142 if not words: |
151 words = lexer.defaultWords(style, substyle) |
143 words = lexer.defaultWords(style, substyle) |
152 styleDict["words"] = words |
144 styleDict["words"] = words |
153 |
145 |
154 if self.isEndElement() and self.name() == "Style": |
146 if self.isEndElement() and self.name() == "Style": |
155 if "description" not in styleDict: |
147 if "description" not in styleDict: |
156 styleDict["description"] = "" |
148 styleDict["description"] = "" |
157 if "words" not in styleDict: |
149 if "words" not in styleDict: |
158 styleDict["words"] = "" |
150 styleDict["words"] = "" |
159 self.__lexersList[-1]["styles"].append(styleDict) |
151 self.__lexersList[-1]["styles"].append(styleDict) |
160 return |
152 return |
161 |
153 |
162 while not self.atEnd(): |
154 while not self.atEnd(): |
163 self.readNext() |
155 self.readNext() |
164 if self.isEndElement() and self.name() == "Style": |
156 if self.isEndElement() and self.name() == "Style": |
165 break |
157 break |