]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_1_2.py
Python cleanup from Thibaut.
[lyx.git] / lib / lyx2lyx / lyx_1_2.py
index 755069bc7661021379942d5b6a191ca2bb23c115..b697a918c588159ed37bb42a18eacb283ef8a9a6 100644 (file)
 #
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 """ Convert files to the file format generated by lyx 1.2"""
 
-import string
 import re
 
 from parser_tools import find_token, find_token_backwards, \
-                         find_tokens, find_tokens_backwards,
+                         find_tokens, find_tokens_backwards, \
                          find_beginning_of, find_end_of, find_re, \
                          is_nonempty_line, find_nonempty_line, \
                          get_value, check_token
@@ -33,7 +32,7 @@ from parser_tools import find_token, find_token_backwards, \
 
 def get_layout(line, default_layout):
     " Get layout, if empty return the default layout."
-    tokens = string.split(line)
+    tokens = line.split()
     if len(tokens) > 1:
         return tokens[1]
     return default_layout
@@ -147,15 +146,15 @@ def remove_oldfloat(document):
     " Change \begin_float .. \end_float into \begin_inset Float .. \end_inset"
     lines = document.body
     i = 0
-    while 1:
+    while True:
         i = find_token(lines, "\\begin_float", i)
         if i == -1:
             break
         # There are no nested floats, so finding the end of the float is simple
         j = find_token(lines, "\\end_float", i+1)
 
-        floattype = string.split(lines[i])[1]
-        if not floats.has_key(floattype):
+        floattype = lines[i].split()[1]
+        if floattype not in floats:
             document.warning("Error! Unknown float type " + floattype)
             floattype = "fig"
 
@@ -216,7 +215,7 @@ def remove_pextra(document):
     lines = document.body
     i = 0
     flag = 0
-    while 1:
+    while True:
         i = find_re(lines, pextra_type2_rexp, i)
         if i == -1:
             break
@@ -259,7 +258,7 @@ def remove_pextra(document):
         j = get_next_paragraph(lines, i, document.format + 1)
 
         count = 0
-        while 1:
+        while True:
             # collect more paragraphs to the minipage
             count = count+1
             if j == -1 or not check_token(lines[j], "\\layout"):
@@ -285,7 +284,7 @@ def remove_pextra(document):
 
 def is_empty(lines):
     " Are all the lines empty?"
-    return filter(is_nonempty_line, lines) == []
+    return list(filter(is_nonempty_line, lines)) == []
 
 
 move_rexp =  re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun|end_deeper)")
@@ -302,12 +301,12 @@ def remove_oldert(document):
                  ""]
     lines = document.body
     i = 0
-    while 1:
+    while True:
         i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
         if i == -1:
             break
         j = i+1
