]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/LyX.py
AEA.lyx: update to address latest AEA version
[lyx.git] / lib / lyx2lyx / LyX.py
index 0f3b9b7b6be9345fd8a3f0ad83d05565e96ce459..6bc0fb6f7c4bdec17bb3d5d13021744c03414217 100644 (file)
@@ -1,5 +1,6 @@
 # This file is part of lyx2lyx
 # -*- coding: utf-8 -*-
+# Copyright (C) 2002-2015 The LyX Team
 # Copyright (C) 2002-2004 Dekel Tsur <dekel@lyx.org>
 # Copyright (C) 2002-2006 José Matos <jamatos@lyx.org>
 #
@@ -15,7 +16,7 @@
 #
 # 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
 
 " The LyX module has all the rules related with different lyx file formats."
 
@@ -32,7 +33,7 @@ try:
     import lyx2lyx_version
     version__ = lyx2lyx_version.version
 except: # we are running from build directory so assume the last version
-    version__ = '2.0.0svn'
+    version__ = '2.2'
 
 default_debug__ = 2
 
@@ -63,6 +64,7 @@ def minor_versions(major, last_minor_version):
 format_re = re.compile(r"(\d)[\.,]?(\d\d)")
 fileformat = re.compile(r"\\lyxformat\s*(\S*)")
 original_version = re.compile(r".*?LyX ([\d.]*)")
+original_tex2lyx_version = re.compile(r".*?tex2lyx ([\d.]*)")
 
 ##
 # file format information:
@@ -78,10 +80,14 @@ format_relation = [("0_06",    [200], minor_versions("0.6" , 4)),
                    ("1_1_6_3", [218], ["1.1", "1.1.6.3","1.1.6.4"]),
                    ("1_2",     [220], minor_versions("1.2" , 4)),
                    ("1_3",     [221], minor_versions("1.3" , 7)),
-                   ("1_4", range(222,246), minor_versions("1.4" , 5)),
-                   ("1_5", range(246,277), minor_versions("1.5" , 7)),
-                   ("1_6", range(277,346), minor_versions("1.6" , 0)),
-                   ("2_0",     [],    minor_versions("2.0", 0))]
+                   # Note that range(i,j) is up to j *excluded*.
+                   ("1_4", list(range(222,246)), minor_versions("1.4" , 5)),
+                   ("1_5", list(range(246,277)), minor_versions("1.5" , 7)),
+                   ("1_6", list(range(277,346)), minor_versions("1.6" , 10)),
+                   ("2_0", list(range(346,414)), minor_versions("2.0" , 8)),
+                   ("2_1", list(range(414,475)), minor_versions("2.1" , 0)),
+                   ("2_2", list(range(475,505)), minor_versions("2.2" , 0))
+                  ]
 
 ####################################################################
 # This is useful just for development versions                     #
@@ -124,7 +130,11 @@ def format_info():
 
 def get_end_format():
     " Returns the more recent file format available."
-    return format_relation[-1][1][-1]
+    # this check will fail only when we have a new version
+    # and there is no format change yet.
+    if format_relation[-1][1]:
+      return format_relation[-1][1][-1]
+    return format_relation[-2][1][-1]
 
 
 def get_backend(textclass):
@@ -138,6 +148,9 @@ def get_backend(textclass):
 
 def trim_eol(line):
     " Remove end of line char(s)."
+    if line[-1] != '\n' and line[-1] != '\r':
+        # May happen for the last line of a document
+        return line
     if line[-2:-1] == '\r':
         return line[:-2]
     else:
