eric6/QScintilla/Exporters/ExporterTEX.py

changeset 8205
4a0f1f896341
parent 7949
17c0a4ec3cf0
child 8259
2bbec88047dd
--- a/eric6/QScintilla/Exporters/ExporterTEX.py	Thu Apr 08 17:27:12 2021 +0200
+++ b/eric6/QScintilla/Exporters/ExporterTEX.py	Thu Apr 08 18:27:47 2021 +0200
@@ -161,146 +161,142 @@
                 styleIsUsed[index] = True
         styleIsUsed[QsciScintilla.STYLE_DEFAULT] = True
         
-        try:
-            with E5OverrideCursor():
-                with open(filename, "w", encoding="utf-8") as f:
-                    
-                    f.write("\\documentclass[a4paper]{article}\n")
-                    f.write("\\usepackage[a4paper,margin=1.5cm]{geometry}\n")
-                    f.write("\\usepackage[T1]{fontenc}\n")
-                    f.write("\\usepackage{color}\n")
-                    f.write("\\usepackage{alltt}\n")
-                    f.write("\\usepackage{times}\n")
-                    if self.editor.isUtf8():
-                        f.write("\\usepackage[utf8]{inputenc}\n")
-                    else:
-                        f.write("\\usepackage[latin1]{inputenc}\n")
-                    
-                    if lex:
-                        istyle = 0
-                        while istyle <= QsciScintilla.STYLE_MAX:
-                            if (
-                                (istyle <= QsciScintilla.STYLE_DEFAULT or
-                                 istyle > QsciScintilla.STYLE_LASTPREDEFINED
-                                 ) and styleIsUsed[istyle]
-                            ):
-                                if (
-                                    lex.description(istyle) or
-                                    istyle == QsciScintilla.STYLE_DEFAULT
-                                ):
-                                    font = lex.font(istyle)
-                                    colour = lex.color(istyle)
-                                    paper = lex.paper(istyle)
-                                    
-                                    self.__defineTexStyle(
-                                        font, colour, paper, f, istyle)
-                                    # get substyles
-                                    subs_start, subs_count = (
-                                        self.editor.getSubStyleRange(istyle)
-                                    )
-                                    for subs_idx in range(subs_count):
-                                        font = lex.font(subs_start + subs_idx)
-                                        colour = lex.color(
-                                            subs_start + subs_idx)
-                                        paper = lex.paper(
-                                            subs_start + subs_idx)
-                                        
-                                        self.__defineTexStyle(
-                                            font, colour, paper, f,
-                                            subs_idx - subs_start)
-                                    
-                            istyle += 1
-                    else:
-                        colour = self.editor.color()
-                        paper = self.editor.paper()
-                        font = Preferences.getEditorOtherFonts("DefaultFont")
-                        
-                        self.__defineTexStyle(font, colour, paper, f, 0)
-                        self.__defineTexStyle(font, colour, paper, f,
-                                              QsciScintilla.STYLE_DEFAULT)
-                    
-                    f.write("\\begin{document}\n\n")
-                    if titleFullPath:
-                        title = self.editor.getFileName()
-                    else:
-                        title = os.path.basename(self.editor.getFileName())
-                    f.write(
-                        "Source File: {0}\n\n\\noindent\n\\tiny{{\n"
-                        .format(title))
-                    
-                    styleCurrent = self.editor.styleAt(0)
-                    f.write("\\eric{0}{{"
-                            .format(self.__texStyle(styleCurrent)))
+        with E5OverrideCursor(), open(filename, "w", encoding="utf-8") as f:
+            try:
+                f.write("\\documentclass[a4paper]{article}\n")
+                f.write("\\usepackage[a4paper,margin=1.5cm]{geometry}\n")
+                f.write("\\usepackage[T1]{fontenc}\n")
+                f.write("\\usepackage{color}\n")
+                f.write("\\usepackage{alltt}\n")
+                f.write("\\usepackage{times}\n")
+                if self.editor.isUtf8():
+                    f.write("\\usepackage[utf8]{inputenc}\n")
+                else:
+                    f.write("\\usepackage[latin1]{inputenc}\n")
+                
+                if lex:
+                    istyle = 0
+                    while istyle <= QsciScintilla.STYLE_MAX:
+                        if (
+                            (istyle <= QsciScintilla.STYLE_DEFAULT or
+                             istyle > QsciScintilla.STYLE_LASTPREDEFINED) and
+                            styleIsUsed[istyle] and
+                            (lex.description(istyle) or
+                             istyle == QsciScintilla.STYLE_DEFAULT)
+                        ):
+                            font = lex.font(istyle)
+                            colour = lex.color(istyle)
+                            paper = lex.paper(istyle)
+                            
+                            self.__defineTexStyle(
+                                font, colour, paper, f, istyle)
+                            # get substyles
+                            subs_start, subs_count = (
+                                self.editor.getSubStyleRange(istyle)
+                            )
+                            for subs_idx in range(subs_count):
+                                font = lex.font(subs_start + subs_idx)
+                                colour = lex.color(
+                                    subs_start + subs_idx)
+                                paper = lex.paper(
+                                    subs_start + subs_idx)
+                                
+                                self.__defineTexStyle(
+                                    font, colour, paper, f,
+                                    subs_idx - subs_start)
+                                
+                        istyle += 1
+                else:
+                    colour = self.editor.color()
+                    paper = self.editor.paper()
+                    font = Preferences.getEditorOtherFonts("DefaultFont")
                     
