eric6/E5XML/HighlightingStylesReader.py

branch
jsonfiles
changeset 8026
d3eacdbcb18b
parent 8022
2da0139f4f91
child 8066
1208cdae96b6
equal deleted inserted replaced
8025:aaad60a23960 8026:d3eacdbcb18b
5 5
6 6
7 """ 7 """
8 Module implementing a class for reading a highlighting styles XML file. 8 Module implementing a class for reading a highlighting styles XML file.
9 """ 9 """
10
11 from PyQt5.QtGui import QColor, QFont
12 10
13 from .Config import highlightingStylesFileFormatVersion 11 from .Config import highlightingStylesFileFormatVersion
14 from .XMLStreamReaderBase import XMLStreamReaderBase 12 from .XMLStreamReaderBase import XMLStreamReaderBase
15 13
16 14
36 34
37 def readXML(self): 35 def readXML(self):
38 """ 36 """
39 Public method to read and parse the XML document. 37 Public method to read and parse the XML document.
40 """ 38 """
39 self.__lexersList = []
40
41 while not self.atEnd(): 41 while not self.atEnd():
42 self.readNext() 42 self.readNext()
43 if self.isStartElement(): 43 if self.isStartElement():
44 if self.name() == "HighlightingStyles": 44 if self.name() == "HighlightingStyles":
45 self.version = self.attribute( 45 self.version = self.attribute(
51 self.__readLexer() 51 self.__readLexer()
52 else: 52 else:
53 self.raiseUnexpectedStartTag(self.name()) 53 self.raiseUnexpectedStartTag(self.name())
54 54
55 self.showErrorMessage() 55 self.showErrorMessage()
56
57 return self.__lexersList
56 58
57 def __readLexer(self): 59 def __readLexer(self):
58 """ 60 """
59 Private method to read the lexer info. 61 Private method to read the lexer info.
60 """ 62 """
61 language = self.attribute("name") 63 language = self.attribute("name")
64 self.__lexersList.append({
65 "name": language,
66 "styles": [],
67 })
62 if language and language in self.lexers: 68 if language and language in self.lexers:
63 lexer = self.lexers[language] 69 lexer = self.lexers[language]
64 else: 70 else:
65 lexer = None 71 lexer = None
66 72
86 if style: 92 if style:
87 style = int(style) 93 style = int(style)
88 substyle = int(self.attribute("substyle", "-1")) 94 substyle = int(self.attribute("substyle", "-1"))
89 # -1 is default for base styles 95 # -1 is default for base styles
90 96
91 # add sub-style if not already there 97 styleDict = {
92 if not lexer.hasStyle(style, substyle): 98 "style": style,
93 substyle = lexer.addSubstyle(style) 99 "substyle": substyle,
100 }
94 101
95 color = self.attribute("color") 102 color = self.attribute("color")
96 if color: 103 if color:
97 color = QColor(color) 104 styleDict["color"] = color
98 else: 105 else:
99 color = lexer.defaultColor(style, substyle) 106 styleDict["color"] = (
100 lexer.setColor(color, style, substyle) 107 lexer.defaultColor(style, substyle).name()
108 )
101 109
102 paper = self.attribute("paper") 110 paper = self.attribute("paper")
103 if paper: 111 if paper:
104 paper = QColor(paper) 112 styleDict["paper"] = paper
105 else: 113 else:
106 paper = lexer.defaultPaper(style, substyle) 114 styleDict["paper"] = (
107 lexer.setPaper(paper, style, substyle) 115 lexer.defaultPaper(style, substyle).name()
116 )
108 117
109 fontStr = self.attribute("font") 118 fontStr = self.attribute("font")
110 if fontStr: 119 if fontStr:
111 font = QFont() 120 styleDict["font"] = fontStr
112 font.fromString(fontStr)
113 else: 121 else:
114 font = lexer.defaultFont(style, substyle) 122 styleDict["font"] = (
115 lexer.setFont(font, style, substyle) 123 lexer.defaultFont(style, substyle).toString()
124 )
116 125
117 eolfill = self.attribute("eolfill") 126 eolfill = self.attribute("eolfill")
118 if eolfill: 127 if eolfill:
119 eolfill = self.toBool(eolfill) 128 eolfill = self.toBool(eolfill)
120 if eolfill is None: 129 if eolfill is None:
121 eolfill = lexer.defaulEolFill(style, substyle) 130 eolfill = lexer.defaulEolFill(style, substyle)
122 else: 131 else:
123 eolfill = lexer.defaulEolFill(style, substyle) 132 eolfill = lexer.defaulEolFill(style, substyle)
124 lexer.setEolFill(eolfill, style, substyle) 133 styleDict["eolfill"] = eolfill
125 134
126 while not self.atEnd(): 135 while not self.atEnd():
127 self.readNext() 136 self.readNext()
128 if self.isStartElement(): 137 if self.isStartElement():
129 if self.name() == "Description" and substyle >= 0: 138 if self.name() == "Description":
130 # description can only be set for sub-styles
131 description = self.readElementText().strip() 139 description = self.readElementText().strip()
132 if not description: 140 if not description:
133 description = lexer.defaultDescription( 141 description = lexer.defaultDescription(
134 style, substyle) 142 style, substyle)
135 lexer.setDescription(description, style, substyle) 143 styleDict["description"] = description
136 elif self.name() == "Words" and substyle >= 0: 144 elif self.name() == "Words":
137 # words can only be set for sub-styles
138 words = self.readElementText().strip() 145 words = self.readElementText().strip()
139 if not words: 146 if not words:
140 words = lexer.defaultWords(style, substyle) 147 words = lexer.defaultWords(style, substyle)
141 lexer.setWords(words, style, substyle) 148 styleDict["words"] = words
142 149
143 if self.isEndElement() and self.name() == "Style": 150 if self.isEndElement() and self.name() == "Style":
151 if "description" not in styleDict:
152 styleDict["description"] = ""
153 if "words" not in styleDict:
154 styleDict["words"] = ""
155 self.__lexersList[-1]["styles"].append(styleDict)
144 return 156 return
145 157
146 while not self.atEnd(): 158 while not self.atEnd():
147 self.readNext() 159 self.readNext()
148 if self.isEndElement() and self.name() == "Style": 160 if self.isEndElement() and self.name() == "Style":

eric ide

mercurial