]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_0_12.py
Fix typo
[lyx.git] / lib / lyx2lyx / lyx_0_12.py
index 414b0c93032d2ba077486c54d36f8ac0571bbd56..b742dd52723dc2ee72265d6d3b67b168b012bce9 100644 (file)
@@ -1,6 +1,6 @@
 # This file is part of lyx2lyx
-# -*- coding: iso-8859-1 -*-
-# Copyright (C) 2003-2004 José Matos <jamatos@lyx.org>
+# -*- coding: utf-8 -*-
+# Copyright (C) 2003-2004 José Matos <jamatos@lyx.org>
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
 #
 # 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 0.12"""
 
 import re
-import string
 from parser_tools import find_token, find_re, check_token
 
 
-def space_before_layout(lines):
+def space_before_layout(document):
+    " Remove empty line before \\layout. "
+    lines = document.body
     i = 2 # skip first layout
-    while 1:
+    while True:
         i = find_token(lines, '\\layout', i)
         if i == -1:
             break
 
-        if lines[i - 1] == '' and string.find(lines[i-2],'\\protected_separator') == -1:
+        prot_space = lines[i-2].find('\\protected_separator')
+        if lines[i - 1] == '' and prot_space == -1:
             del lines[i-1]
         i = i + 1
 
 
-def formula_inset_space_eat(lines):
-    i=0
-    while 1:
+def formula_inset_space_eat(document):
+    " Remove space after inset formula."
+    lines = document.body
+    i = 0
+    while True:
         i = find_token(lines, "\\begin_inset Formula", i)
-        if i == -1: break
+        if i == -1:
+            break
 
         if len(lines[i]) > 22 and lines[i][21] == ' ':
             lines[i] = lines[i][:20] + lines[i][21:]
         i = i + 1
 
 
-# Update from tabular format 2 to 4
-def update_tabular(lines):
+def update_tabular(document):
+    " Update from tabular format 1 or 2 to 4."
+    lines = document.body
     lyxtable_re = re.compile(r".*\\LyXTable$")
-    i=0
-    while 1:
+    i = 0
+    while True:
         i = find_re(lines, lyxtable_re, i)
         if i == -1:
             break
         i = i + 1
-        format = lines[i][8]
+        format = lines[i][8:]
 
-        lines[i]='multicol4'
+        lines[i] = 'multicol4'
         i = i + 1
-        rows = int(string.split(lines[i])[0])
-        columns = int(string.split(lines[i])[1])
+        rows = int(lines[i].split()[0])
+        columns = int(lines[i].split()[1])
 
         lines[i] = lines[i] + ' 0 0 -1 -1 -1 -1'
         i = i + 1
@@ -71,24 +79,36 @@ def update_tabular(lines):
             lines[i] = lines[i] + ' '
             i = i + 1
 
-        while lines[i]:
+        while lines[i].strip():
+            if not format:
+                lines[i] = lines[i] + ' 1 1'
             lines[i] = lines[i] + ' 0 0 0'
             i = i + 1
 
+        lines[i] = lines[i].strip()
+
 
-def final_dot(lines):
+def final_dot(document):
+    " Merge lines if the dot is the final character."
+    lines = document.body
     i = 0
     while i < len(lines):
-        if lines[i][-1:] == '.' and lines[i+1][:1] != '\\' and  lines[i+1][:1] != ' ' and len(lines[i]) + len(lines[i+1])<= 72 and lines[i+1] != '':
+        
+        if lines[i][-1:] == '.' and lines[i+1][:1] != '\\' and \
+               lines[i+1][:1] != ' ' and len(lines[i]) + len(lines[i+1])<= 72 \
+               and lines[i+1] != '':
+
             lines[i] = lines[i] + lines[i+1]
             del lines[i+1]
         else:
             i = i + 1
 
 
-def update_inset_label(lines):
+def update_inset_label(document):
+    " Update inset Label."
+    lines = document.body
     i = 0
-    while 1:
+    while True:
         i = find_token(lines, '\\begin_inset Label', i)
         if i == -1:
             return
@@ -96,23 +116,32 @@ def update_inset_label(lines):
         i = i + 1
 
 
-def update_latexdel(lines):
+def update_latexdel(document):
+    " Update inset LatexDel."
+    lines = document.body
     i = 0
-    while 1:
+    while True:
         i = find_token(lines, '\\begin_inset LatexDel', i)
         if i == -1:
             return
-        lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand')
+        lines[i] = lines[i].replace('\\begin_inset LatexDel',
+                                    '\\begin_inset LatexCommand')
         i = i + 1
 
 
-def update_vfill(lines):
+def update_vfill(document):
+    " Update fill_top and fill_bottom."
+    lines = document.body
     for i in range(len(lines)):
-        lines[i] = string.replace(lines[i],'\\fill_top','\\added_space_top vfill')
-        lines[i] = string.replace(lines[i],'\\fill_bottom','\\added_space_bottom vfill')
+        lines[i] = lines[i].replace('\\fill_top',
+                                    '\\added_space_top vfill')
+        lines[i] = lines[i].replace('\\fill_bottom',
+                                    '\\added_space_bottom vfill')
 
 
-def update_space_units(lines):
+def update_space_units(document):
+    " Update space units."
+    lines = document.body
     added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)')
     added_space_top    = re.compile(r'\\added_space_top ([^ ]*)')
     for i in range(len(lines)):
@@ -120,35 +149,35 @@ def update_space_units(lines):
         if result:
             old = '\\added_space_bottom ' + result.group(1)
             new = '\\added_space_bottom ' + str(float(result.group(1))) + 'cm'
-            lines[i] = string.replace(lines[i], old, new)
+            lines[i] = lines[i].replace(old, new)
 
         result = added_space_top.search(lines[i])
         if result:
             old = '\\added_space_top ' + result.group(1)
             new = '\\added_space_top ' + str(float(result.group(1))) + 'cm'
