This commit was manufactured by cvs2svn to create tag 'lyx-1_3_2'.
[lyx.git] / lib / lyx2lyx / lyx2lyx
blob756eccfd9efd3bd637d5093b212075431ec9b61e
1 #! /usr/bin/env python
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
22 version = "0.0.2"
24 # Allow the dummy object to be able to carry related data
25 # like a C struct
26 class struct:
27 pass
29 # options object, with default values
30 opt = struct()
32 opt.output = sys.stdout
33 opt.input = sys.stdin
34 opt.start = None
35 opt.end = None
36 opt.quiet = 0
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", "221"]
42 def usage():
43 print """Usage: lyx2lyx [options] file1
44 Convert old lyx file <file1> to newer format.
45 Options:
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)
50 default: level=1
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", "debug=", "from=", "to=", "output=", "quiet"]
59 try:
60 opts, args = getopt.getopt(argv[1:], "d:f:hlo:qt:v", _options)
61 except getopt.error:
62 usage()
63 sys.exit(2)
65 for o, a in opts:
66 if o in ("-h", "--help"):
67 usage()
68 sys.exit()
69 if o in ("-v", "--version"):
70 print "lyxconvert, version %s" %(version)
71 print "Copyright (C) 2002 LyX Team"
72 sys.exit()
73 if o in ("-d", "--debug"):
74 opt.debug = int(a)
75 if o in ("-q", "--quiet"):
76 opt.debug = 0
77 if o in ("-l", "--list"):
78 print lst_ft
79 sys.exit()
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)
87 if not opt.end:
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)
92 sys.exit()
94 if opt.start > opt.end:
95 sys.stderr.write(error.newer_format)
96 sys.exit(1)
98 if args:
99 opt.input = open(args[0])
101 def lyxformat(fmt):
102 result = format.match(fmt)
103 if result:
104 fmt = result.group(1)+result.group(2)
105 else:
106 sys.stderr.write(fmt + ": " + error.invalid_format)
107 sys.exit(2)
108 if fmt not in lst_ft:
109 sys.stderr.write(fmt + ": " + error.format_not_supported)
110 sys.exit(1)
111 return fmt
113 def read_file(file, header, body):
114 """Reads a file into the header and body parts"""
115 fmt = None
116 preamble = 0
118 while 1:
119 line = file.readline()
120 if not line:
121 sys.stderr.write(error.invalid_file)
122 sys.exit(3)
124 line = line[:-1]
125 if check_token(line, '\\begin_preamble'):
126 preamble = 1
127 if check_token(line, '\\end_preamble'):
128 preamble = 0
130 if not preamble:
131 line = string.strip(line)
133 if not line and not preamble:
134 break
136 header.append(line)
137 result = fileformat.match(line)
138 if result:
139 fmt = lyxformat(result.group(1))
141 while 1:
142 line = file.readline()
143 if not line:
144 break
145 body.append(line[:-1])
147 if not fmt:
148 sys.stderr.write(error.invalid_file)
149 sys.exit(3)
150 return fmt
152 def write_file(file, header, body):
153 for line in header:
154 file.write(line+"\n")
155 file.write("\n")
156 for line in body:
157 file.write(line+"\n")
159 def main(argv):
160 parse_options(argv)
162 header, body = [], []
163 fmt = read_file(opt.input, header, body)
165 if opt.start:
166 if opt.start != fmt:
167 sys.stderr.write("%s: %s %s\n" % (warning.dont_match, opt.start, fmt))
168 else:
169 opt.start = fmt
171 # Convertion chain
172 start = lst_ft.index(opt.start)
173 end = lst_ft.index(opt.end)
175 for fmt in lst_ft[start:end]:
176 __import__("lyxconvert_" + fmt).convert(header,body)
178 set_comment(header, opt.end)
179 set_format(header, opt.end)
180 write_file(opt.output, header, body)
182 if __name__ == "__main__":
183 main(sys.argv)