diff --git a/lib/lyx2lyx/LyX.py b/lib/lyx2lyx/LyX.py new file mode 100644 index 0000000000..7647dc8304 --- /dev/null +++ b/lib/lyx2lyx/LyX.py @@ -0,0 +1,528 @@ +# This file is part of lyx2lyx +# -*- coding: iso-8859-1 -*- +# Copyright (C) 2002-2004 Dekel Tsur , José Matos +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +from parser_tools import get_value, check_token, find_token,\ + find_tokens, find_end_of, find_end_of_inset +import os.path +import gzip +import sys +import re +import string +import time + +version_lyx2lyx = "1.4.0cvs" +default_debug_level = 2 + +# Regular expressions used +format_re = re.compile(r"(\d)[\.,]?(\d\d)") +fileformat = re.compile(r"\\lyxformat\s*(\S*)") +original_version = re.compile(r"\#LyX (\S*)") + +## +# file format information: +# file, supported formats, stable release versions +format_relation = [("0_10", [210], ["0.10.7","0.10"]), + ("0_12", [215], ["0.12","0.12.1","0.12"]), + ("1_0_0", [215], ["1.0.0","1.0"]), + ("1_0_1", [215], ["1.0.1","1.0.2","1.0.3","1.0.4", "1.1.2","1.1"]), + ("1_1_4", [215], ["1.1.4","1.1"]), + ("1_1_5", [216], ["1.1.5","1.1.5fix1","1.1.5fix2","1.1"]), + ("1_1_6", [217], ["1.1.6","1.1.6fix1","1.1.6fix2","1.1"]), + ("1_1_6fix3", [218], ["1.1.6fix3","1.1.6fix4","1.1"]), + ("1_2", [220], ["1.2.0","1.2.1","1.2.3","1.2.4","1.2"]), + ("1_3", [221], ["1.3.0","1.3.1","1.3.2","1.3.3","1.3.4","1.3.5","1.3"]), + ("1_4", range(223,243), ["1.4.0cvs","1.4"])] + + +def formats_list(): + " Returns a list with supported file formats." + formats = [] + for version in format_relation: + for format in version[1]: + if format not in formats: + formats.append(format) + return formats + + +def get_end_format(): + " Returns the more recent file format available." + return format_relation[-1][1][-1] + + +def get_backend(textclass): + " For _textclass_ returns its backend." + if textclass == "linuxdoc" or textclass == "manpage": + return "linuxdoc" + if textclass[:7] == "docbook": + return "docbook" + return "latex" + + +## +# Class +# +class LyX_Base: + """This class carries all the information of the LyX file.""" + def __init__(self, end_format = 0, input = "", output = "", error = "", debug = default_debug_level, try_hard = 0): + """Arguments: + end_format: final format that the file should be converted. (integer) + input: the name of the input source, if empty resort to standard input. + output: the name of the output file, if empty use the standard output. + error: the name of the error file, if empty use the standard error. + debug: debug level, O means no debug, as its value increases be more verbose. + """ + if input and input != '-': + self.input = self.open(input) + else: + self.input = sys.stdin + if output: + self.output = open(output, "w") + else: + self.output = sys.stdout + + if error: + self.err = open(error, "w") + else: + self.err = sys.stderr + + self.debug = debug + self.try_hard = try_hard + + if end_format: + self.end_format = self.lyxformat(end_format) + else: + self.end_format = get_end_format() + + self.backend = "latex" + self.textclass = "article" + self.header = [] + self.body = [] + self.status = 0 + + + def warning(self, message, debug_level= default_debug_level): + " Emits warning to self.error, if the debug_level is less than the self.debug." + if debug_level <= self.debug: + self.err.write("Warning: " + message + "\n") + + + def error(self, message): + " Emits a warning and exits if not in try_hard mode." + self.warning(message) + if not self.try_hard: + self.warning("Quiting.") + sys.exit(1) + + self.status = 2 + + + def read(self): + """Reads a file into the self.header and self.body parts, from self.input.""" + preamble = 0 + + while 1: + line = self.input.readline() + if not line: + self.error("Invalid LyX file.") + + line = line[:-1] + # remove '\r' from line's end, if present + if line[-1:] == '\r': + line = line[:-1] + + if check_token(line, '\\begin_preamble'): + preamble = 1 + if check_token(line, '\\end_preamble'): + preamble = 0 + + if not preamble: + line = string.strip(line) + + if not preamble: + if not line: + continue + + if string.split(line)[0] in ("\\layout", "\\begin_layout", "\\begin_body"): + self.body.append(line) + break + + self.header.append(line) + + while 1: + line = self.input.readline() + if not line: + break + # remove '\r' from line's end, if present + if line[-2:-1] == '\r': + self.body.append(line[:-2]) + else: + self.body.append(line[:-1]) + + self.textclass = get_value(self.header, "\\textclass", 0) + self.backend = get_backend(self.textclass) + self.format = self.read_format() + self.language = get_value(self.header, "\\language", 0) + if self.language == "": + self.language = "english" + self.initial_version = self.read_version() + + + def write(self): + " Writes the LyX file to self.output." + self.set_version() + self.set_format() + + for line in self.header: + self.output.write(line+"\n") + self.output.write("\n") + for line in self.body: + self.output.write(line+"\n") + + + def open(self, file): + """Transparently deals with compressed files.""" + + self.dir = os.path.dirname(os.path.abspath(file)) + try: + gzip.open(file).readline() + self.output = gzip.GzipFile("","wb",6,self.output) + return gzip.open(file) + except: + return open(file) + + + def lyxformat(self, format): + " Returns the file format representation, an integer." + result = format_re.match(format) + if result: + format = int(result.group(1) + result.group(2)) + else: + self.error(str(format) + ": " + "Invalid LyX file.") + + if format in formats_list(): + return format + + self.error(str(format) + ": " + "Format not supported.") + return None + + + def read_version(self): + """ Searchs for clues of the LyX version used to write the file, returns the + most likely value, or None otherwise.""" + for line in self.header: + if line[0] != "#": + return None + + result = original_version.match(line) + if result: + return result.group(1) + return None + + + def set_version(self): + " Set the header with the version used." + self.header[0] = "#LyX %s created this file. For more info see http://www.lyx.org/" % version_lyx2lyx + if self.header[1][0] == '#': + del self.header[1] + + + def read_format(self): + " Read from the header the fileformat of the present LyX file." + for line in self.header: + result = fileformat.match(line) + if result: + return self.lyxformat(result.group(1)) + else: + self.error("Invalid LyX File.") + return None + + + def set_format(self): + " Set the file format of the file, in the header." + if self.format <= 217: + format = str(float(self.format)/100) + else: + format = str(self.format) + i = find_token(self.header, "\\lyxformat", 0) + self.header[i] = "\\lyxformat %s" % format + + + def set_parameter(self, param, value): + " Set the value of the header parameter." + i = find_token(self.header, '\\' + param, 0) + if i == -1: + self.warning(3, 'Parameter not found in the header: %s' % param) + return + self.header[i] = '\\%s %s' % (param, str(value)) + + + def convert(self): + "Convert from current (self.format) to self.end_format." + mode, convertion_chain = self.chain() + self.warning("convertion chain: " + str(convertion_chain), 3) + + for step in convertion_chain: + steps = getattr(__import__("lyx_" + step), mode) + + self.warning("Convertion step: %s - %s" % (step, mode), default_debug_level + 1) + if not steps: + self.error("The convertion to an older format (%s) is not implemented." % self.format) + + multi_conv = len(steps) != 1 + for version, table in steps: + if multi_conv and \ + (self.format >= version and mode == "convert") or\ + (self.format <= version and mode == "revert"): + continue + + for conv in table: + init_t = time.time() + try: + conv(self) + except: + self.warning("An error ocurred in %s, %s" % (version, str(conv)), + default_debug_level) + if not self.try_hard: + raise + self.status = 2 + else: + self.warning("%lf: Elapsed time on %s" % (time.time() - init_t, str(conv)), + default_debug_level + 1) + + self.format = version + if self.end_format == self.format: + return + + + def chain(self): + """ This is where all the decisions related with the convertion are taken. + It returns a list of modules needed to convert the LyX file from + self.format to self.end_format""" + + self.start = self.format + format = self.format + correct_version = 0 + + for rel in format_relation: + if self.initial_version in rel[2]: + if format in rel[1]: + initial_step = rel[0] + correct_version = 1 + break + + if not correct_version: + if format <= 215: + self.warning("Version does not match file format, discarding it.") + for rel in format_relation: + if format in rel[1]: + initial_step = rel[0] + break + else: + # This should not happen, really. + self.error("Format not supported.") + + # Find the final step + for rel in format_relation: + if self.end_format in rel[1]: + final_step = rel[0] + break + else: + self.error("Format not supported.") + + # Convertion mode, back or forth + steps = [] + if (initial_step, self.start) < (final_step, self.end_format): + mode = "convert" + first_step = 1 + for step in format_relation: + if initial_step <= step[0] <= final_step: + if first_step and len(step[1]) == 1: + first_step = 0 + continue + steps.append(step[0]) + else: + mode = "revert" + relation_format = format_relation + relation_format.reverse() + last_step = None + + for step in relation_format: + if final_step <= step[0] <= initial_step: + steps.append(step[0]) + last_step = step + + if last_step[1][-1] == self.end_format: + steps.pop() + + return mode, steps + + + def get_toc(self, depth = 4): + " Returns the TOC of this LyX document." + paragraphs_filter = {'Title' : 0,'Chapter' : 1, 'Section' : 2, 'Subsection' : 3, 'Subsubsection': 4} + allowed_insets = ['Quotes'] + allowed_parameters = '\\paragraph_spacing', '\\noindent', '\\align', '\\labelwidthstring', "\\start_of_appendix" + + sections = [] + for section in paragraphs_filter.keys(): + sections.append('\\begin_layout %s' % section) + + toc_par = [] + i = 0 + while 1: + i = find_tokens(self.body, sections, i) + if i == -1: + break + + j = find_end_of(self.body, i + 1, '\\begin_layout', '\\end_layout') + if j == -1: + self.warning('Incomplete file.', 0) + break + + section = string.split(self.body[i])[1] + if section[-1] == '*': + section = section[:-1] + + par = [] + + k = i + 1 + # skip paragraph parameters + while not string.strip(self.body[k]) and string.split(self.body[k])[0] in allowed_parameters: + k = k +1 + + while k < j: + if check_token(self.body[k], '\\begin_inset'): + inset = string.split(self.body[k])[1] + end = find_end_of_inset(self.body, k) + if end == -1 or end > j: + self.warning('Malformed file.', 0) + + if inset in allowed_insets: + par.extend(self.body[k: end+1]) + k = end + 1 + else: + par.append(self.body[k]) + k = k + 1 + + # trim empty lines in the end. + while string.strip(par[-1]) == '' and par: + par.pop() + + toc_par.append(Paragraph(section, par)) + + i = j + 1 + + return toc_par + + +class File(LyX_Base): + " This class reads existing LyX files." + def __init__(self, end_format = 0, input = "", output = "", error = "", debug = default_debug_level, try_hard = 0): + LyX_Base.__init__(self, end_format, input, output, error, debug, try_hard) + self.read() + + +class NewFile(LyX_Base): + " This class is to create new LyX files." + def set_header(self, **params): + # set default values + self.header.extend([ + "#LyX xxxx created this file. For more info see http://www.lyx.org/", + "\\lyxformat xxx", + "\\begin_document", + "\\begin_header", + "\\textclass article", + "\\language english", + "\\inputencoding auto", + "\\fontscheme default", + "\\graphics default", + "\\paperfontsize default", + "\\papersize default", + "\\paperpackage none", + "\\use_geometry false", + "\\use_amsmath 1", + "\\cite_engine basic", + "\\use_bibtopic false", + "\\paperorientation portrait", + "\\secnumdepth 3", + "\\tocdepth 3", + "\\paragraph_separation indent", + "\\defskip medskip", + "\\quotes_language english", + "\\quotes_times 2", + "\\papercolumns 1", + "\\papersides 1", + "\\paperpagestyle default", + "\\tracking_changes false", + "\\end_header"]) + + self.format = get_end_format() + for param in params: + self.set_parameter(param, params[param]) + + + def set_body(self, paragraphs): + self.body.extend(['\\begin_body','']) + + for par in paragraphs: + self.body.extend(par.asLines()) + + self.body.extend(['','\\end_body', '\\end_document']) + + +class Paragraph: + # unfinished implementation, it is missing the Text and Insets representation. + " This class represents the LyX paragraphs." + def __init__(self, name, body=[], settings = [], child = []): + """ Parameters: + name: paragraph name. + body: list of lines of body text. + child: list of paragraphs that descend from this paragraph. + """ + self.name = name + self.body = body + self.settings = settings + self.child = child + + def asLines(self): + " Converts the paragraph to a list of strings, representing it in the LyX file." + result = ['','\\begin_layout %s' % self.name] + result.extend(self.settings) + result.append('') + result.extend(self.body) + result.append('\\end_layout') + + if not self.child: + return result + + result.append('\\begin_deeper') + for node in self.child: + result.extend(node.asLines()) + result.append('\\end_deeper') + + return result + + +class Inset: + " This class represents the LyX insets." + pass + + +class Text: + " This class represents simple chuncks of text." + pass diff --git a/lib/lyx2lyx/lyx_0_12.py b/lib/lyx2lyx/lyx_0_12.py new file mode 100644 index 0000000000..bb89fb57bd --- /dev/null +++ b/lib/lyx2lyx/lyx_0_12.py @@ -0,0 +1,303 @@ +# This file is part of lyx2lyx +# -*- coding: iso-8859-1 -*- +# Copyright (C) 2003-2004 José Matos +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +import re +import string +from parser_tools import find_token, find_re, check_token + + +def space_before_layout(file): + lines = file.body + i = 2 # skip first layout + while 1: + i = find_token(lines, '\\layout', i) + if i == -1: + break + + if lines[i - 1] == '' and string.find(lines[i-2],'\\protected_separator') == -1: + del lines[i-1] + i = i + 1 + + +def formula_inset_space_eat(file): + lines = file.body + i=0 + while 1: + i = find_token(lines, "\\begin_inset Formula", i) + if i == -1: break + + if len(lines[i]) > 22 and lines[i][21] == ' ': + lines[i] = lines[i][:20] + lines[i][21:] + i = i + 1 + + +# Update from tabular format 1 or 2 to 4 +def update_tabular(file): + lines = file.body + lyxtable_re = re.compile(r".*\\LyXTable$") + i=0 + while 1: + i = find_re(lines, lyxtable_re, i) + if i == -1: + break + i = i + 1 + format = lines[i][8:] + + lines[i]='multicol4' + i = i + 1 + rows = int(string.split(lines[i])[0]) + columns = int(string.split(lines[i])[1]) + + lines[i] = lines[i] + ' 0 0 -1 -1 -1 -1' + i = i + 1 + + for j in range(rows): + lines[i] = lines[i] + ' 0 0' + i = i + 1 + + for j in range(columns): + lines[i] = lines[i] + ' ' + i = i + 1 + + while string.strip(lines[i]): + if not format: + lines[i] = lines[i] + ' 1 1' + lines[i] = lines[i] + ' 0 0 0' + i = i + 1 + + lines[i] = string.strip(lines[i]) + +def final_dot(file): + lines = file.body + i = 0 + while i < len(lines): + if lines[i][-1:] == '.' and lines[i+1][:1] != '\\' and lines[i+1][:1] != ' ' and len(lines[i]) + len(lines[i+1])<= 72 and lines[i+1] != '': + lines[i] = lines[i] + lines[i+1] + del lines[i+1] + else: + i = i + 1 + + +def update_inset_label(file): + lines = file.body + i = 0 + while 1: + i = find_token(lines, '\\begin_inset Label', i) + if i == -1: + return + lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}' + i = i + 1 + + +def update_latexdel(file): + lines = file.body + i = 0 + while 1: + i = find_token(lines, '\\begin_inset LatexDel', i) + if i == -1: + return + lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand') + i = i + 1 + + +def update_vfill(file): + lines = file.body + for i in range(len(lines)): + lines[i] = string.replace(lines[i],'\\fill_top','\\added_space_top vfill') + lines[i] = string.replace(lines[i],'\\fill_bottom','\\added_space_bottom vfill') + + +def update_space_units(file): + lines = file.body + added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)') + added_space_top = re.compile(r'\\added_space_top ([^ ]*)') + for i in range(len(lines)): + result = added_space_bottom.search(lines[i]) + if result: + old = '\\added_space_bottom ' + result.group(1) + new = '\\added_space_bottom ' + str(float(result.group(1))) + 'cm' + lines[i] = string.replace(lines[i], old, new) + + result = added_space_top.search(lines[i]) + if result: + old = '\\added_space_top ' + result.group(1) + new = '\\added_space_top ' + str(float(result.group(1))) + 'cm' + lines[i] = string.replace(lines[i], old, new) + + +def remove_cursor(file): + lines = file.body + i = 0 + cursor_re = re.compile(r'.*(\\cursor \d*)') + while 1: + i = find_re(lines, cursor_re, i) + if i == -1: + break + cursor = cursor_re.search(lines[i]).group(1) + lines[i]= string.replace(lines[i], cursor, '') + i = i + 1 + + +def remove_empty_insets(file): + lines = file.body + i = 0 + while 1: + i = find_token(lines, '\\begin_inset ',i) + if i == -1: + break + if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ': + del lines[i] + del lines[i] + i = i + 1 + + +def remove_formula_latex(file): + lines = file.body + i = 0 + while 1: + i = find_token(lines, '\\latex formula_latex ', i) + if i == -1: + break + del lines[i] + + i = find_token(lines, '\\latex default', i) + if i == -1: + break + del lines[i] + + +def add_end_document(file): + lines = file.body + i = find_token(lines, '\\the_end', 0) + if i == -1: + lines.append('\\the_end') + + +def header_update(file): + lines = file.header + i = 0 + l = len(lines) + while i < l: + if check_token(lines[i], '\\begin_preamble'): + i = find_token(lines, '\\end_preamble', i) + if i == -1: + file.error('Unfinished preamble') + i = i + 1 + continue + + if lines[i][-1:] == ' ': + lines[i] = lines[i][:-1] + + if check_token(lines[i], '\\epsfig'): + lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics') + i = i + 1 + continue + + if check_token(lines[i], '\\papersize'): + size = string.split(lines[i])[1] + new_size = size + paperpackage = "" + + if size == 'usletter': + new_size = 'letterpaper' + if size == 'a4wide': + new_size = 'Default' + paperpackage = "widemarginsa4" + + lines[i] = '\\papersize ' + new_size + i = i + 1 + if paperpackage: + lines.insert(i, '\\paperpackage ' + paperpackage) + i = i + 1 + + lines.insert(i,'\\use_geometry 0') + lines.insert(i + 1,'\\use_amsmath 0') + i = i + 2 + continue + + + if check_token(lines[i], '\\baselinestretch'): + size = string.split(lines[i])[1] + if size == '1.00': + name = 'single' + elif size == '1.50': + name = 'onehalf' + elif size == '2.00': + name = 'double' + else: + name = 'other ' + size + lines[i] = '\\spacing %s ' % name + i = i + 1 + continue + + i = i + 1 + + +def update_latexaccents(file): + body = file.body + i = 1 + while 1: + i = find_token(body, '\\i ', i) + if i == -1: + return + + contents = string.strip(body[i][2:]) + + if string.find(contents, '{') != -1 and string.find(contents, '}') != -1: + i = i + 1 + continue + + if len(contents) == 2: + contents = contents + '{}' + elif len(contents) == 3: + contents = contents[:2] + '{' + contents[2] + '}' + elif len(contents) == 4: + if contents[2] == ' ': + contents = contents[:2] + '{' + contents[3] + '}' + elif contents[2:4] == '\\i' or contents[2:4] == '\\j': + contents = contents[:2] + '{' + contents[2:] + '}' + + body[i] = '\\i ' + contents + i = i + 1 + + +def obsolete_latex_title(file): + body = file.body + i = 0 + while 1: + i = find_token(body, '\\layout', i) + if i == -1: + return + + if string.find(string.lower(body[i]),'latex_title') != -1: + body[i] = '\\layout Title' + + i = i + 1 + + +convert = [[215, [header_update, add_end_document, remove_cursor, + final_dot, update_inset_label, update_latexdel, + update_space_units, space_before_layout, + formula_inset_space_eat, update_tabular, + update_vfill, remove_empty_insets, + remove_formula_latex, update_latexaccents, obsolete_latex_title]]] +revert = [] + + +if __name__ == "__main__": + pass diff --git a/lib/lyx2lyx/lyx_1_1_5.py b/lib/lyx2lyx/lyx_1_1_5.py new file mode 100644 index 0000000000..645c1657d4 --- /dev/null +++ b/lib/lyx2lyx/lyx_1_1_5.py @@ -0,0 +1,246 @@ +# This file is part of lyx2lyx +# -*- coding: iso-8859-1 -*- +# Copyright (C) 2002-2004 José Matos +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +import re +import string +from parser_tools import find_token, find_token_backwards, find_re + + +layout_exp = re.compile(r"\\layout (\S*)") +math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"] + +def replace_protected_separator(file): + lines = file.body + i=0 + while 1: + i = find_token(lines, "\\protected_separator", i) + if i == -1: + break + j = find_token_backwards(lines, "\\layout", i) + #if j == -1: print error + layout_m = layout_exp.match(lines[j]) + if layout_m: + layout = layout_m.group(1) + else: + layout = "Standard" + + if layout == "LyX-Code": + result = "" + while lines[i] == "\\protected_separator ": + result = result + " " + del lines[i] + + lines[i-1] = lines[i-1] + result + lines[i] + else: + lines[i-1] = lines[i-1]+ "\\SpecialChar ~" + + del lines[i] + + +def merge_formula_inset(file): + lines = file.body + i=0 + while 1: + i = find_token(lines, "\\begin_inset Formula", i) + if i == -1: break + if lines[i+1] in math_env: + lines[i] = lines[i] + lines[i+1] + del lines[i+1] + i = i + 1 + + +# Update from tabular format 4 to 5 if necessary +def update_tabular(file): + lines = file.body + lyxtable_re = re.compile(r".*\\LyXTable$") + i=0 + while 1: + i = find_re(lines, lyxtable_re, i) + if i == -1: + break + i = i + 1 + format = lines[i][8] + if format != '4': + continue + + lines[i]='multicol5' + i = i + 1 + rows = int(string.split(lines[i])[0]) + columns = int(string.split(lines[i])[1]) + + i = i + rows + 1 + for j in range(columns): + col_info = string.split(lines[i]) + if len(col_info) == 3: + lines[i] = lines[i] + '"" ""' + else: + lines[i] = string.join(col_info[:3]) + ' "%s" ""' % col_info[3] + i = i + 1 + + while lines[i]: + lines[i] = lines[i] + ' "" ""' + i = i + 1 + + +def update_toc(file): + lines = file.body + i = 0 + while 1: + i = find_token(lines, '\\begin_inset LatexCommand \\tableofcontents', i) + if i == -1: + break + lines[i] = lines[i] + '{}' + i = i + 1 + + +def remove_cursor(file): + lines = file.body + i = find_token(lines, '\\cursor', 0) + if i != -1: + del lines[i] + + +def remove_vcid(file): + lines = file.header + i = find_token(lines, '\\lyxvcid', 0) + if i != -1: + del lines[i] + i = find_token(lines, '\\lyxrcsid', 0) + if i != -1: + del lines[i] + + +def first_layout(file): + lines = file.body + while (lines[0] == ""): + del lines[0] + if lines[0][:7] != "\\layout": + lines[:0] = ["\\layout Standard"] + + +def remove_space_in_units(file): + lines = file.header + margins = ["\\topmargin","\\rightmargin", + "\\leftmargin","\\bottommargin"] + + unit_rexp = re.compile(r'[^ ]* (.*) (.*)') + + begin_preamble = find_token(lines,"\\begin_preamble", 0) + end_preamble = find_token(lines, "\\end_preamble", 0) + for margin in margins: + i = 0 + while 1: + i = find_token(lines, margin, i) + if i == -1: + break + + if i > begin_preamble and i < end_preamble: + i = i + 1 + continue + + result = unit_rexp.search(lines[i]) + if result: + lines[i] = margin + " " + result.group(1) + result.group(2) + i = i + 1 + + +def latexdel_getargs(file, i): + lines = file.body + + # play safe, clean empty lines + while 1: + if lines[i]: + break + del lines[i] + + j = find_token(lines, '\\end_inset', i) + + if i == j: + del lines[i] + else: + file.warning("Unexpected end of inset.") + j = find_token(lines, '\\begin_inset LatexDel }{', i) + + ref = string.join(lines[i:j]) + del lines[i:j + 1] + + # play safe, clean empty lines + while 1: + if lines[i]: + break + del lines[i] + + j = find_token(lines, '\\end_inset', i - 1) + if i == j: + del lines[i] + else: + file.warning("Unexpected end of inset.") + j = find_token(lines, '\\begin_inset LatexDel }', i) + label = string.join(lines[i:j]) + del lines[i:j + 1] + + return ref, label + + +def update_ref(file): + lines = file.body + i = 0 + while 1: + i = find_token(lines, '\\begin_inset LatexCommand', i) + if i == -1: + return + + if string.split(lines[i])[-1] == "\\ref{": + i = i + 1 + ref, label = latexdel_getargs(file, i) + lines[i - 1] = "%s[%s]{%s}" % (lines[i - 1][:-1], ref, label) + + i = i + 1 + + +def update_latexdel(file): + lines = file.body + i = 0 + latexdel_re = re.compile(r".*\\begin_inset LatexDel") + while 1: + i = find_re(lines, latexdel_re, i) + if i == -1: + return + lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand') + + j = string.find(lines[i],'\\begin_inset') + lines.insert(i+1, lines[i][j:]) + lines[i] = string.strip(lines[i][:j]) + i = i + 1 + + if string.split(lines[i])[-1] in ("\\url{", "\\htmlurl{"): + i = i + 1 + + ref, label = latexdel_getargs(file, i) + lines[i -1] = "%s[%s]{%s}" % (lines[i-1][:-1], label, ref) + + i = i + 1 + + +convert = [[216, [first_layout, remove_vcid, remove_cursor, update_toc, + replace_protected_separator, merge_formula_inset, + update_tabular, remove_space_in_units, update_ref, update_latexdel]]] +revert = [] + +if __name__ == "__main__": + pass diff --git a/lib/lyx2lyx/lyx_1_2.py b/lib/lyx2lyx/lyx_1_2.py new file mode 100644 index 0000000000..672d44fd8c --- /dev/null +++ b/lib/lyx2lyx/lyx_1_2.py @@ -0,0 +1,750 @@ +# This file is part of lyx2lyx +# -*- coding: iso-8859-1 -*- +# Copyright (C) 2002 Dekel Tsur +# Copyright (C) 2004 José Matos +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +import string +import re + +from parser_tools import find_token, find_token_backwards, get_next_paragraph,\ + find_tokens, find_end_of_inset, find_re, \ + is_nonempty_line, get_paragraph, find_nonempty_line, \ + get_value, get_tabular_lines, check_token + +floats = { + "footnote": ["\\begin_inset Foot", + "collapsed true"], + "margin": ["\\begin_inset Marginal", + "collapsed true"], + "fig": ["\\begin_inset Float figure", + "wide false", + "collapsed false"], + "tab": ["\\begin_inset Float table", + "wide false", + "collapsed false"], + "alg": ["\\begin_inset Float algorithm", + "wide false", + "collapsed false"], + "wide-fig": ["\\begin_inset Float figure", + "wide true", + "collapsed false"], + "wide-tab": ["\\begin_inset Float table", + "wide true", + "collapsed false"] +} + +font_tokens = ["\\family", "\\series", "\\shape", "\\size", "\\emph", + "\\bar", "\\noun", "\\color", "\\lang", "\\latex"] + +pextra_type3_rexp = re.compile(r".*\\pextra_type\s+3") +pextra_rexp = re.compile(r"\\pextra_type\s+(\S+)"+\ + r"(\s+\\pextra_alignment\s+(\S+))?"+\ + r"(\s+\\pextra_hfill\s+(\S+))?"+\ + r"(\s+\\pextra_start_minipage\s+(\S+))?"+\ + r"(\s+(\\pextra_widthp?)\s+(\S*))?") + + +def get_width(mo): + if mo.group(10): + if mo.group(9) == "\\pextra_widthp": + return mo.group(10)+"col%" + else: + return mo.group(10) + else: + return "100col%" + + +# +# Change \begin_float .. \end_float into \begin_inset Float .. \end_inset +# +def remove_oldfloat(file): + lines = file.body + i = 0 + while 1: + i = find_token(lines, "\\begin_float", i) + if i == -1: + break + # There are no nested floats, so finding the end of the float is simple + j = find_token(lines, "\\end_float", i+1) + + floattype = string.split(lines[i])[1] + if not floats.has_key(floattype): + file.warning("Error! Unknown float type " + floattype) + floattype = "fig" + + # skip \end_deeper tokens + i2 = i+1 + while check_token(lines[i2], "\\end_deeper"): + i2 = i2+1 + if i2 > i+1: + j2 = get_next_paragraph(lines, j+1) + lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1)) + + new = floats[floattype]+[""] + + # Check if the float is floatingfigure + k = find_re(lines, pextra_type3_rexp, i, j) + if k != -1: + mo = pextra_rexp.search(lines[k]) + width = get_width(mo) + lines[k] = re.sub(pextra_rexp, "", lines[k]) + new = ["\\begin_inset Wrap figure", + 'width "%s"' % width, + "collapsed false", + ""] + + new = new+lines[i2:j]+["\\end_inset ", ""] + + # After a float, all font attributes are reseted. + # We need to output '\foo default' for every attribute foo + # whose value is not default before the float. + # The check here is not accurate, but it doesn't matter + # as extra '\foo default' commands are ignored. + # In fact, it might be safer to output '\foo default' for all + # font attributes. + k = get_paragraph(lines, i) + flag = 0 + for token in font_tokens: + if find_token(lines, token, k, i) != -1: + if not flag: + # This is not necessary, but we want the output to be + # as similar as posible to the lyx format + flag = 1 + new.append("") + if token == "\\lang": + new.append(token+" "+ file.language) + else: + new.append(token+" default ") + + lines[i:j+1] = new + i = i+1 + + +pextra_type2_rexp = re.compile(r".*\\pextra_type\s+[12]") +pextra_type2_rexp2 = re.compile(r".*(\\layout|\\pextra_type\s+2)") +pextra_widthp = re.compile(r"\\pextra_widthp") + +def remove_pextra(file): + lines = file.body + i = 0 + flag = 0 + while 1: + i = find_re(lines, pextra_type2_rexp, i) + if i == -1: + break + + # Sometimes the \pextra_widthp argument comes in it own + # line. If that happens insert it back in this line. + if pextra_widthp.search(lines[i+1]): + lines[i] = lines[i] + ' ' + lines[i+1] + del lines[i+1] + + mo = pextra_rexp.search(lines[i]) + width = get_width(mo) + + if mo.group(1) == "1": + # handle \pextra_type 1 (indented paragraph) + lines[i] = re.sub(pextra_rexp, "\\leftindent "+width+" ", lines[i]) + i = i+1 + continue + + # handle \pextra_type 2 (minipage) + position = mo.group(3) + hfill = mo.group(5) + lines[i] = re.sub(pextra_rexp, "", lines[i]) + + start = ["\\begin_inset Minipage", + "position " + position, + "inner_position 0", + 'height "0pt"', + 'width "%s"' % width, + "collapsed false" + ] + if flag: + flag = 0 + if hfill: + start = ["","\hfill",""]+start + else: + start = ["\\layout Standard"] + start + + j0 = find_token_backwards(lines,"\\layout", i-1) + j = get_next_paragraph(lines, i) + + count = 0 + while 1: + # collect more paragraphs to the minipage + count = count+1 + if j == -1 or not check_token(lines[j], "\\layout"): + break + i = find_re(lines, pextra_type2_rexp2, j+1) + if i == -1: + break + mo = pextra_rexp.search(lines[i]) + if not mo: + break + if mo.group(7) == "1": + flag = 1 + break + lines[i] = re.sub(pextra_rexp, "", lines[i]) + j = find_tokens(lines, ["\\layout", "\\end_float"], i+1) + + mid = lines[j0:j] + end = ["\\end_inset "] + + lines[j0:j] = start+mid+end + i = i+1 + + +def is_empty(lines): + return filter(is_nonempty_line, lines) == [] + + +move_rexp = re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun|end_deeper)") +ert_rexp = re.compile(r"\\begin_inset|\\hfill|.*\\SpecialChar") +spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)") +ert_begin = ["\\begin_inset ERT", + "status Collapsed", + "", + "\\layout Standard"] + + +def remove_oldert(file): + lines = file.body + i = 0 + while 1: + i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i) + if i == -1: + break + j = i+1 + while 1: + # \end_inset is for ert inside a tabular cell. The other tokens + # are obvious. + j = find_tokens(lines, ["\\latex default", "\\layout", "\\begin_inset", "\\end_inset", "\\end_float", "\\the_end"], + j) + if check_token(lines[j], "\\begin_inset"): + j = find_end_of_inset(lines, j)+1 + else: + break + + if check_token(lines[j], "\\layout"): + while j-1 >= 0 and check_token(lines[j-1], "\\begin_deeper"): + j = j-1 + + # We need to remove insets, special chars & font commands from ERT text + new = [] + new2 = [] + if check_token(lines[i], "\\layout LaTeX"): + new = ["\layout Standard", "", ""] + # We have a problem with classes in which Standard is not the default layout! + + k = i+1 + while 1: + k2 = find_re(lines, ert_rexp, k, j) + inset = hfill = specialchar = 0 + if k2 == -1: + k2 = j + elif check_token(lines[k2], "\\begin_inset"): + inset = 1 + elif check_token(lines[k2], "\\hfill"): + hfill = 1 + del lines[k2] + j = j-1 + else: + specialchar = 1 + mo = spchar_rexp.match(lines[k2]) + lines[k2] = mo.group(1) + specialchar_str = mo.group(2) + k2 = k2+1 + + tmp = [] + for line in lines[k:k2]: + # Move some lines outside the ERT inset: + if move_rexp.match(line): + if new2 == []: + # This is not necessary, but we want the output to be + # as similar as posible to the lyx format + new2 = [""] + new2.append(line) + elif not check_token(line, "\\latex"): + tmp.append(line) + + if is_empty(tmp): + if filter(lambda x:x != "", tmp) != []: + if new == []: + # This is not necessary, but we want the output to be + # as similar as posible to the lyx format + lines[i-1] = lines[i-1]+" " + else: + new = new+[" "] + else: + new = new+ert_begin+tmp+["\\end_inset ", ""] + + if inset: + k3 = find_end_of_inset(lines, k2) + new = new+[""]+lines[k2:k3+1]+[""] # Put an empty line after \end_inset + k = k3+1 + # Skip the empty line after \end_inset + if not is_nonempty_line(lines[k]): + k = k+1 + new.append("") + elif hfill: + new = new + ["\\hfill", ""] + k = k2 + elif specialchar: + if new == []: + # This is not necessary, but we want the output to be + # as similar as posible to the lyx format + lines[i-1] = lines[i-1]+specialchar_str + new = [""] + else: + new = new+[specialchar_str, ""] + k = k2 + else: + break + + new = new+new2 + if not check_token(lines[j], "\\latex "): + new = new+[""]+[lines[j]] + lines[i:j+1] = new + i = i+1 + + # Delete remaining "\latex xxx" tokens + i = 0 + while 1: + i = find_token(lines, "\\latex ", i) + if i == -1: + break + del lines[i] + + +# ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future. +def remove_oldertinset(file): + lines = file.body + i = 0 + while 1: + i = find_token(lines, "\\begin_inset ERT", i) + if i == -1: + break + j = find_end_of_inset(lines, i) + k = find_token(lines, "\\layout", i+1) + l = get_paragraph(lines, i) + if lines[k] == lines[l]: # same layout + k = k+1 + new = lines[k:j] + lines[i:j+1] = new + i = i+1 + + +def is_ert_paragraph(lines, i): + if not check_token(lines[i], "\\layout Standard"): + return 0 + + i = find_nonempty_line(lines, i+1) + if not check_token(lines[i], "\\begin_inset ERT"): + return 0 + + j = find_end_of_inset(lines, i) + k = find_nonempty_line(lines, j+1) + return check_token(lines[k], "\\layout") + + +def combine_ert(file): + lines = file.body + i = 0 + while 1: + i = find_token(lines, "\\begin_inset ERT", i) + if i == -1: + break + j = get_paragraph(lines, i) + count = 0 + text = [] + while is_ert_paragraph(lines, j): + + count = count+1 + i2 = find_token(lines, "\\layout", j+1) + k = find_token(lines, "\\end_inset", i2+1) + text = text+lines[i2:k] + j = find_token(lines, "\\layout", k+1) + if j == -1: + break + + if count >= 2: + j = find_token(lines, "\\layout", i+1) + lines[j:k] = text + + i = i+1 + + +oldunits = ["pt", "cm", "in", "text%", "col%"] + +def get_length(lines, name, start, end): + i = find_token(lines, name, start, end) + if i == -1: + return "" + x = string.split(lines[i]) + return x[2]+oldunits[int(x[1])] + + +def write_attribute(x, token, value): + if value != "": + x.append("\t"+token+" "+value) + + +def remove_figinset(file): + lines = file.body + i = 0 + while 1: + i = find_token(lines, "\\begin_inset Figure", i) + if i == -1: + break + j = find_end_of_inset(lines, i) + + if ( len(string.split(lines[i])) > 2 ): + lyxwidth = string.split(lines[i])[3]+"pt" + lyxheight = string.split(lines[i])[4]+"pt" + else: + lyxwidth = "" + lyxheight = "" + + filename = get_value(lines, "file", i+1, j) + + width = get_length(lines, "width", i+1, j) + # what does width=5 mean ? + height = get_length(lines, "height", i+1, j) + rotateAngle = get_value(lines, "angle", i+1, j) + if width == "" and height == "": + size_type = "0" + else: + size_type = "1" + + flags = get_value(lines, "flags", i+1, j) + x = int(flags)%4 + if x == 1: + display = "monochrome" + elif x == 2: + display = "gray" + else: + display = "color" + + subcaptionText = "" + subcaptionLine = find_token(lines, "subcaption", i+1, j) + if subcaptionLine != -1: + subcaptionText = lines[subcaptionLine][11:] + if subcaptionText != "": + subcaptionText = '"'+subcaptionText+'"' + + k = find_token(lines, "subfigure", i+1,j) + if k == -1: + subcaption = 0 + else: + subcaption = 1 + + new = ["\\begin_inset Graphics FormatVersion 1"] + write_attribute(new, "filename", filename) + write_attribute(new, "display", display) + if subcaption: + new.append("\tsubcaption") + write_attribute(new, "subcaptionText", subcaptionText) + write_attribute(new, "size_type", size_type) + write_attribute(new, "width", width) + write_attribute(new, "height", height) + if rotateAngle != "": + new.append("\trotate") + write_attribute(new, "rotateAngle", rotateAngle) + write_attribute(new, "rotateOrigin", "leftBaseline") + write_attribute(new, "lyxsize_type", "1") + write_attribute(new, "lyxwidth", lyxwidth) + write_attribute(new, "lyxheight", lyxheight) + new = new + ["\\end_inset"] + lines[i:j+1] = new + + +## +# Convert tabular format 2 to 3 +# +attr_re = re.compile(r' \w*="(false|0|)"') +line_re = re.compile(r'<(features|column|row|cell)') + +def update_tabular(file): + regexp = re.compile(r'^\\begin_inset\s+Tabular') + lines = file.body + i = 0 + while 1: + i = find_re(lines, regexp, i) + if i == -1: + break + + for k in get_tabular_lines(lines, i): + if check_token(lines[k], "= 2.3 has real booleans (False and True) +false = 0 +true = 1 + +# simple data structure to deal with long table info +class row: + def __init__(self): + self.endhead = false # header row + self.endfirsthead = false # first header row + self.endfoot = false # footer row + self.endlastfoot = false # last footer row + + +def haveLTFoot(row_info): + for row_ in row_info: + if row_.endfoot: + return true + return false + + +def setHeaderFooterRows(hr, fhr, fr, lfr, rows_, row_info): + endfirsthead_empty = false + endlastfoot_empty = false + # set header info + while (hr > 0): + hr = hr - 1 + row_info[hr].endhead = true + + # set firstheader info + if fhr and fhr < rows_: + if row_info[fhr].endhead: + while fhr > 0: + fhr = fhr - 1 + row_info[fhr].endfirsthead = true + row_info[fhr].endhead = false + elif row_info[fhr - 1].endhead: + endfirsthead_empty = true + else: + while fhr > 0 and not row_info[fhr - 1].endhead: + fhr = fhr - 1 + row_info[fhr].endfirsthead = true + + # set footer info + if fr and fr < rows_: + if row_info[fr].endhead and row_info[fr - 1].endhead: + while fr > 0 and not row_info[fr - 1].endhead: + fr = fr - 1 + row_info[fr].endfoot = true + row_info[fr].endhead = false + elif row_info[fr].endfirsthead and row_info[fr - 1].endfirsthead: + while fr > 0 and not row_info[fr - 1].endfirsthead: + fr = fr - 1 + row_info[fr].endfoot = true + row_info[fr].endfirsthead = false + elif not row_info[fr - 1].endhead and not row_info[fr - 1].endfirsthead: + while fr > 0 and not row_info[fr - 1].endhead and not row_info[fr - 1].endfirsthead: + fr = fr - 1 + row_info[fr].endfoot = true + + # set lastfooter info + if lfr and lfr < rows_: + if row_info[lfr].endhead and row_info[lfr - 1].endhead: + while lfr > 0 and not row_info[lfr - 1].endhead: + lfr = lfr - 1 + row_info[lfr].endlastfoot = true + row_info[lfr].endhead = false + elif row_info[lfr].endfirsthead and row_info[lfr - 1].endfirsthead: + while lfr > 0 and not row_info[lfr - 1].endfirsthead: + lfr = lfr - 1 + row_info[lfr].endlastfoot = true + row_info[lfr].endfirsthead = false + elif row_info[lfr].endfoot and row_info[lfr - 1].endfoot: + while lfr > 0 and not row_info[lfr - 1].endfoot: + lfr = lfr - 1 + row_info[lfr].endlastfoot = true + row_info[lfr].endfoot = false + elif not row_info[fr - 1].endhead and not row_info[fr - 1].endfirsthead and not row_info[fr - 1].endfoot: + while lfr > 0 and not row_info[lfr - 1].endhead and not row_info[lfr - 1].endfirsthead and not row_info[lfr - 1].endfoot: + lfr = lfr - 1 + row_info[lfr].endlastfoot = true + elif haveLTFoot(row_info): + endlastfoot_empty = true + + return endfirsthead_empty, endlastfoot_empty + + +def insert_attribute(lines, i, attribute): + last = string.find(lines[i],'>') + lines[i] = lines[i][:last] + ' ' + attribute + lines[i][last:] + + +rows_re = re.compile(r'rows="(\d*)"') +longtable_re = re.compile(r'islongtable="(\w)"') +ltvalues_re = re.compile(r'endhead="(-?\d*)" endfirsthead="(-?\d*)" endfoot="(-?\d*)" endlastfoot="(-?\d*)"') +lt_features_re = re.compile(r'(endhead="-?\d*" endfirsthead="-?\d*" endfoot="-?\d*" endlastfoot="-?\d*")') +def update_longtables(file): + regexp = re.compile(r'^\\begin_inset\s+Tabular') + body = file.body + i = 0 + while 1: + i = find_re(body, regexp, i) + if i == -1: + break + i = i + 1 + i = find_token(body, "