Ported the exporters to Python3.

Thu, 07 Jan 2010 10:06:18 +0000

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 07 Jan 2010 10:06:18 +0000
changeset 25
5163e6f94ba5
parent 24
9233b51b54d1
child 26
a9885e2f3a8c

Ported the exporters to Python3.

QScintilla/Exporters/ExporterHTML.py file | annotate | diff | comparison | revisions
QScintilla/Exporters/ExporterPDF.py file | annotate | diff | comparison | revisions
QScintilla/Exporters/ExporterRTF.py file | annotate | diff | comparison | revisions
QScintilla/Exporters/ExporterTEX.py file | annotate | diff | comparison | revisions
QScintilla/QsciScintillaCompat.py file | annotate | diff | comparison | revisions
--- a/QScintilla/Exporters/ExporterHTML.py	Wed Jan 06 19:24:47 2010 +0000
+++ b/QScintilla/Exporters/ExporterHTML.py	Thu Jan 07 10:06:18 2010 +0000
@@ -212,35 +212,35 @@
                 column = 0
                 pos = 0
                 utf8 = self.editor.isUtf8()
-                utf8Ch = ""
+                utf8Ch = b""
                 utf8Len = 0
                 
                 while pos < lengthDoc:
-                    ch = self.editor.rawCharAt(pos)
+                    ch = self.editor.byteAt(pos)
                     style = self.editor.styleAt(pos)
                     if style != styleCurrent:
                         if inStyleSpan:
                             f.write('''</span>''')
                             inStyleSpan = False
-                        if ch not in ['\r', '\n']: # no need of a span for the EOL
+                        if ch not in [b'\r', b'\n']: # no need of a span for the EOL
                             if styleIsUsed[style]:
                                 f.write('''<span class="S%d">''' % style)
                                 inStyleSpan = True
                             styleCurrent = style
                     
-                    if ch == ' ':
+                    if ch == b' ':
                         if wysiwyg:
-                            prevCh = ''
+                            prevCh = b''
                             if column == 0: 
                                 # at start of line, must put a &nbsp; 
                                 # because regular space will be collapsed
-                                prevCh = ' '
-                            while pos < lengthDoc and self.editor.rawCharAt(pos) == ' ':
-                                if prevCh != ' ':
+                                prevCh = b' '
+                            while pos < lengthDoc and self.editor.byteAt(pos) == b' ':
+                                if prevCh != b' ':
                                     f.write(' ')
                                 else:
                                     f.write('''&nbsp;''')
-                                prevCh = self.editor.rawCharAt(pos)
+                                prevCh = self.editor.byteAt(pos)
                                 pos += 1
                                 column += 1
                             pos -= 1 
@@ -248,26 +248,26 @@
                         else:
                             f.write(' ')
                             column += 1
-                    elif ch == '\t':
+                    elif ch == b'\t':
                         ts = tabSize - (column % tabSize)
                         if wysiwyg:
                             f.write('''&nbsp;''' * ts)
                             column += ts
                         else:
                             if tabs:
-                                f.write(ch)
+                                f.write('\t')
                                 column += 1
                             else:
                                 f.write(' ' * ts)
                                 column += ts
-                    elif ch in ['\r', '\n']:
+                    elif ch in [b'\r', b'\n']:
                         if inStyleSpan:
                             f.write('''</span>''')
                             inStyleSpan = False
                         if inFoldSpan:
                             f.write('''</span>''')
                             inFoldSpan = False
-                        if ch == '\r' and self.editor.rawCharAt(pos + 1) == '\n':
+                        if ch == b'\r' and self.editor.byteAt(pos + 1) == b'\n':
                             pos += 1 # CR+LF line ending, skip the "extra" EOL char
                         column = 0
                         if wysiwyg:
@@ -299,38 +299,38 @@
                             f.write('\n')
                         
                         if styleIsUsed[styleCurrent] and \
-                           self.editor.rawCharAt(pos + 1) not in ['\r', '\n']:
+                           self.editor.byteAt(pos + 1) not in [b'\r', b'\n']:
                             # We know it's the correct next style,
                             # but no (empty) span for an empty line
                             f.write('''<span class="S%0d">''' % styleCurrent)
                             inStyleSpan = True
                     else:
