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
24 # Allow the dummy object to be able to carry related data
29 # options object, with default values
32 opt.output = sys.stdout
38 format = re.compile(r"(\d)[\.,]?(\d\d)")
39 fileformat = re.compile(r"\\lyxformat\s*(\S*)")
40 lst_ft = ["210", "215", "216", "217", "218", "220"]
43 print """Usage: lyx2lyx [options] file1
44 Convert old lyx file <file1> to newer format.
46 -h, --help this information
47 -v, --version output version information and exit
48 -l, --list list all available formats
49 -d, --debug level level=0..2 (O_ no debug information,2_verbose)
51 -f, --from version initial version (optional)
52 -t, --to version final version (optional)
53 -o, --output name name of the output file or else goes to stdout
54 -q, --quiet same as --debug=0"""
57 def parse_options(argv):
58 _options = ["help", "version", "list", "from=", "to=", "output=", "quiet"]
60 opts, args = getopt.getopt(argv[1:], "f:hlo:qt:v", _options)
66 if o in ("-h", "--help"):
69 if o in ("-v", "--version"):
70 print "lyxconvert, version %s" %(version)
71 print "Copyright (C) 2002 LyX Team"
73 if o in ("-d", "--debug"):
75 if o in ("-q", "--quiet"):
77 if o in ("-l", "--list"):
80 if o in ("-o", "--output"):
81 opt.output = open(a, "w")
82 if o in ("-f", "--from"):
83 opt.start = lyxformat(a)
84 if o in ("-t", "--to"):
85 opt.end = lyxformat(a)
88 opt.end = lst_ft[len(lst_ft)-1]
90 if opt.start and opt.start == opt.end:
91 sys.stderr.write(error.same_format)
94 if opt.start > opt.end:
95 sys.stderr.write(error.newer_format)
99 opt.input = open(args[0])
102 result = format.match(fmt)
104 fmt = result.group(1)+result.group(2)
106 sys.stderr.write(fmt + ": " + error.invalid_format)
108 if fmt not in lst_ft:
109 sys.stderr.write(fmt + ": " + error.format_not_supported)
113 def read_file(file, header, body):
114 """Reads a file into the header and body parts"""
119 line = file.readline()
121 sys.stderr.write(error.invalid_file)
125 if check_token(line, '\\begin_preamble'):
127 if check_token(line, '\\end_preamble'):
130 if not line and not preamble:
134 result = fileformat.match(line)
136 fmt = lyxformat(result.group(1))
139 line = file.readline()
142 body.append(line[:-1])
145 sys.stderr.write(error.invalid_file)
149 def write_file(file, header, body):
151 file.write(line+"\n")
154 file.write(line+"\n")
159 header, body = [], []
160 fmt = read_file(opt.input, header, body)
164 print warning.dont_match + ":", opt.start, fmt
169 start = lst_ft.index(opt.start)
170 end = lst_ft.index(opt.end)
172 for fmt in lst_ft[start:end]:
173 __import__("lyxconvert_" + fmt).convert(header,body)
175 set_comment(header, opt.end)
176 set_format(header, opt.end)
177 write_file(opt.output, header, body)
179 if __name__ == "__main__":