From b4db3ea13710afc38054005a4d88cb4e36659d88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Matos?= Date: Sat, 15 Jun 2024 10:06:06 +0100 Subject: [PATCH] Reformat lyx2lyx code using ruff --- lib/lyx2lyx/LyX.py | 492 +-- lib/lyx2lyx/generate_encoding_info.py | 26 +- lib/lyx2lyx/lyx2lyx_lang.py | 185 +- lib/lyx2lyx/lyx2lyx_tools.py | 691 ++-- lib/lyx2lyx/lyx_0_06.py | 5 +- lib/lyx2lyx/lyx_0_08.py | 10 +- lib/lyx2lyx/lyx_0_10.py | 83 +- lib/lyx2lyx/lyx_0_12.py | 228 +- lib/lyx2lyx/lyx_1_0.py | 28 +- lib/lyx2lyx/lyx_1_1.py | 5 +- lib/lyx2lyx/lyx_1_1_5.py | 124 +- lib/lyx2lyx/lyx_1_1_6_0.py | 199 +- lib/lyx2lyx/lyx_1_1_6_3.py | 88 +- lib/lyx2lyx/lyx_1_2.py | 449 +-- lib/lyx2lyx/lyx_1_3.py | 33 +- lib/lyx2lyx/lyx_1_4.py | 1908 ++++++----- lib/lyx2lyx/lyx_1_5.py | 1689 +++++----- lib/lyx2lyx/lyx_1_6.py | 2615 +++++++++------- lib/lyx2lyx/lyx_2_0.py | 3217 ++++++++++--------- lib/lyx2lyx/lyx_2_1.py | 4184 +++++++++++++++---------- lib/lyx2lyx/lyx_2_2.py | 1555 +++++---- lib/lyx2lyx/lyx_2_3.py | 1191 ++++--- lib/lyx2lyx/lyx_2_4.py | 4022 +++++++++++++++--------- lib/lyx2lyx/lyx_2_5.py | 68 +- lib/lyx2lyx/parser_tools.py | 315 +- lib/lyx2lyx/profiling.py | 6 +- lib/lyx2lyx/test_lyx2lyx_tools.py | 83 +- lib/lyx2lyx/test_parser_tools.py | 54 +- lib/lyx2lyx/unicode_symbols.py | 29 +- 29 files changed, 13802 insertions(+), 9780 deletions(-) diff --git a/lib/lyx2lyx/LyX.py b/lib/lyx2lyx/LyX.py index e0ca06da95..df7528e7d5 100644 --- a/lib/lyx2lyx/LyX.py +++ b/lib/lyx2lyx/LyX.py @@ -17,10 +17,16 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -" The LyX module has all the rules related with different lyx file formats." +"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, find_complete_lines) +from parser_tools import ( + get_value, + check_token, + find_token, + find_tokens, + find_end_of, + find_complete_lines, +) import os.path import gzip import locale @@ -32,10 +38,11 @@ import codecs try: import lyx2lyx_version + version__ = lyx2lyx_version.version stable_version = True -except: # we are running from build directory so assume the last version - version__ = '2.5' +except: # we are running from build directory so assume the last version + version__ = "2.5" stable_version = False default_debug__ = 2 @@ -44,12 +51,14 @@ default_debug__ = 2 #################################################################### # Private helper functions + def find_end_of_inset(lines, i): - " Find beginning of inset, where lines[i] is included." + "Find beginning of inset, where lines[i] is included." return find_end_of(lines, i, "\\begin_inset", "\\end_inset") + def minor_versions(major, last_minor_version): - """ Generate minor versions, using major as prefix and minor + """Generate minor versions, using major as prefix and minor versions from 0 until last_minor_version, plus the generic version. Example: @@ -73,28 +82,29 @@ original_tex2lyx_version = re.compile(b".*?tex2lyx ([\\d.]*)") ## # file format information: # file, supported formats, stable release versions -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)), - # Note that range(i,j) is up to j *excluded*. - ("1_4", list(range(222,246)), minor_versions("1.4" , 5)), - ("1_5", list(range(246,277)), minor_versions("1.5" , 7)), - ("1_6", list(range(277,346)), minor_versions("1.6" , 10)), - ("2_0", list(range(346,414)), minor_versions("2.0" , 8)), - ("2_1", list(range(414,475)), minor_versions("2.1" , 5)), - ("2_2", list(range(475,509)), minor_versions("2.2" , 4)), - ("2_3", list(range(509,545)), minor_versions("2.3" , 7)), - ("2_4", list(range(545,621)), minor_versions("2.4" , 0)), - ("2_5", (), minor_versions("2.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)), + # Note that range(i,j) is up to j *excluded*. + ("1_4", list(range(222, 246)), minor_versions("1.4", 5)), + ("1_5", list(range(246, 277)), minor_versions("1.5", 7)), + ("1_6", list(range(277, 346)), minor_versions("1.6", 10)), + ("2_0", list(range(346, 414)), minor_versions("2.0", 8)), + ("2_1", list(range(414, 475)), minor_versions("2.1", 5)), + ("2_2", list(range(475, 509)), minor_versions("2.2", 4)), + ("2_3", list(range(509, 545)), minor_versions("2.3", 7)), + ("2_4", list(range(545, 621)), minor_versions("2.4", 0)), + ("2_5", (), minor_versions("2.5", 0)), +] #################################################################### # This is useful just for development versions # @@ -102,14 +112,13 @@ format_relation = [("0_06", [200], minor_versions("0.6" , 4)), 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]) + format_relation[-1] = (step, [conv[0] for conv in convert], format_relation[-1][2]) # # #################################################################### + def formats_list(): - " Returns a list with supported file formats." + "Returns a list with supported file formats." formats = [] for version in format_relation: for format in version[1]: @@ -119,7 +128,7 @@ def formats_list(): def format_info(): - " Returns a list with the supported file formats." + "Returns a list with the supported file formats." template = """ %s\tstable format: %s \tstable versions: %s @@ -142,20 +151,20 @@ def format_info(): stable_format = str(version[1][-1]) out += template % (major, stable_format, versions, formats) - return out + '\n' + return out + "\n" def get_end_format(): - " Returns the more recent file format available." + "Returns the more recent file format available." # this check will fail only when we have a new version # and there is no format change yet. if format_relation[-1][1]: - return format_relation[-1][1][-1] + return format_relation[-1][1][-1] return format_relation[-2][1][-1] def get_backend(textclass): - " For _textclass_ returns its backend." + "For _textclass_ returns its backend." if textclass == "linuxdoc" or textclass == "manpage": return "linuxdoc" if textclass.startswith("docbook") or textclass.startswith("agu-"): @@ -164,18 +173,18 @@ def get_backend(textclass): def trim_eol(line): - " Remove end of line char(s)." - if line[-1] != '\n' and line[-1] != '\r': + "Remove end of line char(s)." + if line[-1] != "\n" and line[-1] != "\r": # May happen for the last line of a document return line - if line[-2:-1] == '\r': + if line[-2:-1] == "\r": return line[:-2] else: return line[:-1] def trim_eol_binary(line): - " Remove end of line char(s)." + "Remove end of line char(s)." if line[-1] != 10 and line[-1] != 13: # May happen for the last line of a document return line @@ -186,18 +195,19 @@ def trim_eol_binary(line): def get_encoding(language, inputencoding, format, cjk_encoding): - " Returns enconding of the lyx file" + "Returns enconding of the lyx file" if format > 248: return "utf8" # CJK-LyX encodes files using the current locale encoding. # This means that files created by CJK-LyX can only be converted using # the correct locale settings unless the encoding is given as commandline # argument. - if cjk_encoding == 'auto': + if cjk_encoding == "auto": return locale.getpreferredencoding() elif cjk_encoding: return cjk_encoding from lyx2lyx_lang import lang + if inputencoding == "auto" or inputencoding == "default": return lang[language][3] if inputencoding == "": @@ -209,17 +219,27 @@ def get_encoding(language, inputencoding, format, cjk_encoding): return "iso-8859-15" return inputencoding + ## # 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__, try_hard = 0, cjk_encoding = '', - final_version = '', systemlyxdir = '', language = 'english', - encoding = 'auto'): - + def __init__( + self, + end_format=0, + input="", + output="", + error="", + debug=default_debug__, + try_hard=0, + cjk_encoding="", + final_version="", + systemlyxdir="", + language="english", + encoding="auto", + ): """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. @@ -247,7 +267,9 @@ class LyX_base: # and ignore the version. if final_version: message = "Incompatible version %s for specified format %d" % ( - final_version, self.end_format) + final_version, + self.end_format, + ) for version in format_relation: if self.end_format in version[1]: if final_version not in version[2]: @@ -277,7 +299,7 @@ class LyX_base: # This is a hack: We use '' since we don't know the default # layout of the text class. LyX will parse it as default layout. # FIXME: Read the layout file and use the real default layout - self.default_layout = '' + self.default_layout = "" self.header = [] self.preamble = [] self.body = [] @@ -286,16 +308,14 @@ class LyX_base: self.language = language self.systemlyxdir = systemlyxdir - - def warning(self, message, debug_level= default_debug__): - """ Emits warning to self.error, if the debug_level is less + 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("lyx2lyx warning: " + message + "\n") - def error(self, message): - " Emits a warning and exits if not in try_hard mode." + "Emits a warning and exits if not in try_hard mode." self.warning(message) if not self.try_hard: self.warning("Quitting.") @@ -303,7 +323,6 @@ class LyX_base: self.status = 2 - def read(self): """Reads a file into the self.header and self.body parts, from self.input.""" @@ -325,13 +344,13 @@ class LyX_base: if first_line: # Remove UTF8 BOM marker if present if line.startswith(codecs.BOM_UTF8): - line = line[len(codecs.BOM_UTF8):] + line = line[len(codecs.BOM_UTF8) :] first_line = False line = trim_eol_binary(line) - decoded = line.decode('latin1') - if check_token(decoded, '\\begin_preamble'): + decoded = line.decode("latin1") + if check_token(decoded, "\\begin_preamble"): while True: line = self.input.readline() if not line: @@ -339,51 +358,60 @@ class LyX_base: self.error("Invalid LyX file: Missing body.") line = trim_eol_binary(line) - decoded = line.decode('latin1') - if check_token(decoded, '\\end_preamble'): + decoded = line.decode("latin1") + if check_token(decoded, "\\end_preamble"): break - if decoded.split()[:0] in ("\\layout", - "\\begin_layout", "\\begin_body"): - - self.warning("Malformed LyX file:" - "Missing '\\end_preamble'." - "\nAdding it now and hoping" - "for the best.") + if decoded.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) - if check_token(decoded, '\\end_preamble'): + if check_token(decoded, "\\end_preamble"): continue line = line.rstrip() if not line: continue - if decoded.split()[0] in ("\\layout", "\\begin_layout", - "\\begin_body", "\\begin_deeper"): + if decoded.split()[0] in ( + "\\layout", + "\\begin_layout", + "\\begin_body", + "\\begin_deeper", + ): self.body.append(line) break self.header.append(line) - i = find_token(self.header, b'\\textclass', 0) + i = find_token(self.header, b"\\textclass", 0) if i == -1: self.warning("Malformed LyX file: Missing '\\textclass'.") - i = find_token(self.header, b'\\lyxformat', 0) + 1 - self.header[i:i] = [b'\\textclass article'] + i = find_token(self.header, b"\\lyxformat", 0) + 1 + self.header[i:i] = [b"\\textclass article"] - self.textclass = get_value(self.header, b"\\textclass", 0, - default = b"") - self.language = get_value(self.header, b"\\language", 0, - default = b"english").decode('ascii') - self.inputencoding = get_value(self.header, b"\\inputencoding", 0, - default = b"auto").decode('ascii') + self.textclass = get_value(self.header, b"\\textclass", 0, default=b"") + self.language = get_value(self.header, b"\\language", 0, default=b"english").decode( + "ascii" + ) + self.inputencoding = get_value( + self.header, b"\\inputencoding", 0, default=b"auto" + ).decode("ascii") self.format = self.read_format() self.initial_format = self.format - self.encoding = get_encoding(self.language, - self.inputencoding, self.format, - self.cjk_encoding) + 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 @@ -404,26 +432,25 @@ class LyX_base: break self.body.append(trim_eol(line)) - def write(self): - " Writes the LyX file to self.output." + "Writes the LyX file to self.output." self.choose_output(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'] + i = find_token(self.header, "\\textclass", 0) + 1 + preamble = ["\\begin_preamble"] + self.preamble + ["\\end_preamble"] header = self.header[:i] + preamble + self.header[i:] else: header = self.header - for line in header + [''] + self.body: - self.output.write(line+'\n') - + for line in header + [""] + self.body: + self.output.write(line + "\n") def choose_output(self, output): """Choose output streams dealing transparently with @@ -435,20 +462,19 @@ class LyX_base: # interfaces. if self.compressed: if output: - outputfileobj = open(output, 'wb') + outputfileobj = open(output, "wb") else: # We cannot not use stdout directly since it needs text, not bytes in python 3 - outputfileobj = os.fdopen(sys.stdout.fileno(), 'wb') + outputfileobj = os.fdopen(sys.stdout.fileno(), "wb") # We cannot not use gzip.open() since it is not supported by python 2 - zipbuffer = gzip.GzipFile(mode='wb', fileobj=outputfileobj) + zipbuffer = gzip.GzipFile(mode="wb", fileobj=outputfileobj) # We do not want to use different newlines on different OSes inside zipped files - self.output = io.TextIOWrapper(zipbuffer, encoding=self.encoding, newline='\n') + self.output = io.TextIOWrapper(zipbuffer, encoding=self.encoding, newline="\n") else: if output: - self.output = open(output, 'w', encoding=self.encoding) + self.output = open(output, "w", encoding=self.encoding) else: - self.output = open(sys.stdout.fileno(), 'w', encoding=self.encoding) - + self.output = open(sys.stdout.fileno(), "w", encoding=self.encoding) def choose_input(self, input): """Choose input stream, dealing transparently with @@ -456,27 +482,26 @@ class LyX_base: # Since we do not know the encoding yet we need to read the input as # bytes in binary mode, and convert later to unicode. - if input and input != '-': + if input and input != "-": self.dir = os.path.dirname(os.path.abspath(input)) try: gzip.open(input).readline() self.input = gzip.open(input) self.compressed = True except: - self.input = open(input, 'rb') + self.input = open(input, "rb") self.compressed = False else: - self.dir = '' - self.input = os.fdopen(sys.stdin.fileno(), 'rb') + self.dir = "" + self.input = os.fdopen(sys.stdin.fileno(), "rb") self.compressed = False - def lyxformat(self, format): - " Returns the file format representation, an integer." + "Returns the file format representation, an integer." result = format_re.match(format) if result: format = int(result.group(1) + result.group(2)) - elif format == '2': + elif format == "2": format = 200 else: self.error(str(format) + ": " + "Invalid LyX file.") @@ -487,16 +512,15 @@ class LyX_base: self.error(str(format) + ": " + "Format not supported.") return None - def read_version(self): - """ Searchs for clues of the LyX version used to write the + """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:1] != b"#": return None - line = line.replace(b"fix",b".") + line = line.replace(b"fix", b".") # need to test original_tex2lyx_version first because tex2lyx # writes "#LyX file created by tex2lyx 2.2" result = original_tex2lyx_version.match(line) @@ -510,17 +534,20 @@ class LyX_base: res = result.group(1) if not res: self.warning(line) - #self.warning("Version %s" % result.group(1)) - return res.decode('ascii') + # self.warning("Version %s" % result.group(1)) + return res.decode("ascii") self.warning(str(self.header[:2])) return None - def set_version(self): - " Set the header with the version used." + "Set the header with the version used." - initial_comment = " ".join(["#LyX %s created this file." % version__, - "For more info see https://www.lyx.org/"]) + initial_comment = " ".join( + [ + "#LyX %s created this file." % version__, + "For more info see https://www.lyx.org/", + ] + ) # Simple heuristic to determine the comment that always starts # a lyx file @@ -534,61 +561,56 @@ class LyX_base: # 2) the second line had the lyx version used # later we decided that 1) was a privacy risk for no gain # here we remove the second line effectively erasing 1) - if self.header[1][0] == '#': + if self.header[1][0] == "#": del self.header[1] - def read_format(self): - " Read from the header the fileformat of the present LyX file." + "Read from the header the fileformat of the present LyX file." for line in self.header: - result = fileformat.match(line.decode('ascii')) + result = fileformat.match(line.decode("ascii")) if result: return self.lyxformat(result.group(1)) else: self.error("Invalid LyX File: Missing format.") return None - def set_format(self): - " Set the file format of the file, in the header." + "Set the file format of the file, in the header." if self.format <= 217: - format = str(float(self.format)/100) + 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_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 + # Note that the module will be added at the END of the extant ones def add_module(self, module): - " Append module to the modules list." - i = find_token(self.header, "\\begin_modules", 0) - if i == -1: - #No modules yet included - i = find_token(self.header, "\\textclass", 0) + "Append module to the modules list." + i = find_token(self.header, "\\begin_modules", 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) - + # 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 del_module(self, module): - " Delete `module` from module list, return success." + "Delete `module` from module list, return success." modlist = self.get_module_list() if module not in modlist: return False @@ -596,56 +618,55 @@ class LyX_base: return True def get_module_list(self): - " Return list of modules." - 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] - + "Return list of modules." + 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): - i = find_token(self.header, "\\begin_modules", 0) - if (i == -1): - #No modules yet included - tclass = find_token(self.header, "\\textclass", 0) - if tclass == -1: - self.warning("Malformed LyX document: No \\textclass!!") - return - i = j = tclass + 1 - else: - j = find_token(self.header, "\\end_modules", i) - if j == -1: - self.warning("(set_module_list) Malformed LyX document: No \\end_modules.") - return - j += 1 - if mlist: - mlist = ['\\begin_modules'] + mlist + ['\\end_modules'] - self.header[i:j] = mlist - + i = find_token(self.header, "\\begin_modules", 0) + if i == -1: + # No modules yet included + tclass = find_token(self.header, "\\textclass", 0) + if tclass == -1: + self.warning("Malformed LyX document: No \\textclass!!") + return + i = j = tclass + 1 + else: + j = find_token(self.header, "\\end_modules", i) + if j == -1: + self.warning("(set_module_list) Malformed LyX document: No \\end_modules.") + return + j += 1 + if mlist: + mlist = ["\\begin_modules"] + mlist + ["\\end_modules"] + self.header[i:j] = mlist def set_parameter(self, param, value): - " Set the value of the header parameter." - i = find_token(self.header, '\\' + param, 0) + "Set the value of the header parameter." + i = find_token(self.header, "\\" + param, 0) if i == -1: - self.warning('Parameter not found in the header: %s' % param, 3) + self.warning("Parameter not found in the header: %s" % param, 3) return - self.header[i] = f'\\{param} {str(value)}' - + self.header[i] = f"\\{param} {str(value)}" def is_default_layout(self, layout): - " Check whether a layout is the default layout of this class." + "Check whether a layout is the default layout of this class." # FIXME: Check against the real text class default layout - if layout == 'Standard' or layout == self.default_layout: + if layout == "Standard" or layout == self.default_layout: return 1 return 0 - def convert(self): "Convert from current (self.format) to self.end_format." if self.format == self.end_format: - self.warning("No conversion needed: Target format %s " - "same as current format!" % self.format, default_debug__) + self.warning( + "No conversion needed: Target format %s " + "same as current format!" % self.format, + default_debug__, + ) return mode, conversion_chain = self.chain() @@ -654,17 +675,20 @@ class LyX_base: for step in conversion_chain: steps = getattr(__import__("lyx_" + step), mode) - self.warning(f"Convertion step: {step} - {mode}", - default_debug__ + 1) + self.warning(f"Convertion step: {step} - {mode}", default_debug__ + 1) if not steps: - self.error("The conversion to an older " - "format (%s) is not implemented." % self.format) + self.error( + "The conversion 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"): + if ( + multi_conv + and (self.format >= version and mode == "convert") + or (self.format <= version and mode == "revert") + ): continue for conv in table: @@ -672,24 +696,24 @@ class LyX_base: try: conv(self) except: - self.warning("An error occurred in %s, %s" % - (version, str(conv)), - default_debug__) + self.warning( + "An error occurred 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__ + - 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 + """This is where all the decisions related with the conversion are taken. It returns a list of modules needed to convert the LyX file from self.format to self.end_format""" @@ -705,9 +729,11 @@ 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] @@ -730,7 +756,7 @@ class LyX_base: mode = "convert" full_steps = [] for step in format_relation: - if initial_step <= step[0] <= final_step and step[2][0] <= self.final_version: + 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:] @@ -743,19 +769,18 @@ class LyX_base: last_step = None for step in relation_format: - if final_step <= step[0] <= initial_step: + if final_step <= step[0] <= initial_step: steps.append(step[0]) last_step = step if last_step[1][-1] == self.end_format: steps.pop() - self.warning("Convertion mode: %s\tsteps%s" %(mode, steps), 10) + self.warning("Convertion mode: %s\tsteps%s" % (mode, steps), 10) return mode, steps - def append_local_layout(self, new_layout): - " Append `new_layout` to the local layouts." + "Append `new_layout` to the local layouts." # new_layout may be a string or a list of strings (lines) try: new_layout = new_layout.splitlines() @@ -768,7 +793,7 @@ class LyX_base: # this should not happen self.warning("Malformed LyX document! No \\language header found!") return - self.header[k : k] = ["\\begin_local_layout", "\\end_local_layout"] + self.header[k:k] = ["\\begin_local_layout", "\\end_local_layout"] i = k j = find_end_of(self.header, i, "\\begin_local_layout", "\\end_local_layout") @@ -777,23 +802,25 @@ class LyX_base: self.warning("Malformed LyX document: Can't find end of local layout!") return - self.header[i+1 : i+1] = new_layout + self.header[i + 1 : i + 1] = new_layout def del_local_layout(self, layout_def): - " Delete `layout_def` from local layouts, return success." + "Delete `layout_def` from local layouts, return success." i = find_complete_lines(self.header, layout_def) if i == -1: return False - j = i+len(layout_def) - if (self.header[i-1] == "\\begin_local_layout" and - self.header[j] == "\\end_local_layout"): - i -=1 - j +=1 + j = i + len(layout_def) + if ( + self.header[i - 1] == "\\begin_local_layout" + and self.header[j] == "\\end_local_layout" + ): + i -= 1 + j += 1 self.header[i:j] = [] return True def del_from_header(self, lines): - " Delete `lines` from the document header, return success." + "Delete `lines` from the document header, return success." i = find_complete_lines(self.header, lines) if i == -1: return False @@ -801,6 +828,7 @@ class LyX_base: self.header[i:j] = [] return True + # Part of an unfinished attempt to make lyx2lyx gave a more # structured view of the document. # def get_toc(self, depth = 4): @@ -865,19 +893,37 @@ class LyX_base: class File(LyX_base): - " This class reads existing LyX files." + "This class reads existing LyX files." - def __init__(self, end_format = 0, input = '', output = '', error = '', - debug = default_debug__, try_hard = 0, cjk_encoding = '', - final_version = '', systemlyxdir = ''): - LyX_base.__init__(self, end_format, input, output, error, - debug, try_hard, cjk_encoding, final_version, - systemlyxdir) + def __init__( + self, + end_format=0, + input="", + output="", + error="", + debug=default_debug__, + try_hard=0, + cjk_encoding="", + final_version="", + systemlyxdir="", + ): + LyX_base.__init__( + self, + end_format, + input, + output, + error, + debug, + try_hard, + cjk_encoding, + final_version, + systemlyxdir, + ) self.read() # FIXME: header settings are completely outdated, don't use like this -#class NewFile(LyX_base): +# class NewFile(LyX_base): # " This class is to create new LyX files." # def set_header(self, **params): # # set default values @@ -934,7 +980,7 @@ class File(LyX_base): # Part of an unfinished attempt to make lyx2lyx gave a more # structured view of the document. -#class Paragraph: +# class Paragraph: # # unfinished implementation, it is missing the Text and Insets # # representation. # " This class represents the LyX paragraphs." diff --git a/lib/lyx2lyx/generate_encoding_info.py b/lib/lyx2lyx/generate_encoding_info.py index 85904d216b..5364023c52 100644 --- a/lib/lyx2lyx/generate_encoding_info.py +++ b/lib/lyx2lyx/generate_encoding_info.py @@ -15,28 +15,29 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -""" This module parses lib/languages and prints it as a python +"""This module parses lib/languages and prints it as a python dictionary, ready to use by other python modules""" import pprint + def parse_line(line): - " Parse line from languages and return it as a list. " + "Parse line from languages and return it as a list." j = 0 tmp = [] - while j< len(line): + while j < len(line): token = line[j:].split()[0] if not token: break if token[0] != '"': tmp.append(token) j += len(token) + 1 - elif line[j+1:].find('"') != -1: + elif line[j + 1 :].find('"') != -1: k = line.find('"', j + 1) - tmp.append(line[j+1:k]) + tmp.append(line[j + 1 : k]) j = k + 1 else: - tmp.append(line[j+1:]) + tmp.append(line[j + 1 :]) break while j < len(line) and line[j].isspace(): @@ -45,17 +46,16 @@ def parse_line(line): return tmp -if __name__ == '__main__': +if __name__ == "__main__": lines = open("../languages", "rb") lang = {} for line in lines: - if line[:1] != '#': + if line[:1] != "#": tmp = parse_line(line[:-1]) lang[tmp[0]] = tmp[1:] - - print ("# This file is generated by generate_incoding_info.py from lib/languages file.") - print ("# Do not change this file directly.") - print () - print ("lang = ", end = " ") + print("# This file is generated by generate_incoding_info.py from lib/languages file.") + print("# Do not change this file directly.") + print() + print("lang = ", end=" ") pprint.pprint(lang) diff --git a/lib/lyx2lyx/lyx2lyx_lang.py b/lib/lyx2lyx/lyx2lyx_lang.py index 870ebb2fab..06a7b170f1 100644 --- a/lib/lyx2lyx/lyx2lyx_lang.py +++ b/lib/lyx2lyx/lyx2lyx_lang.py @@ -1,103 +1,88 @@ # This file is generated by generate_incoding_info.py from lib/languages file. # Do not change this file directly. -lang = {'afrikaans': ['afrikaans', 'Afrikaans', 'false', 'iso8859-1', 'af_ZA', ''], - 'american': ['american', 'American', 'false', 'iso8859-1', 'en_US', ''], - 'arabic': ['arabic', 'Arabic', 'true', 'iso8859-6', 'ar_SA', ''], - 'austrian': ['austrian', 'Austrian', 'false', 'iso8859-1', 'de_AT', ''], - 'bahasa': ['bahasa', 'Bahasa', 'false', 'iso8859-1', 'in_ID', ''], - 'basque': ['basque', 'Basque', 'false', 'iso8859-1', 'eu_ES', ''], - 'belarusian': ['belarusian', 'Belarusian', 'false', 'cp1251', 'be_BY', ''], - 'brazil': ['brazil', - 'Portuguese (Brazil)', - 'false', - 'iso8859-1', - 'pt_BR', - ''], - 'breton': ['breton', 'Breton', 'false', 'iso8859-1', 'br_FR', ''], - 'british': ['british', 'British', 'false', 'iso8859-1', 'en_GB', ''], - 'bulgarian': ['bulgarian', 'Bulgarian', 'false', 'cp1251', 'bg_BG', ''], - 'canadian': ['canadian', 'Canadian', 'false', 'iso8859-1', 'en_CA', ''], - 'canadien': ['canadien', - 'French Canadian', - 'false', - 'iso8859-1', - 'fr_CA', - ''], - 'catalan': ['catalan', 'Catalan', 'false', 'iso8859-1', 'ca_ES', ''], - 'croatian': ['croatian', 'Croatian', 'false', 'iso8859-2', 'hr_HR', ''], - 'czech': ['czech', 'Czech', 'false', 'iso8859-2', 'cs_CZ', ''], - 'danish': ['danish', 'Danish', 'false', 'iso8859-1', 'da_DK', ''], - 'default': ['default', 'default', 'false', 'iso8859-1', 'C', ''], - 'dutch': ['dutch', 'Dutch', 'false', 'iso8859-1', 'nl_NL', ''], - 'english': ['english', 'English', 'false', 'iso8859-1', 'en_US', ''], - 'esperanto': ['esperanto', 'Esperanto', 'false', 'iso8859-3', 'eo', ''], - 'estonian': ['estonian', 'Estonian', 'false', 'iso8859-1', 'et_EE', ''], - 'finnish': ['finnish', 'Finnish', 'false', 'iso8859-1', 'fi_FI', ''], - 'french': ['french', - 'French', - 'false', - 'iso8859-1', - 'fr_FR', - '\\addto\\extrasfrench{\\providecommand{\\og}{\\leavevmode\\flqq~}\\providecommand{\\fg}{\\ifdim\\lastskip>\\z@\\unskip\\fi~\\frqq}}'], - 'frenchb': ['french', 'French', 'false', 'iso8859-1', 'fr_FR', ''], # for compatibility reasons - 'galician': ['galician', 'Galician', 'false', 'iso8859-1', 'gl_ES', ''], - 'german': ['german', 'German', 'false', 'iso8859-1', 'de_DE', ''], - 'greek': ['greek', 'Greek', 'false', 'iso8859-7', 'el_GR', ''], - 'hebrew': ['hebrew', 'Hebrew', 'true', 'cp1255', 'he_IL', ''], - 'icelandic': ['icelandic', 'Icelandic', 'false', 'iso8859-1', 'is_IS', ''], - 'irish': ['irish', 'Irish', 'false', 'iso8859-1', 'ga_IE', ''], - 'italian': ['italian', 'Italian', 'false', 'iso8859-1', 'it_IT', ''], - 'kazakh': ['kazakh', 'Kazakh', 'false', 'pt154', 'kk_KZ', ''], - 'latvian': ['latvian', 'Latvian', 'false', 'iso8859-13', 'lv_LV', ''], - 'lithuanian': ['lithuanian', - 'Lithuanian', - 'false', - 'iso8859-13', - 'lt_LT', - ''], - 'magyar': ['magyar', 'Magyar', 'false', 'iso8859-2', 'hu_HU', ''], - 'naustrian': ['naustrian', - 'Austrian (new spelling)', - 'false', - 'iso8859-1', - 'de_AT', - ''], - 'ngerman': ['ngerman', - 'German (new spelling)', - 'false', - 'iso8859-1', - 'de_DE', - ''], - 'norsk': ['norsk', 'Norsk', 'false', 'iso8859-1', 'no_NO', ''], - 'nynorsk': ['nynorsk', 'Nynorsk', 'false', 'iso8859-1', 'nn_NO', ''], - 'polish': ['polish', 'Polish', 'false', 'iso8859-2', 'pl_PL', ''], - 'portuges': ['portuges', 'Portugese', 'false', 'iso8859-1', 'pt_PT', ''], - 'romanian': ['romanian', 'Romanian', 'false', 'iso8859-2', 'ro_RO', ''], - 'russian': ['russian', 'Russian', 'false', 'koi8-r', 'ru_RU', ''], - 'scottish': ['scottish', 'Scottish', 'false', 'iso8859-1', 'gd_GB', ''], - 'serbian': ['croatian', 'Serbian', 'false', 'iso8859-5', 'sr_HR', ''], - 'serbocroatian': ['croatian', - 'Serbo-Croatian', - 'false', - 'iso8859-2', - 'sh_HR', - ''], - 'slovak': ['slovak', 'Slovak', 'false', 'iso8859-2', 'sk_SK', ''], - 'slovene': ['slovene', 'Slovene', 'false', 'iso8859-2', 'sl_SI', ''], - 'spanish': ['spanish', - 'Spanish', - 'false', - 'iso8859-1', - 'es_ES', - '\\deactivatetilden'], - 'swedish': ['swedish', 'Swedish', 'false', 'iso8859-1', 'sv_SE', ''], - 'thai': ['thai', - 'Thai', - 'false', - 'tis620-0', - 'th_TH', - '\\usepackage{thswitch}'], - 'turkish': ['turkish', 'Turkish', 'false', 'iso8859-9', 'tr_TR', ''], - 'ukrainian': ['ukrainian', 'Ukrainian', 'false', 'koi8-u', 'uk_UA', ''], - 'welsh': ['welsh', 'Welsh', 'false', 'iso8859-1', 'cy_GB', '']} +lang = { + "afrikaans": ["afrikaans", "Afrikaans", "false", "iso8859-1", "af_ZA", ""], + "american": ["american", "American", "false", "iso8859-1", "en_US", ""], + "arabic": ["arabic", "Arabic", "true", "iso8859-6", "ar_SA", ""], + "austrian": ["austrian", "Austrian", "false", "iso8859-1", "de_AT", ""], + "bahasa": ["bahasa", "Bahasa", "false", "iso8859-1", "in_ID", ""], + "basque": ["basque", "Basque", "false", "iso8859-1", "eu_ES", ""], + "belarusian": ["belarusian", "Belarusian", "false", "cp1251", "be_BY", ""], + "brazil": ["brazil", "Portuguese (Brazil)", "false", "iso8859-1", "pt_BR", ""], + "breton": ["breton", "Breton", "false", "iso8859-1", "br_FR", ""], + "british": ["british", "British", "false", "iso8859-1", "en_GB", ""], + "bulgarian": ["bulgarian", "Bulgarian", "false", "cp1251", "bg_BG", ""], + "canadian": ["canadian", "Canadian", "false", "iso8859-1", "en_CA", ""], + "canadien": ["canadien", "French Canadian", "false", "iso8859-1", "fr_CA", ""], + "catalan": ["catalan", "Catalan", "false", "iso8859-1", "ca_ES", ""], + "croatian": ["croatian", "Croatian", "false", "iso8859-2", "hr_HR", ""], + "czech": ["czech", "Czech", "false", "iso8859-2", "cs_CZ", ""], + "danish": ["danish", "Danish", "false", "iso8859-1", "da_DK", ""], + "default": ["default", "default", "false", "iso8859-1", "C", ""], + "dutch": ["dutch", "Dutch", "false", "iso8859-1", "nl_NL", ""], + "english": ["english", "English", "false", "iso8859-1", "en_US", ""], + "esperanto": ["esperanto", "Esperanto", "false", "iso8859-3", "eo", ""], + "estonian": ["estonian", "Estonian", "false", "iso8859-1", "et_EE", ""], + "finnish": ["finnish", "Finnish", "false", "iso8859-1", "fi_FI", ""], + "french": [ + "french", + "French", + "false", + "iso8859-1", + "fr_FR", + "\\addto\\extrasfrench{\\providecommand{\\og}{\\leavevmode\\flqq~}\\providecommand{\\fg}{\\ifdim\\lastskip>\\z@\\unskip\\fi~\\frqq}}", + ], + "frenchb": [ + "french", + "French", + "false", + "iso8859-1", + "fr_FR", + "", + ], # for compatibility reasons + "galician": ["galician", "Galician", "false", "iso8859-1", "gl_ES", ""], + "german": ["german", "German", "false", "iso8859-1", "de_DE", ""], + "greek": ["greek", "Greek", "false", "iso8859-7", "el_GR", ""], + "hebrew": ["hebrew", "Hebrew", "true", "cp1255", "he_IL", ""], + "icelandic": ["icelandic", "Icelandic", "false", "iso8859-1", "is_IS", ""], + "irish": ["irish", "Irish", "false", "iso8859-1", "ga_IE", ""], + "italian": ["italian", "Italian", "false", "iso8859-1", "it_IT", ""], + "kazakh": ["kazakh", "Kazakh", "false", "pt154", "kk_KZ", ""], + "latvian": ["latvian", "Latvian", "false", "iso8859-13", "lv_LV", ""], + "lithuanian": ["lithuanian", "Lithuanian", "false", "iso8859-13", "lt_LT", ""], + "magyar": ["magyar", "Magyar", "false", "iso8859-2", "hu_HU", ""], + "naustrian": [ + "naustrian", + "Austrian (new spelling)", + "false", + "iso8859-1", + "de_AT", + "", + ], + "ngerman": ["ngerman", "German (new spelling)", "false", "iso8859-1", "de_DE", ""], + "norsk": ["norsk", "Norsk", "false", "iso8859-1", "no_NO", ""], + "nynorsk": ["nynorsk", "Nynorsk", "false", "iso8859-1", "nn_NO", ""], + "polish": ["polish", "Polish", "false", "iso8859-2", "pl_PL", ""], + "portuges": ["portuges", "Portugese", "false", "iso8859-1", "pt_PT", ""], + "romanian": ["romanian", "Romanian", "false", "iso8859-2", "ro_RO", ""], + "russian": ["russian", "Russian", "false", "koi8-r", "ru_RU", ""], + "scottish": ["scottish", "Scottish", "false", "iso8859-1", "gd_GB", ""], + "serbian": ["croatian", "Serbian", "false", "iso8859-5", "sr_HR", ""], + "serbocroatian": ["croatian", "Serbo-Croatian", "false", "iso8859-2", "sh_HR", ""], + "slovak": ["slovak", "Slovak", "false", "iso8859-2", "sk_SK", ""], + "slovene": ["slovene", "Slovene", "false", "iso8859-2", "sl_SI", ""], + "spanish": [ + "spanish", + "Spanish", + "false", + "iso8859-1", + "es_ES", + "\\deactivatetilden", + ], + "swedish": ["swedish", "Swedish", "false", "iso8859-1", "sv_SE", ""], + "thai": ["thai", "Thai", "false", "tis620-0", "th_TH", "\\usepackage{thswitch}"], + "turkish": ["turkish", "Turkish", "false", "iso8859-9", "tr_TR", ""], + "ukrainian": ["ukrainian", "Ukrainian", "false", "koi8-u", "uk_UA", ""], + "welsh": ["welsh", "Welsh", "false", "iso8859-1", "cy_GB", ""], +} diff --git a/lib/lyx2lyx/lyx2lyx_tools.py b/lib/lyx2lyx/lyx2lyx_tools.py index d9a74d516a..8d9bf35940 100644 --- a/lib/lyx2lyx/lyx2lyx_tools.py +++ b/lib/lyx2lyx/lyx2lyx_tools.py @@ -15,7 +15,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -''' +""" This module offers several free functions to help with lyx2lyx'ing. More documentaton is below, but here is a quick guide to what they do. Optional arguments are marked by brackets. @@ -87,41 +87,48 @@ revert_language(document, lyxname, babelname="", polyglossianame=""): Reverts native language support to ERT If babelname or polyglossianame is empty, it is assumed this language package is not supported for the given language. -''' +""" import re import sys -from parser_tools import (find_token, find_end_of_inset, get_containing_layout, - get_containing_inset, get_value, get_bool_value) +from parser_tools import ( + find_token, + find_end_of_inset, + get_containing_layout, + get_containing_inset, + get_value, + get_bool_value, +) from unicode_symbols import unicode_reps + # This will accept either a list of lines or a single line. # It is bad practice to pass something with embedded newlines, # though we will handle that. def add_to_preamble(document, text): - " Add text to the preamble if it is not already there. " + "Add text to the preamble if it is not already there." if not type(text) is list: - # split on \n just in case - # it'll give us the one element list we want - # if there's no \n, too - text = text.split('\n') + # split on \n just in case + # it'll give us the one element list we want + # if there's no \n, too + text = text.split("\n") i = 0 prelen = len(document.preamble) while True: - i = find_token(document.preamble, text[0], i) - if i == -1: - break - # we need a perfect match - matched = True - for line in text: - if i >= prelen or line != document.preamble[i]: - matched = False - break - i += 1 - if matched: - return + i = find_token(document.preamble, text[0], i) + if i == -1: + break + # we need a perfect match + matched = True + for line in text: + if i >= prelen or line != document.preamble[i]: + matched = False + break + i += 1 + if matched: + return document.preamble.extend(["% Added by lyx2lyx"]) document.preamble.extend(text) @@ -129,14 +136,14 @@ def add_to_preamble(document, text): # Note that text can be either a list of lines or a single line. # It should really be a list. -def insert_to_preamble(document, text, index = 0): - """ Insert text to the preamble at a given line""" +def insert_to_preamble(document, text, index=0): + """Insert text to the preamble at a given line""" if not type(text) is list: - # split on \n just in case - # it'll give us the one element list we want - # if there's no \n, too - text = text.split('\n') + # split on \n just in case + # it'll give us the one element list we want + # if there's no \n, too + text = text.split("\n") text.insert(0, "% Added by lyx2lyx") document.preamble[index:index] = text @@ -146,6 +153,7 @@ def insert_to_preamble(document, text, index = 0): # Created from the reversed list to keep the first of alternative definitions. licr_table = {ord(ch): cmd for cmd, ch in unicode_reps[::-1]} + def put_cmd_in_ert(cmd, is_open=False, as_paragraph=False): """ Return ERT inset wrapping `cmd` as a list of strings. @@ -156,15 +164,27 @@ def put_cmd_in_ert(cmd, is_open=False, as_paragraph=False): `as_paragraph` wraps the ERT inset in a Standard paragraph. """ - status = {False:"collapsed", True:"open"} - ert_inset = ["\\begin_inset ERT", "status %s"%status[is_open], "", - "\\begin_layout Plain Layout", "", - # content here ([5:5]) - "\\end_layout", "", "\\end_inset"] + status = {False: "collapsed", True: "open"} + ert_inset = [ + "\\begin_inset ERT", + "status %s" % status[is_open], + "", + "\\begin_layout Plain Layout", + "", + # content here ([5:5]) + "\\end_layout", + "", + "\\end_inset", + ] - paragraph = ["\\begin_layout Standard", - # content here ([1:1]) - "", "", "\\end_layout", ""] + paragraph = [ + "\\begin_layout Standard", + # content here ([1:1]) + "", + "", + "\\end_layout", + "", + ] # ensure cmd is an unicode instance and make it "LyX safe". if isinstance(cmd, list): cmd = "\n".join(cmd) @@ -178,8 +198,8 @@ def put_cmd_in_ert(cmd, is_open=False, as_paragraph=False): return paragraph -def get_ert(lines, i, verbatim = False): - 'Convert an ERT inset into LaTeX.' +def get_ert(lines, i, verbatim=False): + "Convert an ERT inset into LaTeX." if not lines[i].startswith("\\begin_inset ERT"): return "" j = find_end_of_inset(lines, i) @@ -196,10 +216,10 @@ def get_ert(lines, i, verbatim = False): first = False else: ret = ret + "\n" - while i + 1 < j and lines[i+1] == "": + while i + 1 < j and lines[i + 1] == "": i = i + 1 elif lines[i] == "\\end_layout": - while i + 1 < j and lines[i+1] == "": + while i + 1 < j and lines[i + 1] == "": i = i + 1 elif lines[i] == "\\backslash": if verbatim: @@ -213,7 +233,7 @@ def get_ert(lines, i, verbatim = False): def lyx2latex(document, lines): - 'Convert some LyX stuff into corresponding LaTeX stuff, as best we can.' + "Convert some LyX stuff into corresponding LaTeX stuff, as best we can." content = "" ert_end = 0 @@ -221,118 +241,137 @@ def lyx2latex(document, lines): hspace = "" for curline in range(len(lines)): - line = lines[curline] - if line.startswith("\\begin_inset Note Note"): - # We want to skip LyX notes, so remember where the inset ends - note_end = find_end_of_inset(lines, curline + 1) - continue - elif note_end >= curline: - # Skip LyX notes - continue - elif line.startswith("\\begin_inset ERT"): - # We don't want to replace things inside ERT, so figure out - # where the end of the inset is. - ert_end = find_end_of_inset(lines, curline + 1) - continue - elif line.startswith("\\begin_inset Formula"): - line = line[20:] - elif line.startswith("\\begin_inset Quotes"): - # For now, we do a very basic reversion. Someone who understands - # quotes is welcome to fix it up. - qtype = line[20:].strip() - # lang = qtype[0] - side = qtype[1] - dbls = qtype[2] - if side == "l": - if dbls == "d": - line = "``" - else: - line = "`" - else: - if dbls == "d": - line = "''" - else: - line = "'" - elif line.startswith("\\begin_inset Newline newline"): - line = "\\\\ " - elif line.startswith("\\noindent"): - line = "\\noindent " # we need the space behind the command - elif line.startswith("\\begin_inset space"): - line = line[18:].strip() - if line.startswith("\\hspace"): - # Account for both \hspace and \hspace* - hspace = line[:-2] - continue - elif line == "\\space{}": - line = "\\ " - elif line == "\\thinspace{}": - line = "\\," - elif hspace != "": - # The LyX length is in line[8:], after the \length keyword - length = latex_length(line[8:])[1] - line = hspace + "{" + length + "}" - hspace = "" - elif line.isspace() or \ - line.startswith("\\begin_layout") or \ - line.startswith("\\end_layout") or \ - line.startswith("\\begin_inset") or \ - line.startswith("\\end_inset") or \ - line.startswith("\\lang") or \ - line.strip() == "status collapsed" or \ - line.strip() == "status open": - #skip all that stuff - continue + line = lines[curline] + if line.startswith("\\begin_inset Note Note"): + # We want to skip LyX notes, so remember where the inset ends + note_end = find_end_of_inset(lines, curline + 1) + continue + elif note_end >= curline: + # Skip LyX notes + continue + elif line.startswith("\\begin_inset ERT"): + # We don't want to replace things inside ERT, so figure out + # where the end of the inset is. + ert_end = find_end_of_inset(lines, curline + 1) + continue + elif line.startswith("\\begin_inset Formula"): + line = line[20:] + elif line.startswith("\\begin_inset Quotes"): + # For now, we do a very basic reversion. Someone who understands + # quotes is welcome to fix it up. + qtype = line[20:].strip() + # lang = qtype[0] + side = qtype[1] + dbls = qtype[2] + if side == "l": + if dbls == "d": + line = "``" + else: + line = "`" + else: + if dbls == "d": + line = "''" + else: + line = "'" + elif line.startswith("\\begin_inset Newline newline"): + line = "\\\\ " + elif line.startswith("\\noindent"): + line = "\\noindent " # we need the space behind the command + elif line.startswith("\\begin_inset space"): + line = line[18:].strip() + if line.startswith("\\hspace"): + # Account for both \hspace and \hspace* + hspace = line[:-2] + continue + elif line == "\\space{}": + line = "\\ " + elif line == "\\thinspace{}": + line = "\\," + elif hspace != "": + # The LyX length is in line[8:], after the \length keyword + length = latex_length(line[8:])[1] + line = hspace + "{" + length + "}" + hspace = "" + elif ( + line.isspace() + or line.startswith("\\begin_layout") + or line.startswith("\\end_layout") + or line.startswith("\\begin_inset") + or line.startswith("\\end_inset") + or line.startswith("\\lang") + or line.strip() == "status collapsed" + or line.strip() == "status open" + ): + # skip all that stuff + continue - # this needs to be added to the preamble because of cases like - # \textmu, \textbackslash, etc. - add_to_preamble(document, ['% added by lyx2lyx for converted index entries', - '\\@ifundefined{textmu}', - ' {\\usepackage{textcomp}}{}']) - # a lossless reversion is not possible - # try at least to handle some common insets and settings - if ert_end >= curline: - line = line.replace(r'\backslash', '\\') - else: - # No need to add "{}" after single-nonletter macros - line = line.replace('&', '\\&') - line = line.replace('#', '\\#') - line = line.replace('^', '\\textasciicircum{}') - line = line.replace('%', '\\%') - line = line.replace('_', '\\_') - line = line.replace('$', '\\$') + # this needs to be added to the preamble because of cases like + # \textmu, \textbackslash, etc. + add_to_preamble( + document, + [ + "% added by lyx2lyx for converted index entries", + "\\@ifundefined{textmu}", + " {\\usepackage{textcomp}}{}", + ], + ) + # a lossless reversion is not possible + # try at least to handle some common insets and settings + if ert_end >= curline: + line = line.replace(r"\backslash", "\\") + else: + # No need to add "{}" after single-nonletter macros + line = line.replace("&", "\\&") + line = line.replace("#", "\\#") + line = line.replace("^", "\\textasciicircum{}") + line = line.replace("%", "\\%") + line = line.replace("_", "\\_") + line = line.replace("$", "\\$") - # Do the LyX text --> LaTeX conversion - for rep in unicode_reps: - line = line.replace(rep[1], rep[0]) - line = line.replace(r'\backslash', r'\textbackslash{}') - line = line.replace(r'\series bold', r'\bfseries{}').replace(r'\series default', r'\mdseries{}') - line = line.replace(r'\shape italic', r'\itshape{}').replace(r'\shape smallcaps', r'\scshape{}') - line = line.replace(r'\shape slanted', r'\slshape{}').replace(r'\shape default', r'\upshape{}') - line = line.replace(r'\emph on', r'\em{}').replace(r'\emph default', r'\em{}') - line = line.replace(r'\noun on', r'\scshape{}').replace(r'\noun default', r'\upshape{}') - line = line.replace(r'\bar under', r'\underbar{').replace(r'\bar default', r'}') - line = line.replace(r'\family sans', r'\sffamily{}').replace(r'\family default', r'\normalfont{}') - line = line.replace(r'\family typewriter', r'\ttfamily{}').replace(r'\family roman', r'\rmfamily{}') - line = line.replace(r'\InsetSpace ', r'').replace(r'\SpecialChar ', r'') - content += line + # Do the LyX text --> LaTeX conversion + for rep in unicode_reps: + line = line.replace(rep[1], rep[0]) + line = line.replace(r"\backslash", r"\textbackslash{}") + line = line.replace(r"\series bold", r"\bfseries{}").replace( + r"\series default", r"\mdseries{}" + ) + line = line.replace(r"\shape italic", r"\itshape{}").replace( + r"\shape smallcaps", r"\scshape{}" + ) + line = line.replace(r"\shape slanted", r"\slshape{}").replace( + r"\shape default", r"\upshape{}" + ) + line = line.replace(r"\emph on", r"\em{}").replace(r"\emph default", r"\em{}") + line = line.replace(r"\noun on", r"\scshape{}").replace( + r"\noun default", r"\upshape{}" + ) + line = line.replace(r"\bar under", r"\underbar{").replace(r"\bar default", r"}") + line = line.replace(r"\family sans", r"\sffamily{}").replace( + r"\family default", r"\normalfont{}" + ) + line = line.replace(r"\family typewriter", r"\ttfamily{}").replace( + r"\family roman", r"\rmfamily{}" + ) + line = line.replace(r"\InsetSpace ", r"").replace(r"\SpecialChar ", r"") + content += line return content def lyx2verbatim(document, lines): - 'Convert some LyX stuff into corresponding verbatim stuff, as best we can.' + "Convert some LyX stuff into corresponding verbatim stuff, as best we can." content = lyx2latex(document, lines) - content = re.sub(r'\\(?!backslash)', r'\n\\backslash\n', content) + content = re.sub(r"\\(?!backslash)", r"\n\\backslash\n", content) return content def latex_length(slen): - ''' + """ Convert lengths to their LaTeX representation. Returns (bool, length), where the bool tells us if it was a percentage, and the length is the LaTeX representation. - ''' + """ i = 0 percent = False # the slen has the form @@ -342,14 +381,15 @@ def latex_length(slen): # the + always precedes the - # Convert relative lengths to LaTeX units - units = {"col%": "\\columnwidth", - "text%": "\\textwidth", - "page%": "\\paperwidth", - "line%": "\\linewidth", - "theight%": "\\textheight", - "pheight%": "\\paperheight", - "baselineskip%": "\\baselineskip" - } + units = { + "col%": "\\columnwidth", + "text%": "\\textwidth", + "page%": "\\paperwidth", + "line%": "\\linewidth", + "theight%": "\\textheight", + "pheight%": "\\paperheight", + "baselineskip%": "\\baselineskip", + } for unit in list(units.keys()): i = slen.find(unit) if i == -1: @@ -360,19 +400,19 @@ def latex_length(slen): latex_unit = units[unit] if plus == -1 and minus == -1: value = slen[:i] - value = str(float(value)/100) - end = slen[i + len(unit):] + value = str(float(value) / 100) + end = slen[i + len(unit) :] slen = value + latex_unit + end if plus > minus: - value = slen[plus + 1:i] - value = str(float(value)/100) - begin = slen[:plus + 1] - end = slen[i+len(unit):] + value = slen[plus + 1 : i] + value = str(float(value) / 100) + begin = slen[: plus + 1] + end = slen[i + len(unit) :] slen = begin + value + latex_unit + end if plus < minus: - value = slen[minus + 1:i] - value = str(float(value)/100) - begin = slen[:minus + 1] + value = slen[minus + 1 : i] + value = str(float(value) / 100) + begin = slen[: minus + 1] slen = begin + value + latex_unit # replace + and -, but only if the - is not the first character @@ -387,31 +427,33 @@ def latex_length(slen): def length_in_bp(length): - " Convert a length in LyX format to its value in bp units " + "Convert a length in LyX format to its value in bp units" - em_width = 10.0 / 72.27 # assume 10pt font size - text_width = 8.27 / 1.7 # assume A4 with default margins + em_width = 10.0 / 72.27 # assume 10pt font size + text_width = 8.27 / 1.7 # assume A4 with default margins # scale factors are taken from Length::inInch() - scales = {"bp" : 1.0, - "cc" : (72.0 / (72.27 / (12.0 * 0.376 * 2.845))), - "cm" : (72.0 / 2.54), - "dd" : (72.0 / (72.27 / (0.376 * 2.845))), - "em" : (72.0 * em_width), - "ex" : (72.0 * em_width * 0.4305), - "in" : 72.0, - "mm" : (72.0 / 25.4), - "mu" : (72.0 * em_width / 18.0), - "pc" : (72.0 / (72.27 / 12.0)), - "pt" : (72.0 / (72.27)), - "sp" : (72.0 / (72.27 * 65536.0)), - "text%" : (72.0 * text_width / 100.0), - "col%" : (72.0 * text_width / 100.0), # assume 1 column - "page%" : (72.0 * text_width * 1.7 / 100.0), - "line%" : (72.0 * text_width / 100.0), - "theight%" : (72.0 * text_width * 1.787 / 100.0), - "pheight%" : (72.0 * text_width * 2.2 / 100.0)} + scales = { + "bp": 1.0, + "cc": (72.0 / (72.27 / (12.0 * 0.376 * 2.845))), + "cm": (72.0 / 2.54), + "dd": (72.0 / (72.27 / (0.376 * 2.845))), + "em": (72.0 * em_width), + "ex": (72.0 * em_width * 0.4305), + "in": 72.0, + "mm": (72.0 / 25.4), + "mu": (72.0 * em_width / 18.0), + "pc": (72.0 / (72.27 / 12.0)), + "pt": (72.0 / (72.27)), + "sp": (72.0 / (72.27 * 65536.0)), + "text%": (72.0 * text_width / 100.0), + "col%": (72.0 * text_width / 100.0), # assume 1 column + "page%": (72.0 * text_width * 1.7 / 100.0), + "line%": (72.0 * text_width / 100.0), + "theight%": (72.0 * text_width * 1.787 / 100.0), + "pheight%": (72.0 * text_width * 2.2 / 100.0), + } - rx = re.compile(r'^\s*([^a-zA-Z%]+)([a-zA-Z%]+)\s*$') + rx = re.compile(r"^\s*([^a-zA-Z%]+)([a-zA-Z%]+)\s*$") m = rx.match(length) if not m: document.warning("Invalid length value: " + length + ".") @@ -425,132 +467,132 @@ def length_in_bp(length): def revert_flex_inset(lines, name, LaTeXname): - " Convert flex insets to TeX code " - i = 0 - while True: - i = find_token(lines, '\\begin_inset Flex ' + name, i) - if i == -1: - return - z = find_end_of_inset(lines, i) - if z == -1: - document.warning("Can't find end of Flex " + name + " inset.") - i += 1 - continue - # remove the \end_inset - lines[z - 2:z + 1] = put_cmd_in_ert("}") - # we need to reset character layouts if necessary - j = find_token(lines, '\\emph on', i, z) - k = find_token(lines, '\\noun on', i, z) - l = find_token(lines, '\\series', i, z) - m = find_token(lines, '\\family', i, z) - n = find_token(lines, '\\shape', i, z) - o = find_token(lines, '\\color', i, z) - p = find_token(lines, '\\size', i, z) - q = find_token(lines, '\\bar under', i, z) - r = find_token(lines, '\\uuline on', i, z) - s = find_token(lines, '\\uwave on', i, z) - t = find_token(lines, '\\strikeout on', i, z) - if j != -1: - lines.insert(z - 2, "\\emph default") - if k != -1: - lines.insert(z - 2, "\\noun default") - if l != -1: - lines.insert(z - 2, "\\series default") - if m != -1: - lines.insert(z - 2, "\\family default") - if n != -1: - lines.insert(z - 2, "\\shape default") - if o != -1: - lines.insert(z - 2, "\\color inherit") - if p != -1: - lines.insert(z - 2, "\\size default") - if q != -1: - lines.insert(z - 2, "\\bar default") - if r != -1: - lines.insert(z - 2, "\\uuline default") - if s != -1: - lines.insert(z - 2, "\\uwave default") - if t != -1: - lines.insert(z - 2, "\\strikeout default") - lines[i:i + 4] = put_cmd_in_ert(LaTeXname + "{") - i += 1 + "Convert flex insets to TeX code" + i = 0 + while True: + i = find_token(lines, "\\begin_inset Flex " + name, i) + if i == -1: + return + z = find_end_of_inset(lines, i) + if z == -1: + document.warning("Can't find end of Flex " + name + " inset.") + i += 1 + continue + # remove the \end_inset + lines[z - 2 : z + 1] = put_cmd_in_ert("}") + # we need to reset character layouts if necessary + j = find_token(lines, "\\emph on", i, z) + k = find_token(lines, "\\noun on", i, z) + l = find_token(lines, "\\series", i, z) + m = find_token(lines, "\\family", i, z) + n = find_token(lines, "\\shape", i, z) + o = find_token(lines, "\\color", i, z) + p = find_token(lines, "\\size", i, z) + q = find_token(lines, "\\bar under", i, z) + r = find_token(lines, "\\uuline on", i, z) + s = find_token(lines, "\\uwave on", i, z) + t = find_token(lines, "\\strikeout on", i, z) + if j != -1: + lines.insert(z - 2, "\\emph default") + if k != -1: + lines.insert(z - 2, "\\noun default") + if l != -1: + lines.insert(z - 2, "\\series default") + if m != -1: + lines.insert(z - 2, "\\family default") + if n != -1: + lines.insert(z - 2, "\\shape default") + if o != -1: + lines.insert(z - 2, "\\color inherit") + if p != -1: + lines.insert(z - 2, "\\size default") + if q != -1: + lines.insert(z - 2, "\\bar default") + if r != -1: + lines.insert(z - 2, "\\uuline default") + if s != -1: + lines.insert(z - 2, "\\uwave default") + if t != -1: + lines.insert(z - 2, "\\strikeout default") + lines[i : i + 4] = put_cmd_in_ert(LaTeXname + "{") + i += 1 def revert_font_attrs(lines, name, LaTeXname): - " Reverts font changes to TeX code " - i = 0 - changed = False - while True: - i = find_token(lines, name + ' on', i) - if i == -1: - break - j = find_token(lines, name + ' default', i) - k = find_token(lines, name + ' on', i + 1) - # if there is no default set, the style ends with the layout - # assure hereby that we found the correct layout end - if j != -1 and (j < k or k == -1): - lines[j:j + 1] = put_cmd_in_ert("}") - else: - j = find_token(lines, '\\end_layout', i) - lines[j:j] = put_cmd_in_ert("}") - lines[i:i + 1] = put_cmd_in_ert(LaTeXname + "{") - changed = True - i += 1 + "Reverts font changes to TeX code" + i = 0 + changed = False + while True: + i = find_token(lines, name + " on", i) + if i == -1: + break + j = find_token(lines, name + " default", i) + k = find_token(lines, name + " on", i + 1) + # if there is no default set, the style ends with the layout + # assure hereby that we found the correct layout end + if j != -1 and (j < k or k == -1): + lines[j : j + 1] = put_cmd_in_ert("}") + else: + j = find_token(lines, "\\end_layout", i) + lines[j:j] = put_cmd_in_ert("}") + lines[i : i + 1] = put_cmd_in_ert(LaTeXname + "{") + changed = True + i += 1 - # now delete all remaining lines that manipulate this attribute - i = 0 - while True: - i = find_token(lines, name, i) - if i == -1: - break - del lines[i] + # now delete all remaining lines that manipulate this attribute + i = 0 + while True: + i = find_token(lines, name, i) + if i == -1: + break + del lines[i] - return changed + return changed def revert_layout_command(lines, name, LaTeXname): - " Reverts a command from a layout to TeX code " - i = 0 - while True: - i = find_token(lines, '\\begin_layout ' + name, i) - if i == -1: - return - k = -1 - # find the next layout - j = i + 1 - while k == -1: - j = find_token(lines, '\\begin_layout', j) - l = len(lines) - # if nothing was found it was the last layout of the document - if j == -1: - lines[l - 4:l - 4] = put_cmd_in_ert("}") - k = 0 - # exclude plain layout because this can be TeX code or another inset - elif lines[j] != '\\begin_layout Plain Layout': - lines[j - 2:j - 2] = put_cmd_in_ert("}") - k = 0 - else: - j += 1 - lines[i] = '\\begin_layout Standard' - lines[i + 1:i + 1] = put_cmd_in_ert(LaTeXname + "{") - i += 1 + "Reverts a command from a layout to TeX code" + i = 0 + while True: + i = find_token(lines, "\\begin_layout " + name, i) + if i == -1: + return + k = -1 + # find the next layout + j = i + 1 + while k == -1: + j = find_token(lines, "\\begin_layout", j) + l = len(lines) + # if nothing was found it was the last layout of the document + if j == -1: + lines[l - 4 : l - 4] = put_cmd_in_ert("}") + k = 0 + # exclude plain layout because this can be TeX code or another inset + elif lines[j] != "\\begin_layout Plain Layout": + lines[j - 2 : j - 2] = put_cmd_in_ert("}") + k = 0 + else: + j += 1 + lines[i] = "\\begin_layout Standard" + lines[i + 1 : i + 1] = put_cmd_in_ert(LaTeXname + "{") + i += 1 def hex2ratio(s): - " Converts an RRGGBB-type hexadecimal string to a float in [0.0,1.0] " - try: - val = int(s, 16) - except: - val = 0 - if val != 0: - val += 1 - return str(val / 256.0) + "Converts an RRGGBB-type hexadecimal string to a float in [0.0,1.0]" + try: + val = int(s, 16) + except: + val = 0 + if val != 0: + val += 1 + return str(val / 256.0) def str2bool(s): - "'true' goes to True, case-insensitively, and we strip whitespace." - s = s.strip().lower() - return s == "true" + "'true' goes to True, case-insensitively, and we strip whitespace." + s = s.strip().lower() + return s == "true" def convert_info_insets(document, type, func): @@ -587,17 +629,17 @@ def insert_document_option(document, option): def remove_document_option(document, option): - """ Remove _option_ as a document option.""" + """Remove _option_ as a document option.""" i = find_token(document.header, "\\options") options = get_value(document.header, "\\options", i) - options = [op.strip() for op in options.split(',')] + options = [op.strip() for op in options.split(",")] # Remove `option` from \options options = [op for op in options if op != option] if options: - document.header[i] = "\\options " + ','.join(options) + document.header[i] = "\\options " + ",".join(options) else: del document.header[i] @@ -606,22 +648,25 @@ def is_document_option(document, option): "Find if _option_ is a document option" options = get_value(document.header, "\\options") - options = [op.strip() for op in options.split(',')] + options = [op.strip() for op in options.split(",")] return option in options -singlepar_insets = [s.strip() for s in - "Argument, Caption Above, Caption Below, Caption Bicaption," +singlepar_insets = [ + s.strip() + for s in "Argument, Caption Above, Caption Below, Caption Bicaption," "Caption Centered, Caption FigCaption, Caption Standard, Caption Table," "Flex Chemistry, Flex Fixme_Note, Flex Latin, Flex ListOfSlides," "Flex Missing_Figure, Flex PDF-Annotation, Flex PDF-Comment-Setup," "Flex Reflectbox, Flex S/R expression, Flex Sweave Input File," "Flex Sweave Options, Flex Thanks_Reference, Flex URL, Foot InTitle," - "IPADeco, Index, Info, Phantom, Script".split(',')] + "IPADeco, Index, Info, Phantom, Script".split(",") +] # print(singlepar_insets) + def revert_language(document, lyxname, babelname="", polyglossianame=""): - " Revert native language support " + "Revert native language support" # Does the document use polyglossia? use_polyglossia = False @@ -656,7 +701,7 @@ def revert_language(document, lyxname, babelname="", polyglossianame=""): # Now look for occurences in the body i = 0 while True: - i = find_token(document.body, "\\lang", i+1) + i = find_token(document.body, "\\lang", i + 1) if i == -1: break if document.body[i].startswith("\\lang %s" % lyxname): @@ -669,12 +714,12 @@ def revert_language(document, lyxname, babelname="", polyglossianame=""): continue parent = get_containing_layout(document.body, i) - i_e = parent[2] # end line no, + i_e = parent[2] # end line no, # print(i, texname, parent, document.body[i+1], file=sys.stderr) # Move leading space to the previous line: - if document.body[i+1].startswith(" "): - document.body[i+1] = document.body[i+1][1:] + if document.body[i + 1].startswith(" "): + document.body[i + 1] = document.body[i + 1][1:] document.body.insert(i, " ") continue @@ -691,35 +736,42 @@ def revert_language(document, lyxname, babelname="", polyglossianame=""): # \end_layout # Ensure correct handling of list labels - if (parent[0] in ["Labeling", "Description"] - and not " " in "\n".join(document.body[parent[3]:i])): + if parent[0] in ["Labeling", "Description"] and not " " in "\n".join( + document.body[parent[3] : i] + ): # line `i+1` is first line of a list item, # part before a space character is the label # TODO: insets or language change before first space character - labelline = document.body[i+1].split(' ', 1) + labelline = document.body[i + 1].split(" ", 1) if len(labelline) > 1: # Insert a space in the (original) document language # between label and remainder. # print(" Label:", labelline, file=sys.stderr) - lines = [labelline[0], + lines = [ + labelline[0], "\\lang %s" % orig_doc_language, " ", "\\lang %s" % (primary and "english" or lyxname), - labelline[1]] - document.body[i+1:i+2] = lines + labelline[1], + ] + document.body[i + 1 : i + 2] = lines i_e += 4 # Find out where to end the language change. langswitch = i while True: - langswitch = find_token(document.body, "\\lang", langswitch+1, i_e) + langswitch = find_token(document.body, "\\lang", langswitch + 1, i_e) if langswitch == -1: break # print(" ", langswitch, document.body[langswitch], file=sys.stderr) # skip insets - i_a = parent[3] # paragraph start line - container = get_containing_inset(document.body[i_a:i_e], langswitch-i_a) - if container and container[1] < langswitch-i_a and container[2] > langswitch-i_a: + i_a = parent[3] # paragraph start line + container = get_containing_inset(document.body[i_a:i_e], langswitch - i_a) + if ( + container + and container[1] < langswitch - i_a + and container[2] > langswitch - i_a + ): # print(" inset", container, file=sys.stderr) continue i_e = langswitch @@ -733,29 +785,29 @@ def revert_language(document, lyxname, babelname="", polyglossianame=""): singlepar = container[0] in singlepar_insets # Delete empty language switches: - if not "".join(document.body[i+1:i_e]): + if not "".join(document.body[i + 1 : i_e]): del document.body[i:i_e] i -= 1 continue if singlepar: if with_polyglossia: - begin_cmd = "\\text%s{"%texname + begin_cmd = "\\text%s{" % texname elif with_babel: begin_cmd = "\\foreignlanguage{%s}{" % texname end_cmd = "}" else: if with_polyglossia: - begin_cmd = "\\begin{%s}"%texname - end_cmd = "\\end{%s}"%texname + begin_cmd = "\\begin{%s}" % texname + end_cmd = "\\end{%s}" % texname elif with_babel: begin_cmd = "\\begin{otherlanguage}{%s}" % texname end_cmd = "\\end{otherlanguage}" - if (not primary or texname == "english"): + if not primary or texname == "english": try: document.body[i_e:i_e] = put_cmd_in_ert(end_cmd) - document.body[i+1:i+1] = put_cmd_in_ert(begin_cmd) + document.body[i + 1 : i + 1] = put_cmd_in_ert(begin_cmd) except UnboundLocalError: pass del document.body[i] @@ -776,13 +828,14 @@ def revert_language(document, lyxname, babelname="", polyglossianame=""): if with_polyglossia: # Define language in the user preamble # (don't use \AtBeginDocument, this fails with some languages). - add_to_preamble(document, ["\\usepackage{polyglossia}", - "\\setotherlanguage{%s}" % polyglossianame]) + add_to_preamble( + document, + ["\\usepackage{polyglossia}", "\\setotherlanguage{%s}" % polyglossianame], + ) if primary: # Changing the main language must be done in the document body. doc_lang_switch = "\\resetdefaultlanguage{%s}" % polyglossianame # Reset LaTeX main language if required and not already done if doc_lang_switch and doc_lang_switch[1:] not in document.body[8:20]: - document.body[2:2] = put_cmd_in_ert(doc_lang_switch, - is_open=True, as_paragraph=True) + document.body[2:2] = put_cmd_in_ert(doc_lang_switch, is_open=True, as_paragraph=True) diff --git a/lib/lyx2lyx/lyx_0_06.py b/lib/lyx2lyx/lyx_0_06.py index 7c8f5d5e46..0f2ecef3f9 100644 --- a/lib/lyx2lyx/lyx_0_06.py +++ b/lib/lyx2lyx/lyx_0_06.py @@ -15,13 +15,12 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -""" Convert files to the file format generated by lyx 0.6""" +"""Convert files to the file format generated by lyx 0.6""" supported_versions = ["0.6.%d" % i for i in range(5)] + ["0.6"] convert = [[200, []]] -revert = [] +revert = [] if __name__ == "__main__": pass - diff --git a/lib/lyx2lyx/lyx_0_08.py b/lib/lyx2lyx/lyx_0_08.py index 5867609b97..dd6481ffdd 100644 --- a/lib/lyx2lyx/lyx_0_08.py +++ b/lib/lyx2lyx/lyx_0_08.py @@ -15,19 +15,19 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -""" Convert files to the file format generated by lyx 0.8""" +"""Convert files to the file format generated by lyx 0.8""" + def add_inputencoding(document): - " Add the input encoding, latin1" - document.header.append('\\inputencoding latin1') + "Add the input encoding, latin1" + document.header.append("\\inputencoding latin1") document.inputencoding = "latin1" supported_versions = ["0.8.%d" % i for i in range(7)] + ["0.8"] convert = [[210, [add_inputencoding]]] -revert = [] +revert = [] if __name__ == "__main__": pass - diff --git a/lib/lyx2lyx/lyx_0_10.py b/lib/lyx2lyx/lyx_0_10.py index 747c4dab75..8e3f088387 100644 --- a/lib/lyx2lyx/lyx_0_10.py +++ b/lib/lyx2lyx/lyx_0_10.py @@ -15,29 +15,30 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -""" Convert files to the file format generated by lyx 0.10""" +"""Convert files to the file format generated by lyx 0.10""" + def regularise_header(document): - " Put each entry in header into a separate line. " + "Put each entry in header into a separate line." i = 0 while i < len(document.header): line = document.header[i] - if len(line.split('\\')) > 1: - tmp = [ '\\'+ token.strip() for token in line.split('\\')][1:] - document.header[i: i+1] = tmp + if len(line.split("\\")) > 1: + tmp = ["\\" + token.strip() for token in line.split("\\")][1:] + document.header[i : i + 1] = tmp i += len(tmp) i += 1 def find_next_space(line, j): - """ Return position of next space or backslash, which one comes + """Return position of next space or backslash, which one comes first, starting from position j, if none exists returns last position in line (+1).""" - space_pos = line.find(' ', j) + space_pos = line.find(" ", j) if space_pos == -1: space_pos = len(line) - bksl_pos = line.find('\\', j) + bksl_pos = line.find("\\", j) if bksl_pos == -1: bksl_pos = len(line) @@ -45,19 +46,42 @@ def find_next_space(line, j): def regularise_body(document): - """ Place tokens starting with a backslash into a separate line. """ + """Place tokens starting with a backslash into a separate line.""" - getline_tokens = ["added_space_bottom", "added_space_top", - "align", "layout", "fill_bottom", "fill_top", - "labelwidthstring", "pagebreak_top", - "pagebreak_bottom", "noindent"] + getline_tokens = [ + "added_space_bottom", + "added_space_top", + "align", + "layout", + "fill_bottom", + "fill_top", + "labelwidthstring", + "pagebreak_top", + "pagebreak_bottom", + "noindent", + ] - noargs_tokens = ["backslash", "begin_deeper", "end_deeper", - "end_float", "end_inset", "hfill", "newline", - "protected_separator"] + noargs_tokens = [ + "backslash", + "begin_deeper", + "end_deeper", + "end_float", + "end_inset", + "hfill", + "newline", + "protected_separator", + ] - onearg_tokens = ["bar", "begin_float", "family", "latex", "shape", - "size", "series", "cursor"] + onearg_tokens = [ + "bar", + "begin_float", + "family", + "latex", + "shape", + "size", + "series", + "cursor", + ] i = 0 while i < len(document.body): @@ -65,23 +89,23 @@ def regularise_body(document): j = 0 new_block = [] while j < len(line): - k = line.find('\\', j) + k = line.find("\\", j) if k == -1: new_block += [line[j:]] break if k != j: - #document.warning("j=%d\tk=%d\t#%s#%s#" % (j,k,line,line[j: k])) - new_block += [line[j: k]] + # document.warning("j=%d\tk=%d\t#%s#%s#" % (j,k,line,line[j: k])) + new_block += [line[j:k]] j = k - k = find_next_space(line, j+1) + k = find_next_space(line, j + 1) - token = line[j+1:k] + token = line[j + 1 : k] # These tokens take the rest of the line if token in getline_tokens: - #document.warning("getline_token:%s\tj=%d\t\t#%s#%s#" % (token,j,line,line[j:])) + # document.warning("getline_token:%s\tj=%d\t\t#%s#%s#" % (token,j,line,line[j:])) new_block += [line[j:]] break @@ -101,15 +125,14 @@ def regularise_body(document): # Special treatment for insets if token in ["begin_inset"]: l = find_next_space(line, k + 1) - inset = line[k+1: l] + inset = line[k + 1 : l] if inset == "Latex": new_block += [line[j:l]] j = l continue - if inset in ["LatexCommand", "LatexDel", "Label", "Figure", - "Formula"]: + if inset in ["LatexCommand", "LatexDel", "Label", "Figure", "Formula"]: new_block += [line[j:]] break @@ -120,19 +143,19 @@ def regularise_body(document): continue document.warning("unkown inset %s" % inset) - assert(False) + assert False # We are inside a latex inset, pass the text verbatim new_block += [line[j:]] break - document.body[i: i+1] = new_block + document.body[i : i + 1] = new_block i += len(new_block) supported_versions = ["0.10.%d" % i for i in range(8)] + ["0.10"] convert = [[210, [regularise_header, regularise_body]]] -revert = [] +revert = [] if __name__ == "__main__": diff --git a/lib/lyx2lyx/lyx_0_12.py b/lib/lyx2lyx/lyx_0_12.py index 864830065f..50405213ab 100644 --- a/lib/lyx2lyx/lyx_0_12.py +++ b/lib/lyx2lyx/lyx_0_12.py @@ -15,29 +15,29 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -""" Convert files to the file format generated by lyx 0.12""" +"""Convert files to the file format generated by lyx 0.12""" import re from parser_tools import find_token, find_re, check_token def space_before_layout(document): - " Remove empty line before \\layout. " + "Remove empty line before \\layout." lines = document.body - i = 2 # skip first layout + i = 2 # skip first layout while True: - i = find_token(lines, '\\layout', i) + i = find_token(lines, "\\layout", i) if i == -1: break - prot_space = lines[i-2].find('\\protected_separator') - if lines[i - 1] == '' and prot_space == -1: - del lines[i-1] + prot_space = lines[i - 2].find("\\protected_separator") + if lines[i - 1] == "" and prot_space == -1: + del lines[i - 1] i = i + 1 def formula_inset_space_eat(document): - " Remove space after inset formula." + "Remove space after inset formula." lines = document.body i = 0 while True: @@ -45,13 +45,13 @@ def formula_inset_space_eat(document): if i == -1: break - if len(lines[i]) > 22 and lines[i][21] == ' ': + if len(lines[i]) > 22 and lines[i][21] == " ": lines[i] = lines[i][:20] + lines[i][21:] i = i + 1 def update_tabular(document): - " Update from tabular format 1 or 2 to 4." + "Update from tabular format 1 or 2 to 4." lines = document.body lyxtable_re = re.compile(r".*\\LyXTable$") i = 0 @@ -62,201 +62,199 @@ def update_tabular(document): i = i + 1 format = lines[i][8:] - lines[i] = 'multicol4' + lines[i] = "multicol4" i = i + 1 rows = int(lines[i].split()[0]) columns = int(lines[i].split()[1]) - lines[i] = lines[i] + ' 0 0 -1 -1 -1 -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' + lines[i] = lines[i] + " 0 0" i = i + 1 for j in range(columns): - lines[i] = lines[i] + ' ' + lines[i] = lines[i] + " " i = i + 1 while lines[i].strip(): if not format: - lines[i] = lines[i] + ' 1 1' - lines[i] = lines[i] + ' 0 0 0' + lines[i] = lines[i] + " 1 1" + lines[i] = lines[i] + " 0 0 0" i = i + 1 lines[i] = lines[i].strip() def final_dot(document): - " Merge lines if the dot is the final character." + "Merge lines if the dot is the final character." lines = document.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] + 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(document): - " Update inset Label." + "Update inset Label." lines = document.body i = 0 while True: - i = find_token(lines, '\\begin_inset Label', i) + i = find_token(lines, "\\begin_inset Label", i) if i == -1: return - lines[i] = '\\begin_inset LatexCommand \\label{' + lines[i][19:] + '}' + lines[i] = "\\begin_inset LatexCommand \\label{" + lines[i][19:] + "}" i = i + 1 def update_latexdel(document): - " Update inset LatexDel." + "Update inset LatexDel." lines = document.body i = 0 while True: - i = find_token(lines, '\\begin_inset LatexDel', i) + i = find_token(lines, "\\begin_inset LatexDel", i) if i == -1: return - lines[i] = lines[i].replace('\\begin_inset LatexDel', - '\\begin_inset LatexCommand') + lines[i] = lines[i].replace("\\begin_inset LatexDel", "\\begin_inset LatexCommand") i = i + 1 def update_vfill(document): - " Update fill_top and fill_bottom." + "Update fill_top and fill_bottom." lines = document.body for i in range(len(lines)): - lines[i] = lines[i].replace('\\fill_top', - '\\added_space_top vfill') - lines[i] = lines[i].replace('\\fill_bottom', - '\\added_space_bottom vfill') + lines[i] = lines[i].replace("\\fill_top", "\\added_space_top vfill") + lines[i] = lines[i].replace("\\fill_bottom", "\\added_space_bottom vfill") def update_space_units(document): - " Update space units." + "Update space units." lines = document.body - added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)') - added_space_top = re.compile(r'\\added_space_top ([^ ]*)') + 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' + old = "\\added_space_bottom " + result.group(1) + new = "\\added_space_bottom " + str(float(result.group(1))) + "cm" lines[i] = lines[i].replace(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' + old = "\\added_space_top " + result.group(1) + new = "\\added_space_top " + str(float(result.group(1))) + "cm" lines[i] = lines[i].replace(old, new) def remove_cursor(document): - " Remove cursor, it is not saved on the file anymore." + "Remove cursor, it is not saved on the file anymore." lines = document.body i = 0 - cursor_re = re.compile(r'.*(\\cursor \d*)') + cursor_re = re.compile(r".*(\\cursor \d*)") while True: i = find_re(lines, cursor_re, i) if i == -1: break cursor = cursor_re.search(lines[i]).group(1) - lines[i] = lines[i].replace(cursor, '') + lines[i] = lines[i].replace(cursor, "") i = i + 1 def remove_empty_insets(document): - " Remove empty insets." + "Remove empty insets." lines = document.body i = 0 while True: - i = find_token(lines, '\\begin_inset ', i) + i = find_token(lines, "\\begin_inset ", i) if i == -1: break - if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ': + if lines[i] == "\\begin_inset " and lines[i + 1] == "\\end_inset ": del lines[i] del lines[i] i = i + 1 def remove_formula_latex(document): - " Remove formula latex." + "Remove formula latex." lines = document.body i = 0 while True: - i = find_token(lines, '\\latex formula_latex ', i) + i = find_token(lines, "\\latex formula_latex ", i) if i == -1: break del lines[i] - i = find_token(lines, '\\latex default', i) + i = find_token(lines, "\\latex default", i) if i == -1: break del lines[i] def add_end_document(document): - " Add \\the_end to the end of the document." + "Add \\the_end to the end of the document." lines = document.body - i = find_token(lines, '\\the_end', 0) + i = find_token(lines, "\\the_end", 0) if i == -1: - lines.append('\\the_end') + lines.append("\\the_end") def header_update(document): - " Update document header." + "Update document header." lines = document.header i = 0 l = len(lines) while i < l: - if lines[i][-1:] == ' ': + if lines[i][-1:] == " ": lines[i] = lines[i][:-1] - if check_token(lines[i], '\\epsfig'): - lines[i] = lines[i].replace('\\epsfig', '\\graphics') + if check_token(lines[i], "\\epsfig"): + lines[i] = lines[i].replace("\\epsfig", "\\graphics") i = i + 1 continue - if check_token(lines[i], '\\papersize'): + if check_token(lines[i], "\\papersize"): size = lines[i].split()[1] new_size = size paperpackage = "" - if size == 'usletter': - new_size = 'letterpaper' - if size == 'a4wide': - new_size = 'Default' + if size == "usletter": + new_size = "letterpaper" + if size == "a4wide": + new_size = "Default" paperpackage = "widemarginsa4" - lines[i] = '\\papersize ' + new_size + lines[i] = "\\papersize " + new_size i = i + 1 if paperpackage: - lines.insert(i, '\\paperpackage ' + paperpackage) + lines.insert(i, "\\paperpackage " + paperpackage) i = i + 1 - lines.insert(i,'\\use_geometry 0') - lines.insert(i + 1,'\\use_amsmath 0') + lines.insert(i, "\\use_geometry 0") + lines.insert(i + 1, "\\use_amsmath 0") i = i + 2 continue - - if check_token(lines[i], '\\baselinestretch'): + if check_token(lines[i], "\\baselinestretch"): size = lines[i].split()[1] - if size == '1.00': - name = 'single' - elif size == '1.50': - name = 'onehalf' - elif size == '2.00': - name = 'double' + 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 + name = "other " + size + lines[i] = "\\spacing %s " % name i = i + 1 continue @@ -264,45 +262,45 @@ def header_update(document): def update_latexaccents(document): - " Update latex accent insets." + "Update latex accent insets." body = document.body i = 1 while True: - i = find_token(body, '\\i ', i) + i = find_token(body, "\\i ", i) if i == -1: return contents = body[i][2:].strip() - if contents.find('{') != -1 and contents.find('}') != -1: + if contents.find("{") != -1 and contents.find("}") != -1: i = i + 1 continue if len(contents) == 2: - contents = contents + '{}' + contents = contents + "{}" elif len(contents) == 3: - contents = contents[:2] + '{' + contents[2] + '}' + 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:] + '}' + 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 + body[i] = "\\i " + contents i = i + 1 def obsolete_latex_title(document): - " Replace layout Latex_Title with Title." + "Replace layout Latex_Title with Title." body = document.body i = 0 while True: - i = find_token(body, '\\layout', i) + i = find_token(body, "\\layout", i) if i == -1: return - if body[i].lower().find('latex_title') != -1: - body[i] = '\\layout Title' + if body[i].lower().find("latex_title") != -1: + body[i] = "\\layout Title" i = i + 1 @@ -313,27 +311,43 @@ def remove_inset_latex(document): i = 0 while True: - i = find_token(body, '\\begin_inset Latex', i) + i = find_token(body, "\\begin_inset Latex", i) if i == -1: return - body[i] = body[i].replace('\\begin_inset Latex', '\\layout LaTeX') - i = find_token(body, '\\end_inset', i) + body[i] = body[i].replace("\\begin_inset Latex", "\\layout LaTeX") + i = find_token(body, "\\end_inset", i) if i == -1: - #this should not happen + # this should not happen return del body[i] - - -supported_versions = ["0.12.0","0.12.1","0.12"] -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, remove_inset_latex]]] -revert = [] + + +supported_versions = ["0.12.0", "0.12.1", "0.12"] +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, + remove_inset_latex, + ], + ] +] +revert = [] if __name__ == "__main__": diff --git a/lib/lyx2lyx/lyx_1_0.py b/lib/lyx2lyx/lyx_1_0.py index 9f57de9b88..46cf16fbc1 100644 --- a/lib/lyx2lyx/lyx_1_0.py +++ b/lib/lyx2lyx/lyx_1_0.py @@ -15,29 +15,30 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -""" Convert files to the file format generated by lyx 1.0""" +"""Convert files to the file format generated by lyx 1.0""" import re from parser_tools import find_token, find_re + def obsolete_latex_title(document): - " Replace LatexTitle layout with Title. " + "Replace LatexTitle layout with Title." body = document.body i = 0 while True: - i = find_token(body, '\\layout', i) + i = find_token(body, "\\layout", i) if i == -1: return - if body[i].lower().find('latex title') != -1: - body[i] = '\\layout Title' + if body[i].lower().find("latex title") != -1: + body[i] = "\\layout Title" i = i + 1 def update_tabular(document): - " Update from tabular format 3 to 4 if necessary." + "Update from tabular format 3 to 4 if necessary." lines = document.body lyxtable_re = re.compile(r".*\\LyXTable$") @@ -49,27 +50,27 @@ def update_tabular(document): i = i + 1 format = lines[i][8:] - if format != '3': + if format != "3": continue - lines[i] = 'multicol4' + lines[i] = "multicol4" i = i + 1 rows = int(lines[i].split()[0]) columns = int(lines[i].split()[1]) - lines[i] = lines[i] + ' 0 0 -1 -1 -1 -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' + lines[i] = lines[i] + " 0 0" i = i + 1 for j in range(columns): - lines[i] = lines[i] + ' ' + lines[i] = lines[i] + " " i = i + 1 while lines[i].strip(): - lines[i] = lines[i] + ' 0 0 0' + lines[i] = lines[i] + " 0 0 0" i = i + 1 lines[i] = lines[i].strip() @@ -77,9 +78,8 @@ def update_tabular(document): supported_versions = ["1.0.%d" % i for i in range(5)] + ["1.0"] convert = [[215, [obsolete_latex_title, update_tabular]]] -revert = [] +revert = [] if __name__ == "__main__": pass - diff --git a/lib/lyx2lyx/lyx_1_1.py b/lib/lyx2lyx/lyx_1_1.py index 8da8503adf..cd3352fad6 100644 --- a/lib/lyx2lyx/lyx_1_1.py +++ b/lib/lyx2lyx/lyx_1_1.py @@ -15,13 +15,12 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -""" Convert files to the file format generated by lyx 1.1 series, until 1.1.4""" +"""Convert files to the file format generated by lyx 1.1 series, until 1.1.4""" supported_versions = ["1.1.%d" % i for i in range(5)] + ["1.1"] convert = [[215, []]] -revert = [] +revert = [] if __name__ == "__main__": pass - diff --git a/lib/lyx2lyx/lyx_1_1_5.py b/lib/lyx2lyx/lyx_1_1_5.py index 8d8166dcb5..b8cf734f09 100644 --- a/lib/lyx2lyx/lyx_1_1_5.py +++ b/lib/lyx2lyx/lyx_1_1_5.py @@ -15,7 +15,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -""" Convert files to the file format generated by lyx 1.1.5""" +"""Convert files to the file format generated by lyx 1.1.5""" import re from parser_tools import find_token, find_token_backwards, find_re @@ -23,8 +23,9 @@ from parser_tools import find_token, find_token_backwards, find_re #################################################################### # Private helper functions + def get_layout(line, default_layout): - " Get the line layout, beware of the empty layout." + "Get the line layout, beware of the empty layout." tokens = line.split() if len(tokens) > 1: return tokens[1] @@ -33,18 +34,19 @@ def get_layout(line, default_layout): #################################################################### -math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"] +math_env = ["\\[", "\\begin{eqnarray*}", "\\begin{eqnarray}", "\\begin{equation}"] + def replace_protected_separator(document): - " Replace protected separator. " + "Replace protected separator." lines = document.body - i=0 + i = 0 while True: i = find_token(lines, "\\protected_separator", i) if i == -1: break j = find_token_backwards(lines, "\\layout", i) - #if j == -1: print error + # if j == -1: print error layout = get_layout(lines[j], document.default_layout) if layout == "LyX-Code": @@ -53,41 +55,42 @@ def replace_protected_separator(document): result = result + " " del lines[i] - lines[i-1] = lines[i-1] + result + lines[i] + lines[i - 1] = lines[i - 1] + result + lines[i] else: - lines[i-1] = lines[i-1]+ "\\SpecialChar ~" + lines[i - 1] = lines[i - 1] + "\\SpecialChar ~" del lines[i] def merge_formula_inset(document): - " Merge formula insets. " + "Merge formula insets." lines = document.body - i=0 + i = 0 while True: 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] + 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 def update_tabular(document): - " Update from tabular format 4 to 5 if necessary. " + "Update from tabular format 4 to 5 if necessary." lines = document.body lyxtable_re = re.compile(r".*\\LyXTable$") - i=0 + i = 0 while True: i = find_re(lines, lyxtable_re, i) if i == -1: break i = i + 1 format = lines[i][8] - if format != '4': + if format != "4": continue - lines[i]='multicol5' + lines[i] = "multicol5" i = i + 1 rows = int(lines[i].split()[0]) columns = int(lines[i].split()[1]) @@ -107,53 +110,51 @@ def update_tabular(document): def update_toc(document): - " Update table of contents. " + "Update table of contents." lines = document.body i = 0 while True: - i = find_token(lines, - '\\begin_inset LatexCommand \\tableofcontents', i) + i = find_token(lines, "\\begin_inset LatexCommand \\tableofcontents", i) if i == -1: break - lines[i] = lines[i] + '{}' + lines[i] = lines[i] + "{}" i = i + 1 def remove_cursor(document): - " Remove cursor. " + "Remove cursor." lines = document.body - i = find_token(lines, '\\cursor', 0) + i = find_token(lines, "\\cursor", 0) if i != -1: del lines[i] def remove_vcid(document): - " Remove \\lyxvcid and \\lyxrcsid. " + "Remove \\lyxvcid and \\lyxrcsid." lines = document.header - i = find_token(lines, '\\lyxvcid', 0) + i = find_token(lines, "\\lyxvcid", 0) if i != -1: del lines[i] - i = find_token(lines, '\\lyxrcsid', 0) + i = find_token(lines, "\\lyxrcsid", 0) if i != -1: del lines[i] def first_layout(document): - " Fix first layout, if empty use the default layout." + "Fix first layout, if empty use the default layout." lines = document.body - while (lines[0] == ""): + while lines[0] == "": del lines[0] if lines[0][:7] != "\\layout": - lines[:0] = ['\\layout %s' % document.default_layout, ''] + lines[:0] = ["\\layout %s" % document.default_layout, ""] def remove_space_in_units(document): - " Remove space in units. " + "Remove space in units." lines = document.header - margins = ["\\topmargin","\\rightmargin", - "\\leftmargin","\\bottommargin"] + margins = ["\\topmargin", "\\rightmargin", "\\leftmargin", "\\bottommargin"] - unit_rexp = re.compile(r'[^ ]* (.*) (.*)') + unit_rexp = re.compile(r"[^ ]* (.*) (.*)") for margin in margins: i = 0 @@ -169,7 +170,7 @@ def remove_space_in_units(document): def latexdel_getargs(document, i): - " Get arguments from latexdel insets. " + "Get arguments from latexdel insets." lines = document.body # play safe, clean empty lines @@ -178,16 +179,16 @@ def latexdel_getargs(document, i): break del lines[i] - j = find_token(lines, '\\end_inset', i) + j = find_token(lines, "\\end_inset", i) if i == j: del lines[i] else: document.warning("Unexpected end of inset.") - j = find_token(lines, '\\begin_inset LatexDel }{', i) + j = find_token(lines, "\\begin_inset LatexDel }{", i) ref = " ".join(lines[i:j]) - del lines[i:j + 1] + del lines[i : j + 1] # play safe, clean empty lines while True: @@ -195,24 +196,24 @@ def latexdel_getargs(document, i): break del lines[i] - j = find_token(lines, '\\end_inset', i - 1) + j = find_token(lines, "\\end_inset", i - 1) if i == j: del lines[i] else: document.warning("Unexpected end of inset.") - j = find_token(lines, '\\begin_inset LatexDel }', i) + j = find_token(lines, "\\begin_inset LatexDel }", i) label = " ".join(lines[i:j]) - del lines[i:j + 1] + del lines[i : j + 1] return ref, label def update_ref(document): - " Update reference inset. " + "Update reference inset." lines = document.body i = 0 while True: - i = find_token(lines, '\\begin_inset LatexCommand', i) + i = find_token(lines, "\\begin_inset LatexCommand", i) if i == -1: return @@ -225,7 +226,7 @@ def update_ref(document): def update_latexdel(document): - " Remove latexdel insets. " + "Remove latexdel insets." lines = document.body i = 0 latexdel_re = re.compile(r".*\\begin_inset LatexDel") @@ -233,11 +234,10 @@ def update_latexdel(document): i = find_re(lines, latexdel_re, i) if i == -1: return - lines[i] = lines[i].replace('\\begin_inset LatexDel', - '\\begin_inset LatexCommand') + lines[i] = lines[i].replace("\\begin_inset LatexDel", "\\begin_inset LatexCommand") - j = lines[i].find('\\begin_inset') - lines.insert(i+1, lines[i][j:]) + j = lines[i].find("\\begin_inset") + lines.insert(i + 1, lines[i][j:]) lines[i] = lines[i][:j].strip() i = i + 1 @@ -245,19 +245,31 @@ def update_latexdel(document): i = i + 1 ref, label = latexdel_getargs(document, i) - lines[i -1] = f"{lines[i-1][:-1]}[{label}]{{{ref}}}" + lines[i - 1] = f"{lines[i-1][:-1]}[{label}]{{{ref}}}" i = i + 1 -supported_versions = ["1.1.5","1.1.5fix1","1.1.5fix2","1.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]]] +supported_versions = ["1.1.5", "1.1.5fix1", "1.1.5fix2", "1.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 = [] +revert = [] if __name__ == "__main__": pass diff --git a/lib/lyx2lyx/lyx_1_1_6_0.py b/lib/lyx2lyx/lyx_1_1_6_0.py index e7c63c3bd4..635056215a 100644 --- a/lib/lyx2lyx/lyx_1_1_6_0.py +++ b/lib/lyx2lyx/lyx_1_1_6_0.py @@ -15,29 +15,38 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -""" Convert files to the file format generated by lyx 1.1.6, until fix2""" +"""Convert files to the file format generated by lyx 1.1.6, until fix2""" import re from parser_tools import find_re, find_tokens, find_token, check_token lyxtable_re = re.compile(r".*\\LyXTable$") + + def update_tabular(document): - " Update tabular to version 1 (xml like syntax). " + "Update tabular to version 1 (xml like syntax)." lines = document.body - i=0 + i = 0 while True: i = find_re(lines, lyxtable_re, i) if i == -1: break - prop_dict = {"family" : "default", "series" : "default", - "shape" : "default", "size" : "default", - "emph" : "default", "bar" : "default", - "noun" : "default", "latex" : "default", "color" : "default"} + prop_dict = { + "family": "default", + "series": "default", + "shape": "default", + "size": "default", + "emph": "default", + "bar": "default", + "noun": "default", + "latex": "default", + "color": "default", + } # remove \LyXTable lines[i] = lines[i][:-9] i = i + 1 - lines.insert(i,'') + lines.insert(i, "") i = i + 1 lines[i] = "\\begin_inset Tabular" i = i + 1 @@ -46,16 +55,19 @@ def update_tabular(document): columns = int(head[1]) tabular_line = i - i = i +1 - lines.insert(i, f'') + i = i + 1 + lines.insert( + i, + f'', + ) - i = i +1 + i = i + 1 row_info = [] cont_row = [] for j in range(rows): row_info.append(lines[i].split()) - if lines[i].split()[2] == '1': + if lines[i].split()[2] == "1": cont_row.append(j) del lines[i] @@ -71,14 +83,16 @@ def update_tabular(document): cell_re = re.compile(r'(\d) (\d) (\d) (\d) (\d) (\d) (\d) (".*") (".*")') for j in range(rows): for k in range(columns): - #add column location to read properties + # add column location to read properties cell_info.append(cell_re.match(lines[i]).groups()) cell_col.append(k) if lines[i][0] != "2": ncells = ncells + 1 del lines[i] - lines[tabular_line] = f'' + lines[tabular_line] = ( + f'' + ) del lines[i] if not lines[i]: del lines[i] @@ -95,15 +109,19 @@ def update_tabular(document): for j in range(rows): for k in range(columns): - m = j*columns + k - if cell_info[m][0] == '2': + m = j * columns + k + if cell_info[m][0] == "2": continue - if l == ncells -1: + if l == ncells - 1: # the end variable refers to cell end, not to document end. - end = find_tokens(lines, ['\\layout','\\the_end','\\end_deeper','\\end_float'], i) + end = find_tokens( + lines, + ["\\layout", "\\the_end", "\\end_deeper", "\\end_float"], + i, + ) else: - end = find_token(lines, '\\newline', i) + end = find_token(lines, "\\newline", i) if end == -1: document.error("Malformed LyX file.") @@ -112,9 +130,9 @@ def update_tabular(document): while end > 0: cell_content[j][k].append(lines[i]) del lines[i] - end = end -1 + end = end - 1 - if lines[i].find('\\newline') != -1: + if lines[i].find("\\newline") != -1: del lines[i] l = l + 1 @@ -124,83 +142,120 @@ def update_tabular(document): for j in range(rows): if j in cont_row: continue - tmp.append(f'') + tmp.append( + f'' + ) for k in range(columns): if j: - tmp.append('') + tmp.append("") else: - tmp.append(f'') - m = j*columns + k + tmp.append( + f'' + ) + m = j * columns + k leftline = int(column_info[k][1]) - if cell_info[m][0] == '1': + if cell_info[m][0] == "1": n = m + 1 - while n < rows * columns - 1 and cell_info[n][0] == '2': + while n < rows * columns - 1 and cell_info[n][0] == "2": n = n + 1 - rightline = int(column_info[cell_col[n-1]][2]) + rightline = int(column_info[cell_col[n - 1]][2]) else: # not a multicolumn main cell rightline = int(column_info[k][2]) - tmp.append('' % (cell_info[m][0],cell_info[m][1],cell_info[m][2],cell_info[m][3],leftline,rightline,cell_info[m][5],cell_info[m][6],cell_info[m][7],cell_info[m][8])) - tmp.append('\\begin_inset Text') - tmp.append('') - tmp.append('\\layout %s' % document.default_layout) - tmp.append('') + tmp.append( + '' + % ( + cell_info[m][0], + cell_info[m][1], + cell_info[m][2], + cell_info[m][3], + leftline, + rightline, + cell_info[m][5], + cell_info[m][6], + cell_info[m][7], + cell_info[m][8], + ) + ) + tmp.append("\\begin_inset Text") + tmp.append("") + tmp.append("\\layout %s" % document.default_layout) + tmp.append("") - if cell_info[m][0] != '2': + if cell_info[m][0] != "2": paragraph = [] - if cell_info[m][4] == '1': + if cell_info[m][4] == "1": l = j paragraph = paragraph + cell_content[j][k] - while cell_info[m][4] == '1': + while cell_info[m][4] == "1": m = m + columns l = l + 1 - if l >= rows: break + if l >= rows: + break paragraph = paragraph + cell_content[l][k] else: paragraph = cell_content[j][k] tmp = tmp + set_paragraph_properties(paragraph, prop_dict) - tmp.append('\\end_inset ') - tmp.append('') - tmp.append('') - tmp.append('') + tmp.append("\\end_inset ") + tmp.append("") + tmp.append("") + tmp.append("") - tmp.append('') - tmp.append('') - tmp.append('\\end_inset ') - tmp.append('') - tmp.append('') + tmp.append("") + tmp.append("") + tmp.append("\\end_inset ") + tmp.append("") + tmp.append("") lines[i:i] = tmp i = i + len(tmp) prop_exp = re.compile(r"\\(\S*)\s*(\S*)") + + def set_paragraph_properties(lines, prop_dict): - " Set paragraph properties." + "Set paragraph properties." # we need to preserve the order of options - properties = ["family","series","shape","size", - "emph","bar","noun","latex","color"] - prop_value = {"family" : "default", "series" : "medium", - "shape" : "up", "size" : "normal", - "emph" : "off", "bar" : "no", - "noun" : "off", "latex" : "no_latex", "color" : "none"} + properties = [ + "family", + "series", + "shape", + "size", + "emph", + "bar", + "noun", + "latex", + "color", + ] + prop_value = { + "family": "default", + "series": "medium", + "shape": "up", + "size": "normal", + "emph": "off", + "bar": "no", + "noun": "off", + "latex": "no_latex", + "color": "none", + } start = 0 end = 0 i = 0 n = len(lines) - #skip empty lines - while i') + def update_tabular(document): - " Update tabular format to version 2 (xml like syntax)." - regexp = re.compile(r'^\\begin_inset\s+Tabular') + "Update tabular format to version 2 (xml like syntax)." + regexp = re.compile(r"^\\begin_inset\s+Tabular") lines = document.body - i=0 + i = 0 while True: i = find_re(lines, regexp, i) if i == -1: break - i = i +1 + i = i + 1 # scan table header meta-info - res = table_meta_re.match( lines[i] ) + res = table_meta_re.match(lines[i]) if res: val = res.groups() lines[i] = '' % val - j = find_token(lines, '', i) + 1 + j = find_token(lines, "", i) + 1 if j == 0: - document.warning( "Error: Bad lyx format i=%d j=%d" % (i,j)) + document.warning("Error: Bad lyx format i=%d j=%d" % (i, j)) break new_table = table_update(lines[i:j]) @@ -61,50 +63,63 @@ def update_tabular(document): i = i + len(new_table) -col_re = re.compile(r'') -cell_re = re.compile(r'') -features_re = re.compile(r'') +col_re = re.compile( + r'' +) +cell_re = re.compile( + r'' +) +features_re = re.compile( + r'' +) row_re = re.compile(r'') + def table_update(lines): - " Update table's internal content to format 2." - lines[1] = lines[1].replace('' + lines[1] = ( + f'' + ) - if lines[2]=="": + if lines[2] == "": del lines[2] i = 2 col_info = [] while i < len(lines): - lines[i] = lines[i].replace('' or lines[i] == '': + if lines[i] == "" or lines[i] == "": del lines[i] continue res = cell_re.match(lines[i]) if res: val = res.groups() - lines[i] = f'' + lines[i] = ( + f'' + ) res = row_re.match(lines[i]) if res: val = res.groups() - lines[i] = f'' + lines[i] = ( + f'' + ) i = i + 1 @@ -113,15 +128,24 @@ def table_update(lines): res = col_re.match(col_info[i]) if res: val = res.groups() - col_info[i] = '' \ - % ( align_table[val[0]], align_vertical[val[1]], bool_table(val[2]), bool_table(val[3]), val[4],val[5]) + col_info[i] = ( + '' + % ( + align_table[val[0]], + align_vertical[val[1]], + bool_table(val[2]), + bool_table(val[3]), + val[4], + val[5], + ) + ) return lines[:2] + col_info + lines[2:] -supported_versions = ["1.1.6fix3","1.1.6fix4","1.1"] +supported_versions = ["1.1.6fix3", "1.1.6fix4", "1.1"] convert = [[218, [update_tabular]]] -revert = [] +revert = [] if __name__ == "__main__": diff --git a/lib/lyx2lyx/lyx_1_2.py b/lib/lyx2lyx/lyx_1_2.py index 036dca7573..8fd4f3592d 100644 --- a/lib/lyx2lyx/lyx_1_2.py +++ b/lib/lyx2lyx/lyx_1_2.py @@ -16,21 +16,30 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -""" Convert files to the file format generated by lyx 1.2""" +"""Convert files to the file format generated by lyx 1.2""" import re -from parser_tools import find_token, find_token_backwards, \ - find_tokens, find_tokens_backwards, \ - find_beginning_of, find_end_of, find_re, \ - is_nonempty_line, find_nonempty_line, \ - get_value, check_token +from parser_tools import ( + find_token, + find_token_backwards, + find_tokens, + find_tokens_backwards, + find_beginning_of, + find_end_of, + find_re, + is_nonempty_line, + find_nonempty_line, + get_value, + check_token, +) #################################################################### # Private helper functions + def get_layout(line, default_layout): - " Get layout, if empty return the default layout." + "Get layout, if empty return the default layout." tokens = line.split() if len(tokens) > 1: return tokens[1] @@ -38,12 +47,13 @@ def get_layout(line, default_layout): def get_paragraph(lines, i, format): - " Finds the paragraph that contains line i." + "Finds the paragraph that contains line i." begin_layout = "\\layout" while i != -1: i = find_tokens_backwards(lines, ["\\end_inset", begin_layout], i) - if i == -1: return -1 + if i == -1: + return -1 if check_token(lines[i], begin_layout): return i i = find_beginning_of_inset(lines, i) @@ -51,7 +61,7 @@ def get_paragraph(lines, i, format): def get_next_paragraph(lines, i, format): - " Finds the paragraph after the paragraph that contains line i." + "Finds the paragraph after the paragraph that contains line i." tokens = ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"] while i != -1: @@ -63,78 +73,79 @@ def get_next_paragraph(lines, i, format): def find_beginning_of_inset(lines, i): - " Find beginning of inset, where lines[i] is included." + "Find beginning of inset, where lines[i] is included." return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset") def find_end_of_inset(lines, i): - r" Finds the matching \end_inset" + r"Finds the matching \end_inset" return find_end_of(lines, i, "\\begin_inset", "\\end_inset") def find_end_of_tabular(lines, i): - " Finds the matching end of tabular." + "Finds the matching end of tabular." return find_end_of(lines, i, " i+1: + i2 = i2 + 1 + if i2 > i + 1: j2 = get_next_paragraph(lines, j + 1, document.format + 1) - lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1)) + lines[j2:j2] = ["\\end_deeper "] * (i2 - (i + 1)) - new = floats[floattype]+[""] + new = floats[floattype] + [""] # Check if the float is floatingfigure k = find_re(lines, pextra_type3_rexp, i, j) @@ -173,12 +184,14 @@ def remove_oldfloat(document): 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 = [ + "\\begin_inset Wrap figure", + 'width "%s"' % width, + "collapsed false", + "", + ] - new = new+lines[i2:j]+["\\end_inset ", ""] + 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 @@ -197,20 +210,21 @@ def remove_oldfloat(document): flag = 1 new.append("") if token == "\\lang": - new.append(token+" "+ document.language) + new.append(token + " " + document.language) else: - new.append(token+" default ") + new.append(token + " default ") - lines[i:j+1] = new - i = i+1 + 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(document): - " Remove pextra token." + "Remove pextra token." lines = document.body i = 0 flag = 0 @@ -221,17 +235,17 @@ def remove_pextra(document): # 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] + 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 + lines[i] = re.sub(pextra_rexp, "\\leftindent " + width + " ", lines[i]) + i = i + 1 continue # handle \pextra_type 2 (minipage) @@ -239,30 +253,31 @@ def remove_pextra(document): 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" - ] + start = [ + "\\begin_inset Minipage", + "position " + position, + "inner_position 0", + 'height "0pt"', + 'width "%s"' % width, + "collapsed false", + ] if flag: flag = 0 if hfill: - start = ["",r"\hfill",""]+start + start = ["", r"\hfill", ""] + start else: - start = ['\\layout %s' % document.default_layout,''] + start + start = ["\\layout %s" % document.default_layout, ""] + start - j0 = find_token_backwards(lines,"\\layout", i-1) + j0 = find_token_backwards(lines, "\\layout", i - 1) j = get_next_paragraph(lines, i, document.format + 1) count = 0 while True: # collect more paragraphs to the minipage - count = count+1 + count = count + 1 if j == -1 or not check_token(lines[j], "\\layout"): break - i = find_re(lines, pextra_type2_rexp2, j+1) + i = find_re(lines, pextra_type2_rexp2, j + 1) if i == -1: break mo = pextra_rexp.search(lines[i]) @@ -272,60 +287,72 @@ def remove_pextra(document): flag = 1 break lines[i] = re.sub(pextra_rexp, "", lines[i]) - j = find_tokens(lines, ["\\layout", "\\end_float"], i+1) + 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 + lines[j0:j] = start + mid + end + i = i + 1 def is_empty(lines): - " Are all the lines empty?" + "Are all the lines empty?" return list(filter(is_nonempty_line, lines)) == [] -move_rexp = re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun|end_deeper)") +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.*)") def remove_oldert(document): - " Remove old ERT inset." - ert_begin = ["\\begin_inset ERT", - "status Collapsed", - "", - '\\layout %s' % document.default_layout, - ""] + "Remove old ERT inset." + ert_begin = [ + "\\begin_inset ERT", + "status Collapsed", + "", + "\\layout %s" % document.default_layout, + "", + ] lines = document.body i = 0 while True: i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i) if i == -1: break - j = i+1 + j = i + 1 while True: # \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) + 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 + 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 + 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 = [r'\layout %s' % document.default_layout, "", ""] + new = [r"\layout %s" % document.default_layout, "", ""] - k = i+1 + k = i + 1 while True: k2 = find_re(lines, ert_rexp, k, j) inset = hfill = specialchar = 0 @@ -336,13 +363,13 @@ def remove_oldert(document): elif check_token(lines[k2], "\\hfill"): hfill = 1 del lines[k2] - j = j-1 + 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 + k2 = k2 + 1 tmp = [] for line in lines[k:k2]: @@ -361,19 +388,21 @@ def remove_oldert(document): 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]+" " + lines[i - 1] = lines[i - 1] + " " else: - new = new+[" "] + new = new + [" "] else: - new = new+ert_begin+tmp+["\\end_inset ", ""] + 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 + 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 + k = k + 1 new.append("") elif hfill: new = new + ["\\hfill", ""] @@ -382,19 +411,19 @@ def remove_oldert(document): 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 + lines[i - 1] = lines[i - 1] + specialchar_str new = [""] else: - new = new+[specialchar_str, ""] + new = new + [specialchar_str, ""] k = k2 else: break - new = new+new2 + new = new + new2 if not check_token(lines[j], "\\latex "): - new = new+[""]+[lines[j]] - lines[i:j+1] = new - i = i+1 + new = new + [""] + [lines[j]] + lines[i : j + 1] = new + i = i + 1 # Delete remaining "\latex xxx" tokens i = 0 @@ -406,7 +435,7 @@ def remove_oldert(document): def remove_oldertinset(document): - " ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future." + "ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future." lines = document.body i = 0 while True: @@ -414,34 +443,34 @@ def remove_oldertinset(document): if i == -1: break j = find_end_of_inset(lines, i) - k = find_token(lines, "\\layout", i+1) + k = find_token(lines, "\\layout", i + 1) l = get_paragraph(lines, i, document.format + 1) - if lines[k] == lines[l]: # same layout - k = k+1 + if lines[k] == lines[l]: # same layout + k = k + 1 new = lines[k:j] - lines[i:j+1] = new - i = i+1 + lines[i : j + 1] = new + i = i + 1 def is_ert_paragraph(document, i): - " Is this a ert paragraph? " + "Is this a ert paragraph?" lines = document.body if not check_token(lines[i], "\\layout"): return 0 if not document.is_default_layout(get_layout(lines[i], document.default_layout)): return 0 - i = find_nonempty_line(lines, i+1) + 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) + k = find_nonempty_line(lines, j + 1) return check_token(lines[k], "\\layout") def combine_ert(document): - " Combine ERT paragraphs." + "Combine ERT paragraphs." lines = document.body i = 0 while True: @@ -452,41 +481,41 @@ def combine_ert(document): count = 0 text = [] while is_ert_paragraph(document, 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) + 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) + j = find_token(lines, "\\layout", i + 1) lines[j:k] = text - i = i+1 + i = i + 1 oldunits = ["pt", "cm", "in", "text%", "col%"] + def get_length(lines, name, start, end): - " Get lenght." + "Get lenght." i = find_token(lines, name, start, end) if i == -1: return "" x = lines[i].split() - return x[2]+oldunits[int(x[1])] + return x[2] + oldunits[int(x[1])] def write_attribute(x, token, value): - " Write attribute." + "Write attribute." if value != "": - x.append("\t"+token+" "+value) + x.append("\t" + token + " " + value) def remove_figinset(document): - " Remove figinset." + "Remove figinset." lines = document.body i = 0 while True: @@ -495,26 +524,26 @@ def remove_figinset(document): break j = find_end_of_inset(lines, i) - if ( len(lines[i].split()) > 2 ): - lyxwidth = lines[i].split()[3]+"pt" - lyxheight = lines[i].split()[4]+"pt" + if len(lines[i].split()) > 2: + lyxwidth = lines[i].split()[3] + "pt" + lyxheight = lines[i].split()[4] + "pt" else: lyxwidth = "" lyxheight = "" - filename = get_value(lines, "file", i+1, j) + filename = get_value(lines, "file", i + 1, j) - width = get_length(lines, "width", 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) + 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 + flags = get_value(lines, "flags", i + 1, j) + x = int(flags) % 4 if x == 1: display = "monochrome" elif x == 2: @@ -523,13 +552,13 @@ def remove_figinset(document): display = "color" subcaptionText = "" - subcaptionLine = find_token(lines, "subcaption", i+1, j) + subcaptionLine = find_token(lines, "subcaption", i + 1, j) if subcaptionLine != -1: subcaptionText = lines[subcaptionLine][11:] if subcaptionText != "": - subcaptionText = '"'+subcaptionText+'"' + subcaptionText = '"' + subcaptionText + '"' - k = find_token(lines, "subfigure", i+1,j) + k = find_token(lines, "subfigure", i + 1, j) if k == -1: subcaption = 0 else: @@ -552,15 +581,16 @@ def remove_figinset(document): write_attribute(new, "lyxwidth", lyxwidth) write_attribute(new, "lyxheight", lyxheight) new = new + ["\\end_inset"] - lines[i:j+1] = new + lines[i : j + 1] = new attr_re = re.compile(r' \w*="(false|0|)"') -line_re = re.compile(r'<(features|column|row|cell)') +line_re = re.compile(r"<(features|column|row|cell)") + def update_tabular(document): - " Convert tabular format 2 to 3." - regexp = re.compile(r'^\\begin_inset\s+Tabular') + "Convert tabular format 2 to 3." + regexp = re.compile(r"^\\begin_inset\s+Tabular") lines = document.body i = 0 while True: @@ -577,7 +607,7 @@ def update_tabular(document): if line_re.match(lines[k]): lines[k] = re.sub(attr_re, "", lines[k]) - i = i+1 + i = i + 1 ## @@ -597,17 +627,19 @@ def update_tabular(document): false = 0 true = 1 + class row: - " Simple data structure to deal with long table info." + "Simple data structure to deal with long table info." + 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 + 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): - " Does row has LTFoot?" + "Does row has LTFoot?" for row_ in row_info: if row_.endfoot: return true @@ -615,11 +647,11 @@ def haveLTFoot(row_info): def setHeaderFooterRows(hr, fhr, fr, lfr, rows_, row_info): - " Set Header/Footer rows." + "Set Header/Footer rows." endfirsthead_empty = false endlastfoot_empty = false # set header info - while (hr > 0): + while hr > 0: hr = hr - 1 row_info[hr].endhead = true @@ -671,8 +703,17 @@ def setHeaderFooterRows(hr, fhr, fr, lfr, rows_, row_info): 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: + 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): @@ -682,18 +723,24 @@ def setHeaderFooterRows(hr, fhr, fr, lfr, rows_, row_info): def insert_attribute(lines, i, attribute): - " Insert attribute in lines[i]." - last = lines[i].find('>') - lines[i] = lines[i][:last] + ' ' + attribute + lines[i][last:] + "Insert attribute in lines[i]." + last = lines[i].find(">") + 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*")') +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(document): - " Update longtables to new format." - regexp = re.compile(r'^\\begin_inset\s+Tabular') + "Update longtables to new format." + regexp = re.compile(r"^\\begin_inset\s+Tabular") body = document.body i = 0 while True: @@ -709,7 +756,7 @@ def update_longtables(document): rows = int(rows_re.search(body[i]).group(1)) i = i + 1 - i = find_token(body, '