@@ -176,7 +189,8 @@ class LyX_base:
 
     def __init__(self, end_format = 0, input = "", output = "", error = "",
                  debug = default_debug__, try_hard = 0, cjk_encoding = '',
-                 final_version = "", language = "english", encoding = "auto"):
+                 final_version = "", systemlyxdir = '', language = "english",
+                 encoding = "auto"):
 
         """Arguments:
         end_format: final format that the file should be converted. (integer)
@@ -241,6 +255,7 @@ class LyX_base:
         self.status = 0
         self.encoding = encoding
         self.language = language
+        self.systemlyxdir = systemlyxdir
 
 
     def warning(self, message, debug_level= default_debug__):
@@ -323,6 +338,8 @@ class LyX_base:
         self.initial_version = self.read_version()
 
         # Second pass over header and preamble, now we know the file encoding
+        # Do not forget the textclass (Debian bug #700828)
+        self.textclass = self.textclass.decode(self.encoding)
         for i in range(len(self.header)):
             self.header[i] = self.header[i].decode(self.encoding)
         for i in range(len(self.preamble)):
@@ -403,12 +420,16 @@ class LyX_base:
                 return None
 
             line = line.replace("fix",".")
-            result = original_version.match(line)
+            # need to test original_tex2lyx_version first because tex2lyx
+            # writes "#LyX file created by tex2lyx 2.2"
+            result = original_tex2lyx_version.match(line)
+            if not result:
+                result = original_version.match(line)
+                if result:
+                    # Special know cases: reLyX and KLyX
+                    if line.find("reLyX") != -1 or line.find("KLyX") != -1:
+                        return "0.12"
             if result:
-                # Special know cases: reLyX and KLyX
-                if line.find("reLyX") != -1 or line.find("KLyX") != -1:
-                    return "0.12"
-
                 res = result.group(1)
                 if not res:
                     self.warning(line)
@@ -420,8 +441,22 @@ class LyX_base:
 
     def set_version(self):
         " Set the header with the version used."
-        self.header[0] = " ".join(["#LyX %s created this file." % version__,
-                                  "For more info see http://www.lyx.org/"])
+
+        initial_comment = " ".join(["#LyX %s created this file." % version__,
+                                    "For more info see http://www.lyx.org/"])
+
+        # Simple heuristic to determine the comment that always starts
+        # a lyx file
+        if self.header[0].startswith("#"):
+            self.header[0] = initial_comment
+        else:
+            self.header.insert(0, initial_comment)
+
+        # Old lyx files had a two lines comment header:
+        # 1) the first line had the user who had created it
+        # 2) the second line had the lyx version used
+        # later we decided that 1) was a privacy risk for no gain
+        # here we remove the second line effectively erasing 1)
         if self.header[1][0] == '#':
             del self.header[1]
 
@@ -521,6 +556,11 @@ class LyX_base:
 
     def convert(self):
         "Convert from current (self.format) to self.end_format."
+        if self.format == self.end_format:
+            self.warning("No conversion needed: Target format %s "
+                "same as current format!" % self.format, default_debug__)
+            return
+
         mode, conversion_chain = self.chain()
         self.warning("conversion chain: " + str(conversion_chain), 3)
 
@@ -696,12 +736,14 @@ class File(LyX_base):
 
     def __init__(self, end_format = 0, input = "", output = "", error = "",
                  debug = default_debug__, try_hard = 0, cjk_encoding = '',
-                 final_version = ''):
+                 final_version = '', systemlyxdir = ''):
         LyX_base.__init__(self, end_format, input, output, error,
-                          debug, try_hard, cjk_encoding, final_version)
+                          debug, try_hard, cjk_encoding, final_version,
+                          systemlyxdir)
         self.read()
 
 
+# FIXME: header settings are completely outdated, don't use like this
 #class NewFile(LyX_base):
 #    " This class is to create new LyX files."
 #    def set_header(self, **params):
@@ -730,6 +772,7 @@ class File(LyX_base):
 #            "\\use_amsmath 1",
 #            "\\cite_engine basic",
 #            "\\use_bibtopic false",
+#            "\\use_indices false",
 #            "\\paperorientation portrait",
 #            "\\secnumdepth 3",
 #            "\\tocdepth 3",