]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx2lyx
\end_document replaces \the_end.
[lyx.git] / lib / lyx2lyx / lyx2lyx
index 7960ffd27b2ab55353009ba08114ab7bb8777c41..c7629b9c22533a006e51b3d884ac4b62255597cf 100755 (executable)
 
 import getopt, sys, string, re
 from error import error, warning
-from parser_tools import set_comment, set_format
+from parser_tools import set_comment, set_format, check_token
+import gzip
 
-version = "0.0.2"
+version = "1.4.0cvs"
 
 # Allow the dummy object to be able to carry related data
 # like a C struct
@@ -37,11 +38,13 @@ opt.quiet = 0
 
 format = re.compile(r"(\d)[\.,]?(\d\d)")
 fileformat = re.compile(r"\\lyxformat\s*(\S*)")
-lst_ft = ["210", "215", "216", "217", "218", "220"]
+lst_ft = ["210", "215", "216", "217", "218", "220", "221", "223", "224", "225"]
 
 def usage():
-    print """Usage: lyx2lyx [options] file1
-Convert old lyx file <file1> to newer format.
+    print """Usage: lyx2lyx [options] [file]
+Convert old lyx file <file> to newer format, files can be compressed with gzip.
+If there no file is specified then the standard input is assumed, in this case
+gziped files are not handled.
 Options:
     -h, --help                 this information
     -v, --version              output version information and exit
@@ -55,9 +58,9 @@ Options:
 
 
 def parse_options(argv):
-    _options =  ["help", "version", "list", "from=", "to=", "output=", "quiet"]
+    _options =  ["help", "version", "list", "debug=", "from=", "to=", "output=", "quiet"]
     try:
-       opts, args = getopt.getopt(argv[1:], "f:hlo:qt:v", _options)
+       opts, args = getopt.getopt(argv[1:], "d:f:hlo:qt:v", _options)
     except getopt.error:
         usage()
         sys.exit(2)
@@ -68,7 +71,7 @@ def parse_options(argv):
             sys.exit()
         if o in ("-v", "--version"):
             print "lyxconvert, version %s" %(version)
-            print "Copyright (C) 2002 LyX Team"
+            print "Copyright (C) 2002-2003 LyX Team"
             sys.exit()
         if o in ("-d", "--debug"):
             opt.debug = int(a)
@@ -96,7 +99,13 @@ def parse_options(argv):
         sys.exit(1)
 
     if args:
-        opt.input = open(args[0])
+        file = args[0]
+        try:
+            gzip.open(file).readline()
+            opt.output = gzip.GzipFile("","wb",6,opt.output)
+            opt.input = gzip.open(file)
+        except:
+            opt.input = open(file)
 
 def lyxformat(fmt):
     result = format.match(fmt)
@@ -105,14 +114,23 @@ def lyxformat(fmt):
     else:
         sys.stderr.write(fmt + ": " + error.invalid_format)
         sys.exit(2)
-    if fmt not in lst_ft:
-        sys.stderr.write(fmt + ": " + error.format_not_supported)
-        sys.exit(1)
-    return fmt
+
+    if fmt in lst_ft:
+        return fmt
+
+    x = int(fmt)
+    if x < int(lst_ft[-1]) and x > int(lst_ft[-2]):
+        sys.stderr.write("lyx2lyx: A development version file.\n")
+        return lst_ft[-2]
+
+    sys.stderr.write(fmt + ": " + error.format_not_supported)
+    sys.exit(1)
 
 def read_file(file, header, body):
     """Reads a file into the header and body parts"""
     fmt = None
+    preamble = 0
+
     while 1:
         line = file.readline()
         if not line:
@@ -120,7 +138,15 @@ def read_file(file, header, body):
             sys.exit(3)
 
         line = line[:-1]
-        if not line:
+        if check_token(line, '\\begin_preamble'):
+            preamble = 1
+        if check_token(line, '\\end_preamble'):
+            preamble = 0
+
+        if not preamble:
+            line = string.strip(line)
+
+        if not line and not preamble:
             break
 
         header.append(line)
@@ -154,7 +180,7 @@ def main(argv):
 
     if opt.start:
         if opt.start != fmt:
-            print warning.dont_match + ":", opt.start, fmt
+            sys.stderr.write("%s: %s %s\n" % (warning.dont_match, opt.start, fmt))
     else:
         opt.start = fmt
 
@@ -165,7 +191,7 @@ def main(argv):
     for fmt in lst_ft[start:end]:
        __import__("lyxconvert_" + fmt).convert(header,body)
 
-    set_comment(header, opt.end)
+    set_comment(header, version)
     set_format(header, opt.end)
     write_file(opt.output, header, body)