-                    lineIdx = 0
-                    pos = 0
-                    utf8 = self.editor.isUtf8()
-                    utf8Ch = b""
-                    utf8Len = 0
+                    self.__defineTexStyle(font, colour, paper, f, 0)
+                    self.__defineTexStyle(font, colour, paper, f,
+                                          QsciScintilla.STYLE_DEFAULT)
+                
+                f.write("\\begin{document}\n\n")
+                if titleFullPath:
+                    title = self.editor.getFileName()
+                else:
+                    title = os.path.basename(self.editor.getFileName())
+                f.write(
+                    "Source File: {0}\n\n\\noindent\n\\tiny{{\n"
+                    .format(title))
+                
+                styleCurrent = self.editor.styleAt(0)
+                f.write("\\eric{0}{{"
+                        .format(self.__texStyle(styleCurrent)))
+                
+                lineIdx = 0
+                pos = 0
+                utf8 = self.editor.isUtf8()
+                utf8Ch = b""
+                utf8Len = 0
+                
+                while pos < lengthDoc:
+                    ch = self.editor.byteAt(pos)
+                    style = self.editor.styleAt(pos)
+                    if style != styleCurrent:
+                        # new style
+                        f.write(
+                            "}}\n\\eric{0}{{".format(
+                                self.__texStyle(style)))
+                        styleCurrent = style
                     
