]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx2lyx
51e830babd553325afd364c4de986012c3de1826
[lyx.git] / lib / lyx2lyx / lyx2lyx
1 #! /usr/bin/env python
2 # Copyright (C) 2002 José Matos <jamatos@lyx.org>
3 #
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.
8 #
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.
13 #
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.
17
18 import getopt, sys, string, re
19 from error import error, warning
20 from parser_tools import set_format
21
22 version = "0.0.2"
23
24 # Allow the dummy object to be able to carry related data
25 # like a C struct
26 class struct:
27     pass
28
29 # options object, with default values
30 opt = struct()
31
32 opt.output = sys.stdout
33 opt.input = sys.stdin
34 opt.start = None
35 opt.end = None
36 opt.quiet = 0
37
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"}
43 def usage():
44     print """Usage: lyx2lyx [options] file1
45 Convert old lyx file <file1> to newer format.
46 Options:
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)
51                                 default: level=1
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"""
56
57
58 def parse_options(argv):
59     _options =  ["help", "version", "list", "from=", "to=", "output=", "quiet"]
60     try:
61        opts, args = getopt.getopt(argv[1:], "f:hlo:qt:v", _options)
62     except getopt.error:
63         usage()
64         sys.exit(2)
65
66     for o, a in opts:
67         if o in ("-h", "--help"):
68             usage()
69             sys.exit()
70         if o in ("-v", "--version"):
71             print "lyxconvert, version %s" %(version)
72             print "Copyright (C) 2002 LyX Team"
73             sys.exit()
74         if o in ("-d", "--debug"):
75             opt.debug = int(a)
76         if o in ("-q", "--quiet"):
77             opt.debug = 0
78         if o in ("-l", "--list"):
79             print lst_ft
80             sys.exit()
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)
87
88     if not opt.end:
89         opt.end = lst_ft[len(lst_ft)-1]
90
91     if opt.start and opt.start == opt.end:
92         sys.stderr.write(error.same_format)
93         sys.exit()
94
95     if opt.start > opt.end:
96         sys.stderr.write(error.newer_format)
97         sys.exit(1)
98
99     if args:
100         opt.input = open(args[0])
101
102 def lyxformat(fmt):
103     result = format.match(fmt)
104     if result:
105         fmt = result.group(1)+result.group(2)
106     else:
107         sys.stderr.write(fmt + ": " + error.invalid_format)
108         sys.exit(2)
109     if fmt not in lst_ft:
110         sys.stderr.write(fmt + ": " + error.format_not_supported)
111         sys.exit(1)
112     return fmt
113
114 def read_file(file, header, body):
115     """Reads a file into the header and body parts"""
116     fmt = None
117     while 1:
118         line = file.readline()
119         if not line:
120             sys.stderr.write(error.invalid_file)
121             sys.exit(3)
122
123         line = line[:-1]
124         if not line:
125             break
126
127         header.append(line)
128         result = fileformat.match(line)
129         if result:
130             fmt = lyxformat(result.group(1))
131
132     while 1:
133         line = file.readline()
134         if not line:
135             break
136         body.append(line[:-1])
137
138     if not fmt:
139         sys.stderr.write(error.invalid_file)
140         sys.exit(3)
141     return fmt
142
143 def write_file(file, header, body):
144     for line in header:
145         file.write(line+"\n")
146     file.write("\n")
147     for line in body:
148         file.write(line+"\n")
149
150 def main(argv):
151     parse_options(argv)
152
153     header, body = [], []
154     fmt =  read_file(opt.input, header, body)
155
156     if opt.start:
157         if opt.start != fmt:
158             print warning.dont_match + ":", opt.start, fmt
159     else:
160         opt.start = fmt
161
162     # Convertion chain
163     start = lst_ft.index(opt.start)
164     end = lst_ft.index(opt.end)
165
166     for fmt in lst_ft[start:end]:
167         __import__("lyxconvert_" + fmt).convert(header,body)
168
169     set_format(header,format_name[opt.end])
170     write_file(opt.output, header, body)
171     
172 if __name__ == "__main__":
173     main(sys.argv)