-                        if ch == '<':
+                        if ch == b'<':
                             f.write('''&lt;''')
-                        elif ch == '>':
+                        elif ch == b'>':
                             f.write('''&gt''')
-                        elif ch == '&':
+                        elif ch == b'&':
                             f.write('''&amp;''')
                         else:
                             if ord(ch) > 127 and utf8:
                                 utf8Ch += ch
                                 if utf8Len == 0:
-                                    if (ord(utf8Ch[0]) & 0xF0) == 0xF0:
+                                    if (utf8Ch[0] & 0xF0) == 0xF0:
                                         utf8Len = 4
-                                    elif (ord(utf8Ch[0]) & 0xE0) == 0xE0:
+                                    elif (utf8Ch[0] & 0xE0) == 0xE0:
                                         utf8Len = 3
-                                    elif (ord(utf8Ch[0]) & 0xC0) == 0xC0:
+                                    elif (utf8Ch[0] & 0xC0) == 0xC0:
                                         utf8Len = 2
                                     column -= 1 # will be incremented again later
                                 elif len(utf8Ch) == utf8Len:
                                     ch = utf8Ch.decode('utf8')
                                     f.write(Utilities.html_encode(ch))
-                                    utf8Ch = ""
+                                    utf8Ch = b""
                                     utf8Len = 0
                                 else:
                                     column -= 1 # will be incremented again later
                             else:
-                                f.write(ch)
+                                f.write(ch.decode())
                         column += 1
                     
                     pos += 1
@@ -361,4 +361,4 @@
                     QMessageBox.StandardButtons(\
                         QMessageBox.Ok))
         finally:
-            QApplication.restoreOverrideCursor()
\ No newline at end of file
+            QApplication.restoreOverrideCursor()
--- a/QScintilla/Exporters/ExporterPDF.py	Wed Jan 06 19:24:47 2010 +0000
+++ b/QScintilla/Exporters/ExporterPDF.py	Thu Jan 07 10:06:18 2010 +0000
@@ -511,7 +511,8 @@
                     self.pr.fontSize = PDF_FONTSIZE_DEFAULT
             
             try:
-                f = open(filename, "w")
+                # save file in win ansi using cp1250
+                f = open(filename, "w", encoding = "cp1250", errors = "backslashreplace")
                 
                 # initialise PDF rendering
                 ot = PDFObjectTracker(f)
@@ -527,20 +528,20 @@
                     pos = 0
                     column = 0
                     utf8 = self.editor.isUtf8()
-                    utf8Ch = ""
+                    utf8Ch = b""
                     utf8Len = 0
                     
                     while pos < lengthDoc:
-                        ch = self.editor.rawCharAt(pos)
+                        ch = self.editor.byteAt(pos)
                         style = self.editor.styleAt(pos)
                         
-                        if ch == '\t':
+                        if ch == b'\t':
                             # expand tabs
                             ts = tabSize - (column % tabSize)
                             column += ts
                             self.pr.add(' ' * ts, style)
-                        elif ch == '\r' or ch == '\n':
-                            if ch == '\r' and self.editor.rawCharAt(pos + 1) == '\n':
+                        elif ch == b'\r' or ch == b'\n':
+                            if ch == b'\r' and self.editor.byteAt(pos + 1) == b'\n':
                                 pos += 1
                             # close and begin a newline...
                             self.pr.nextLine()
@@ -550,24 +551,22 @@
                             if ord(ch) > 127 and utf8:
                                 utf8Ch += ch
                                 if utf8Len == 0:
-                                    if (ord(utf8Ch[0]) & 0xF0) == 0xF0:
+                                    if (utf8Ch[0] & 0xF0) == 0xF0:
                                         utf8Len = 4
-                                    elif (ord(utf8Ch[0]) & 0xE0) == 0xE0:
+                                    elif (utf8Ch[0] & 0xE0) == 0xE0:
                                         utf8Len = 3
-                                    elif (ord(utf8Ch[0]) & 0xC0) == 0xC0:
+                                    elif (utf8Ch[0] & 0xC0) == 0xC0:
                                         utf8Len = 2
                                     column -= 1 # will be incremented again later
                                 elif len(utf8Ch) == utf8Len:
