2 # Copyright (C) 2002 José Matos <jamatos@lyx.org>
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 import getopt, sys, string, re
19 from error import error, warning
20 from parser_tools import set_comment, set_format, check_token
25 # Allow the dummy object to be able to carry related data
30 # options object, with default values
33 opt.output = sys.stdout
39 format = re.compile(r"(\d)[\.,]?(\d\d)")
40 fileformat = re.compile(r"\\lyxformat\s*(\S*)")
41 lst_ft = ["210", "215", "216", "217", "218", "220", "221", "223", "224", "225"]
44 print """Usage: lyx2lyx [options] [file]
45 Convert old lyx file <file> to newer format, files can be compressed with gzip.
46 If there no file is specified then the standard input is assumed, in this case
47 gziped files are not handled.
49 -h, --help this information
50 -v, --version output version information and exit
51 -l, --list list all available formats
52 -d, --debug level level=0..2 (O_ no debug information,2_verbose)
54 -f, --from version initial version (optional)
55 -t, --to version final version (optional)
56 -o, --output name name of the output file or else goes to stdout
57 -q, --quiet same as --debug=0"""
60 def parse_options(argv):
61 _options = ["help", "version", "list", "debug=", "from=", "to=", "output=", "quiet"]
63 opts, args = getopt.getopt(argv[1:], "d:f:hlo:qt:v", _options)
69 if o in ("-h", "--help"):
72 if o in ("-v", "--version"):
73 print "lyxconvert, version %s" %(version)
74 print "Copyright (C) 2002-2003 LyX Team"
76 if o in ("-d", "--debug"):
78 if o in ("-q", "--quiet"):
80 if o in ("-l", "--list"):
83 if o in ("-o", "--output"):
84 opt.output = open(a, "w")
85 if o in ("-f", "--from"):
86 opt.start = lyxformat(a)
87 if o in ("-t", "--to"):
88 opt.end = lyxformat(a)
91 opt.end = lst_ft[len(lst_ft)-1]
93 if opt.start and opt.start == opt.end:
94 sys.stderr.write(error.same_format)
97 if opt.start > opt.end:
98 sys.stderr.write(error.newer_format)
104 gzip.open(file).readline()
105 opt.output = gzip.GzipFile("","wb",6,opt.output)
106 opt.input = gzip.open(file)
108 opt.input = open(file)
111 result = format.match(fmt)
113 fmt = result.group(1)+result.group(2)
115 sys.stderr.write(fmt + ": " + error.invalid_format)
122 if x < int(lst_ft[-1]) and x > int(lst_ft[-2]):
123 sys.stderr.write("lyx2lyx: A development version file.\n")
126 sys.stderr.write(fmt + ": " + error.format_not_supported)
129 def read_file(file, header, body):
130 """Reads a file into the header and body parts"""
135 line = file.readline()
137 sys.stderr.write(error.invalid_file)
141 if check_token(line, '\\begin_preamble'):
143 if check_token(line, '\\end_preamble'):
147 line = string.strip(line)
149 if not line and not preamble:
153 result = fileformat.match(line)
155 fmt = lyxformat(result.group(1))
158 line = file.readline()
161 body.append(line[:-1])
164 sys.stderr.write(error.invalid_file)
168 def write_file(file, header, body):
170 file.write(line+"\n")
173 file.write(line+"\n")
178 header, body = [], []
179 fmt = read_file(opt.input, header, body)
183 sys.stderr.write("%s: %s %s\n" % (warning.dont_match, opt.start, fmt))
188 start = lst_ft.index(opt.start)
189 end = lst_ft.index(opt.end)
191 for fmt in lst_ft[start:end]:
192 __import__("lyxconvert_" + fmt).convert(header,body)
194 set_comment(header, version)
195 set_format(header, opt.end)
196 write_file(opt.output, header, body)
198 if __name__ == "__main__":