|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2008 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the writer class for writing a highlighting styles XML |
|
8 file. |
|
9 """ |
|
10 |
|
11 from __future__ import unicode_literals |
|
12 |
|
13 import time |
|
14 |
|
15 from .XMLStreamWriterBase import XMLStreamWriterBase |
|
16 from .Config import highlightingStylesFileFormatVersion |
|
17 |
|
18 import Preferences |
|
19 |
|
20 |
|
21 class HighlightingStylesWriter(XMLStreamWriterBase): |
|
22 """ |
|
23 Class implementing the writer class for writing a highlighting styles XML |
|
24 file. |
|
25 """ |
|
26 def __init__(self, device, lexers): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param device reference to the I/O device to write to (QIODevice) |
|
31 @param lexers list of lexer objects for which to export the styles |
|
32 """ |
|
33 XMLStreamWriterBase.__init__(self, device) |
|
34 |
|
35 self.lexers = lexers |
|
36 self.email = Preferences.getUser("Email") |
|
37 |
|
38 def writeXML(self): |
|
39 """ |
|
40 Public method to write the XML to the file. |
|
41 """ |
|
42 XMLStreamWriterBase.writeXML(self) |
|
43 |
|
44 self.writeDTD( |
|
45 '<!DOCTYPE HighlightingStyles SYSTEM' |
|
46 ' "HighlightingStyles-{0}.dtd">'.format( |
|
47 highlightingStylesFileFormatVersion)) |
|
48 |
|
49 # add some generation comments |
|
50 self.writeComment(" Eric6 highlighting styles ") |
|
51 self.writeComment( |
|
52 " Saved: {0}".format(time.strftime('%Y-%m-%d, %H:%M:%S'))) |
|
53 self.writeComment(" Author: {0} ".format(self.email)) |
|
54 |
|
55 # add the main tag |
|
56 self.writeStartElement("HighlightingStyles") |
|
57 self.writeAttribute("version", highlightingStylesFileFormatVersion) |
|
58 |
|
59 for lexer in self.lexers: |
|
60 self.writeStartElement("Lexer") |
|
61 self.writeAttribute("name", lexer.language()) |
|
62 for description, style, substyle in lexer.getStyles(): |
|
63 self.writeStartElement("Style") |
|
64 self.writeAttribute("style", str(style)) |
|
65 self.writeAttribute("substyle", str(substyle)) |
|
66 self.writeAttribute("color", |
|
67 lexer.color(style, substyle).name()) |
|
68 self.writeAttribute("paper", |
|
69 lexer.paper(style, substyle).name()) |
|
70 self.writeAttribute("font", |
|
71 lexer.font(style, substyle).toString()) |
|
72 self.writeAttribute("eolfill", |
|
73 str(lexer.eolFill(style, substyle))) |
|
74 self.writeStartElement("Description") |
|
75 self.writeCharacters(description) |
|
76 self.writeEndElement() # Description |
|
77 if substyle >= 0: |
|
78 self.writeStartElement("Words") |
|
79 self.writeCharacters(lexer.words(style, substyle).strip()) |
|
80 self.writeEndElement() # Words |
|
81 self.writeEndElement() # Style |
|
82 self.writeEndElement() # Lexer |
|
83 |
|
84 self.writeEndElement() # HighlightingStyles |
|
85 self.writeEndDocument() |