diff --git a/lib/examples/fr/seminar.lyx b/lib/examples/fr/seminar.lyx index c851113992..5fc80445c7 100644 --- a/lib/examples/fr/seminar.lyx +++ b/lib/examples/fr/seminar.lyx @@ -1,4 +1,4 @@ -#LyX 2.3 created this file. For more info see http://www.lyx.org/ +#LyX 2.4 created this file. For more info see https://www.lyx.org/ \lyxformat 544 \begin_document \begin_header @@ -582,13 +582,7 @@ slideframe[ \backslash setlength{ \backslash -shadowsize}{1pt}]{shadow}% -\end_layout - -\begin_layout LyX-Code - -\lang english - framecolor "black" backgroundcolor +shadowsize}{1pt}]{shadow} \end_layout \begin_layout Standard diff --git a/lib/examples/seminar.lyx b/lib/examples/seminar.lyx index a9936874ed..f5a82c22b2 100644 --- a/lib/examples/seminar.lyx +++ b/lib/examples/seminar.lyx @@ -1,4 +1,4 @@ -#LyX 2.3 created this file. For more info see http://www.lyx.org/ +#LyX 2.4 created this file. For more info see https://www.lyx.org/ \lyxformat 544 \begin_document \begin_header @@ -513,11 +513,7 @@ slideframe[ \backslash setlength{ \backslash -shadowsize}{1pt}]{shadow}% -\end_layout - -\begin_layout LyX-Code - framecolor "black" backgroundcolor "none" +shadowsize}{1pt}]{shadow} \end_layout \begin_layout Standard diff --git a/lib/lyx2lyx/lyx_2_0.py b/lib/lyx2lyx/lyx_2_0.py index eefe0b28ea..3faf944a97 100644 --- a/lib/lyx2lyx/lyx_2_0.py +++ b/lib/lyx2lyx/lyx_2_0.py @@ -514,8 +514,6 @@ def convert_ulinelatex(document): " Remove preamble code for \\uline font attribute. " del_complete_lines(document.preamble, ['% Added by lyx2lyx']+ulinelatex_preamble) - for line in document.preamble: - print line def revert_ulinelatex(document): " Add preamble code for \\uline font attribute in citations. " @@ -835,6 +833,9 @@ def revert_suppress_date(document): del document.header[i] +mhchem_preamble = [r"\PassOptionsToPackage{version=3}{mhchem}", + r"\usepackage{mhchem}"] + def convert_mhchem(document): "Set mhchem to off for versions older than 1.6.x" if document.initial_format < 277: @@ -852,47 +853,44 @@ def convert_mhchem(document): # pre-1.5.x document i = find_token(document.header, "\\use_amsmath", 0) if i == -1: - document.warning("Malformed LyX document: Could not find amsmath os esint setting.") + document.warning("Malformed LyX document: " + "Could not find amsmath or esint setting.") return document.header.insert(i + 1, "\\use_mhchem %d" % mhchem) + # remove LyX-inserted preamble + if mhchem != 0: + del_complete_lines(document.preamble, + ['% Added by lyx2lyx']+mhchem_preamble) def revert_mhchem(document): - "Revert mhchem loading to preamble code" + "Revert mhchem loading to preamble code." - mhchem = "off" - i = find_token(document.header, "\\use_mhchem", 0) - if i == -1: - document.warning("Malformed LyX document: Could not find mhchem setting.") - mhchem = "auto" - else: - val = get_value(document.header, "\\use_mhchem", i) - if val == "1": - mhchem = "auto" - elif val == "2": - mhchem = "on" - del document.header[i] + mhchem = get_value(document.header, "\\use_mhchem", delete=True) + try: + mhchem = int(mhchem) + except ValueError: + document.warning("Malformed LyX document: " + "Could not find mhchem setting.") + mhchem = 1 # "auto" + # mhchem in {0: "off", 1: "auto", 2: "on"} - if mhchem == "off": - # don't load case - return - - if mhchem == "auto": + if mhchem == 1: # "auto" i = 0 - while True: + while i != 1 and mhchem == 1: i = find_token(document.body, "\\begin_inset Formula", i) - if i == -1: - break - line = document.body[i] - if line.find("\\ce{") != -1 or line.find("\\cf{") != -1: - mhchem = "on" - break + j = find_end_of_inset(document.body, i) + if j == -1: + break + if (True for line in document.body[i:j] + if r"\ce{" in line or r"\cf{" in line): + mhchem = 2 + break i += 1 - if mhchem == "on": - pre = ["\\PassOptionsToPackage{version=3}{mhchem}", - "\\usepackage{mhchem}"] - insert_to_preamble(document, pre) + if (mhchem == 2 # on + and find_token(document.preamble, r"\usepackage{mhchem}") == -1): + insert_to_preamble(document, mhchem_preamble) def revert_fontenc(document): @@ -1681,12 +1679,10 @@ def revert_nameref(document): i += 1 # Make sure it is actually in an inset! # A normal line could begin with "LatexCommand nameref"! - val = is_in_inset(document.body, cmdloc, \ - "\\begin_inset CommandInset ref") - if not val: + stins, endins = is_in_inset(document.body, cmdloc, + "\\begin_inset CommandInset ref") + if endins == -1: continue - stins, endins = val - # ok, so it is in an InsetRef refline = find_token(document.body, "reference", stins, endins) if refline == -1: @@ -1716,10 +1712,9 @@ def remove_Nameref(document): break cmdloc = i i += 1 - # Make sure it is actually in an inset! - val = is_in_inset(document.body, cmdloc, \ - "\\begin_inset CommandInset ref") + val = is_in_inset(document.body, cmdloc, + "\\begin_inset CommandInset ref", default=False) if not val: continue document.body[cmdloc] = "LatexCommand nameref" diff --git a/lib/lyx2lyx/lyx_2_2.py b/lib/lyx2lyx/lyx_2_2.py index 1c7f3dc37f..b1b3a728ef 100644 --- a/lib/lyx2lyx/lyx_2_2.py +++ b/lib/lyx2lyx/lyx_2_2.py @@ -34,10 +34,11 @@ from lyx2lyx_tools import (add_to_preamble, put_cmd_in_ert, get_ert, # insert_to_preamble, latex_length, revert_flex_inset, # revert_font_attrs, hex2ratio, str2bool -from parser_tools import (del_complete_lines, +from parser_tools import (check_token, del_complete_lines, find_end_of_inset, find_end_of_layout, find_nonempty_line, find_re, find_token, find_token_backwards, get_containing_layout, - get_value, check_token) + get_value, is_in_inset) + #################################################################### # Private helper functions @@ -1253,15 +1254,21 @@ def revert_textcolor(document): def convert_colorbox(document): - " adds color settings for boxes " - - i = 0 + "Add color settings for boxes." + i = 1 while True: i = find_token(document.body, "shadowsize", i) if i == -1: return - document.body[i+1:i+1] = ['framecolor "black"', 'backgroundcolor "none"'] - i = i + 3 + # check whether this is really a LyX Box setting + start, end = is_in_inset(document.body, i, "\\begin_inset Box") + if (end == -1 or + find_token(document.body, "\\begin_layout", start, i) != -1): + i += 1 + continue + document.body[i+1:i+1] = ['framecolor "black"', + 'backgroundcolor "none"'] + i += 3 def revert_colorbox(document): diff --git a/lib/lyx2lyx/parser_tools.py b/lib/lyx2lyx/parser_tools.py index 44914a90eb..06cb41df91 100644 --- a/lib/lyx2lyx/parser_tools.py +++ b/lib/lyx2lyx/parser_tools.py @@ -113,17 +113,17 @@ find_end_of_sequence(lines, i): the position of the last \end_deeper is returned, else the position of the last \end_layout. -is_in_inset(lines, i, inset): - Checks if line i is in an inset of the given type. +is_in_inset(lines, i, inset, default=(-1,-1)): + Check if line i is in an inset of the given type. If so, returns starting and ending lines. Otherwise, - returns False. + return default. Example: is_in_inset(document.body, i, "\\begin_inset Tabular") - returns False unless i is within a table. If it is, then + returns (-1,-1) unless i is within a table. If it is, then it returns the line on which the table begins and the one on which it ends. Note that this pair will evaulate to boolean True, so - if is_in_inset(...): + if is_in_inset(..., default=False): will do what you expect. get_containing_inset(lines, i): @@ -535,29 +535,26 @@ def find_end_of_layout(lines, i): return find_end_of(lines, i, "\\begin_layout", "\\end_layout") -def is_in_inset(lines, i, inset): - ''' - Checks if line i is in an inset of the given type. - If so, returns starting and ending lines. - Otherwise, returns False. +def is_in_inset(lines, i, inset, default=(-1,-1)): + """ + Check if line i is in an inset of the given type. + If so, return starting and ending lines, otherwise `default`. Example: is_in_inset(document.body, i, "\\begin_inset Tabular") - returns False unless i is within a table. If it is, then - it returns the line on which the table begins and the one + returns (-1,-1) if `i` is not within a "Tabular" inset (i.e. a table). + If it is, then it returns the line on which the table begins and the one on which it ends. Note that this pair will evaulate to boolean True, so - if is_in_inset(...): + if is_in_inset(..., default=False): will do what you expect. - ''' - defval = (-1, -1) - stins = find_token_backwards(lines, inset, i) - if stins == -1: - return defval - endins = find_end_of_inset(lines, stins) - # note that this includes the notfound case. - if endins < i: - return defval - return (stins, endins) + """ + start = find_token_backwards(lines, inset, i) + if start == -1: + return default + end = find_end_of_inset(lines, start) + if end < i: # this includes the notfound case. + return default + return (start, end) def get_containing_inset(lines, i):