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_format
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"]
41 format_name = {"210" : "2.10", "215":"2.15", "216": "2.16",
42 "217" : "2.17", "218":"218" , "220":"220"}
44 print """Usage: lyx2lyx [options] file1
45 Convert old lyx file <file1> to newer format.
47 -h, --help this information
48 -v, --version output version information and exit
49 -l, --list list all available formats
50 -d, --debug level level=0..2 (O_ no debug information,2_verbose)
52 -f, --from version initial version (optional)
53 -t, --to version final version (optional)
54 -o, --output name name of the output file or else goes to stdout
55 -q, --quiet same as --debug=0"""
58 def parse_options(argv):
59 _options = ["help", "version", "list", "from=", "to=", "output=", "quiet"]
61 opts, args = getopt.getopt(argv[1:], "f:hlo:qt:v", _options)
67 if o in ("-h", "--help"):
70 if o in ("-v", "--version"):
71 print "lyxconvert, version %s" %(version)
72 print "Copyright (C) 2002 LyX Team"
74 if o in ("-d", "--debug"):
76 if o in ("-q", "--quiet"):
78 if o in ("-l", "--list"):
81 if o in ("-o", "--output"):
82 opt.output = open(a, "w")
83 if o in ("-f", "--from"):
84 opt.start = lyxformat(a)
85 if o in ("-t", "--to"):
86 opt.end = lyxformat(a)
89 opt.end = lst_ft[len(lst_ft)-1]
91 if opt.start and opt.start == opt.end:
92 sys.stderr.write(error.same_format)
95 if opt.start > opt.end:
96 sys.stderr.write(error.newer_format)
100 opt.input = open(args[0])
103 result = format.match(fmt)
105 fmt = result.group(1)+result.group(2)
107 sys.stderr.write(fmt + ": " + error.invalid_format)
109 if fmt not in lst_ft:
110 sys.stderr.write(fmt + ": " + error.format_not_supported)
114 def read_file(file, header, body):
115 """Reads a file into the header and body parts"""
118 line = file.readline()
120 sys.stderr.write(error.invalid_file)
128 result = fileformat.match(line)
130 fmt = lyxformat(result.group(1))
133 line = file.readline()
136 body.append(line[:-1])
139 sys.stderr.write(error.invalid_file)
143 def write_file(file, header, body):
145 file.write(line+"\n")
148 file.write(line+"\n")
153 header, body = [], []
154 fmt = read_file(opt.input, header, body)
158 print warning.dont_match + ":", opt.start, fmt
163 start = lst_ft.index(opt.start)
164 end = lst_ft.index(opt.end)
166 for fmt in lst_ft[start:end]:
167 __import__("lyxconvert_" + fmt).convert(header,body)
169 set_format(header,format_name[opt.end])
170 write_file(opt.output, header, body)
172 if __name__ == "__main__":