-                    while pos < lengthDoc:
-                        ch = self.editor.byteAt(pos)
-                        style = self.editor.styleAt(pos)
-                        if style != styleCurrent:
-                            # new style
-                            f.write(
-                                "}}\n\\eric{0}{{".format(
-                                    self.__texStyle(style)))
-                            styleCurrent = style
-                        
-                        if ch == b'\t':
-                            ts = tabSize - (lineIdx % tabSize)
-                            lineIdx += ts - 1
-                            f.write("\\hspace*{{{0:d}em}}".format(ts))
-                        elif ch == b'\\':
-                            f.write("{\\textbackslash}")
-                        elif ch in [b'>', b'<', b'@']:
-                            f.write("${0}$".format(ch[0]))
-                        elif ch in [b'{', b'}', b'^', b'_', b'&', b'$', b'#',
-                                    b'%', b'~']:
-                            f.write("\\{0}".format(ch[0]))
-                        elif ch in [b'\r', b'\n']:
-                            lineIdx = -1    # because incremented below
-                            if (
-                                ch == b'\r' and
-                                self.editor.byteAt(pos + 1) == b'\n'
-                            ):
-                                pos += 1    # skip the LF
-                            styleCurrent = self.editor.styleAt(pos + 1)
-                            f.write("}} \\\\\n\\eric{0}{{".format(
-                                    self.__texStyle(styleCurrent)))
-                        elif ch == b' ':
-                            if self.editor.byteAt(pos + 1) == b' ':
-                                f.write("{\\hspace*{1em}}")
-                            else:
-                                f.write(' ')
+                    if ch == b'\t':
+                        ts = tabSize - (lineIdx % tabSize)
+                        lineIdx += ts - 1
+                        f.write("\\hspace*{{{0:d}em}}".format(ts))
+                    elif ch == b'\\':
+                        f.write("{\\textbackslash}")
+                    elif ch in [b'>', b'<', b'@']:
+                        f.write("${0}$".format(ch[0]))
+                    elif ch in [b'{', b'}', b'^', b'_', b'&', b'$', b'#',
+                                b'%', b'~']:
+                        f.write("\\{0}".format(ch[0]))
+                    elif ch in [b'\r', b'\n']:
+                        lineIdx = -1    # because incremented below
+                        if (
+                            ch == b'\r' and
+                            self.editor.byteAt(pos + 1) == b'\n'
+                        ):
+                            pos += 1    # skip the LF
+                        styleCurrent = self.editor.styleAt(pos + 1)
+                        f.write("}} \\\\\n\\eric{0}{{".format(
+                                self.__texStyle(styleCurrent)))
+                    elif ch == b' ':
+                        if self.editor.byteAt(pos + 1) == b' ':
+                            f.write("{\\hspace*{1em}}")
                         else:
-                            if ord(ch) > 127 and utf8:
-                                utf8Ch += ch
-                                if utf8Len == 0:
-                                    if (utf8Ch[0] & 0xF0) == 0xF0:
-                                        utf8Len = 4
-                                    elif (utf8Ch[0] & 0xE0) == 0xE0:
-                                        utf8Len = 3
-                                    elif (utf8Ch[0] & 0xC0) == 0xC0:
-                                        utf8Len = 2
-                                elif len(utf8Ch) == utf8Len:
-                                    ch = utf8Ch.decode('utf8')
-                                    f.write(ch)
-                                    utf8Ch = b""
-                                    utf8Len = 0
-                            else:
-                                f.write(ch.decode())
-                        lineIdx += 1
-                        pos += 1
-                    
-                    # close last empty style macros and document too
-                    f.write("}\n} %end tiny\n\n\\end{document}\n")
-        except OSError as err:
-            E5MessageBox.critical(
-                self.editor,
-                self.tr("Export source"),
-                self.tr(
-                    """<p>The source could not be exported to"""
-                    """ <b>{0}</b>.</p><p>Reason: {1}</p>""")
-                .format(filename, str(err)))
+                            f.write(' ')
+                    else:
+                        if ord(ch) > 127 and utf8:
+                            utf8Ch += ch
+                            if utf8Len == 0:
+                                if (utf8Ch[0] & 0xF0) == 0xF0:
+                                    utf8Len = 4
+                                elif (utf8Ch[0] & 0xE0) == 0xE0:
+                                    utf8Len = 3
+                                elif (utf8Ch[0] & 0xC0) == 0xC0:
+                                    utf8Len = 2
+                            elif len(utf8Ch) == utf8Len:
+                                ch = utf8Ch.decode('utf8')
+                                f.write(ch)
+                                utf8Ch = b""
+                                utf8Len = 0
+                        else:
+                            f.write(ch.decode())
+                    lineIdx += 1
+                    pos += 1
+                
+                # close last empty style macros and document too
+                f.write("}\n} %end tiny\n\n\\end{document}\n")
+            except OSError as err:
+                E5MessageBox.critical(
+                    self.editor,
+                    self.tr("Export source"),
+                    self.tr(
+                        """<p>The source could not be exported to"""
+                        """ <b>{0}</b>.</p><p>Reason: {1}</p>""")
+                    .format(filename, str(err)))

eric ide

mercurial