2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002 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.
19 import getopt, sys, string, re
20 from error import error, warning
21 from parser_tools import set_comment, set_format, check_token
26 # Allow the dummy object to be able to carry related data
31 # options object, with default values
34 opt.output = sys.stdout
40 format = re.compile(r"(\d)[\.,]?(\d\d)")
41 fileformat = re.compile(r"\\lyxformat\s*(\S*)")
42 lst_ft = [210, 215, 216, 217, 218, 220, 221, 223, 224, 225]
45 print """Usage: lyx2lyx [options] [file]
46 Convert old lyx file <file> to newer format, files can be compressed with gzip.
47 If there no file is specified then the standard input is assumed, in this case
48 gziped files are not handled.
50 -h, --help this information
51 -v, --version output version information and exit
52 -l, --list list all available formats
53 -d, --debug level level=0..2 (O_ no debug information,2_verbose)
55 -f, --from version initial version (optional)
56 -t, --to version final version (optional)
57 -o, --output name name of the output file or else goes to stdout
58 -q, --quiet same as --debug=0"""
61 def parse_options(argv):
62 _options = ["help", "version", "list", "debug=", "from=", "to=", "output=", "quiet"]
64 opts, args = getopt.getopt(argv[1:], "d:f:hlo:qt:v", _options)
70 if o in ("-h", "--help"):
73 if o in ("-v", "--version"):
74 print "lyxconvert, version %s" %(version)
75 print "Copyright (C) 2002-2003 José Matos and Dekel Tsur"
77 if o in ("-d", "--debug"):
79 if o in ("-q", "--quiet"):
81 if o in ("-l", "--list"):
84 if o in ("-o", "--output"):
85 opt.output = open(a, "w")
86 if o in ("-f", "--from"):
87 opt.start = lyxformat(a)
88 if o in ("-t", "--to"):
89 opt.end = lyxformat(a)
92 opt.end = lst_ft[len(lst_ft)-1]
97 gzip.open(file).readline()
98 opt.output = gzip.GzipFile("","wb",6,opt.output)
99 opt.input = gzip.open(file)
101 opt.input = open(file)
104 result = format.match(fmt)
106 fmt = int(result.group(1) + result.group(2))
108 sys.stderr.write(str(fmt) + ": " + error.invalid_format)
114 sys.stderr.write(fmt + ": " + error.format_not_supported)
117 def read_file(file, header, body):
118 """Reads a file into the header and body parts"""
123 line = file.readline()
125 sys.stderr.write(error.invalid_file)
129 if check_token(line, '\\begin_preamble'):
131 if check_token(line, '\\end_preamble'):
135 line = string.strip(line)
137 if not line and not preamble:
141 result = fileformat.match(line)
143 fmt = lyxformat(result.group(1))
146 line = file.readline()
149 body.append(line[:-1])
152 sys.stderr.write(error.invalid_file)
156 def write_file(file, header, body):
158 file.write(line+"\n")
161 file.write(line+"\n")
166 header, body = [], []
167 fmt = read_file(opt.input, header, body)
171 sys.stderr.write("%s: %s %s\n" % (warning.dont_match, opt.start, fmt))
176 if opt.start < opt.end:
182 start = lst_ft.index(opt.start)
183 end = lst_ft.index(opt.end)
185 for fmt in lst_ft[start:end]:
186 __import__(mode + str(fmt)).convert(header,body)
188 set_comment(header, version)
189 set_format(header, opt.end)
190 write_file(opt.output, header, body)
192 if __name__ == "__main__":