]> git.lyx.org Git - features.git/blob - lib/lyx2lyx/lyx2lyx
The big lyx2lyx rewrite.
[features.git] / lib / lyx2lyx / lyx2lyx
1 #! /usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 import getopt
20 import gzip
21 import sys
22 from parser_tools import read_file, write_file, read_version, set_version, \
23      read_format, set_format, chain, lyxformat, get_value
24
25 # Allow the dummy object to be able to carry related data
26 # like a C struct
27 class struct:
28     def __init__(self):
29         self.output = sys.stdout
30         self.input = sys.stdin
31         self.err = sys.stderr
32         self.debug = 1
33         self.start = None
34         self.end = None
35
36     def warning(self, message, debug_level= 10):
37         if debug_level <= self.debug:
38             self.err.write(message + "\n")
39
40     def error(self, message):
41         self.warning(message)
42         self.warning("Quiting.")
43         sys.exit(1)
44
45 def usage():
46     print """Usage: lyx2lyx [options] [file]
47 Convert old lyx file <file> to newer format, files can be compressed with gzip.
48 If there no file is specified then the standard input is assumed, in this case
49 gziped files are not handled.
50 Options:
51     -h, --help                  this information
52     -v, --version               output version information and exit
53     -l, --list                  list all available formats
54     -d, --debug level           level=0..2 (O_ no debug information, 2_verbose)
55                                 default: level=1
56     -e, --err error_file        name of the error file or else goes to stderr
57     -f, --from version          initial version (optional)
58     -t, --to version            final version (optional)
59     -o, --output name           name of the output file or else goes to stdout
60     -q, --quiet                 same as --debug=0"""
61
62
63 def parse_options(argv, version, opt):
64     _options =  ["help", "version", "list", "debug=", "err=", "from=", "to=", "output=", "quiet"]
65     try:
66        opts, args = getopt.getopt(argv[1:], "d:e:f:hlo:qt:v", _options)
67     except getopt.error:
68         usage()
69         sys.exit(2)
70
71     for o, a in opts:
72         if o in ("-h", "--help"):
73             usage()
74             sys.exit()
75         if o in ("-v", "--version"):
76             print "lyx2lyx, version %s" %(version)
77             print "Copyright (C) 2002-2004 José Matos and Dekel Tsur"
78             sys.exit()
79         if o in ("-d", "--debug"):
80             opt.debug = int(a)
81         if o in ("-q", "--quiet"):
82             opt.debug = 0
83         if o in ("-l", "--list"):
84             # list available formats
85             sys.exit()
86         if o in ("-o", "--output"):
87             opt.output = open(a, "w")
88         if o in ("-f", "--from"):
89             opt.start = lyxformat(a, opt)
90         if o in ("-t", "--to"):
91             opt.end = lyxformat(a, opt)
92         if o in ("-e","--err"):
93             opt.err = open(a, "w")
94
95     if args:
96         file = args[0]
97         try:
98             gzip.open(file).readline()
99             opt.output = gzip.GzipFile("","wb",6,opt.output)
100             opt.input = gzip.open(file)
101         except:
102             opt.input = open(file)
103
104 def main(argv):
105     version = "1.4.0cvs"
106
107     # options object, with default values
108     opt = struct()
109     
110     parse_options(argv, version, opt)
111
112     header, body = [], []
113
114     read_file(header, body, opt)
115
116     initial_version = read_version(header)
117     opt.format  = read_format(header, opt)
118     opt.language = get_value(header, "\\language", 0)
119     if opt.language == "":
120         opt.language = "english"
121
122     mode, convertion_chain = chain(opt, initial_version)
123     opt.warning("convertion chain: " + str(convertion_chain), 3)
124
125     for step in convertion_chain:
126         convert = getattr(__import__("lyx_" + step), mode)
127         convert(header,body, opt)
128
129     set_version(header, version)
130     set_format(header, opt.format)
131     write_file(header, body, opt)
132
133 if __name__ == "__main__":
134     main(sys.argv)