-                                    # convert utf-8 character to win ansi using cp1250
-                                    ch = utf8Ch.decode('utf8')\
-                                               .encode('cp1250', 'replace')
+                                    ch = utf8Ch.decode('utf8')
                                     self.pr.add(ch, style)
-                                    utf8Ch = ""
+                                    utf8Ch = b""
                                     utf8Len = 0
                                 else:
                                     column -= 1 # will be incremented again later
                             else:
-                                self.pr.add(ch, style)
+                                self.pr.add(ch.decode(), style)
                             column += 1
                         
                         pos += 1
--- a/QScintilla/Exporters/ExporterRTF.py	Wed Jan 06 19:24:47 2010 +0000
+++ b/QScintilla/Exporters/ExporterRTF.py	Thu Jan 07 10:06:18 2010 +0000
@@ -264,11 +264,11 @@
                 deltaStyle = ""
                 styleCurrent = -1
                 utf8 = self.editor.isUtf8()
-                utf8Ch = ""
+                utf8Ch = b""
                 utf8Len = 0
                 
                 while pos < lengthDoc:
-                    ch = self.editor.rawCharAt(pos)
+                    ch = self.editor.byteAt(pos)
                     style = self.editor.styleAt(pos)
                     if style != styleCurrent:
                         deltaStyle = self.__GetRTFStyleChange(lastStyle, styles[style])
@@ -277,35 +277,35 @@
                         styleCurrent = style
                         lastStyle = styles[style]
                     
-                    if ch == '{':
+                    if ch == b'{':
                         f.write('\\{')
-                    elif ch == '}':
+                    elif ch == b'}':
                         f.write('\\}')
-                    elif ch == '\\':
+                    elif ch == b'\\':
                         f.write('\\\\')
-                    elif ch == '\t':
+                    elif ch == b'\t':
                         if tabs:
                             f.write(self.RTF_TAB)
                         else:
                             ts = tabSize - (column % tabSize)
                             f.write(' ' * ts)
                             column += ts - 1
-                    elif ch == '\n':
+                    elif ch == b'\n':
                         if not prevCR:
                             f.write(self.RTF_EOLN)
                             column -= 1
-                    elif ch == '\r':
+                    elif ch == b'\r':
                         f.write(self.RTF_EOLN)
                         column -= 1
                     else:
                         if ord(ch) > 0x7F and utf8:
                             utf8Ch += ch
                             if utf8Len == 0:
-                                if (ord(utf8Ch[0]) & 0xF0) == 0xF0:
+                                if (utf8Ch[0] & 0xF0) == 0xF0:
                                     utf8Len = 4
-                                elif (ord(utf8Ch[0]) & 0xE0) == 0xE0:
+                                elif (utf8Ch[0] & 0xE0) == 0xE0:
                                     utf8Len = 3
-                                elif (ord(utf8Ch[0]) & 0xC0) == 0xC0:
+                                elif (utf8Ch[0] & 0xC0) == 0xC0:
                                     utf8Len = 2
                                 column -= 1 # will be incremented again later
                             elif len(utf8Ch) == utf8Len:
@@ -314,12 +314,12 @@
                                     f.write("\\'%x" % ord(ch))
                                 else:
                                     f.write("\\u%d\\'%x" % (ord(ch), ord(ch) & 0xFF))
-                                utf8Ch = ""
+                                utf8Ch = b""
                                 utf8Len = 0
                             else:
                                 column -= 1 # will be incremented again later
                         else:
-                            f.write(ch)
+                            f.write(ch.decode())
                     
                     column += 1
                     prevCR = ch == '\r'
@@ -338,4 +338,4 @@
                     QMessageBox.StandardButtons(\
                         QMessageBox.Ok))
         finally:
-            QApplication.restoreOverrideCursor()
\ No newline at end of file
+            QApplication.restoreOverrideCursor()
--- a/QScintilla/Exporters/ExporterTEX.py	Wed Jan 06 19:24:47 2010 +0000
+++ b/QScintilla/Exporters/ExporterTEX.py	Thu Jan 07 10:06:18 2010 +0000
@@ -67,7 +67,7 @@
         else:
             while style > 0:
                 buf += chr(ord('a') + (style % self.CHARZ))
-                style /= self.CHARZ
+                style //= self.CHARZ
         return buf
     
     def __defineTexStyle(self, font, color, paper, file, istyle):
