]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/LyX.py
clean up french language handling
[lyx.git] / lib / lyx2lyx / LyX.py
index ec6e1297879023a1cc55411a30f35ff9f968ab27..7647dc830448578e7f6bbaa286d18dd0ad508536 100644 (file)
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-from parser_tools import get_value, check_token, find_token, find_tokens, find_end_of, find_end_of_inset
+from parser_tools import get_value, check_token, find_token,\
+     find_tokens, find_end_of, find_end_of_inset
 import os.path
 import gzip
 import sys
 import re
 import string
+import time
 
-version = "1.4.0cvs"
+version_lyx2lyx = "1.4.0cvs"
 default_debug_level = 2
 
 # Regular expressions used
@@ -44,7 +46,7 @@ format_relation = [("0_10",  [210], ["0.10.7","0.10"]),
                    ("1_1_6fix3", [218], ["1.1.6fix3","1.1.6fix4","1.1"]),
                    ("1_2", [220], ["1.2.0","1.2.1","1.2.3","1.2.4","1.2"]),
                    ("1_3", [221], ["1.3.0","1.3.1","1.3.2","1.3.3","1.3.4","1.3.5","1.3"]),
-                   ("1_4", range(223,238), ["1.4.0cvs","1.4"])]
+                   ("1_4", range(223,243), ["1.4.0cvs","1.4"])]
 
 
 def formats_list():
@@ -76,7 +78,7 @@ def get_backend(textclass):
 #
 class LyX_Base:
     """This class carries all the information of the LyX file."""
-    def __init__(self, end_format = 0, input = "", output = "", error = "", debug = default_debug_level):
+    def __init__(self, end_format = 0, input = "", output = "", error = "", debug = default_debug_level, try_hard = 0):
         """Arguments:
         end_format: final format that the file should be converted. (integer)
         input: the name of the input source, if empty resort to standard input.
@@ -99,6 +101,7 @@ class LyX_Base:
             self.err = sys.stderr
 
         self.debug = debug
+        self.try_hard = try_hard
 
         if end_format:
             self.end_format = self.lyxformat(end_format)
@@ -109,19 +112,23 @@ class LyX_Base:
         self.textclass = "article"
         self.header = []
         self.body = []
+        self.status = 0
 
 
     def warning(self, message, debug_level= default_debug_level):
         " Emits warning to self.error, if the debug_level is less than the self.debug."
         if debug_level <= self.debug:
-            self.err.write(message + "\n")
+            self.err.write("Warning: " + message + "\n")
 
 
     def error(self, message):
-        " Emits a warning and exist incondicionally."
+        " Emits a warning and exits if not in try_hard mode."
         self.warning(message)
-        self.warning("Quiting.")
-        sys.exit(1)
+        if not self.try_hard:
+            self.warning("Quiting.")
+            sys.exit(1)
+
+        self.status = 2
 
 
     def read(self):
@@ -146,8 +153,13 @@ class LyX_Base:
             if not preamble:
                 line = string.strip(line)
 
-            if not line and not preamble:
-                break
+            if not preamble:
+                if not line:
+                    continue
+
+                if string.split(line)[0] in ("\\layout", "\\begin_layout", "\\begin_body"):
+                    self.body.append(line)
+                    break
 
             self.header.append(line)
 
@@ -224,7 +236,7 @@ class LyX_Base:
 
     def set_version(self):
         " Set the header with the version used."
-        self.header[0] = "#LyX %s created this file. For more info see http://www.lyx.org/" % version
+        self.header[0] = "#LyX %s created this file. For more info see http://www.lyx.org/" % version_lyx2lyx
         if self.header[1][0] == '#':
             del self.header[1]
 
@@ -243,7 +255,7 @@ class LyX_Base:
     def set_format(self):
         " Set the file format of the file, in the header."
         if self.format <= 217:
-            format = str(float(format)/100)
+            format = str(float(self.format)/100)
         else:
             format = str(self.format)
         i = find_token(self.header, "\\lyxformat", 0)
@@ -265,8 +277,36 @@ class LyX_Base:
         self.warning("convertion chain: " + str(convertion_chain), 3)
 
         for step in convertion_chain:
-            convert_step = getattr(__import__("lyx_" + step), mode)
-            convert_step(self)
+            steps = getattr(__import__("lyx_" + step), mode)
+
+            self.warning("Convertion step: %s - %s" % (step, mode), default_debug_level + 1)
+            if not steps:
+                    self.error("The convertion to an older format (%s) is not implemented." % self.format)
+
+            multi_conv = len(steps) != 1
+            for version, table in steps:
+                if multi_conv and \
+                   (self.format >= version and mode == "convert") or\
+                   (self.format <= version and mode == "revert"):
+                    continue
+
+                for conv in table:
+                    init_t = time.time()
+                    try:
+                        conv(self)
+                    except:
+                        self.warning("An error ocurred in %s, %s" % (version, str(conv)),
+                                     default_debug_level)
+                        if not self.try_hard:
+                            raise
+                        self.status = 2
+                    else:
+                        self.warning("%lf: Elapsed time on %s"  % (time.time() - init_t, str(conv)),
+                                     default_debug_level + 1)
+
+                self.format = version
+                if self.end_format == self.format:
+                    return
 
 
     def chain(self):
@@ -336,6 +376,7 @@ class LyX_Base:
         " Returns the TOC of this LyX document."
         paragraphs_filter = {'Title' : 0,'Chapter' : 1, 'Section' : 2, 'Subsection' : 3, 'Subsubsection': 4}
         allowed_insets = ['Quotes']
+        allowed_parameters = '\\paragraph_spacing', '\\noindent', '\\align', '\\labelwidthstring', "\\start_of_appendix"
 
         sections = []
         for section in paragraphs_filter.keys():
@@ -361,7 +402,7 @@ class LyX_Base:
 
             k = i + 1
             # skip paragraph parameters
-            while not self.body[k] or self.body[k][0] == '\\':
+            while not string.strip(self.body[k]) and string.split(self.body[k])[0] in allowed_parameters:
                 k = k +1
 
             while k < j:
@@ -391,8 +432,8 @@ class LyX_Base:
 
 class File(LyX_Base):
     " This class reads existing LyX files."
-    def __init__(self, end_format = 0, input = "", output = "", error = "", debug = default_debug_level):
-        LyX_Base.__init__(self, end_format, input, output, error, debug)
+    def __init__(self, end_format = 0, input = "", output = "", error = "", debug = default_debug_level, try_hard = 0):
+        LyX_Base.__init__(self, end_format, input, output, error, debug, try_hard)
         self.read()