-            lines[i] = string.replace(lines[i], old, new)
+            lines[i] = lines[i].replace(old, new)
 
 
-def update_inset_accent(lines):
-    pass
-
-
-def remove_cursor(lines):
+def remove_cursor(document):
+    " Remove cursor, it is not saved on the file anymore."
+    lines = document.body
     i = 0
     cursor_re = re.compile(r'.*(\\cursor \d*)')
-    while 1:
+    while True:
         i = find_re(lines, cursor_re, i)
         if i == -1:
             break
         cursor = cursor_re.search(lines[i]).group(1)
-        lines[i]= string.replace(lines[i], cursor, '')
+        lines[i] = lines[i].replace(cursor, '')
         i = i + 1
 
 
-def remove_empty_insets(lines):
+def remove_empty_insets(document):
+    " Remove empty insets."
+    lines = document.body
     i = 0
-    while 1:
-        i = find_token(lines, '\\begin_inset ',i)
+    while True:
+        i = find_token(lines, '\\begin_inset ', i)
         if i == -1:
             break
         if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ':
@@ -157,9 +186,11 @@ def remove_empty_insets(lines):
         i = i + 1
 
 
-def remove_formula_latex(lines):
+def remove_formula_latex(document):
+    " Remove formula latex."
+    lines = document.body
     i = 0
-    while 1:
+    while True:
         i = find_token(lines, '\\latex formula_latex ', i)
         if i == -1:
             break
@@ -171,33 +202,30 @@ def remove_formula_latex(lines):
         del lines[i]
 
 
-def add_end_document(lines):
+def add_end_document(document):
+    " Add \\the_end to the end of the document."
+    lines = document.body
     i = find_token(lines, '\\the_end', 0)
     if i == -1:
         lines.append('\\the_end')
 
 
-def header_update(lines, file):
+def header_update(document):
+    " Update document header."
+    lines = document.header
     i = 0
     l = len(lines)
     while i < l:
-        if check_token(lines[i], '\\begin_preamble'):
-            i = find_token(lines, '\\end_preamble', i)
-            if i == -1:
-                file.error('Unfinished preamble')
-            i = i + 1
-            continue
-
         if lines[i][-1:] == ' ':
             lines[i] = lines[i][:-1]
 
         if check_token(lines[i], '\\epsfig'):
-            lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
+            lines[i] = lines[i].replace('\\epsfig', '\\graphics')
             i = i + 1
             continue
 
         if check_token(lines[i], '\\papersize'):
-            size = string.split(lines[i])[1]
+            size = lines[i].split()[1]
             new_size = size
             paperpackage = ""
 
@@ -220,7 +248,7 @@ def header_update(lines, file):
 
 
         if check_token(lines[i], '\\baselinestretch'):
-            size = string.split(lines[i])[1]
+            size = lines[i].split()[1]
             if size == '1.00':
                 name = 'single'
             elif size == '1.50':
@@ -236,17 +264,18 @@ def header_update(lines, file):
         i = i + 1
 
 
-def update_latexaccents(file):
-    body = file.body
+def update_latexaccents(document):
+    " Update latex accent insets."
+    body = document.body
     i = 1
-    while 1:
+    while True:
         i = find_token(body, '\\i ', i)
         if i == -1:
             return
 
-        contents = string.strip(body[i][2:])
+        contents = body[i][2:].strip()
 
-        if string.find(contents, '{') != -1 and string.find(contents, '}') != -1:
+        if contents.find('{') != -1 and contents.find('}') != -1:
             i = i + 1
             continue
 
@@ -264,27 +293,48 @@ def update_latexaccents(file):
         i = i + 1
 
 
-def convert(file):
-    header_update(file.header, file)
-    add_end_document(file.body)
-    remove_cursor(file.body)
-    final_dot(file.body)
-    update_inset_label(file.body)
-    update_latexdel(file.body)
-    update_space_units(file.body)
-    update_inset_accent(file.body)
-    space_before_layout(file.body)
-    formula_inset_space_eat(file.body)
-    update_tabular(file.body)
-    update_vfill(file.body)
-    remove_empty_insets(file.body)
-    remove_formula_latex(file.body)
-    update_latexaccents(file)
-    file.format = 215
-
-
-def revert(file):
-    file.error("The convertion to an older format (%s) is not implemented." % file.format)
+def obsolete_latex_title(document):
+    " Replace layout Latex_Title with Title."
+    body = document.body
+    i = 0
+    while True:
+        i = find_token(body, '\\layout', i)
+        if i == -1:
+            return
+
+        if body[i].lower().find('latex_title') != -1:
+            body[i] = '\\layout Title'
+
+        i = i + 1
+
+
+def remove_inset_latex(document):
+    "Replace inset latex with layout LaTeX"
+    body = document.body
+
+    i = 0
+    while True:
+        i = find_token(body, '\\begin_inset Latex', i)
+        if i == -1:
+            return
+
+        body[i] = body[i].replace('\\begin_inset Latex', '\\layout LaTeX')
+        i = find_token(body, '\\end_inset', i)
+        if i == -1:
+            #this should not happen
+            return
+        del body[i]
+        
+    
+supported_versions = ["0.12.0","0.12.1","0.12"]
+convert = [[215, [header_update, add_end_document, remove_cursor,
+                  final_dot, update_inset_label, update_latexdel,
+                  update_space_units, space_before_layout,
+                  formula_inset_space_eat, update_tabular,
+                  update_vfill, remove_empty_insets,
+                  remove_formula_latex, update_latexaccents,
+                  obsolete_latex_title, remove_inset_latex]]]
+revert  = []
 
 
 if __name__ == "__main__":