@@ -193,38 +193,56 @@
                 
                 lineIdx = 0
                 pos = 0
+                utf8 = self.editor.isUtf8()
+                utf8Ch = b""
+                utf8Len = 0
                 
                 while pos < lengthDoc:
-                    ch = self.editor.rawCharAt(pos)
+                    ch = self.editor.byteAt(pos)
                     style = self.editor.styleAt(pos)
                     if style != styleCurrent:
                         # new style
                         f.write("}\n\\eric%s{" % self.__texStyle(style))
                         styleCurrent = style
                     
-                    if ch == '\t':
+                    if ch == b'\t':
                         ts = tabSize - (lineIdx % tabSize)
                         lineIdx += ts - 1
                         f.write("\\hspace*{%dem}" % ts)
-                    elif ch == '\\':
+                    elif ch == b'\\':
                         f.write("{\\textbackslash}")
-                    elif ch in ['>', '<', '@']:
-                        f.write("$%c$" % ch)
-                    elif ch in ['{', '}', '^', '_', '&', '$', '#', '%', '~']:
-                        f.write("\\%c" % ch)
-                    elif ch in ['\r', '\n']:
+                    elif ch in [b'>', b'<', b'@']:
+                        f.write("$%c$" % ch[0])
+                    elif ch in [b'{', b'}', b'^', b'_', b'&', b'$', b'#', b'%', b'~']:
+                        f.write("\\%c" % ch[0])
+                    elif ch in [b'\r', b'\n']:
                         lineIdx = -1    # because incremented below
-                        if ch == '\r' and self.editor.rawCharAt(pos + 1) == '\n':
+                        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%s{" % self.__texStyle(styleCurrent))
-                    elif ch == ' ':
-                        if self.editor.rawCharAt(pos + 1) == ' ':
+                    elif ch == b' ':
+                        if self.editor.byteAt(pos + 1) == b' ':
                             f.write("{\\hspace*{1em}}")
                         else:
                             f.write(' ')
                     else:
-                        f.write(ch)
+                        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
                 
--- a/QScintilla/QsciScintillaCompat.py	Wed Jan 06 19:24:47 2010 +0000
+++ b/QScintilla/QsciScintillaCompat.py	Thu Jan 07 10:06:18 2010 +0000
@@ -194,39 +194,38 @@
         multibyte characters.
         
         @param pos position in the text (integer)
-        @return raw character at the requested position or empty string, if the position
+        @return character at the requested position or empty string, if the position
             is negative or past the end of the document (string)
         """
-        ch = self.rawCharAt(pos)
+        ch = self.byteAt(pos)
         if ch and ord(ch) > 127 and self.isUtf8():
-            if (ord(ch[0]) & 0xF0) == 0xF0:
+            if (ch[0] & 0xF0) == 0xF0:
                 utf8Len = 4
-            elif (ord(ch[0]) & 0xE0) == 0xE0:
+            elif (ch[0] & 0xE0) == 0xE0:
                 utf8Len = 3
-            elif (ord(ch[0]) & 0xC0) == 0xC0:
+            elif (ch[0] & 0xC0) == 0xC0:
                 utf8Len = 2
             while len(ch) < utf8Len:
                 pos += 1
-                ch += self.rawCharAt(pos)
+                ch += self.byteAt(pos)
             return ch.decode('utf8')
         else:
-            return ch
+            return ch.decode()
     
-    def rawCharAt(self, pos):
+    def byteAt(self, pos):
         """
-        Public method to get the raw character at a position in the text.
+        Public method to get the raw character (bytes) at a position in the text.
         
         @param pos position in the text (integer)
-        @return raw character at the requested position or empty string, if the position
-            is negative or past the end of the document (string)
+        @return raw character at the requested position or empty bytes, if the position
+            is negative or past the end of the document (bytes)
         """
         char = self.SendScintilla(QsciScintilla.SCI_GETCHARAT, pos)
         if char == 0:
-            return ""
-        elif char < 0:
-            return chr(char + 256)
-        else:
-            return chr(char)
+            return b""
+        if char < 0:
+            char += 256
+        return bytes.fromhex("{0:02x}".format(char))
     
     def foldLevelAt(self, line):
         """

eric ide

mercurial