Fix some more lyx2lyx round-trips.

Also restore corrupted seminar example.
Add default return value to parser_tools.is_in_inset().
This commit is contained in:
Günter Milde 2018-01-24 01:02:24 +01:00
parent 6f64e3cb0f
commit a77cfef1c6
5 changed files with 73 additions and 84 deletions

View File

@ -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 \lyxformat 544
\begin_document \begin_document
\begin_header \begin_header
@ -582,13 +582,7 @@ slideframe[
\backslash \backslash
setlength{ setlength{
\backslash \backslash
shadowsize}{1pt}]{shadow}% shadowsize}{1pt}]{shadow}
\end_layout
\begin_layout LyX-Code
\lang english
framecolor "black" backgroundcolor
\end_layout \end_layout
\begin_layout Standard \begin_layout Standard

View File

@ -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 \lyxformat 544
\begin_document \begin_document
\begin_header \begin_header
@ -513,11 +513,7 @@ slideframe[
\backslash \backslash
setlength{ setlength{
\backslash \backslash
shadowsize}{1pt}]{shadow}% shadowsize}{1pt}]{shadow}
\end_layout
\begin_layout LyX-Code
framecolor "black" backgroundcolor "none"
\end_layout \end_layout
\begin_layout Standard \begin_layout Standard

View File

@ -514,8 +514,6 @@ def convert_ulinelatex(document):
" Remove preamble code for \\uline font attribute. " " Remove preamble code for \\uline font attribute. "
del_complete_lines(document.preamble, del_complete_lines(document.preamble,
['% Added by lyx2lyx']+ulinelatex_preamble) ['% Added by lyx2lyx']+ulinelatex_preamble)
for line in document.preamble:
print line
def revert_ulinelatex(document): def revert_ulinelatex(document):
" Add preamble code for \\uline font attribute in citations. " " Add preamble code for \\uline font attribute in citations. "
@ -835,6 +833,9 @@ def revert_suppress_date(document):
del document.header[i] del document.header[i]
mhchem_preamble = [r"\PassOptionsToPackage{version=3}{mhchem}",
r"\usepackage{mhchem}"]
def convert_mhchem(document): def convert_mhchem(document):
"Set mhchem to off for versions older than 1.6.x" "Set mhchem to off for versions older than 1.6.x"
if document.initial_format < 277: if document.initial_format < 277:
@ -852,47 +853,44 @@ def convert_mhchem(document):
# pre-1.5.x document # pre-1.5.x document
i = find_token(document.header, "\\use_amsmath", 0) i = find_token(document.header, "\\use_amsmath", 0)
if i == -1: 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 return
document.header.insert(i + 1, "\\use_mhchem %d" % mhchem) 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): def revert_mhchem(document):
"Revert mhchem loading to preamble code" "Revert mhchem loading to preamble code."
mhchem = "off" mhchem = get_value(document.header, "\\use_mhchem", delete=True)
i = find_token(document.header, "\\use_mhchem", 0) try:
if i == -1: mhchem = int(mhchem)
document.warning("Malformed LyX document: Could not find mhchem setting.") except ValueError:
mhchem = "auto" document.warning("Malformed LyX document: "
else: "Could not find mhchem setting.")
val = get_value(document.header, "\\use_mhchem", i) mhchem = 1 # "auto"
if val == "1": # mhchem in {0: "off", 1: "auto", 2: "on"}
mhchem = "auto"
elif val == "2":
mhchem = "on"
del document.header[i]
if mhchem == "off": if mhchem == 1: # "auto"
# don't load case
return
if mhchem == "auto":
i = 0 i = 0
while True: while i != 1 and mhchem == 1:
i = find_token(document.body, "\\begin_inset Formula", i) i = find_token(document.body, "\\begin_inset Formula", i)
if i == -1: j = find_end_of_inset(document.body, i)
break if j == -1:
line = document.body[i] break
if line.find("\\ce{") != -1 or line.find("\\cf{") != -1: if (True for line in document.body[i:j]
mhchem = "on" if r"\ce{" in line or r"\cf{" in line):
break mhchem = 2
break
i += 1 i += 1
if mhchem == "on": if (mhchem == 2 # on
pre = ["\\PassOptionsToPackage{version=3}{mhchem}", and find_token(document.preamble, r"\usepackage{mhchem}") == -1):
"\\usepackage{mhchem}"] insert_to_preamble(document, mhchem_preamble)
insert_to_preamble(document, pre)
def revert_fontenc(document): def revert_fontenc(document):
@ -1681,12 +1679,10 @@ def revert_nameref(document):
i += 1 i += 1
# Make sure it is actually in an inset! # Make sure it is actually in an inset!
# A normal line could begin with "LatexCommand nameref"! # A normal line could begin with "LatexCommand nameref"!
val = is_in_inset(document.body, cmdloc, \ stins, endins = is_in_inset(document.body, cmdloc,
"\\begin_inset CommandInset ref") "\\begin_inset CommandInset ref")
if not val: if endins == -1:
continue continue
stins, endins = val
# ok, so it is in an InsetRef # ok, so it is in an InsetRef
refline = find_token(document.body, "reference", stins, endins) refline = find_token(document.body, "reference", stins, endins)
if refline == -1: if refline == -1:
@ -1716,10 +1712,9 @@ def remove_Nameref(document):
break break
cmdloc = i cmdloc = i
i += 1 i += 1
# Make sure it is actually in an inset! # Make sure it is actually in an inset!
val = is_in_inset(document.body, cmdloc, \ val = is_in_inset(document.body, cmdloc,
"\\begin_inset CommandInset ref") "\\begin_inset CommandInset ref", default=False)
if not val: if not val:
continue continue
document.body[cmdloc] = "LatexCommand nameref" document.body[cmdloc] = "LatexCommand nameref"

