2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
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.
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.
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.
23 from parser_tools import read_file, write_file, read_version, set_version, \
24 read_format, set_format, chain, lyxformat, get_value, get_backend
27 # Allow the dummy object to be able to carry related data
31 self.output = sys.stdout
32 self.input = sys.stdin
38 self.backend = "latex"
39 self.textclass = "article"
41 def warning(self, message, debug_level= 1):
42 if debug_level <= self.debug:
43 self.err.write(message + "\n")
45 def error(self, message):
47 self.warning("Quiting.")
52 print """Usage: lyx2lyx [options] [file]
53 Convert old lyx file <file> to newer format, files can be compressed with gzip.
54 If there no file is specified then the standard input is assumed, in this case
55 gziped files are not handled.
57 -h, --help this information
58 -v, --version output version information and exit
59 -l, --list list all available formats
60 -d, --debug level level=0..2 (O_ no debug information, 2_verbose)
62 -e, --err error_file name of the error file or else goes to stderr
63 -f, --from version initial version (optional)
64 -t, --to version final version (optional)
65 -o, --output name name of the output file or else goes to stdout
66 -q, --quiet same as --debug=0"""
69 def parse_options(argv, version, opt):
70 _options = ["help", "version", "list", "debug=", "err=", "from=", "to=", "output=", "quiet"]
72 opts, args = getopt.getopt(argv[1:], "d:e:f:hlo:qt:v", _options)
78 if o in ("-h", "--help"):
81 if o in ("-v", "--version"):
82 print "lyx2lyx, version %s" %(version)
83 print "Copyright (C) 2002-2004 José Matos and Dekel Tsur"
85 if o in ("-d", "--debug"):
87 if o in ("-q", "--quiet"):
89 if o in ("-l", "--list"):
90 # list available formats
92 if o in ("-o", "--output"):
93 opt.output = open(a, "w")
94 if o in ("-f", "--from"):
95 opt.start = lyxformat(a, opt)
96 if o in ("-t", "--to"):
97 opt.end = lyxformat(a, opt)
98 if o in ("-e","--err"):
99 opt.err = open(a, "w")
103 opt.dir = os.path.dirname(os.path.abspath(file))
105 gzip.open(file).readline()
106 opt.output = gzip.GzipFile("","wb",6,opt.output)
107 opt.input = gzip.open(file)
109 opt.input = open(file)
115 # options object, with default values
118 parse_options(argv, version, opt)
120 header, body = [], []
122 read_file(header, body, opt)
124 initial_version = read_version(header)
126 opt.textclass = get_value(header, "\\textclass", 0)
127 opt.backend = get_backend( opt.textclass)
128 opt.format = read_format(header, opt)
129 opt.language = get_value(header, "\\language", 0)
130 if opt.language == "":
131 opt.language = "english"
133 mode, convertion_chain = chain(opt, initial_version)
134 opt.warning("convertion chain: " + str(convertion_chain), 3)
136 for step in convertion_chain:
137 convert = getattr(__import__("lyx_" + step), mode)
138 convert(header,body, opt)
140 set_version(header, version)
141 set_format(header, opt.format)
142 write_file(header, body, opt)
145 if __name__ == "__main__":