From 3faefeea73127c5a215d0248486a55a90bb1f253 Mon Sep 17 00:00:00 2001 From: =?utf8?q?G=C3=BCnter=20Milde?= Date: Mon, 3 Jun 2019 16:45:05 +0200 Subject: [PATCH] lyx2lyx refactoring and minor fixes. --- lib/lyx2lyx/lyx2lyx_tools.py | 63 ++++++++++++------------------------ lib/lyx2lyx/lyx_2_3.py | 34 +++++++------------ lib/lyx2lyx/parser_tools.py | 24 ++++++-------- 3 files changed, 40 insertions(+), 81 deletions(-) diff --git a/lib/lyx2lyx/lyx2lyx_tools.py b/lib/lyx2lyx/lyx2lyx_tools.py index 97745b1dfd..9c4fe0bb0b 100644 --- a/lib/lyx2lyx/lyx2lyx_tools.py +++ b/lib/lyx2lyx/lyx2lyx_tools.py @@ -578,63 +578,40 @@ def insert_document_option(document, option): "Insert _option_ as a document option." # Find \options in the header - options_line = find_token(document.header, "\\options", 0) - + i = find_token(document.header, "\\options", 0) # if the options does not exists add it after the textclass - if options_line == -1: - textclass_line = find_token(document.header, "\\textclass", 0) - document.header.insert(textclass_line +1, - r"\options %s" % option) + if i == -1: + i = find_token(document.header, "\\textclass", 0) + 1 + document.header.insert(i, r"\options %s" % option) return - - # add it to the end of the options - document.header[options_line] += ",%s" % option + # otherwise append to options + if not is_document_option(document, option): + document.header[i] += ",%s" % option def remove_document_option(document, option): - """ Remove _option_ as a document option. - - It is assumed that option belongs to the \options. - That can be done running is_document_option(document, option).""" - - options_line = find_token(document.header, "\\options", 0) - option_pos = document.header[options_line].find(option) - - # Remove option from \options - comma_before_pos = document.header[options_line].rfind(',', 0, option_pos) - comma_after_pos = document.header[options_line].find(',', option_pos) + """ Remove _option_ as a document option.""" - # if there are no commas then it is the single option - # and the options line should be removed since it will be empty - if comma_before_pos == comma_after_pos == -1: - del document.header[options_line] - return + i = find_token(document.header, "\\options") + options = get_value(document.header, "\\options", i) + options = [op.strip() for op in options.split(',')] - # last option - options = document.header[options_line] - if comma_after_pos == -1: - document.header[options_line] = options[:comma_before_pos].rsplit() - return + # Remove `option` from \options + options = [op for op in options if op != option] - document.header[options_line] = options[comma_before_pos: comma_after_pos] + if options: + document.header[i] = "\\options " + ','.join(options) + else: + del document.header[i] def is_document_option(document, option): "Find if _option_ is a document option" - # Find \options in the header - options_line = find_token(document.header, "\\options", 0) - - # \options is not present in the header - if options_line == -1: - return False - - option_pos = document.header[options_line].find(option) - # option is not present in the \options - if option_pos == -1: - return False + options = get_value(document.header, "\\options") + options = [op.strip() for op in options.split(',')] + return option in options - return True singlepar_insets = [s.strip() for s in u"Argument, Caption Above, Caption Below, Caption Bicaption," diff --git a/lib/lyx2lyx/lyx_2_3.py b/lib/lyx2lyx/lyx_2_3.py index ff9f784c88..6c4797f7f0 100644 --- a/lib/lyx2lyx/lyx_2_3.py +++ b/lib/lyx2lyx/lyx_2_3.py @@ -32,7 +32,8 @@ from parser_tools import (del_token, del_value, del_complete_lines, # find_tokens, find_token_exact, check_token, get_option_value from lyx2lyx_tools import (add_to_preamble, put_cmd_in_ert, revert_font_attrs, - insert_to_preamble, latex_length, revert_language) + insert_to_preamble, latex_length, is_document_option, + insert_document_option, remove_document_option, revert_language) #################################################################### # Private helper functions @@ -149,9 +150,8 @@ def revert_ibranches(document): if j == -1: document.warning("Malformed LyX document! Can't find end of branch " + old) continue - # ourbranches[old] - 1 inverts the selection status of the old branch lines = ["\\branch " + new, - "\\selected " + str(ourbranches[old] - 1)] + "\\selected %d" % (not ourbranches[old])] # these are the old lines telling us color, etc. lines += document.header[i+2 : j+1] document.header[i:i] = lines @@ -1873,35 +1873,23 @@ def revert_allowbreak(document): def convert_mathnumberpos(document): " add the \\math_number_before tag " + i = find_token(document.header, "\\quotes_style") # check if the document uses the class option "leqno" - i = find_token(document.header, "\\options") - k = find_token(document.header, "\\quotes_style") - if 'leqno' in document.header[i]: - document.header.insert(k, "\\math_number_before 1") - # delete the found option - document.header[i] = document.header[i].replace(",leqno", "") - document.header[i] = document.header[i].replace(", leqno", "") - document.header[i] = document.header[i].replace("leqno,", "") - if 'leqno' in document.header[i]: - # then we have leqno as the only option - del document.header[i] + if is_document_option(document, "leqno"): + remove_document_option(document, "leqno") + document.header.insert(i, "\\math_number_before 1") else: - document.header.insert(k, "\\math_number_before 0") + document.header.insert(i, "\\math_number_before 0") def revert_mathnumberpos(document): """Remove \\math_number_before tag, add the document class option leqno if required. """ - math_number_before = get_bool_value(document.header, - '\\math_number_before', delete=True) + math_number_before = get_bool_value(document.header, '\\math_number_before', + delete=True) if math_number_before: - i = find_token(document.header, "\\options") - if i != -1 and 'leqno' not in document.header[i]: - document.header[i] = document.header[i].replace("\\options", "\\options leqno,") - else: - i = find_token(document.header, "\\use_default_options") - document.header.insert(i, "\\options leqno") + insert_document_option(document, "leqno") def convert_mathnumberingname(document): diff --git a/lib/lyx2lyx/parser_tools.py b/lib/lyx2lyx/parser_tools.py index d6be0c213d..bfc4e34c87 100644 --- a/lib/lyx2lyx/parser_tools.py +++ b/lib/lyx2lyx/parser_tools.py @@ -428,25 +428,21 @@ def get_quoted_value(lines, token, start=0, end=0, default="", delete=False): return default return val.strip('"') -bool_values = {True: ("true", "1"), - False: ("false", "0")} + +bool_values = {"true": True, "1": True, + "false": False, "0": False} def get_bool_value(lines, token, start=0, end=0, default=None, delete=False): """ get_bool_value(lines, token, start[[, end], default]) -> string Find the next line that looks like: - token + `token` - Return True if is 1 or "true", False if bool_value + Return True if is 1 or "true", False if is 0 or "false", else `default`. """ - val = get_quoted_value(lines, token, start, end, default, delete) - if val in bool_values[True]: - return True - if val in bool_values[False]: - return False - return default + return bool_values.get(val, default) def set_bool_value(lines, token, value, start=0, end=0): @@ -462,13 +458,11 @@ def set_bool_value(lines, token, value, start=0, end=0): oldvalue = get_bool_value(lines, token, i, i+1) if oldvalue is value: return oldvalue - # Use 0/1 or true/false? + # set to new value if get_quoted_value(lines, token, i, i+1) in ('0', '1'): - value_string = bool_values[value][1] + lines[i] = "%s %d" % (token, value) else: - value_string = bool_values[value][0] - # set to new value - lines[i] = "%s %s" % (token, value_string) + lines[i] = "%s %s" % (token, str(value).lower()) return oldvalue -- 2.39.5