View File

@ -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, # insert_to_preamble, latex_length, revert_flex_inset,
# revert_font_attrs, hex2ratio, str2bool # 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_end_of_inset, find_end_of_layout, find_nonempty_line, find_re,
find_token, find_token_backwards, get_containing_layout, find_token, find_token_backwards, get_containing_layout,
get_value, check_token) get_value, is_in_inset)
#################################################################### ####################################################################
# Private helper functions # Private helper functions
@ -1253,15 +1254,21 @@ def revert_textcolor(document):
def convert_colorbox(document): def convert_colorbox(document):
" adds color settings for boxes " "Add color settings for boxes."
i = 1
i = 0
while True: while True:
i = find_token(document.body, "shadowsize", i) i = find_token(document.body, "shadowsize", i)
if i == -1: if i == -1:
return return
document.body[i+1:i+1] = ['framecolor "black"', 'backgroundcolor "none"'] # check whether this is really a LyX Box setting
i = i + 3 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): def revert_colorbox(document):

View File

@ -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_deeper is returned, else
the position of the last \end_layout. the position of the last \end_layout.
is_in_inset(lines, i, inset): is_in_inset(lines, i, inset, default=(-1,-1)):
Checks if line i is in an inset of the given type. Check if line i is in an inset of the given type.
If so, returns starting and ending lines. Otherwise, If so, returns starting and ending lines. Otherwise,
returns False. return default.
Example: Example:
is_in_inset(document.body, i, "\\begin_inset Tabular") 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 it returns the line on which the table begins and the one
on which it ends. Note that this pair will evaulate to on which it ends. Note that this pair will evaulate to
boolean True, so boolean True, so
if is_in_inset(...): if is_in_inset(..., default=False):
will do what you expect. will do what you expect.
get_containing_inset(lines, i): 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") return find_end_of(lines, i, "\\begin_layout", "\\end_layout")
def is_in_inset(lines, i, inset): def is_in_inset(lines, i, inset, default=(-1,-1)):
''' """
Checks if line i is in an inset of the given type. Check if line i is in an inset of the given type.
If so, returns starting and ending lines. If so, return starting and ending lines, otherwise `default`.
Otherwise, returns False.
Example: Example:
is_in_inset(document.body, i, "\\begin_inset Tabular") is_in_inset(document.body, i, "\\begin_inset Tabular")
returns False unless i is within a table. If it is, then returns (-1,-1) if `i` is not within a "Tabular" inset (i.e. a table).
it returns the line on which the table begins and the one 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 on which it ends. Note that this pair will evaulate to
boolean True, so boolean True, so
if is_in_inset(...): if is_in_inset(..., default=False):
will do what you expect. will do what you expect.
''' """
defval = (-1, -1) start = find_token_backwards(lines, inset, i)
stins = find_token_backwards(lines, inset, i) if start == -1:
if stins == -1: return default
return defval end = find_end_of_inset(lines, start)
endins = find_end_of_inset(lines, stins) if end < i: # this includes the notfound case.
# note that this includes the notfound case. return default
if endins < i: return (start, end)
return defval
return (stins, endins)
def get_containing_inset(lines, i): def get_containing_inset(lines, i):