]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/LyX.py
Consistent output of breakable/non-breakable dashes on all TeX engines.
[lyx.git] / lib / lyx2lyx / LyX.py
1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2002-2015 The LyX Team
4 # Copyright (C) 2002-2004 Dekel Tsur <dekel@lyx.org>
5 # Copyright (C) 2002-2006 José Matos <jamatos@lyx.org>
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21 " The LyX module has all the rules related with different lyx file formats."
22
23 from parser_tools import get_value, check_token, find_token, \
24      find_tokens, find_end_of
25 import os.path
26 import gzip
27 import locale
28 import sys
29 import re
30 import time
31 import io
32 import codecs
33
34 try:
35     import lyx2lyx_version
36     version__ = lyx2lyx_version.version
37 except: # we are running from build directory so assume the last version
38     version__ = '2.3'
39
40 default_debug__ = 2
41
42 # Provide support for both python 2 and 3
43 PY2 = sys.version_info[0] == 2
44 # End of code to support for both python 2 and 3
45
46 ####################################################################
47 # Private helper functions
48
49 def find_end_of_inset(lines, i):
50     " Find beginning of inset, where lines[i] is included."
51     return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
52
53 def minor_versions(major, last_minor_version):
54     """ Generate minor versions, using major as prefix and minor
55     versions from 0 until last_minor_version, plus the generic version.
56
57     Example:
58
59       minor_versions("1.2", 4) ->
60       [ "1.2", "1.2.0", "1.2.1", "1.2.2", "1.2.3"]
61     """
62     return [major] + [major + ".%d" % i for i in range(last_minor_version + 1)]
63
64
65 # End of helper functions
66 ####################################################################
67
68
69 # Regular expressions used
70 format_re = re.compile(r"(\d)[\.,]?(\d\d)")
71 fileformat = re.compile(r"\\lyxformat\s*(\S*)")
72 original_version = re.compile(r".*?LyX ([\d.]*)")
73 original_tex2lyx_version = re.compile(r".*?tex2lyx ([\d.]*)")
74
75 ##
76 # file format information:
77 #  file, supported formats, stable release versions
78 format_relation = [("0_06",    [200], minor_versions("0.6" , 4)),
79                    ("0_08",    [210], minor_versions("0.8" , 6) + ["0.7"]),
80                    ("0_10",    [210], minor_versions("0.10", 7) + ["0.9"]),
81                    ("0_12",    [215], minor_versions("0.12", 1) + ["0.11"]),
82                    ("1_0",     [215], minor_versions("1.0" , 4)),
83                    ("1_1",     [215], minor_versions("1.1" , 4)),
84                    ("1_1_5",   [216], ["1.1", "1.1.5","1.1.5.1","1.1.5.2"]),
85                    ("1_1_6_0", [217], ["1.1", "1.1.6","1.1.6.1","1.1.6.2"]),
86                    ("1_1_6_3", [218], ["1.1", "1.1.6.3","1.1.6.4"]),
87                    ("1_2",     [220], minor_versions("1.2" , 4)),
88                    ("1_3",     [221], minor_versions("1.3" , 7)),
89                    # Note that range(i,j) is up to j *excluded*.
90                    ("1_4", list(range(222,246)), minor_versions("1.4" , 5)),
91                    ("1_5", list(range(246,277)), minor_versions("1.5" , 7)),
92                    ("1_6", list(range(277,346)), minor_versions("1.6" , 10)),
93                    ("2_0", list(range(346,414)), minor_versions("2.0" , 8)),
94                    ("2_1", list(range(414,475)), minor_versions("2.1" , 5)),
95                    ("2_2", list(range(475,509)), minor_versions("2.2" , 0)),
96                    ("2_3", (), minor_versions("2.3" , 0))
97                   ]
98
99 ####################################################################
100 # This is useful just for development versions                     #
101 # if the list of supported formats is empty get it from last step  #
102 if not format_relation[-1][1]:
103     step, mode = format_relation[-1][0], "convert"
104     convert = getattr(__import__("lyx_" + step), mode)
105     format_relation[-1] = (step,
106                            [conv[0] for conv in convert],
107                            format_relation[-1][2])
108 #                                                                  #
109 ####################################################################
110
111 def formats_list():
112     " Returns a list with supported file formats."
113     formats = []
114     for version in format_relation:
115         for format in version[1]:
116             if format not in formats:
117                 formats.append(format)
118     return formats
119
120
121 def format_info():
122     " Returns a list with supported file formats."
123     out = """Major version:
124         minor versions
125         formats
126 """
127     for version in format_relation:
128         major = str(version[2][0])
129         versions = str(version[2][1:])
130         if len(version[1]) == 1:
131             formats = str(version[1][0])
132         else:
133             formats = "%s - %s" % (version[1][-1], version[1][0])
134         out += "%s\n\t%s\n\t%s\n\n" % (major, versions, formats)
135     return out + '\n'
136
137
138 def get_end_format():
139     " Returns the more recent file format available."
140     # this check will fail only when we have a new version
141     # and there is no format change yet.
142     if format_relation[-1][1]:
143       return format_relation[-1][1][-1]
144     return format_relation[-2][1][-1]
145
146
147 def get_backend(textclass):
148     " For _textclass_ returns its backend."
149     if textclass == "linuxdoc" or textclass == "manpage":
150         return "linuxdoc"
151     if textclass.startswith("docbook") or textclass.startswith("agu-"):
152         return "docbook"
153     return "latex"
154
155
156 def trim_eol(line):
157     " Remove end of line char(s)."
158     if line[-1] != '\n' and line[-1] != '\r':
159         # May happen for the last line of a document
160         return line
161     if line[-2:-1] == '\r':
162         return line[:-2]
163     else:
164         return line[:-1]
165
166
167 def trim_eol_binary(line):
168     " Remove end of line char(s)."
169     if line[-1] != 10 and line[-1] != 13:
170         # May happen for the last line of a document
171         return line
172     if line[-2:-1] == 13:
173         return line[:-2]
174     else:
175         return line[:-1]
176
177
178 def get_encoding(language, inputencoding, format, cjk_encoding):
179     " Returns enconding of the lyx file"
180     if format > 248:
181         return "utf8"
182     # CJK-LyX encodes files using the current locale encoding.
183     # This means that files created by CJK-LyX can only be converted using
184     # the correct locale settings unless the encoding is given as commandline
185     # argument.
186     if cjk_encoding == 'auto':
187         return locale.getpreferredencoding()
188     elif cjk_encoding:
189         return cjk_encoding
190     from lyx2lyx_lang import lang
191     if inputencoding == "auto" or inputencoding == "default":
192         return lang[language][3]
193     if inputencoding == "":
194         return "latin1"
195     if inputencoding == "utf8x":
196         return "utf8"
197     # python does not know the alias latin9
198     if inputencoding == "latin9":
199         return "iso-8859-15"
200     return inputencoding
201
202 ##
203 # Class
204 #
205 class LyX_base:
206     """This class carries all the information of the LyX file."""
207
208     def __init__(self, end_format = 0, input = u'', output = u'', error = u'',
209                  debug = default_debug__, try_hard = 0, cjk_encoding = u'',
210                  final_version = u'', systemlyxdir = u'', language = u'english',
211                  encoding = u'auto'):
212
213         """Arguments:
214         end_format: final format that the file should be converted. (integer)
215         input: the name of the input source, if empty resort to standard input.
216         output: the name of the output file, if empty use the standard output.
217         error: the name of the error file, if empty use the standard error.
218         debug: debug level, O means no debug, as its value increases be more verbose.
219         """
220         self.choose_input(input)
221         self.output = output
222
223         if error:
224             self.err = open(error, "w")
225         else:
226             self.err = sys.stderr
227
228         self.debug = debug
229         self.try_hard = try_hard
230         self.cjk_encoding = cjk_encoding
231
232         if end_format:
233             self.end_format = self.lyxformat(end_format)
234
235             # In case the target version and format are both specified
236             # verify that they are compatible. If not send a warning
237             # and ignore the version.
238             if final_version:
239                 message = "Incompatible version %s for specified format %d" % (
240                     final_version, self.end_format)
241                 for version in format_relation:
242                     if self.end_format in version[1]:
243                         if final_version not in version[2]:
244                             self.warning(message)
245                             final_version = ""
246         elif final_version:
247             for version in format_relation:
248                 if final_version in version[2]:
249                     # set the last format for that version
250                     self.end_format = version[1][-1]
251                     break
252             else:
253                 final_version = ""
254         else:
255             self.end_format = get_end_format()
256
257         if not final_version:
258             for step in format_relation:
259                 if self.end_format in step[1]:
260                     final_version = step[2][1]
261         self.final_version = final_version
262         self.warning("Final version: %s" % self.final_version, 10)
263         self.warning("Final format: %d" % self.end_format, 10)
264
265         self.backend = "latex"
266         self.textclass = "article"
267         # This is a hack: We use '' since we don't know the default
268         # layout of the text class. LyX will parse it as default layout.
269         # FIXME: Read the layout file and use the real default layout
270         self.default_layout = ''
271         self.header = []
272         self.preamble = []
273         self.body = []
274         self.status = 0
275         self.encoding = encoding
276         self.language = language
277         self.systemlyxdir = systemlyxdir
278
279
280     def warning(self, message, debug_level= default_debug__):
281         """ Emits warning to self.error, if the debug_level is less
282         than the self.debug."""
283         if debug_level <= self.debug:
284             self.err.write("Warning: " + message + "\n")
285
286
287     def error(self, message):
288         " Emits a warning and exits if not in try_hard mode."
289         self.warning(message)
290         if not self.try_hard:
291             self.warning("Quitting.")
292             sys.exit(1)
293
294         self.status = 2
295
296
297     def read(self):
298         """Reads a file into the self.header and
299         self.body parts, from self.input."""
300
301         # First pass: Read header to determine file encoding
302         # If we are running under python3 then all strings are binary in this
303         # pass. In some cases we need to convert binary to unicode in order to
304         # use our parser tools. Since we do not know the true encoding yet we
305         # use latin1. This works since a) the parts we are interested in are
306         # pure ASCII (subset of latin1) and b) in contrast to pure ascii or
307         # utf8, one can decode any 8byte string using latin1.
308         first_line = True
309         while True:
310             line = self.input.readline()
311             if not line:
312                 # eof found before end of header
313                 self.error("Invalid LyX file: Missing body.")
314
315             if first_line:
316                 # Remove UTF8 BOM marker if present
317                 if line.startswith(codecs.BOM_UTF8):
318                     line = line[len(codecs.BOM_UTF8):]
319
320                 first_line = False
321
322             if PY2:
323                 line = trim_eol(line)
324                 decoded = line
325             else:
326                 line = trim_eol_binary(line)
327                 decoded = line.decode('latin1')
328             if check_token(decoded, '\\begin_preamble'):
329                 while True:
330                     line = self.input.readline()
331                     if not line:
332                         # eof found before end of header
333                         self.error("Invalid LyX file: Missing body.")
334
335                     if PY2:
336                         line = trim_eol(line)
337                         decoded = line
338                     else:
339                         line = trim_eol_binary(line)
340                         decoded = line.decode('latin1')
341                     if check_token(decoded, '\\end_preamble'):
342                         break
343
344                     if decoded.split()[:0] in ("\\layout",
345                                             "\\begin_layout", "\\begin_body"):
346
347                         self.warning("Malformed LyX file:"
348                                      "Missing '\\end_preamble'."
349                                      "\nAdding it now and hoping"
350                                      "for the best.")
351
352                     self.preamble.append(line)
353
354             if check_token(decoded, '\\end_preamble'):
355                 continue
356
357             line = line.rstrip()
358             if not line:
359                 continue
360
361             if decoded.split()[0] in ("\\layout", "\\begin_layout",
362                                    "\\begin_body", "\\begin_deeper"):
363                 self.body.append(line)
364                 break
365
366             self.header.append(line)
367
368         if PY2:
369             i = find_token(self.header, '\\textclass', 0)
370         else:
371             i = find_token(self.header, b'\\textclass', 0)
372         if i == -1:
373             self.warning("Malformed LyX file: Missing '\\textclass'.")
374             if PY2:
375                 i = find_token(self.header, '\\lyxformat', 0) + 1
376                 self.header[i:i] = ['\\textclass article']
377             else:
378                 i = find_token(self.header, b'\\lyxformat', 0) + 1
379                 self.header[i:i] = [b'\\textclass article']
380
381         if PY2:
382             self.textclass = get_value(self.header, "\\textclass", 0,
383                                        default = "")
384             self.language = get_value(self.header, "\\language", 0,
385                                       default = "english")
386             self.inputencoding = get_value(self.header, "\\inputencoding", 0,
387                                            default = "auto")
388         else:
389             self.textclass = get_value(self.header, b"\\textclass", 0,
390                                        default = b"")
391             self.language = get_value(self.header, b"\\language", 0,
392                                       default = b"english").decode('ascii')
393             self.inputencoding = get_value(self.header, b"\\inputencoding", 0,
394                                            default = b"auto").decode('ascii')
395         self.format = self.read_format()
396         self.initial_format = self.format
397         self.encoding = get_encoding(self.language,
398                                      self.inputencoding, self.format,
399                                      self.cjk_encoding)
400         self.initial_version = self.read_version()
401
402         # Second pass over header and preamble, now we know the file encoding
403         # Do not forget the textclass (Debian bug #700828)
404         self.textclass = self.textclass.decode(self.encoding)
405         self.backend = get_backend(self.textclass)
406         for i in range(len(self.header)):
407             self.header[i] = self.header[i].decode(self.encoding)
408         for i in range(len(self.preamble)):
409             self.preamble[i] = self.preamble[i].decode(self.encoding)
410         for i in range(len(self.body)):
411             self.body[i] = self.body[i].decode(self.encoding)
412
413         # Read document body
414         while True:
415             line = self.input.readline().decode(self.encoding)
416             if not line:
417                 break
418             self.body.append(trim_eol(line))
419
420
421     def write(self):
422         " Writes the LyX file to self.output."
423         self.choose_output(self.output)
424         self.set_version()
425         self.set_format()
426         self.set_textclass()
427         if self.encoding == "auto":
428             self.encoding = get_encoding(self.language, self.encoding,
429                                          self.format, self.cjk_encoding)
430         if self.preamble:
431             i = find_token(self.header, '\\textclass', 0) + 1
432             preamble = ['\\begin_preamble'] + self.preamble + ['\\end_preamble']
433             header = self.header[:i] + preamble + self.header[i:]
434         else:
435             header = self.header
436
437         for line in header + [''] + self.body:
438             self.output.write(line+u"\n")
439
440
441     def choose_output(self, output):
442         """Choose output streams dealing transparently with
443         compressed files."""
444
445         # This is a bit complicated, because we need to be compatible both with
446         # python 2 and python 3. Therefore we handle the encoding here and not
447         # when writing individual lines and may need up to 3 layered file like
448         # interfaces.
449         if self.compressed:
450             if output:
451                 outputfileobj = open(output, 'wb')
452             else:
453                 # We cannot not use stdout directly since it needs text, not bytes in python 3
454                 outputfileobj = os.fdopen(sys.stdout.fileno(), 'wb')
455             # We cannot not use gzip.open() since it is not supported by python 2
456             zipbuffer = gzip.GzipFile(mode='wb', fileobj=outputfileobj)
457             # We do not want to use different newlines on different OSes inside zipped files
458             self.output = io.TextIOWrapper(zipbuffer, encoding=self.encoding, newline='\n')
459         else:
460             if output:
461                 self.output = io.open(output, 'w', encoding=self.encoding)
462             else:
463                 self.output = io.open(sys.stdout.fileno(), 'w', encoding=self.encoding)
464
465
466     def choose_input(self, input):
467         """Choose input stream, dealing transparently with
468         compressed files."""
469
470         # Since we do not know the encoding yet we need to read the input as
471         # bytes in binary mode, and convert later to unicode.
472         if input and input != u'-':
473             self.dir = os.path.dirname(os.path.abspath(input))
474             try:
475                 gzip.open(input).readline()
476                 self.input = gzip.open(input)
477                 self.compressed = True
478             except:
479                 self.input = open(input, 'rb')
480                 self.compressed = False
481         else:
482             self.dir = u''
483             self.input = os.fdopen(sys.stdin.fileno(), 'rb')
484             self.compressed = False
485
486
487     def lyxformat(self, format):
488         " Returns the file format representation, an integer."
489         result = format_re.match(format)
490         if result:
491             format = int(result.group(1) + result.group(2))
492         elif format == '2':
493             format = 200
494         else:
495             self.error(str(format) + ": " + "Invalid LyX file.")
496
497         if format in formats_list():
498             return format
499
500         self.error(str(format) + ": " + "Format not supported.")
501         return None
502
503
504     def read_version(self):
505         """ Searchs for clues of the LyX version used to write the
506         file, returns the most likely value, or None otherwise."""
507
508         for line in self.header:
509             if line[0] != "#":
510                 return None
511
512             line = line.replace("fix",".")
513             # need to test original_tex2lyx_version first because tex2lyx
514             # writes "#LyX file created by tex2lyx 2.2"
515             result = original_tex2lyx_version.match(line)
516             if not result:
517                 result = original_version.match(line)
518                 if result:
519                     # Special know cases: reLyX and KLyX
520                     if line.find("reLyX") != -1 or line.find("KLyX") != -1:
521                         return "0.12"
522             if result:
523                 res = result.group(1)
524                 if not res:
525                     self.warning(line)
526                 #self.warning("Version %s" % result.group(1))
527                 return res
528         self.warning(str(self.header[:2]))
529         return None
530
531
532     def set_version(self):
533         " Set the header with the version used."
534
535         initial_comment = " ".join(["#LyX %s created this file." % version__,
536                                     "For more info see http://www.lyx.org/"])
537
538         # Simple heuristic to determine the comment that always starts
539         # a lyx file
540         if self.header[0].startswith("#"):
541             self.header[0] = initial_comment
542         else:
543             self.header.insert(0, initial_comment)
544
545         # Old lyx files had a two lines comment header:
546         # 1) the first line had the user who had created it
547         # 2) the second line had the lyx version used
548         # later we decided that 1) was a privacy risk for no gain
549         # here we remove the second line effectively erasing 1)
550         if self.header[1][0] == '#':
551             del self.header[1]
552
553
554     def read_format(self):
555         " Read from the header the fileformat of the present LyX file."
556         for line in self.header:
557             if PY2:
558                 result = fileformat.match(line)
559             else:
560                 result = fileformat.match(line.decode('ascii'))
561             if result:
562                 return self.lyxformat(result.group(1))
563         else:
564             self.error("Invalid LyX File: Missing format.")
565         return None
566
567
568     def set_format(self):
569         " Set the file format of the file, in the header."
570         if self.format <= 217:
571             format = str(float(self.format)/100)
572         else:
573             format = str(self.format)
574         i = find_token(self.header, "\\lyxformat", 0)
575         self.header[i] = "\\lyxformat %s" % format
576
577
578     def set_textclass(self):
579         i = find_token(self.header, "\\textclass", 0)
580         self.header[i] = "\\textclass %s" % self.textclass
581
582
583     #Note that the module will be added at the END of the extant ones
584     def add_module(self, module):
585       i = find_token(self.header, "\\begin_modules", 0)
586       if i == -1:
587         #No modules yet included
588         i = find_token(self.header, "\\textclass", 0)
589         if i == -1:
590           self.warning("Malformed LyX document: No \\textclass!!")
591           return
592         modinfo = ["\\begin_modules", module, "\\end_modules"]
593         self.header[i + 1: i + 1] = modinfo
594         return
595       j = find_token(self.header, "\\end_modules", i)
596       if j == -1:
597         self.warning("(add_module)Malformed LyX document: No \\end_modules.")
598         return
599       k = find_token(self.header, module, i)
600       if k != -1 and k < j:
601         return
602       self.header.insert(j, module)
603
604
605     def get_module_list(self):
606       i = find_token(self.header, "\\begin_modules", 0)
607       if (i == -1):
608         return []
609       j = find_token(self.header, "\\end_modules", i)
610       return self.header[i + 1 : j]
611
612
613     def set_module_list(self, mlist):
614       modbegin = find_token(self.header, "\\begin_modules", 0)
615       newmodlist = ['\\begin_modules'] + mlist + ['\\end_modules']
616       if (modbegin == -1):
617         #No modules yet included
618         tclass = find_token(self.header, "\\textclass", 0)
619         if tclass == -1:
620           self.warning("Malformed LyX document: No \\textclass!!")
621           return
622         modbegin = tclass + 1
623         self.header[modbegin:modbegin] = newmodlist
624         return
625       modend = find_token(self.header, "\\end_modules", modbegin)
626       if modend == -1:
627         self.warning("(set_module_list)Malformed LyX document: No \\end_modules.")
628         return
629       newmodlist = ['\\begin_modules'] + mlist + ['\\end_modules']
630       self.header[modbegin:modend + 1] = newmodlist
631
632
633     def set_parameter(self, param, value):
634         " Set the value of the header parameter."
635         i = find_token(self.header, '\\' + param, 0)
636         if i == -1:
637             self.warning('Parameter not found in the header: %s' % param, 3)
638             return
639         self.header[i] = '\\%s %s' % (param, str(value))
640
641
642     def is_default_layout(self, layout):
643         " Check whether a layout is the default layout of this class."
644         # FIXME: Check against the real text class default layout
645         if layout == 'Standard' or layout == self.default_layout:
646             return 1
647         return 0
648
649
650     def convert(self):
651         "Convert from current (self.format) to self.end_format."
652         if self.format == self.end_format:
653             self.warning("No conversion needed: Target format %s "
654                 "same as current format!" % self.format, default_debug__)
655             return
656
657         mode, conversion_chain = self.chain()
658         self.warning("conversion chain: " + str(conversion_chain), 3)
659
660         for step in conversion_chain:
661             steps = getattr(__import__("lyx_" + step), mode)
662
663             self.warning("Convertion step: %s - %s" % (step, mode),
664                          default_debug__ + 1)
665             if not steps:
666                 self.error("The conversion to an older "
667                 "format (%s) is not implemented." % self.format)
668
669             multi_conv = len(steps) != 1
670             for version, table in steps:
671                 if multi_conv and \
672                    (self.format >= version and mode == "convert") or\
673                    (self.format <= version and mode == "revert"):
674                     continue
675
676                 for conv in table:
677                     init_t = time.time()
678                     try:
679                         conv(self)
680                     except:
681                         self.warning("An error ocurred in %s, %s" %
682                                      (version, str(conv)),
683                                      default_debug__)
684                         if not self.try_hard:
685                             raise
686                         self.status = 2
687                     else:
688                         self.warning("%lf: Elapsed time on %s" %
689                                      (time.time() - init_t,
690                                       str(conv)), default_debug__ +
691                                      1)
692                 self.format = version
693                 if self.end_format == self.format:
694                     return
695
696
697     def chain(self):
698         """ This is where all the decisions related with the
699         conversion are taken.  It returns a list of modules needed to
700         convert the LyX file from self.format to self.end_format"""
701
702         format = self.format
703         correct_version = 0
704
705         for rel in format_relation:
706             if self.initial_version in rel[2]:
707                 if format in rel[1]:
708                     initial_step = rel[0]
709                     correct_version = 1
710                     break
711
712         if not correct_version:
713             if format <= 215:
714                 self.warning("Version does not match file format, "
715                              "discarding it. (Version %s, format %d)" %
716                              (self.initial_version, self.format))
717             for rel in format_relation:
718                 if format in rel[1]:
719                     initial_step = rel[0]
720                     break
721             else:
722                 # This should not happen, really.
723                 self.error("Format not supported.")
724
725         # Find the final step
726         for rel in format_relation:
727             if self.end_format in rel[1]:
728                 final_step = rel[0]
729                 break
730         else:
731             self.error("Format not supported.")
732
733         # Convertion mode, back or forth
734         steps = []
735         if (initial_step, self.initial_format) < (final_step, self.end_format):
736             mode = "convert"
737             full_steps = []
738             for step in format_relation:
739                 if  initial_step <= step[0] <= final_step and step[2][0] <= self.final_version:
740                     full_steps.append(step)
741             if full_steps[0][1][-1] == self.format:
742                 full_steps = full_steps[1:]
743             for step in full_steps:
744                 steps.append(step[0])
745         else:
746             mode = "revert"
747             relation_format = format_relation[:]
748             relation_format.reverse()
749             last_step = None
750
751             for step in relation_format:
752                 if  final_step <= step[0] <= initial_step:
753                     steps.append(step[0])
754                     last_step = step
755
756             if last_step[1][-1] == self.end_format:
757                 steps.pop()
758
759         self.warning("Convertion mode: %s\tsteps%s" %(mode, steps), 10)
760         return mode, steps
761
762
763 # Part of an unfinished attempt to make lyx2lyx gave a more
764 # structured view of the document.
765 #    def get_toc(self, depth = 4):
766 #        " Returns the TOC of this LyX document."
767 #        paragraphs_filter = {'Title' : 0,'Chapter' : 1, 'Section' : 2,
768 #                             'Subsection' : 3, 'Subsubsection': 4}
769 #        allowed_insets = ['Quotes']
770 #        allowed_parameters = ('\\paragraph_spacing', '\\noindent',
771 #                              '\\align', '\\labelwidthstring',
772 #                              "\\start_of_appendix", "\\leftindent")
773 #        sections = []
774 #        for section in paragraphs_filter.keys():
775 #            sections.append('\\begin_layout %s' % section)
776
777 #        toc_par = []
778 #        i = 0
779 #        while True:
780 #            i = find_tokens(self.body, sections, i)
781 #            if i == -1:
782 #                break
783
784 #            j = find_end_of(self.body,  i + 1, '\\begin_layout', '\\end_layout')
785 #            if j == -1:
786 #                self.warning('Incomplete file.', 0)
787 #                break
788
789 #            section = self.body[i].split()[1]
790 #            if section[-1] == '*':
791 #                section = section[:-1]
792
793 #            par = []
794
795 #            k = i + 1
796 #            # skip paragraph parameters
797 #            while not self.body[k].strip() or self.body[k].split()[0] \
798 #                      in allowed_parameters:
799 #                k += 1
800
801 #            while k < j:
802 #                if check_token(self.body[k], '\\begin_inset'):
803 #                    inset = self.body[k].split()[1]
804 #                    end = find_end_of_inset(self.body, k)
805 #                    if end == -1 or end > j:
806 #                        self.warning('Malformed file.', 0)
807
808 #                    if inset in allowed_insets:
809 #                        par.extend(self.body[k: end+1])
810 #                    k = end + 1
811 #                else:
812 #                    par.append(self.body[k])
813 #                    k += 1
814
815 #            # trim empty lines in the end.
816 #            while par and par[-1].strip() == '':
817 #                par.pop()
818
819 #            toc_par.append(Paragraph(section, par))
820
821 #            i = j + 1
822
823 #        return toc_par
824
825
826 class File(LyX_base):
827     " This class reads existing LyX files."
828
829     def __init__(self, end_format = 0, input = u'', output = u'', error = u'',
830                  debug = default_debug__, try_hard = 0, cjk_encoding = u'',
831                  final_version = u'', systemlyxdir = u''):
832         LyX_base.__init__(self, end_format, input, output, error,
833                           debug, try_hard, cjk_encoding, final_version,
834                           systemlyxdir)
835         self.read()
836
837
838 # FIXME: header settings are completely outdated, don't use like this
839 #class NewFile(LyX_base):
840 #    " This class is to create new LyX files."
841 #    def set_header(self, **params):
842 #        # set default values
843 #        self.header.extend([
844 #            "#LyX xxxx created this file."
845 #            "For more info see http://www.lyx.org/",
846 #            "\\lyxformat xxx",
847 #            "\\begin_document",
848 #            "\\begin_header",
849 #            "\\textclass article",
850 #            "\\language english",
851 #            "\\inputencoding auto",
852 #            "\\font_roman default",
853 #            "\\font_sans default",
854 #            "\\font_typewriter default",
855 #            "\\font_default_family default",
856 #            "\\font_sc false",
857 #            "\\font_osf false",
858 #            "\\font_sf_scale 100",
859 #            "\\font_tt_scale 100",
860 #            "\\graphics default",
861 #            "\\paperfontsize default",
862 #            "\\papersize default",
863 #            "\\use_geometry false",
864 #            "\\use_amsmath 1",
865 #            "\\cite_engine basic",
866 #            "\\use_bibtopic false",
867 #            "\\use_indices false",
868 #            "\\paperorientation portrait",
869 #            "\\secnumdepth 3",
870 #            "\\tocdepth 3",
871 #            "\\paragraph_separation indent",
872 #            "\\defskip medskip",
873 #            "\\quotes_language english",
874 #            "\\papercolumns 1",
875 #            "\\papersides 1",
876 #            "\\paperpagestyle default",
877 #            "\\tracking_changes false",
878 #            "\\end_header"])
879
880 #        self.format = get_end_format()
881 #        for param in params:
882 #            self.set_parameter(param, params[param])
883
884
885 #    def set_body(self, paragraphs):
886 #        self.body.extend(['\\begin_body',''])
887
888 #        for par in paragraphs:
889 #            self.body.extend(par.asLines())
890
891 #        self.body.extend(['','\\end_body', '\\end_document'])
892
893
894 # Part of an unfinished attempt to make lyx2lyx gave a more
895 # structured view of the document.
896 #class Paragraph:
897 #    # unfinished implementation, it is missing the Text and Insets
898 #    # representation.
899 #    " This class represents the LyX paragraphs."
900 #    def __init__(self, name, body=[], settings = [], child = []):
901 #        """ Parameters:
902 #        name: paragraph name.
903 #        body: list of lines of body text.
904 #        child: list of paragraphs that descend from this paragraph.
905 #        """
906 #        self.name = name
907 #        self.body = body
908 #        self.settings = settings
909 #        self.child = child
910
911 #    def asLines(self):
912 #        """ Converts the paragraph to a list of strings, representing
913 #        it in the LyX file."""
914
915 #        result = ['','\\begin_layout %s' % self.name]
916 #        result.extend(self.settings)
917 #        result.append('')
918 #        result.extend(self.body)
919 #        result.append('\\end_layout')
920
921 #        if not self.child:
922 #            return result
923
924 #        result.append('\\begin_deeper')
925 #        for node in self.child:
926 #            result.extend(node.asLines())
927 #        result.append('\\end_deeper')
928
929 #        return result