E5XML/HighlightingStylesWriter.py

changeset 593
32c9b36eec53
parent 411
99409cddaaa9
child 791
9ec2ac20e54e
equal deleted inserted replaced
592:3ad07054e658 593:32c9b36eec53
7 Module implementing the writer class for writing a highlighting styles XML file. 7 Module implementing the writer class for writing a highlighting styles XML file.
8 """ 8 """
9 9
10 import time 10 import time
11 11
12 from .XMLWriterBase import XMLWriterBase 12 from .XMLStreamWriterBase import XMLStreamWriterBase
13 from .Config import highlightingStylesFileFormatVersion 13 from .Config import highlightingStylesFileFormatVersion
14 14
15 import Preferences 15 import Preferences
16 16
17 class HighlightingStylesWriter(XMLWriterBase): 17 class HighlightingStylesWriter(XMLStreamWriterBase):
18 """ 18 """
19 Class implementing the writer class for writing a highlighting styles XML file. 19 Class implementing the writer class for writing a highlighting styles XML file.
20 """ 20 """
21 def __init__(self, file, lexers): 21 def __init__(self, device, lexers):
22 """ 22 """
23 Constructor 23 Constructor
24 24
25 @param file open file (like) object for writing 25 @param device reference to the I/O device to write to (QIODevice)
26 @param lexers list of lexer objects for which to export the styles 26 @param lexers list of lexer objects for which to export the styles
27 """ 27 """
28 XMLWriterBase.__init__(self, file) 28 XMLStreamWriterBase.__init__(self, device)
29 29
30 self.lexers = lexers 30 self.lexers = lexers
31 self.email = Preferences.getUser("Email") 31 self.email = Preferences.getUser("Email")
32 32
33 def writeXML(self): 33 def writeXML(self):
34 """ 34 """
35 Public method to write the XML to the file. 35 Public method to write the XML to the file.
36 """ 36 """
37 XMLWriterBase.writeXML(self) 37 XMLStreamWriterBase.writeXML(self)
38 38
39 self._write('<!DOCTYPE HighlightingStyles SYSTEM "HighlightingStyles-{0}.dtd">'\ 39 self.writeDTD('<!DOCTYPE HighlightingStyles SYSTEM "HighlightingStyles-{0}.dtd">'\
40 .format(highlightingStylesFileFormatVersion)) 40 .format(highlightingStylesFileFormatVersion))
41 41
42 # add some generation comments 42 # add some generation comments
43 self._write("<!-- Eric5 highlighting styles -->") 43 self.writeComment(" Eric5 highlighting styles ")
44 self._write("<!-- Saved: {0} -->".format(time.strftime('%Y-%m-%d, %H:%M:%S'))) 44 self.writeComment(" Saved: {0}".format(time.strftime('%Y-%m-%d, %H:%M:%S')))
45 self._write("<!-- Author: {0} -->".format(self.escape("{0}".format(self.email)))) 45 self.writeComment(" Author: {0} ".format(self.email))
46 46
47 # add the main tag 47 # add the main tag
48 self._write('<HighlightingStyles version="{0}">'.format( 48 self.writeStartElement("HighlightingStyles")
49 highlightingStylesFileFormatVersion)) 49 self.writeAttribute("version", highlightingStylesFileFormatVersion)
50 50
51 for lexer in self.lexers: 51 for lexer in self.lexers:
52 self._write(' <Lexer name="{0}">'.format(lexer.language())) 52 self.writeStartElement("Lexer")
53 self.writeAttribute("name", lexer.language())
53 for style in lexer.descriptions: 54 for style in lexer.descriptions:
54 self._write(' <Style style="{0:d}" ' 55 self.writeStartElement("Style")
55 'color="{1}" paper="{2}" font="{3}" eolfill="{4:d}">{5}</Style>'\ 56 self.writeAttribute("style", str(style))
56 .format(style, lexer.color(style).name(), lexer.paper(style).name(), 57 self.writeAttribute("color", lexer.color(style).name())
57 lexer.font(style).toString(), lexer.eolFill(style), 58 self.writeAttribute("paper", lexer.paper(style).name())
58 self.escape(lexer.description(style))) 59 self.writeAttribute("font", lexer.font(style).toString())
59 ) 60 self.writeAttribute("eolfill", str(lexer.eolFill(style)))
60 self._write(' </Lexer>') 61 self.writeCharacters(lexer.description(style))
62 self.writeEndElement()
63 self.writeEndElement()
61 64
62 self._write("</HighlightingStyles>", newline = False) 65 self.writeEndElement()
66 self.writeEndDocument()

eric ide

mercurial