]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx2lyx
whitespace, bugfixes, and convertions dependent on the document type, if need.
[lyx.git] / lib / lyx2lyx / lyx2lyx
1 #! /usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 import getopt
20 import gzip
21 import sys
22 import os.path
23 from parser_tools import read_file, write_file, read_version, set_version, \
24      read_format, set_format, chain, lyxformat, get_value, get_backend
25
26
27 # Allow the dummy object to be able to carry related data
28 # like a C struct
29 class struct:
30     def __init__(self):
31         self.output = sys.stdout
32         self.input = sys.stdin
33         self.err = sys.stderr
34         self.debug = 1
35         self.dir = ""
36         self.start = None
37         self.end = None
38         self.backend = "latex"
39         self.textclass = "article"
40
41     def warning(self, message, debug_level= 1):
42         if debug_level <= self.debug:
43             self.err.write(message + "\n")
44
45     def error(self, message):
46         self.warning(message)
47         self.warning("Quiting.")
48         sys.exit(1)
49
50
51 def usage():
52     print """Usage: lyx2lyx [options] [file]
53 Convert old lyx file <file> to newer format, files can be compressed with gzip.
54 If there no file is specified then the standard input is assumed, in this case
55 gziped files are not handled.
56 Options:
57     -h, --help                  this information
58     -v, --version               output version information and exit
59     -l, --list                  list all available formats
60     -d, --debug level           level=0..2 (O_ no debug information, 2_verbose)
61                                 default: level=1
62     -e, --err error_file        name of the error file or else goes to stderr
63     -f, --from version          initial version (optional)
64     -t, --to version            final version (optional)
65     -o, --output name           name of the output file or else goes to stdout
66     -q, --quiet                 same as --debug=0"""
67
68
69 def parse_options(argv, version, opt):
70     _options =  ["help", "version", "list", "debug=", "err=", "from=", "to=", "output=", "quiet"]
71     try:
72        opts, args = getopt.getopt(argv[1:], "d:e:f:hlo:qt:v", _options)
73     except getopt.error:
74         usage()
75         sys.exit(2)
76
77     for o, a in opts:
78         if o in ("-h", "--help"):
79             usage()
80             sys.exit()
81         if o in ("-v", "--version"):
82             print "lyx2lyx, version %s" %(version)
83             print "Copyright (C) 2002-2004 José Matos and Dekel Tsur"
84             sys.exit()
85         if o in ("-d", "--debug"):
86             opt.debug = int(a)
87         if o in ("-q", "--quiet"):
88             opt.debug = 0
89         if o in ("-l", "--list"):
90             # list available formats
91             sys.exit()
92         if o in ("-o", "--output"):
93             opt.output = open(a, "w")
94         if o in ("-f", "--from"):
95             opt.start = lyxformat(a, opt)
96         if o in ("-t", "--to"):
97             opt.end = lyxformat(a, opt)
98         if o in ("-e","--err"):
99             opt.err = open(a, "w")
100
101     if args:
102         file = args[0]
103         opt.dir = os.path.dirname(os.path.abspath(file))
104         try:
105             gzip.open(file).readline()
106             opt.output = gzip.GzipFile("","wb",6,opt.output)
107             opt.input = gzip.open(file)
108         except:
109             opt.input = open(file)
110
111
112 def main(argv):
113     version = "1.4.0cvs"
114
115     # options object, with default values
116     opt = struct()
117     
118     parse_options(argv, version, opt)
119
120     header, body = [], []
121
122     read_file(header, body, opt)
123
124     initial_version = read_version(header)
125
126     opt.textclass = get_value(header, "\\textclass", 0)
127     opt.backend = get_backend( opt.textclass)
128     opt.format  = read_format(header, opt)
129     opt.language = get_value(header, "\\language", 0)
130     if opt.language == "":
131         opt.language = "english"
132
133     mode, convertion_chain = chain(opt, initial_version)
134     opt.warning("convertion chain: " + str(convertion_chain), 3)
135
136     for step in convertion_chain:
137         convert = getattr(__import__("lyx_" + step), mode)
138         convert(header,body, opt)
139
140     set_version(header, version)
141     set_format(header, opt.format)
142     write_file(header, body, opt)
143
144
145 if __name__ == "__main__":
146     main(sys.argv)