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