-        while 1:
+        while True:
             # \end_inset is for ert inside a tabular cell. The other tokens
             # are obvious.
             j = find_tokens(lines, ["\\latex default", "\\layout", "\\begin_inset", "\\end_inset", "\\end_float", "\\the_end"],
@@ -328,7 +327,7 @@ def remove_oldert(document):
             new = ['\layout %s' % document.default_layout, "", ""]
 
         k = i+1
-        while 1:
+        while True:
             k2 = find_re(lines, ert_rexp, k, j)
             inset = hfill = specialchar = 0
             if k2 == -1:
@@ -359,7 +358,7 @@ def remove_oldert(document):
                     tmp.append(line)
 
             if is_empty(tmp):
-                if filter(lambda x:x != "", tmp) != []:
+                if [x for x in tmp if x != ""] != []:
                     if new == []:
                         # This is not necessary, but we want the output to be
                         # as similar as posible to the lyx format
@@ -400,7 +399,7 @@ def remove_oldert(document):
 
     # Delete remaining "\latex xxx" tokens
     i = 0
-    while 1:
+    while True:
         i = find_token(lines, "\\latex ", i)
         if i == -1:
             break
@@ -411,7 +410,7 @@ def remove_oldertinset(document):
     " ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future."
     lines = document.body
     i = 0
-    while 1:
+    while True:
         i = find_token(lines, "\\begin_inset ERT", i)
         if i == -1:
             break
@@ -446,7 +445,7 @@ def combine_ert(document):
     " Combine ERT paragraphs."
     lines = document.body
     i = 0
-    while 1:
+    while True:
         i = find_token(lines, "\\begin_inset ERT", i)
         if i == -1:
             break
@@ -477,7 +476,7 @@ def get_length(lines, name, start, end):
     i = find_token(lines, name, start, end)
     if i == -1:
         return ""
-    x = string.split(lines[i])
+    x = lines[i].split()
     return x[2]+oldunits[int(x[1])]
 
 
@@ -491,15 +490,15 @@ def remove_figinset(document):
     " Remove figinset."
     lines = document.body
     i = 0
-    while 1:
+    while True:
         i = find_token(lines, "\\begin_inset Figure", i)
         if i == -1:
             break
         j = find_end_of_inset(lines, i)
 
-        if ( len(string.split(lines[i])) > 2 ):
-            lyxwidth = string.split(lines[i])[3]+"pt"
-            lyxheight = string.split(lines[i])[4]+"pt"
+        if ( len(lines[i].split()) > 2 ):
+            lyxwidth = lines[i].split()[3]+"pt"
+            lyxheight = lines[i].split()[4]+"pt"
         else:
             lyxwidth = ""
             lyxheight = ""
@@ -565,16 +564,16 @@ def update_tabular(document):
     regexp = re.compile(r'^\\begin_inset\s+Tabular')
     lines = document.body
     i = 0
-    while 1:
+    while True:
         i = find_re(lines, regexp, i)
         if i == -1:
             break
 
         for k in get_tabular_lines(lines, i):
             if check_token(lines[k], "<lyxtabular"):
-                lines[k] = string.replace(lines[k], 'version="2"', 'version="3"')
+                lines[k] = lines[k].replace('version="2"', 'version="3"')
             elif check_token(lines[k], "<column"):
-                lines[k] = string.replace(lines[k], 'width=""', 'width="0pt"')
+                lines[k] = lines[k].replace('width=""', 'width="0pt"')
 
             if line_re.match(lines[k]):
                 lines[k] = re.sub(attr_re, "", lines[k])
@@ -685,7 +684,7 @@ def setHeaderFooterRows(hr, fhr, fr, lfr, rows_, row_info):
 
 def insert_attribute(lines, i, attribute):
     " Insert attribute in lines[i]."
-    last = string.find(lines[i],'>')
+    last = lines[i].find('>')
     lines[i] = lines[i][:last] + ' ' + attribute + lines[i][last:]
 
 
@@ -698,7 +697,7 @@ def update_longtables(document):
     regexp = re.compile(r'^\\begin_inset\s+Tabular')
     body = document.body
     i = 0
-    while 1:
+    while True:
         i = find_re(body, regexp, i)
         if i == -1:
             break
@@ -726,7 +725,7 @@ def update_longtables(document):
             # remove longtable elements from features
             features = lt_features_re.search(body[i])
             if features:
-                body[i] = string.replace(body[i], features.group(1), "")
+                body[i] = body[i].replace(features.group(1), "")
             continue
 
         row_info = row() * rows
@@ -767,7 +766,7 @@ def fix_oldfloatinset(document):
     " Figure insert are hidden feature of lyx 1.1.6. This might be removed in the future."
     lines = document.body
     i = 0
-    while 1:
+    while True:
         i = find_token(lines, "\\begin_inset Float ", i)
         if i == -1:
             break
@@ -781,7 +780,7 @@ def change_listof(document):
     " Change listof insets."
     lines = document.body
     i = 0
-    while 1:
+    while True:
         i = find_token(lines, "\\begin_inset LatexCommand \\listof", i)
         if i == -1:
             break
@@ -794,11 +793,11 @@ def change_infoinset(document):
     " Change info inset."
     lines = document.body
     i = 0
-    while 1:
+    while True:
         i = find_token(lines, "\\begin_inset Info", i)
         if i == -1:
             break
-        txt = string.lstrip(lines[i][18:])
+        txt = lines[i][18:].lstrip()
         new = ["\\begin_inset Note", "collapsed true", ""]
         j = find_token(lines, "\\end_inset", i)
         if j == -1:
@@ -810,7 +809,7 @@ def change_infoinset(document):
 
         for line in note_lines:
             new = new + ['\layout %s' % document.default_layout, ""]
-            tmp = string.split(line, '\\')
+            tmp = line.split('\\')
             new = new + [tmp[0]]
             for x in tmp[1:]:
                 new = new + ["\\backslash ", x]
@@ -825,7 +824,7 @@ def change_header(document):
     if i == -1:
         return
     lines[i+1:i+1] = ["\\use_natbib 0",
-                      "\use_numerical_citations 0"]
+                      "\\use_numerical_citations 0"]
 
 
 supported_versions = ["1.2.%d" % i for i in range(5)] + ["1.2"]