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