]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/LyX.py
Prepare for attic movement.
[lyx.git] / lib / lyx2lyx / LyX.py
1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2002-2004 Dekel Tsur <dekel@lyx.org>
4 # Copyright (C) 2002-2006 José Matos <jamatos@lyx.org>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 " The LyX module has all the rules related with different lyx file formats."
21
22 from parser_tools import get_value, check_token, find_token, \
23      find_tokens, find_end_of
24 import os.path
25 import gzip
26 import locale
27 import sys
28 import re
29 import time
30
31 try:
32     import lyx2lyx_version
33     version__ = lyx2lyx_version.version
34 except: # we are running from build directory so assume the last version
35     version__ = '1.6.0svn'
36
37 default_debug__ = 2
38
39 ####################################################################
40 # Private helper functions
41
42 def find_end_of_inset(lines, i):
43     " Find beginning of inset, where lines[i] is included."
44     return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
45
46 def minor_versions(major, last_minor_version):
47     """ Generate minor versions, using major as prefix and minor
48     versions from 0 until last_minor_version, plus the generic version.
49
50     Example:
51
52       minor_versions("1.2", 4) ->
53       [ "1.2", "1.2.0", "1.2.1", "1.2.2", "1.2.3"]
54     """
55     return [major] + [major + ".%d" % i for i in range(last_minor_version + 1)]
56
57
58 # End of helper functions
59 ####################################################################
60
61
62 # Regular expressions used
63 format_re = re.compile(r"(\d)[\.,]?(\d\d)")
64 fileformat = re.compile(r"\\lyxformat\s*(\S*)")
65 original_version = re.compile(r".*?LyX ([\d.]*)")
66
67 ##
68 # file format information:
69 #  file, supported formats, stable release versions
70 format_relation = [("0_06",    [200], minor_versions("0.6" , 4)),
71                    ("0_08",    [210], minor_versions("0.8" , 6) + ["0.7"]),
72                    ("0_10",    [210], minor_versions("0.10", 7) + ["0.9"]),
73                    ("0_12",    [215], minor_versions("0.12", 1) + ["0.11"]),
74                    ("1_0",     [215], minor_versions("1.0" , 4)),
75                    ("1_1",     [215], minor_versions("1.1" , 4)),
76                    ("1_1_5",   [216], ["1.1.5","1.1.5.1","1.1.5.2","1.1"]),
77                    ("1_1_6_0", [217], ["1.1.6","1.1.6.1","1.1.6.2","1.1"]),
78                    ("1_1_6_3", [218], ["1.1.6.3","1.1.6.4","1.1"]),
79                    ("1_2",     [220], minor_versions("1.2" , 4)),
80                    ("1_3",     [221], minor_versions("1.3" , 7)),
81                    ("1_4", range(222,246), minor_versions("1.4" , 5)),
82                    ("1_5", range(246,277), minor_versions("1.5" , 5)),
83                    ("1_6",     []   , minor_versions("1.6" , 0))]
84
85 ####################################################################
86 # This is useful just for development versions                     #
87 # if the list of supported formats is empty get it from last step  #
88 if not format_relation[-1][1]:
89     step, mode = format_relation[-1][0], "convert"
90     convert = getattr(__import__("lyx_" + step), mode)
91     format_relation[-1] = (step,
92                            [conv[0] for conv in convert],
93                            format_relation[-1][2])
94 #                                                                  #
95 ####################################################################
96
97 def formats_list():
98     " Returns a list with supported file formats."
99     formats = []
100     for version in format_relation:
101         for format in version[1]:
102             if format not in formats:
103                 formats.append(format)
104     return formats
105
106
107 def get_end_format():
108     " Returns the more recent file format available."
109     return format_relation[-1][1][-1]
110
111
112 def get_backend(textclass):
113     " For _textclass_ returns its backend."
114     if textclass == "linuxdoc" or textclass == "manpage":
115         return "linuxdoc"
116     if textclass[:7] == "docbook":
117         return "docbook"
118     return "latex"
119
120
121 def trim_eol(line):
122     " Remove end of line char(s)."
123     if line[-2:-1] == '\r':
124         return line[:-2]
125     else:
126         return line[:-1]
127
128
129 def get_encoding(language, inputencoding, format, cjk_encoding):
130     " Returns enconding of the lyx file"
131     if format > 248:
132         return "utf8"
133     # CJK-LyX encodes files using the current locale encoding.
134     # This means that files created by CJK-LyX can only be converted using
135     # the correct locale settings unless the encoding is given as commandline
136     # argument.
137     if cjk_encoding == 'auto':
138         return locale.getpreferredencoding()
139     elif cjk_encoding:
140         return cjk_encoding
141     from lyx2lyx_lang import lang
142     if inputencoding == "auto" or inputencoding == "default":
143         return lang[language][3]
144     if inputencoding == "":
145         return "latin1"
146     if inputencoding == "utf8x":
147         return "utf8"
148     # python does not know the alias latin9
149     if inputencoding == "latin9":
150         return "iso-8859-15"
151     return inputencoding
152
153 ##
154 # Class
155 #
156 class LyX_base:
157     """This class carries all the information of the LyX file."""
158
159     def __init__(self, end_format = 0, input = "", output = "", error = "",
160                  debug = default_debug__, try_hard = 0, cjk_encoding = '',
161                  language = "english", encoding = "auto"):
162
163         """Arguments:
164         end_format: final format that the file should be converted. (integer)
165         input: the name of the input source, if empty resort to standard input.
166         output: the name of the output file, if empty use the standard output.
167         error: the name of the error file, if empty use the standard error.
168         debug: debug level, O means no debug, as its value increases be more verbose.
169         """
170         self.choose_io(input, output)
171
172         if error:
173             self.err = open(error, "w")
174         else:
175             self.err = sys.stderr
176
177         self.debug = debug
178         self.try_hard = try_hard
179         self.cjk_encoding = cjk_encoding
180
181         if end_format:
182             self.end_format = self.lyxformat(end_format)
183         else:
184             self.end_format = get_end_format()
185
186         self.backend = "latex"
187         self.textclass = "article"
188         # This is a hack: We use '' since we don't know the default
189         # layout of the text class. LyX will parse it as default layout.
190         # FIXME: Read the layout file and use the real default layout
191         self.default_layout = ''
192         self.header = []
193         self.preamble = []
194         self.body = []
195         self.status = 0
196         self.encoding = encoding
197         self.language = language
198
199
200     def warning(self, message, debug_level= default_debug__):
201         """ Emits warning to self.error, if the debug_level is less
202         than the self.debug."""
203         if debug_level <= self.debug:
204             self.err.write("Warning: " + message + "\n")
205
206
207     def error(self, message):
208         " Emits a warning and exits if not in try_hard mode."
209         self.warning(message)
210         if not self.try_hard:
211             self.warning("Quiting.")
212             sys.exit(1)
213
214         self.status = 2
215
216
217     def read(self):
218         """Reads a file into the self.header and
219         self.body parts, from self.input."""
220
221         while True:
222             line = self.input.readline()
223             if not line:
224                 self.error("Invalid LyX file.")
225
226             line = trim_eol(line)
227             if check_token(line, '\\begin_preamble'):
228                 while 1:
229                     line = self.input.readline()
230                     if not line:
231                         self.error("Invalid LyX file.")
232
233                     line = trim_eol(line)
234                     if check_token(line, '\\end_preamble'):
235                         break
236
237                     if line.split()[:0] in ("\\layout",
238                                             "\\begin_layout", "\\begin_body"):
239
240                         self.warning("Malformed LyX file:"
241                                      "Missing '\\end_preamble'."
242                                      "\nAdding it now and hoping"
243                                      "for the best.")
244
245                     self.preamble.append(line)
246
247             if check_token(line, '\\end_preamble'):
248                 continue
249
250             line = line.strip()
251             if not line:
252                 continue
253
254             if line.split()[0] in ("\\layout", "\\begin_layout",
255                                    "\\begin_body", "\\begin_deeper"):
256                 self.body.append(line)
257                 break
258
259             self.header.append(line)
260
261         i = find_token(self.header, '\\textclass', 0)
262         if i == -1:
263             self.warning("Malformed LyX file: Missing '\\textclass'.")
264             i = find_token(self.header, '\\lyxformat', 0) + 1
265             self.header[i:i] = ['\\textclass article']
266
267         self.textclass = get_value(self.header, "\\textclass", 0)
268         self.backend = get_backend(self.textclass)
269         self.format  = self.read_format()
270         self.language = get_value(self.header, "\\language", 0,
271                                   default = "english")
272         self.inputencoding = get_value(self.header, "\\inputencoding",
273                                        0, default = "auto")
274         self.encoding = get_encoding(self.language,
275                                      self.inputencoding, self.format,
276                                      self.cjk_encoding)
277         self.initial_version = self.read_version()
278
279         # Second pass over header and preamble, now we know the file encoding
280         for i in range(len(self.header)):
281             self.header[i] = self.header[i].decode(self.encoding)
282         for i in range(len(self.preamble)):
283             self.preamble[i] = self.preamble[i].decode(self.encoding)
284
285         # Read document body
286         while 1:
287             line = self.input.readline().decode(self.encoding)
288             if not line:
289                 break
290             self.body.append(trim_eol(line))
291
292
293     def write(self):
294         " Writes the LyX file to self.output."
295         self.set_version()
296         self.set_format()
297         self.set_textclass()
298         if self.encoding == "auto":
299             self.encoding = get_encoding(self.language, self.encoding,
300                                          self.format, self.cjk_encoding)
301         if self.preamble:
302             i = find_token(self.header, '\\textclass', 0) + 1
303             preamble = ['\\begin_preamble'] + self.preamble + ['\\end_preamble']
304             header = self.header[:i] + preamble + self.header[i:]
305         else:
306             header = self.header
307
308         for line in header + [''] + self.body:
309             self.output.write(line.encode(self.encoding)+"\n")
310
311
312     def choose_io(self, input, output):
313         """Choose input and output streams, dealing transparently with
314         compressed files."""
315
316         if output:
317             self.output = open(output, "wb")
318         else:
319             self.output = sys.stdout
320
321         if input and input != '-':
322             self.dir = os.path.dirname(os.path.abspath(input))
323             try:
324                 gzip.open(input).readline()
325                 self.input = gzip.open(input)
326                 self.output = gzip.GzipFile(mode="wb", fileobj=self.output)
327             except:
328                 self.input = open(input)
329         else:
330             self.dir = ''
331             self.input = sys.stdin
332
333
334     def lyxformat(self, format):
335         " Returns the file format representation, an integer."
336         result = format_re.match(format)
337         if result:
338             format = int(result.group(1) + result.group(2))
339         elif format == '2':
340             format = 200
341         else:
342             self.error(str(format) + ": " + "Invalid LyX file.")
343
344         if format in formats_list():
345             return format
346
347         self.error(str(format) + ": " + "Format not supported.")
348         return None
349
350
351     def read_version(self):
352         """ Searchs for clues of the LyX version used to write the
353         file, returns the most likely value, or None otherwise."""
354
355         for line in self.header:
356             if line[0] != "#":
357                 return None
358
359             line = line.replace("fix",".")
360             result = original_version.match(line)
361             if result:
362                 # Special know cases: reLyX and KLyX
363                 if line.find("reLyX") != -1 or line.find("KLyX") != -1:
364                     return "0.12"
365
366                 res = result.group(1)
367                 if not res:
368                     self.warning(line)
369                 #self.warning("Version %s" % result.group(1))
370                 return res
371         self.warning(str(self.header[:2]))
372         return None
373
374
375     def set_version(self):
376         " Set the header with the version used."
377         self.header[0] = " ".join(["#LyX %s created this file." % version__,
378                                   "For more info see http://www.lyx.org/"])
379         if self.header[1][0] == '#':
380             del self.header[1]
381
382
383     def read_format(self):
384         " Read from the header the fileformat of the present LyX file."
385         for line in self.header:
386             result = fileformat.match(line)
387             if result:
388                 return self.lyxformat(result.group(1))
389         else:
390             self.error("Invalid LyX File.")
391         return None
392
393
394     def set_format(self):
395         " Set the file format of the file, in the header."
396         if self.format <= 217:
397             format = str(float(self.format)/100)
398         else:
399             format = str(self.format)
400         i = find_token(self.header, "\\lyxformat", 0)
401         self.header[i] = "\\lyxformat %s" % format
402
403
404     def set_textclass(self):
405         i = find_token(self.header, "\\textclass", 0)
406         self.header[i] = "\\textclass %s" % self.textclass
407
408
409     #Note that the module will be added at the END of the extant ones
410     def add_module(self, module):
411       i = find_token(self.header, "\\begin_modules", 0)
412       if i == -1:
413         #No modules yet included
414         i = find_token(self.header, "\\textclass", 0)
415         if i == -1:
416           self.warning("Malformed LyX document: No \\textclass!!")
417           return
418         modinfo = ["\\begin_modules", module, "\\end_modules"]
419         self.header[i + 1: i + 1] = modinfo
420         return
421       j = find_token(self.header, "\\end_modules", i)
422       if j == -1:
423         self.warning("(add_module)Malformed LyX document: No \\end_modules.")
424         return
425       k = find_token(self.header, module, i)
426       if k != -1 and k < j:
427         return
428       self.header.insert(j, module)
429
430
431     def get_module_list(self):
432       i = find_token(self.header, "\\begin_modules", 0)
433       if (i == -1):
434         return []
435       j = find_token(self.header, "\\end_modules", i)
436       return self.header[i + 1 : j]
437
438
439     def set_module_list(self, mlist):
440       modbegin = find_token(self.header, "\\begin_modules", 0)
441       newmodlist = ['\\begin_modules'] + mlist + ['\\end_modules']
442       if (modbegin == -1):
443         #No modules yet included
444         tclass = find_token(self.header, "\\textclass", 0)
445         if tclass == -1:
446           self.warning("Malformed LyX document: No \\textclass!!")
447           return
448         modbegin = tclass + 1
449         self.header[modbegin:modbegin] = newmodlist
450         return
451       modend = find_token(self.header, "\\end_modules", modbegin)
452       if modend == -1:
453         self.warning("(set_module_list)Malformed LyX document: No \\end_modules.")
454         return
455       newmodlist = ['\\begin_modules'] + mlist + ['\\end_modules']
456       self.header[modbegin:modend + 1] = newmodlist
457
458
459     def set_parameter(self, param, value):
460         " Set the value of the header parameter."
461         i = find_token(self.header, '\\' + param, 0)
462         if i == -1:
463             self.warning('Parameter not found in the header: %s' % param, 3)
464             return
465         self.header[i] = '\\%s %s' % (param, str(value))
466
467
468     def is_default_layout(self, layout):
469         " Check whether a layout is the default layout of this class."
470         # FIXME: Check against the real text class default layout
471         if layout == 'Standard' or layout == self.default_layout:
472             return 1
473         return 0
474
475
476     def convert(self):
477         "Convert from current (self.format) to self.end_format."
478         mode, convertion_chain = self.chain()
479         self.warning("convertion chain: " + str(convertion_chain), 3)
480
481         for step in convertion_chain:
482             steps = getattr(__import__("lyx_" + step), mode)
483
484             self.warning("Convertion step: %s - %s" % (step, mode),
485                          default_debug__ + 1)
486             if not steps:
487                 self.error("The convertion to an older "
488                 "format (%s) is not implemented." % self.format)
489
490             multi_conv = len(steps) != 1
491             for version, table in steps:
492                 if multi_conv and \
493                    (self.format >= version and mode == "convert") or\
494                    (self.format <= version and mode == "revert"):
495                     continue
496
497                 for conv in table:
498                     init_t = time.time()
499                     try:
500                         conv(self)
501                     except:
502                         self.warning("An error ocurred in %s, %s" %
503                                      (version, str(conv)),
504                                      default_debug__)
505                         if not self.try_hard:
506                             raise
507                         self.status = 2
508                     else:
509                         self.warning("%lf: Elapsed time on %s" %
510                                      (time.time() - init_t,
511                                       str(conv)), default_debug__ +
512                                      1)
513                 self.format = version
514                 if self.end_format == self.format:
515                     return
516
517
518     def chain(self):
519         """ This is where all the decisions related with the
520         convertion are taken.  It returns a list of modules needed to
521         convert the LyX file from self.format to self.end_format"""
522
523         self.start =  self.format
524         format = self.format
525         correct_version = 0
526
527         for rel in format_relation:
528             if self.initial_version in rel[2]:
529                 if format in rel[1]:
530                     initial_step = rel[0]
531                     correct_version = 1
532                     break
533
534         if not correct_version:
535             if format <= 215:
536                 self.warning("Version does not match file format, "
537                              "discarding it. (Version %s, format %d)" %
538                              (self.initial_version, self.format))
539             for rel in format_relation:
540                 if format in rel[1]:
541                     initial_step = rel[0]
542                     break
543             else:
544                 # This should not happen, really.
545                 self.error("Format not supported.")
546
547         # Find the final step
548         for rel in format_relation:
549             if self.end_format in rel[1]:
550                 final_step = rel[0]
551                 break
552         else:
553             self.error("Format not supported.")
554
555         # Convertion mode, back or forth
556         steps = []
557         if (initial_step, self.start) < (final_step, self.end_format):
558             mode = "convert"
559             first_step = 1
560             for step in format_relation:
561                 if  initial_step <= step[0] <= final_step:
562                     if first_step and len(step[1]) == 1:
563                         first_step = 0
564                         continue
565                     steps.append(step[0])
566         else:
567             mode = "revert"
568             relation_format = format_relation[:]
569             relation_format.reverse()
570             last_step = None
571
572             for step in relation_format:
573                 if  final_step <= step[0] <= initial_step:
574                     steps.append(step[0])
575                     last_step = step
576
577             if last_step[1][-1] == self.end_format:
578                 steps.pop()
579
580         self.warning("Convertion mode: %s\tsteps%s" %(mode, steps), 10)
581         return mode, steps
582
583
584     def get_toc(self, depth = 4):
585         " Returns the TOC of this LyX document."
586         paragraphs_filter = {'Title' : 0,'Chapter' : 1, 'Section' : 2,
587                              'Subsection' : 3, 'Subsubsection': 4}
588         allowed_insets = ['Quotes']
589         allowed_parameters = ('\\paragraph_spacing', '\\noindent',
590                               '\\align', '\\labelwidthstring',
591                               "\\start_of_appendix", "\\leftindent")
592         sections = []
593         for section in paragraphs_filter.keys():
594             sections.append('\\begin_layout %s' % section)
595
596         toc_par = []
597         i = 0
598         while 1:
599             i = find_tokens(self.body, sections, i)
600             if i == -1:
601                 break
602
603             j = find_end_of(self.body,  i + 1, '\\begin_layout', '\\end_layout')
604             if j == -1:
605                 self.warning('Incomplete file.', 0)
606                 break
607
608             section = self.body[i].split()[1]
609             if section[-1] == '*':
610                 section = section[:-1]
611
612             par = []
613
614             k = i + 1
615             # skip paragraph parameters
616             while not self.body[k].strip() or self.body[k].split()[0] \
617                       in allowed_parameters:
618                 k += 1
619
620             while k < j:
621                 if check_token(self.body[k], '\\begin_inset'):
622                     inset = self.body[k].split()[1]
623                     end = find_end_of_inset(self.body, k)
624                     if end == -1 or end > j:
625                         self.warning('Malformed file.', 0)
626
627                     if inset in allowed_insets:
628                         par.extend(self.body[k: end+1])
629                     k = end + 1
630                 else:
631                     par.append(self.body[k])
632                     k += 1
633
634             # trim empty lines in the end.
635             while par and par[-1].strip() == '':
636                 par.pop()
637
638             toc_par.append(Paragraph(section, par))
639
640             i = j + 1
641
642         return toc_par
643
644
645 class File(LyX_base):
646     " This class reads existing LyX files."
647
648     def __init__(self, end_format = 0, input = "", output = "", error = "",
649                  debug = default_debug__, try_hard = 0, cjk_encoding = ''):
650         LyX_base.__init__(self, end_format, input, output, error,
651                           debug, try_hard, cjk_encoding)
652         self.read()
653
654
655 class NewFile(LyX_base):
656     " This class is to create new LyX files."
657     def set_header(self, **params):
658         # set default values
659         self.header.extend([
660             "#LyX xxxx created this file."
661             "For more info see http://www.lyx.org/",
662             "\\lyxformat xxx",
663             "\\begin_document",
664             "\\begin_header",
665             "\\textclass article",
666             "\\language english",
667             "\\inputencoding auto",
668             "\\font_roman default",
669             "\\font_sans default",
670             "\\font_typewriter default",
671             "\\font_default_family default",
672             "\\font_sc false",
673             "\\font_osf false",
674             "\\font_sf_scale 100",
675             "\\font_tt_scale 100",
676             "\\graphics default",
677             "\\paperfontsize default",
678             "\\papersize default",
679             "\\use_geometry false",
680             "\\use_amsmath 1",
681             "\\cite_engine basic",
682             "\\use_bibtopic false",
683             "\\paperorientation portrait",
684             "\\secnumdepth 3",
685             "\\tocdepth 3",
686             "\\paragraph_separation indent",
687             "\\defskip medskip",
688             "\\quotes_language english",
689             "\\papercolumns 1",
690             "\\papersides 1",
691             "\\paperpagestyle default",
692             "\\tracking_changes false",
693             "\\end_header"])
694
695         self.format = get_end_format()
696         for param in params:
697             self.set_parameter(param, params[param])
698
699
700     def set_body(self, paragraphs):
701         self.body.extend(['\\begin_body',''])
702
703         for par in paragraphs:
704             self.body.extend(par.asLines())
705
706         self.body.extend(['','\\end_body', '\\end_document'])
707
708
709 class Paragraph:
710     # unfinished implementation, it is missing the Text and Insets
711     # representation.
712     " This class represents the LyX paragraphs."
713     def __init__(self, name, body=[], settings = [], child = []):
714         """ Parameters:
715         name: paragraph name.
716         body: list of lines of body text.
717         child: list of paragraphs that descend from this paragraph.
718         """
719         self.name = name
720         self.body = body
721         self.settings = settings
722         self.child = child
723
724     def asLines(self):
725         """ Converts the paragraph to a list of strings, representing
726         it in the LyX file."""
727
728         result = ['','\\begin_layout %s' % self.name]
729         result.extend(self.settings)
730         result.append('')
731         result.extend(self.body)
732         result.append('\\end_layout')
733
734         if not self.child:
735             return result
736
737         result.append('\\begin_deeper')
738         for node in self.child:
739             result.extend(node.asLines())
740         result.append('\\end_deeper')
741
742         return result