eric6/QScintilla/Exporters/ExporterTEX.py

changeset 8205
4a0f1f896341
parent 7949
17c0a4ec3cf0
child 8259
2bbec88047dd
equal deleted inserted replaced
8204:fd477cded1c1 8205:4a0f1f896341
159 else: 159 else:
160 for index in range(QsciScintilla.STYLE_MAX + 1): 160 for index in range(QsciScintilla.STYLE_MAX + 1):
161 styleIsUsed[index] = True 161 styleIsUsed[index] = True
162 styleIsUsed[QsciScintilla.STYLE_DEFAULT] = True 162 styleIsUsed[QsciScintilla.STYLE_DEFAULT] = True
163 163
164 try: 164 with E5OverrideCursor(), open(filename, "w", encoding="utf-8") as f:
165 with E5OverrideCursor(): 165 try:
166 with open(filename, "w", encoding="utf-8") as f: 166 f.write("\\documentclass[a4paper]{article}\n")
167 f.write("\\usepackage[a4paper,margin=1.5cm]{geometry}\n")
168 f.write("\\usepackage[T1]{fontenc}\n")
169 f.write("\\usepackage{color}\n")
170 f.write("\\usepackage{alltt}\n")
171 f.write("\\usepackage{times}\n")
172 if self.editor.isUtf8():
173 f.write("\\usepackage[utf8]{inputenc}\n")
174 else:
175 f.write("\\usepackage[latin1]{inputenc}\n")
176
177 if lex:
178 istyle = 0
179 while istyle <= QsciScintilla.STYLE_MAX:
180 if (
181 (istyle <= QsciScintilla.STYLE_DEFAULT or
182 istyle > QsciScintilla.STYLE_LASTPREDEFINED) and
183 styleIsUsed[istyle] and
184 (lex.description(istyle) or
185 istyle == QsciScintilla.STYLE_DEFAULT)
186 ):
187 font = lex.font(istyle)
188 colour = lex.color(istyle)
189 paper = lex.paper(istyle)
190
191 self.__defineTexStyle(
192 font, colour, paper, f, istyle)
193 # get substyles
194 subs_start, subs_count = (
195 self.editor.getSubStyleRange(istyle)
196 )
197 for subs_idx in range(subs_count):
198 font = lex.font(subs_start + subs_idx)
199 colour = lex.color(
200 subs_start + subs_idx)
201 paper = lex.paper(
202 subs_start + subs_idx)
203
204 self.__defineTexStyle(
205 font, colour, paper, f,
206 subs_idx - subs_start)
207
208 istyle += 1
209 else:
210 colour = self.editor.color()
211 paper = self.editor.paper()
212 font = Preferences.getEditorOtherFonts("DefaultFont")
167 213
168 f.write("\\documentclass[a4paper]{article}\n") 214 self.__defineTexStyle(font, colour, paper, f, 0)
169 f.write("\\usepackage[a4paper,margin=1.5cm]{geometry}\n") 215 self.__defineTexStyle(font, colour, paper, f,
170 f.write("\\usepackage[T1]{fontenc}\n") 216 QsciScintilla.STYLE_DEFAULT)
171 f.write("\\usepackage{color}\n") 217
172 f.write("\\usepackage{alltt}\n") 218 f.write("\\begin{document}\n\n")
173 f.write("\\usepackage{times}\n") 219 if titleFullPath:
174 if self.editor.isUtf8(): 220 title = self.editor.getFileName()
175 f.write("\\usepackage[utf8]{inputenc}\n") 221 else:
222 title = os.path.basename(self.editor.getFileName())
223 f.write(
224 "Source File: {0}\n\n\\noindent\n\\tiny{{\n"
225 .format(title))
226
227 styleCurrent = self.editor.styleAt(0)
228 f.write("\\eric{0}{{"
229 .format(self.__texStyle(styleCurrent)))
230
231 lineIdx = 0
232 pos = 0
233 utf8 = self.editor.isUtf8()
234 utf8Ch = b""
235 utf8Len = 0
236
237 while pos < lengthDoc:
238 ch = self.editor.byteAt(pos)
239 style = self.editor.styleAt(pos)
240 if style != styleCurrent:
241 # new style
242 f.write(
243 "}}\n\\eric{0}{{".format(
244 self.__texStyle(style)))
245 styleCurrent = style
246
247 if ch == b'\t':
248 ts = tabSize - (lineIdx % tabSize)
249 lineIdx += ts - 1
250 f.write("\\hspace*{{{0:d}em}}".format(ts))
251 elif ch == b'\\':
252 f.write("{\\textbackslash}")
253 elif ch in [b'>', b'<', b'@']:
254 f.write("${0}$".format(ch[0]))
255 elif ch in [b'{', b'}', b'^', b'_', b'&', b'$', b'#',
256 b'%', b'~']:
257 f.write("\\{0}".format(ch[0]))
258 elif ch in [b'\r', b'\n']:
259 lineIdx = -1 # because incremented below
260 if (
261 ch == b'\r' and
262 self.editor.byteAt(pos + 1) == b'\n'
263 ):
264 pos += 1 # skip the LF
265 styleCurrent = self.editor.styleAt(pos + 1)
266 f.write("}} \\\\\n\\eric{0}{{".format(
267 self.__texStyle(styleCurrent)))
268 elif ch == b' ':
269 if self.editor.byteAt(pos + 1) == b' ':
270 f.write("{\\hspace*{1em}}")
271 else:
272 f.write(' ')
176 else: 273 else:
177 f.write("\\usepackage[latin1]{inputenc}\n") 274 if ord(ch) > 127 and utf8:
178 275 utf8Ch += ch
179 if lex: 276 if utf8Len == 0:
180 istyle = 0 277 if (utf8Ch[0] & 0xF0) == 0xF0:
181 while istyle <= QsciScintilla.STYLE_MAX: 278 utf8Len = 4
182 if ( 279 elif (utf8Ch[0] & 0xE0) == 0xE0:
183 (istyle <= QsciScintilla.STYLE_DEFAULT or 280 utf8Len = 3
184 istyle > QsciScintilla.STYLE_LASTPREDEFINED 281 elif (utf8Ch[0] & 0xC0) == 0xC0:
185 ) and styleIsUsed[istyle] 282 utf8Len = 2
186 ): 283 elif len(utf8Ch) == utf8Len:
187 if ( 284 ch = utf8Ch.decode('utf8')
188 lex.description(istyle) or 285 f.write(ch)
189 istyle == QsciScintilla.STYLE_DEFAULT 286 utf8Ch = b""
190 ): 287 utf8Len = 0
191 font = lex.font(istyle)
192 colour = lex.color(istyle)
193 paper = lex.paper(istyle)
194
195 self.__defineTexStyle(
196 font, colour, paper, f, istyle)
197 # get substyles
198 subs_start, subs_count = (
199 self.editor.getSubStyleRange(istyle)
200 )
201 for subs_idx in range(subs_count):
202 font = lex.font(subs_start + subs_idx)
203 colour = lex.color(
204 subs_start + subs_idx)
205 paper = lex.paper(
206 subs_start + subs_idx)
207
208 self.__defineTexStyle(
209 font, colour, paper, f,
210 subs_idx - subs_start)
211
212 istyle += 1
213 else:
214 colour = self.editor.color()
215 paper = self.editor.paper()
216 font = Preferences.getEditorOtherFonts("DefaultFont")
217
218 self.__defineTexStyle(font, colour, paper, f, 0)
219 self.__defineTexStyle(font, colour, paper, f,
220 QsciScintilla.STYLE_DEFAULT)
221
222 f.write("\\begin{document}\n\n")
223 if titleFullPath:
224 title = self.editor.getFileName()
225 else:
226 title = os.path.basename(self.editor.getFileName())
227 f.write(
228 "Source File: {0}\n\n\\noindent\n\\tiny{{\n"
229 .format(title))
230
231 styleCurrent = self.editor.styleAt(0)
232 f.write("\\eric{0}{{"
233 .format(self.__texStyle(styleCurrent)))
234
235 lineIdx = 0
236 pos = 0
237 utf8 = self.editor.isUtf8()
238 utf8Ch = b""
239 utf8Len = 0
240
241 while pos < lengthDoc:
242 ch = self.editor.byteAt(pos)
243 style = self.editor.styleAt(pos)
244 if style != styleCurrent:
245 # new style
246 f.write(
247 "}}\n\\eric{0}{{".format(
248 self.__texStyle(style)))
249 styleCurrent = style
250
251 if ch == b'\t':
252 ts = tabSize - (lineIdx % tabSize)
253 lineIdx += ts - 1
254 f.write("\\hspace*{{{0:d}em}}".format(ts))
255 elif ch == b'\\':
256 f.write("{\\textbackslash}")
257 elif ch in [b'>', b'<', b'@']:
258 f.write("${0}$".format(ch[0]))
259 elif ch in [b'{', b'}', b'^', b'_', b'&', b'$', b'#',
260 b'%', b'~']:
261 f.write("\\{0}".format(ch[0]))
262 elif ch in [b'\r', b'\n']:
263 lineIdx = -1 # because incremented below
264 if (
265 ch == b'\r' and
266 self.editor.byteAt(pos + 1) == b'\n'
267 ):
268 pos += 1 # skip the LF
269 styleCurrent = self.editor.styleAt(pos + 1)
270 f.write("}} \\\\\n\\eric{0}{{".format(
271 self.__texStyle(styleCurrent)))
272 elif ch == b' ':
273 if self.editor.byteAt(pos + 1) == b' ':
274 f.write("{\\hspace*{1em}}")
275 else:
276 f.write(' ')
277 else: 288 else:
278 if ord(ch) > 127 and utf8: 289 f.write(ch.decode())
279 utf8Ch += ch 290 lineIdx += 1
280 if utf8Len == 0: 291 pos += 1
281 if (utf8Ch[0] & 0xF0) == 0xF0: 292
282 utf8Len = 4 293 # close last empty style macros and document too
283 elif (utf8Ch[0] & 0xE0) == 0xE0: 294 f.write("}\n} %end tiny\n\n\\end{document}\n")
284 utf8Len = 3 295 except OSError as err:
285 elif (utf8Ch[0] & 0xC0) == 0xC0: 296 E5MessageBox.critical(
286 utf8Len = 2 297 self.editor,
287 elif len(utf8Ch) == utf8Len: 298 self.tr("Export source"),
288 ch = utf8Ch.decode('utf8') 299 self.tr(
289 f.write(ch) 300 """<p>The source could not be exported to"""
290 utf8Ch = b"" 301 """ <b>{0}</b>.</p><p>Reason: {1}</p>""")
291 utf8Len = 0 302 .format(filename, str(err)))
292 else:
293 f.write(ch.decode())
294 lineIdx += 1
295 pos += 1
296
297 # close last empty style macros and document too
298 f.write("}\n} %end tiny\n\n\\end{document}\n")
299 except OSError as err:
300 E5MessageBox.critical(
301 self.editor,
302 self.tr("Export source"),
303 self.tr(
304 """<p>The source could not be exported to"""
305 """ <b>{0}</b>.</p><p>Reason: {1}</p>""")
306 .format(filename, str(err)))

eric ide

mercurial