X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=lib%2Flyx2lyx%2FLyX.py;h=680f33c88f275ccaea2f1b91d1f608d0b54bc6fc;hb=4c5ba07a7aa2f00c5b92aa6808f82672f1d313d5;hp=4bd8aae94d6ec1dad88cc5874ab7fda766a69514;hpb=5b79207f3d5121d259134d14584395cfd40cdb27;p=lyx.git diff --git a/lib/lyx2lyx/LyX.py b/lib/lyx2lyx/LyX.py index 4bd8aae94d..680f33c88f 100644 --- a/lib/lyx2lyx/LyX.py +++ b/lib/lyx2lyx/LyX.py @@ -17,7 +17,9 @@ # 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,\ +" The LyX module has all the rules related with different lyx file formats." + +from parser_tools import get_value, check_token, find_token, \ find_tokens, find_end_of import os.path import gzip @@ -26,10 +28,13 @@ import sys import re import time -import lyx2lyx_version -version_lyx2lyx = lyx2lyx_version.version +try: + import lyx2lyx_version + version__ = lyx2lyx_version.version +except: # we are running from build directory so assume the last version + version__ = '2.0.0svn' -default_debug_level = 2 +default_debug__ = 2 #################################################################### # Private helper functions @@ -38,13 +43,13 @@ def find_end_of_inset(lines, i): " Find beginning of inset, where lines[i] is included." return find_end_of(lines, i, "\\begin_inset", "\\end_inset") -def generate_minor_versions(major, last_minor_version): +def minor_versions(major, last_minor_version): """ Generate minor versions, using major as prefix and minor versions from 0 until last_minor_version, plus the generic version. Example: - generate_minor_versions("1.2", 4) -> + minor_versions("1.2", 4) -> [ "1.2", "1.2.0", "1.2.1", "1.2.2", "1.2.3"] """ return [major] + [major + ".%d" % i for i in range(last_minor_version + 1)] @@ -62,20 +67,33 @@ original_version = re.compile(r".*?LyX ([\d.]*)") ## # file format information: # file, supported formats, stable release versions -format_relation = [("0_06", [200], generate_minor_versions("0.6" , 4)), - ("0_08", [210], generate_minor_versions("0.8" , 6) + ["0.7"]), - ("0_10", [210], generate_minor_versions("0.10", 7) + ["0.9"]), - ("0_12", [215], generate_minor_versions("0.12", 1) + ["0.11"]), - ("1_0", [215], generate_minor_versions("1.0" , 4)), - ("1_1", [215], generate_minor_versions("1.1" , 4)), - ("1_1_5", [216], ["1.1.5","1.1.5.1","1.1.5.2","1.1"]), - ("1_1_6_0", [217], ["1.1.6","1.1.6.1","1.1.6.2","1.1"]), - ("1_1_6_3", [218], ["1.1.6.3","1.1.6.4","1.1"]), - ("1_2", [220], generate_minor_versions("1.2" , 4)), - ("1_3", [221], generate_minor_versions("1.3" , 7)), - ("1_4", range(222,246), generate_minor_versions("1.4" , 3)), - ("1_5", range(246,260), generate_minor_versions("1.5" , 0))] +format_relation = [("0_06", [200], minor_versions("0.6" , 4)), + ("0_08", [210], minor_versions("0.8" , 6) + ["0.7"]), + ("0_10", [210], minor_versions("0.10", 7) + ["0.9"]), + ("0_12", [215], minor_versions("0.12", 1) + ["0.11"]), + ("1_0", [215], minor_versions("1.0" , 4)), + ("1_1", [215], minor_versions("1.1" , 4)), + ("1_1_5", [216], ["1.1", "1.1.5","1.1.5.1","1.1.5.2"]), + ("1_1_6_0", [217], ["1.1", "1.1.6","1.1.6.1","1.1.6.2"]), + ("1_1_6_3", [218], ["1.1", "1.1.6.3","1.1.6.4"]), + ("1_2", [220], minor_versions("1.2" , 4)), + ("1_3", [221], minor_versions("1.3" , 7)), + ("1_4", range(222,246), minor_versions("1.4" , 5)), + ("1_5", range(246,277), minor_versions("1.5" , 7)), + ("1_6", range(277,346), minor_versions("1.6" , 0)), + ("2_0", [], minor_versions("2.0", 0))] +#################################################################### +# This is useful just for development versions # +# if the list of supported formats is empty get it from last step # +if not format_relation[-1][1]: + step, mode = format_relation[-1][0], "convert" + convert = getattr(__import__("lyx_" + step), mode) + format_relation[-1] = (step, + [conv[0] for conv in convert], + format_relation[-1][2]) +# # +#################################################################### def formats_list(): " Returns a list with supported file formats." @@ -87,6 +105,23 @@ def formats_list(): return formats +def format_info(): + " Returns a list with supported file formats." + out = """Major version: + minor versions + formats +""" + for version in format_relation: + major = str(version[2][0]) + versions = str(version[2][1:]) + if len(version[1]) == 1: + formats = str(version[1][0]) + else: + formats = "%s - %s" % (version[1][-1], version[1][0]) + out += "%s\n\t%s\n\t%s\n\n" % (major, versions, formats) + return out + '\n' + + def get_end_format(): " Returns the more recent file format available." return format_relation[-1][1][-1] @@ -96,7 +131,7 @@ def get_backend(textclass): " For _textclass_ returns its backend." if textclass == "linuxdoc" or textclass == "manpage": return "linuxdoc" - if textclass[:7] == "docbook": + if textclass.startswith("docbook") or textclass.startswith("agu-"): return "docbook" return "latex" @@ -110,6 +145,7 @@ def trim_eol(line): def get_encoding(language, inputencoding, format, cjk_encoding): + " Returns enconding of the lyx file" if format > 248: return "utf8" # CJK-LyX encodes files using the current locale encoding. @@ -118,13 +154,15 @@ def get_encoding(language, inputencoding, format, cjk_encoding): # argument. if cjk_encoding == 'auto': return locale.getpreferredencoding() - elif cjk_encoding != '': + elif cjk_encoding: return cjk_encoding from lyx2lyx_lang import lang if inputencoding == "auto" or inputencoding == "default": return lang[language][3] if inputencoding == "": return "latin1" + if inputencoding == "utf8x": + return "utf8" # python does not know the alias latin9 if inputencoding == "latin9": return "iso-8859-15" @@ -133,12 +171,12 @@ def get_encoding(language, inputencoding, format, cjk_encoding): ## # Class # -class LyX_Base: +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, cjk_encoding = '', - language = "english", encoding = "auto"): + + def __init__(self, end_format = 0, input = "", output = "", error = "", + debug = default_debug__, try_hard = 0, cjk_encoding = '', + final_version = "", language = "english", encoding = "auto"): """Arguments: end_format: final format that the file should be converted. (integer) @@ -160,9 +198,37 @@ class LyX_Base: if end_format: self.end_format = self.lyxformat(end_format) + + # In case the target version and format are both specified + # verify that they are compatible. If not send a warning + # and ignore the version. + if final_version: + message = "Incompatible version %s for specified format %d" % ( + final_version, self.end_format) + for version in format_relation: + if self.end_format in version[1]: + if final_version not in version[2]: + self.warning(message) + final_version = "" + elif final_version: + for version in format_relation: + if final_version in version[2]: + # set the last format for that version + self.end_format = version[1][-1] + break + else: + final_version = "" else: self.end_format = get_end_format() + if not final_version: + for step in format_relation: + if self.end_format in step[1]: + final_version = step[2][1] + self.final_version = final_version + self.warning("Final version: %s" % self.final_version, 10) + self.warning("Final format: %d" % self.end_format, 10) + self.backend = "latex" self.textclass = "article" # This is a hack: We use '' since we don't know the default @@ -177,8 +243,9 @@ class LyX_Base: self.language = language - def warning(self, message, debug_level= default_debug_level): - " Emits warning to self.error, if the debug_level is less than the self.debug." + def warning(self, message, debug_level= default_debug__): + """ 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") @@ -194,9 +261,10 @@ class LyX_Base: def read(self): - """Reads a file into the self.header and self.body parts, from self.input.""" + """Reads a file into the self.header and + self.body parts, from self.input.""" - while 1: + while True: line = self.input.readline() if not line: self.error("Invalid LyX file.") @@ -211,10 +279,14 @@ class LyX_Base: line = trim_eol(line) if check_token(line, '\\end_preamble'): break - - if line.split()[:0] in ("\\layout", "\\begin_layout", "\\begin_body"): - self.warning("Malformed LyX file: Missing '\\end_preamble'.") - self.warning("Adding it now and hoping for the best.") + + if line.split()[:0] in ("\\layout", + "\\begin_layout", "\\begin_body"): + + self.warning("Malformed LyX file:" + "Missing '\\end_preamble'." + "\nAdding it now and hoping" + "for the best.") self.preamble.append(line) @@ -225,18 +297,29 @@ class LyX_Base: if not line: continue - if line.split()[0] in ("\\layout", "\\begin_layout", "\\begin_body"): + if line.split()[0] in ("\\layout", "\\begin_layout", + "\\begin_body", "\\begin_deeper"): self.body.append(line) break self.header.append(line) + i = find_token(self.header, '\\textclass', 0) + if i == -1: + self.warning("Malformed LyX file: Missing '\\textclass'.") + i = find_token(self.header, '\\lyxformat', 0) + 1 + self.header[i:i] = ['\\textclass article'] + 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, default = "english") - self.inputencoding = get_value(self.header, "\\inputencoding", 0, default = "auto") - self.encoding = get_encoding(self.language, self.inputencoding, self.format, self.cjk_encoding) + self.language = get_value(self.header, "\\language", 0, + default = "english") + self.inputencoding = get_value(self.header, "\\inputencoding", + 0, default = "auto") + self.encoding = get_encoding(self.language, + self.inputencoding, self.format, + self.cjk_encoding) self.initial_version = self.read_version() # Second pass over header and preamble, now we know the file encoding @@ -257,16 +340,14 @@ class LyX_Base: " Writes the LyX file to self.output." self.set_version() self.set_format() + self.set_textclass() if self.encoding == "auto": - self.encoding = get_encoding(self.language, self.encoding, self.format, self.cjk_encoding) - + self.encoding = get_encoding(self.language, self.encoding, + self.format, self.cjk_encoding) if self.preamble: i = find_token(self.header, '\\textclass', 0) + 1 preamble = ['\\begin_preamble'] + self.preamble + ['\\end_preamble'] - if i == 0: - self.error("Malformed LyX file: Missing '\\textclass'.") - else: - header = self.header[:i] + preamble + self.header[i:] + header = self.header[:i] + preamble + self.header[i:] else: header = self.header @@ -288,7 +369,7 @@ class LyX_Base: try: gzip.open(input).readline() self.input = gzip.open(input) - self.output = gzip.GzipFile(mode="wb", fileobj=self.output) + self.output = gzip.GzipFile(mode="wb", fileobj=self.output) except: self.input = open(input) else: @@ -314,8 +395,9 @@ class LyX_Base: def read_version(self): - """ Searchs for clues of the LyX version used to write the file, returns the - most likely value, or None otherwise.""" + """ 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 @@ -338,7 +420,8 @@ class LyX_Base: 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 + self.header[0] = " ".join(["#LyX %s created this file." % version__, + "For more info see http://www.lyx.org/"]) if self.header[1][0] == '#': del self.header[1] @@ -364,6 +447,61 @@ class LyX_Base: self.header[i] = "\\lyxformat %s" % format + def set_textclass(self): + i = find_token(self.header, "\\textclass", 0) + self.header[i] = "\\textclass %s" % self.textclass + + + #Note that the module will be added at the END of the extant ones + def add_module(self, module): + i = find_token(self.header, "\\begin_modules", 0) + if i == -1: + #No modules yet included + i = find_token(self.header, "\\textclass", 0) + if i == -1: + self.warning("Malformed LyX document: No \\textclass!!") + return + modinfo = ["\\begin_modules", module, "\\end_modules"] + self.header[i + 1: i + 1] = modinfo + return + j = find_token(self.header, "\\end_modules", i) + if j == -1: + self.warning("(add_module)Malformed LyX document: No \\end_modules.") + return + k = find_token(self.header, module, i) + if k != -1 and k < j: + return + self.header.insert(j, module) + + + def get_module_list(self): + i = find_token(self.header, "\\begin_modules", 0) + if (i == -1): + return [] + j = find_token(self.header, "\\end_modules", i) + return self.header[i + 1 : j] + + + def set_module_list(self, mlist): + modbegin = find_token(self.header, "\\begin_modules", 0) + newmodlist = ['\\begin_modules'] + mlist + ['\\end_modules'] + if (modbegin == -1): + #No modules yet included + tclass = find_token(self.header, "\\textclass", 0) + if tclass == -1: + self.warning("Malformed LyX document: No \\textclass!!") + return + modbegin = tclass + 1 + self.header[modbegin:modbegin] = newmodlist + return + modend = find_token(self.header, "\\end_modules", modbegin) + if modend == -1: + self.warning("(set_module_list)Malformed LyX document: No \\end_modules.") + return + newmodlist = ['\\begin_modules'] + mlist + ['\\end_modules'] + self.header[modbegin:modend + 1] = newmodlist + + def set_parameter(self, param, value): " Set the value of the header parameter." i = find_token(self.header, '\\' + param, 0) @@ -389,9 +527,11 @@ class LyX_Base: for step in convertion_chain: steps = getattr(__import__("lyx_" + step), mode) - self.warning("Convertion step: %s - %s" % (step, mode), default_debug_level + 1) + self.warning("Convertion step: %s - %s" % (step, mode), + default_debug__ + 1) if not steps: - self.error("The convertion to an older format (%s) is not implemented." % self.format) + self.error("The convertion to an older " + "format (%s) is not implemented." % self.format) multi_conv = len(steps) != 1 for version, table in steps: @@ -405,24 +545,26 @@ class LyX_Base: try: conv(self) except: - self.warning("An error ocurred in %s, %s" % (version, str(conv)), - default_debug_level) + self.warning("An error ocurred in %s, %s" % + (version, str(conv)), + default_debug__) 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.warning("%lf: Elapsed time on %s" % + (time.time() - init_t, + str(conv)), default_debug__ + + 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""" + """ 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 @@ -437,7 +579,9 @@ class LyX_Base: if not correct_version: if format <= 215: - self.warning("Version does not match file format, discarding it. (Version %s, format %d)" %(self.initial_version, self.format)) + self.warning("Version does not match file format, " + "discarding it. (Version %s, format %d)" % + (self.initial_version, self.format)) for rel in format_relation: if format in rel[1]: initial_step = rel[0] @@ -458,13 +602,14 @@ class LyX_Base: steps = [] if (initial_step, self.start) < (final_step, self.end_format): mode = "convert" - first_step = 1 + full_steps = [] 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]) + if initial_step <= step[0] <= final_step and step[2][0] <= self.final_version: + full_steps.append(step) + if full_steps[0][1][-1] == self.format: + full_steps = full_steps[1:] + for step in full_steps: + steps.append(step[0]) else: mode = "revert" relation_format = format_relation[:] @@ -479,15 +624,18 @@ class LyX_Base: if last_step[1][-1] == self.end_format: steps.pop() + self.warning("Convertion mode: %s\tsteps%s" %(mode, steps), 10) 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} + 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", "\\leftindent" - + allowed_parameters = ('\\paragraph_spacing', '\\noindent', + '\\align', '\\labelwidthstring', + "\\start_of_appendix", "\\leftindent") sections = [] for section in paragraphs_filter.keys(): sections.append('\\begin_layout %s' % section) @@ -512,8 +660,9 @@ class LyX_Base: k = i + 1 # skip paragraph parameters - while not self.body[k].strip() or self.body[k].split()[0] in allowed_parameters: - k = k +1 + while not self.body[k].strip() or self.body[k].split()[0] \ + in allowed_parameters: + k += 1 while k < j: if check_token(self.body[k], '\\begin_inset'): @@ -527,10 +676,10 @@ class LyX_Base: k = end + 1 else: par.append(self.body[k]) - k = k + 1 + k += 1 # trim empty lines in the end. - while par[-1].strip() == '' and par: + while par and par[-1].strip() == '': par.pop() toc_par.append(Paragraph(section, par)) @@ -540,19 +689,24 @@ class LyX_Base: return toc_par -class File(LyX_Base): +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, cjk_encoding = ''): - LyX_Base.__init__(self, end_format, input, output, error, debug, try_hard, cjk_encoding) + + def __init__(self, end_format = 0, input = "", output = "", error = "", + debug = default_debug__, try_hard = 0, cjk_encoding = '', + final_version = ''): + LyX_base.__init__(self, end_format, input, output, error, + debug, try_hard, cjk_encoding, final_version) self.read() -class NewFile(LyX_Base): +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/", + "#LyX xxxx created this file." + "For more info see http://www.lyx.org/", "\\lyxformat xxx", "\\begin_document", "\\begin_header", @@ -601,7 +755,8 @@ class NewFile(LyX_Base): class Paragraph: - # unfinished implementation, it is missing the Text and Insets representation. + # unfinished implementation, it is missing the Text and Insets + # representation. " This class represents the LyX paragraphs." def __init__(self, name, body=[], settings = [], child = []): """ Parameters: @@ -615,7 +770,9 @@ class Paragraph: self.child = child def asLines(self): - " Converts the paragraph to a list of strings, representing it in the LyX file." + """ 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('') @@ -631,13 +788,3 @@ class Paragraph: 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