58 return "{0:d}.{1:d}, {2:d}.{3:d}, {4:d}.{5:d}".format( |
58 return "{0:d}.{1:d}, {2:d}.{3:d}, {4:d}.{5:d}".format( |
59 r // 10, r % 10, g // 10, g % 10, b // 10, b % 10) |
59 r // 10, r % 10, g // 10, g % 10, b // 10, b % 10) |
60 |
60 |
61 def __texStyle(self, style): |
61 def __texStyle(self, style): |
62 """ |
62 """ |
63 Private method to calculate a style name string for a given style number. |
63 Private method to calculate a style name string for a given style |
|
64 number. |
64 |
65 |
65 @param style style number (integer) |
66 @param style style number (integer) |
66 @return style name string (string) |
67 @return style name string (string) |
67 """ |
68 """ |
68 buf = "" |
69 buf = "" |
83 @param paper the background color to be used (QColor) |
84 @param paper the background color to be used (QColor) |
84 @param file reference to the open file to write to (file object) |
85 @param file reference to the open file to write to (file object) |
85 @param istyle style number (integer) |
86 @param istyle style number (integer) |
86 """ |
87 """ |
87 closing_brackets = 3 |
88 closing_brackets = 3 |
88 file.write("\\newcommand{{\\eric{0}}}[1]{{\\noindent{{\\ttfamily{{".format( |
89 file.write( |
89 self.__texStyle(istyle))) |
90 "\\newcommand{{\\eric{0}}}[1]{{\\noindent{{\\ttfamily{{".format( |
|
91 self.__texStyle(istyle))) |
90 if font.italic(): |
92 if font.italic(): |
91 file.write("\\textit{") |
93 file.write("\\textit{") |
92 closing_brackets += 1 |
94 closing_brackets += 1 |
93 if font.bold(): |
95 if font.bold(): |
94 file.write("\\textbf{") |
96 file.write("\\textbf{") |
95 closing_brackets += 1 |
97 closing_brackets += 1 |
96 if color != self.defaultColor: |
98 if color != self.defaultColor: |
97 file.write("\\textcolor[rgb]{{{0}}}{{".format(self.__getTexRGB(color))) |
99 file.write( |
|
100 "\\textcolor[rgb]{{{0}}}{{".format(self.__getTexRGB(color))) |
98 closing_brackets += 1 |
101 closing_brackets += 1 |
99 if paper != self.defaultPaper: |
102 if paper != self.defaultPaper: |
100 file.write("\\colorbox[rgb]{{{0}}}{{".format(self.__getTexRGB(paper))) |
103 file.write( |
|
104 "\\colorbox[rgb]{{{0}}}{{".format(self.__getTexRGB(paper))) |
101 closing_brackets += 1 |
105 closing_brackets += 1 |
102 file.write("#1{0}\n".format('}' * closing_brackets)) |
106 file.write("#1{0}\n".format('}' * closing_brackets)) |
103 |
107 |
104 def exportSource(self): |
108 def exportSource(self): |
105 """ |
109 """ |
117 |
121 |
118 tabSize = Preferences.getEditor("TabWidth") |
122 tabSize = Preferences.getEditor("TabWidth") |
119 if tabSize == 0: |
123 if tabSize == 0: |
120 tabSize = 4 |
124 tabSize = 4 |
121 |
125 |
122 onlyStylesUsed = Preferences.getEditorExporter("TeX/OnlyStylesUsed") |
126 onlyStylesUsed = Preferences.getEditorExporter( |
123 titleFullPath = Preferences.getEditorExporter("TeX/FullPathAsTitle") |
127 "TeX/OnlyStylesUsed") |
|
128 titleFullPath = Preferences.getEditorExporter( |
|
129 "TeX/FullPathAsTitle") |
124 |
130 |
125 lex = self.editor.getLexer() |
131 lex = self.editor.getLexer() |
126 self.defaultPaper = lex and \ |
132 self.defaultPaper = lex and \ |
127 lex.paper(QsciScintilla.STYLE_DEFAULT) or \ |
133 lex.paper(QsciScintilla.STYLE_DEFAULT) or \ |
128 self.editor.paper().name() |
134 self.editor.paper().name() |
172 istyle == QsciScintilla.STYLE_DEFAULT: |
178 istyle == QsciScintilla.STYLE_DEFAULT: |
173 font = lex.font(istyle) |
179 font = lex.font(istyle) |
174 colour = lex.color(istyle) |
180 colour = lex.color(istyle) |
175 paper = lex.paper(istyle) |
181 paper = lex.paper(istyle) |
176 |
182 |
177 self.__defineTexStyle(font, colour, paper, f, istyle) |
183 self.__defineTexStyle(font, colour, paper, f, |
|
184 istyle) |
178 istyle += 1 |
185 istyle += 1 |
179 else: |
186 else: |
180 colour = self.editor.color() |
187 colour = self.editor.color() |
181 paper = self.editor.paper() |
188 paper = self.editor.paper() |
182 font = Preferences.getEditorOtherFonts("DefaultFont") |
189 font = Preferences.getEditorOtherFonts("DefaultFont") |
188 f.write("\\begin{document}\n\n") |
195 f.write("\\begin{document}\n\n") |
189 if titleFullPath: |
196 if titleFullPath: |
190 title = self.editor.getFileName() |
197 title = self.editor.getFileName() |
191 else: |
198 else: |
192 title = os.path.basename(self.editor.getFileName()) |
199 title = os.path.basename(self.editor.getFileName()) |
193 f.write("Source File: {0}\n\n\\noindent\n\\tiny{{\n".format(title)) |
200 f.write( |
|
201 "Source File: {0}\n\n\\noindent\n\\tiny{{\n".format(title)) |
194 |
202 |
195 styleCurrent = self.editor.styleAt(0) |
203 styleCurrent = self.editor.styleAt(0) |
196 f.write("\\eric{0}{{".format(self.__texStyle(styleCurrent))) |
204 f.write("\\eric{0}{{".format(self.__texStyle(styleCurrent))) |
197 |
205 |
198 lineIdx = 0 |
206 lineIdx = 0 |
204 while pos < lengthDoc: |
212 while pos < lengthDoc: |
205 ch = self.editor.byteAt(pos) |
213 ch = self.editor.byteAt(pos) |
206 style = self.editor.styleAt(pos) |
214 style = self.editor.styleAt(pos) |
207 if style != styleCurrent: |
215 if style != styleCurrent: |
208 # new style |
216 # new style |
209 f.write("}}\n\\eric{0}{{".format(self.__texStyle(style))) |
217 f.write( |
|
218 "}}\n\\eric{0}{{".format(self.__texStyle(style))) |
210 styleCurrent = style |
219 styleCurrent = style |
211 |
220 |
212 if ch == b'\t': |
221 if ch == b'\t': |
213 ts = tabSize - (lineIdx % tabSize) |
222 ts = tabSize - (lineIdx % tabSize) |
214 lineIdx += ts - 1 |
223 lineIdx += ts - 1 |
215 f.write("\\hspace*{{{0:d}em}}".format(ts)) |
224 f.write("\\hspace*{{{0:d}em}}".format(ts)) |
216 elif ch == b'\\': |
225 elif ch == b'\\': |
217 f.write("{\\textbackslash}") |
226 f.write("{\\textbackslash}") |
218 elif ch in [b'>', b'<', b'@']: |
227 elif ch in [b'>', b'<', b'@']: |
219 f.write("${0}$".format(ch[0])) |
228 f.write("${0}$".format(ch[0])) |
220 elif ch in [b'{', b'}', b'^', b'_', b'&', b'$', b'#', b'%', b'~']: |
229 elif ch in [b'{', b'}', b'^', b'_', b'&', b'$', b'#', |
|
230 b'%', b'~']: |
221 f.write("\\{0}".format(ch[0])) |
231 f.write("\\{0}".format(ch[0])) |
222 elif ch in [b'\r', b'\n']: |
232 elif ch in [b'\r', b'\n']: |
223 lineIdx = -1 # because incremented below |
233 lineIdx = -1 # because incremented below |
224 if ch == b'\r' and self.editor.byteAt(pos + 1) == b'\n': |
234 if ch == b'\r' and \ |
|
235 self.editor.byteAt(pos + 1) == b'\n': |
225 pos += 1 # skip the LF |
236 pos += 1 # skip the LF |
226 styleCurrent = self.editor.styleAt(pos + 1) |
237 styleCurrent = self.editor.styleAt(pos + 1) |
227 f.write("}} \\\\\n\\eric{0}{{".format( |
238 f.write("}} \\\\\n\\eric{0}{{".format( |
228 self.__texStyle(styleCurrent))) |
239 self.__texStyle(styleCurrent))) |
229 elif ch == b' ': |
240 elif ch == b' ': |
257 except IOError as err: |
268 except IOError as err: |
258 QApplication.restoreOverrideCursor() |
269 QApplication.restoreOverrideCursor() |
259 E5MessageBox.critical(self.editor, |
270 E5MessageBox.critical(self.editor, |
260 self.trUtf8("Export source"), |
271 self.trUtf8("Export source"), |
261 self.trUtf8( |
272 self.trUtf8( |
262 """<p>The source could not be exported to <b>{0}</b>.</p>""" |
273 """<p>The source could not be exported to""" |
263 """<p>Reason: {1}</p>""")\ |
274 """ <b>{0}</b>.</p><p>Reason: {1}</p>""")\ |
264 .format(filename, str(err))) |
275 .format(filename, str(err))) |
265 finally: |
276 finally: |
266 QApplication.restoreOverrideCursor() |
277 QApplication.restoreOverrideCursor() |