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>
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.
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.
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
21 " The LyX module has all the rules related with different lyx file formats."
23 from parser_tools import get_value, check_token, find_token, \
24 find_tokens, find_end_of
35 import lyx2lyx_version
36 version__ = lyx2lyx_version.version
37 except: # we are running from build directory so assume the last version
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
46 ####################################################################
47 # Private helper functions
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")
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.
59 minor_versions("1.2", 4) ->
60 [ "1.2", "1.2.0", "1.2.1", "1.2.2", "1.2.3"]
62 return [major] + [major + ".%d" % i for i in range(last_minor_version + 1)]
65 # End of helper functions
66 ####################################################################
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.]*)")
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))
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])
110 ####################################################################
113 " Returns a list with supported file formats."
115 for version in format_relation:
116 for format in version[1]:
117 if format not in formats:
118 formats.append(format)
123 " Returns a list with supported file formats."
124 out = """Major version:
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])
134 formats = "%s - %s" % (version[1][-1], version[1][0])
135 out += "%s\n\t%s\n\t%s\n\n" % (major, versions, formats)
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]
148 def get_backend(textclass):
149 " For _textclass_ returns its backend."
150 if textclass == "linuxdoc" or textclass == "manpage":
152 if textclass.startswith("docbook") or textclass.startswith("agu-"):
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
162 if line[-2:-1] == '\r':
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
173 if line[-2:-1] == 13:
179 def get_encoding(language, inputencoding, format, cjk_encoding):
180 " Returns enconding of the lyx file"
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
187 if cjk_encoding == 'auto':
188 return locale.getpreferredencoding()
191 from lyx2lyx_lang import lang
192 if inputencoding == "auto" or inputencoding == "default":
193 return lang[language][3]
194 if inputencoding == "":
196 if inputencoding == "utf8x":
198 # python does not know the alias latin9
199 if inputencoding == "latin9":
207 """This class carries all the information of the LyX file."""
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',
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.
221 self.choose_input(input)
225 self.err = open(error, "w")
227 self.err = sys.stderr
230 self.try_hard = try_hard
231 self.cjk_encoding = cjk_encoding
234 self.end_format = self.lyxformat(end_format)
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.
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)
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]
256 self.end_format = get_end_format()
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)
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 = ''
276 self.encoding = encoding
277 self.language = language
278 self.systemlyxdir = systemlyxdir
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")
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.")
299 """Reads a file into the self.header and
300 self.body parts, from self.input."""
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.
311 line = self.input.readline()
313 # eof found before end of header
314 self.error("Invalid LyX file: Missing body.")
317 # Remove UTF8 BOM marker if present
318 if line.startswith(codecs.BOM_UTF8):
319 line = line[len(codecs.BOM_UTF8):]
324 line = trim_eol(line)
327 line = trim_eol_binary(line)
328 decoded = line.decode('latin1')
329 if check_token(decoded, '\\begin_preamble'):
331 line = self.input.readline()
333 # eof found before end of header
334 self.error("Invalid LyX file: Missing body.")
337 line = trim_eol(line)
340 line = trim_eol_binary(line)
341 decoded = line.decode('latin1')
342 if check_token(decoded, '\\end_preamble'):
345 if decoded.split()[:0] in ("\\layout",
346 "\\begin_layout", "\\begin_body"):
348 self.warning("Malformed LyX file:"
349 "Missing '\\end_preamble'."
350 "\nAdding it now and hoping"
353 self.preamble.append(line)
355 if check_token(decoded, '\\end_preamble'):
362 if decoded.split()[0] in ("\\layout", "\\begin_layout",
363 "\\begin_body", "\\begin_deeper"):
364 self.body.append(line)
367 self.header.append(line)
370 i = find_token(self.header, '\\textclass', 0)
372 i = find_token(self.header, b'\\textclass', 0)
374 self.warning("Malformed LyX file: Missing '\\textclass'.")
376 i = find_token(self.header, '\\lyxformat', 0) + 1
377 self.header[i:i] = ['\\textclass article']
379 i = find_token(self.header, b'\\lyxformat', 0) + 1
380 self.header[i:i] = [b'\\textclass article']
383 self.textclass = get_value(self.header, "\\textclass", 0,
385 self.language = get_value(self.header, "\\language", 0,
387 self.inputencoding = get_value(self.header, "\\inputencoding", 0,
390 self.textclass = get_value(self.header, b"\\textclass", 0,
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,
401 self.initial_version = self.read_version()
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)
416 line = self.input.readline().decode(self.encoding)
419 self.body.append(trim_eol(line))
423 " Writes the LyX file to self.output."
424 self.choose_output(self.output)
428 if self.encoding == "auto":
429 self.encoding = get_encoding(self.language, self.encoding,
430 self.format, self.cjk_encoding)
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:]
438 for line in header + [''] + self.body:
439 self.output.write(line+u"\n")
442 def choose_output(self, output):
443 """Choose output streams dealing transparently with
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
452 outputfileobj = open(output, 'wb')
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')
462 self.output = io.open(output, 'w', encoding=self.encoding)
464 self.output = io.open(sys.stdout.fileno(), 'w', encoding=self.encoding)
467 def choose_input(self, input):
468 """Choose input stream, dealing transparently with
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))
476 gzip.open(input).readline()
477 self.input = gzip.open(input)
478 self.compressed = True
480 self.input = open(input, 'rb')
481 self.compressed = False
484 self.input = os.fdopen(sys.stdin.fileno(), 'rb')
485 self.compressed = False
488 def lyxformat(self, format):
489 " Returns the file format representation, an integer."
490 result = format_re.match(format)
492 format = int(result.group(1) + result.group(2))
496 self.error(str(format) + ": " + "Invalid LyX file.")
498 if format in formats_list():
501 self.error(str(format) + ": " + "Format not supported.")
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."""
509 for line in self.header:
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)
518 result = original_version.match(line)
520 # Special know cases: reLyX and KLyX
521 if line.find("reLyX") != -1 or line.find("KLyX") != -1:
524 res = result.group(1)
527 #self.warning("Version %s" % result.group(1))
529 self.warning(str(self.header[:2]))
533 def set_version(self):
534 " Set the header with the version used."
536 initial_comment = " ".join(["#LyX %s created this file." % version__,
537 "For more info see https://www.lyx.org/"])
539 # Simple heuristic to determine the comment that always starts
541 if self.header[0].startswith("#"):
542 self.header[0] = initial_comment
544 self.header.insert(0, initial_comment)
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] == '#':
555 def read_format(self):
556 " Read from the header the fileformat of the present LyX file."
557 for line in self.header:
559 result = fileformat.match(line)
561 result = fileformat.match(line.decode('ascii'))
563 return self.lyxformat(result.group(1))
565 self.error("Invalid LyX File: Missing format.")
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)
574 format = str(self.format)
575 i = find_token(self.header, "\\lyxformat", 0)
576 self.header[i] = "\\lyxformat %s" % format
579 def set_textclass(self):
580 i = find_token(self.header, "\\textclass", 0)
581 self.header[i] = "\\textclass %s" % self.textclass
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)
588 #No modules yet included
589 i = find_token(self.header, "\\textclass", 0)
591 self.warning("Malformed LyX document: No \\textclass!!")
593 modinfo = ["\\begin_modules", module, "\\end_modules"]
594 self.header[i + 1: i + 1] = modinfo
596 j = find_token(self.header, "\\end_modules", i)
598 self.warning("(add_module)Malformed LyX document: No \\end_modules.")
600 k = find_token(self.header, module, i)
601 if k != -1 and k < j:
603 self.header.insert(j, module)
606 def get_module_list(self):
607 i = find_token(self.header, "\\begin_modules", 0)
610 j = find_token(self.header, "\\end_modules", i)
611 return self.header[i + 1 : j]
614 def set_module_list(self, mlist):
615 modbegin = find_token(self.header, "\\begin_modules", 0)
616 newmodlist = ['\\begin_modules'] + mlist + ['\\end_modules']
618 #No modules yet included
619 tclass = find_token(self.header, "\\textclass", 0)
621 self.warning("Malformed LyX document: No \\textclass!!")
623 modbegin = tclass + 1
624 self.header[modbegin:modbegin] = newmodlist
626 modend = find_token(self.header, "\\end_modules", modbegin)
628 self.warning("(set_module_list)Malformed LyX document: No \\end_modules.")
630 newmodlist = ['\\begin_modules'] + mlist + ['\\end_modules']
631 self.header[modbegin:modend + 1] = newmodlist
634 def set_parameter(self, param, value):
635 " Set the value of the header parameter."
636 i = find_token(self.header, '\\' + param, 0)
638 self.warning('Parameter not found in the header: %s' % param, 3)
640 self.header[i] = '\\%s %s' % (param, str(value))
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:
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__)
658 mode, conversion_chain = self.chain()
659 self.warning("conversion chain: " + str(conversion_chain), 3)
661 for step in conversion_chain:
662 steps = getattr(__import__("lyx_" + step), mode)
664 self.warning("Convertion step: %s - %s" % (step, mode),
667 self.error("The conversion to an older "
668 "format (%s) is not implemented." % self.format)
670 multi_conv = len(steps) != 1
671 for version, table in steps:
673 (self.format >= version and mode == "convert") or\
674 (self.format <= version and mode == "revert"):
682 self.warning("An error ocurred in %s, %s" %
683 (version, str(conv)),
685 if not self.try_hard:
689 self.warning("%lf: Elapsed time on %s" %
690 (time.time() - init_t,
691 str(conv)), default_debug__ +
693 self.format = version
694 if self.end_format == self.format:
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"""
706 for rel in format_relation:
707 if self.initial_version in rel[2]:
709 initial_step = rel[0]
713 if not correct_version:
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:
720 initial_step = rel[0]
723 # This should not happen, really.
724 self.error("Format not supported.")
726 # Find the final step
727 for rel in format_relation:
728 if self.end_format in rel[1]:
732 self.error("Format not supported.")
734 # Convertion mode, back or forth
736 if (initial_step, self.initial_format) < (final_step, self.end_format):
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])
748 relation_format = format_relation[:]
749 relation_format.reverse()
752 for step in relation_format:
753 if final_step <= step[0] <= initial_step:
754 steps.append(step[0])
757 if last_step[1][-1] == self.end_format:
760 self.warning("Convertion mode: %s\tsteps%s" %(mode, steps), 10)
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")
775 # for section in paragraphs_filter.keys():
776 # sections.append('\\begin_layout %s' % section)
781 # i = find_tokens(self.body, sections, i)
785 # j = find_end_of(self.body, i + 1, '\\begin_layout', '\\end_layout')
787 # self.warning('Incomplete file.', 0)
790 # section = self.body[i].split()[1]
791 # if section[-1] == '*':
792 # section = section[:-1]
797 # # skip paragraph parameters
798 # while not self.body[k].strip() or self.body[k].split()[0] \
799 # in allowed_parameters:
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)
809 # if inset in allowed_insets:
810 # par.extend(self.body[k: end+1])
813 # par.append(self.body[k])
816 # # trim empty lines in the end.
817 # while par and par[-1].strip() == '':
820 # toc_par.append(Paragraph(section, par))
827 class File(LyX_base):
828 " This class reads existing LyX files."
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,
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/",
848 # "\\begin_document",
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",
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",
866 # "\\cite_engine basic",
867 # "\\use_bibtopic false",
868 # "\\use_indices false",
869 # "\\paperorientation portrait",
872 # "\\paragraph_separation indent",
873 # "\\defskip medskip",
874 # "\\quotes_language english",
875 # "\\papercolumns 1",
877 # "\\paperpagestyle default",
878 # "\\tracking_changes false",
881 # self.format = get_end_format()
882 # for param in params:
883 # self.set_parameter(param, params[param])
886 # def set_body(self, paragraphs):
887 # self.body.extend(['\\begin_body',''])
889 # for par in paragraphs:
890 # self.body.extend(par.asLines())
892 # self.body.extend(['','\\end_body', '\\end_document'])
895 # Part of an unfinished attempt to make lyx2lyx gave a more
896 # structured view of the document.
898 # # unfinished implementation, it is missing the Text and Insets
900 # " This class represents the LyX paragraphs."
901 # def __init__(self, name, body=[], settings = [], child = []):
903 # name: paragraph name.
904 # body: list of lines of body text.
905 # child: list of paragraphs that descend from this paragraph.
909 # self.settings = settings
913 # """ Converts the paragraph to a list of strings, representing
914 # it in the LyX file."""
916 # result = ['','\\begin_layout %s' % self.name]
917 # result.extend(self.settings)
919 # result.extend(self.body)
920 # result.append('\\end_layout')
925 # result.append('\\begin_deeper')
926 # for node in self.child:
927 # result.extend(node.asLines())
928 # result.append('\\end_deeper')