Simplify lyx2lyx code a bit, and correct a mistake in my previous

patch. Also fix some escaping problems (\end instead of \\end).

My testing revealed some other problems with this routine: stray
\noindent tags popping up here and there. I don't have time to fix
it right now but will try to come back to it.
This commit is contained in:
Richard Heck 2015-11-25 00:37:36 -05:00
parent 13c6350155
commit 2c602ab051

View File

@ -818,35 +818,25 @@ def revert_verbatim(document, starred = False):
" Revert verbatim environments completely to TeX-code. " " Revert verbatim environments completely to TeX-code. "
i = 0 i = 0
consecutive = False consecutive = False
subst_end = ['\end_layout', '', '\\begin_layout Plain Layout',
'\end_layout', '',
'\\begin_layout Plain Layout', '', '',
'\\backslash', '',
'end{verbatim}',
'\\end_layout', '', '\\end_inset',
'', '', '\\end_layout']
subst_begin = ['\\begin_layout Standard', '\\noindent',
'\\begin_inset ERT', 'status open', '',
'\\begin_layout Plain Layout', '', '', '\\backslash',
'begin{verbatim}',
'\\end_layout', '', '\\begin_layout Plain Layout', '']
if starred:
subst_end = ['\end_layout', '', '\\begin_layout Plain Layout',
'\end_layout', '',
'\\begin_layout Plain Layout', '', '',
'\\backslash', '',
'end{verbatim*}',
'\\end_layout', '', '\\end_inset',
'', '', '\\end_layout']
subst_begin = ['\\begin_layout Standard', '\\noindent',
'\\begin_inset ERT', 'status open', '',
'\\begin_layout Plain Layout', '', '', '\\backslash',
'begin{verbatim*}',
'\\end_layout', '', '\\begin_layout Plain Layout', '']
layout_name = "Verbatim" layout_name = "Verbatim"
latex_name = "verbatim"
if starred: if starred:
layout_name = "Verbatim*" layout_name = "Verbatim*"
latex_name = "verbatim*"
subst_end = ['\\end_layout', '', '\\begin_layout Plain Layout',
'\\end_layout', '',
'\\begin_layout Plain Layout', '', '',
'\\backslash', '',
'end{%s}' % (latex_name),
'\\end_layout', '', '\\end_inset',
'', '', '\\end_layout']
subst_begin = ['\\begin_layout Standard', '\\noindent',
'\\begin_inset ERT', 'status open', '',
'\\begin_layout Plain Layout', '', '', '\\backslash',
'begin{%s}' % (latex_name),
'\\end_layout', '', '\\begin_layout Plain Layout', '']
while 1: while 1:
i = find_token(document.body, "\\begin_layout %s" % (layout_name), i) i = find_token(document.body, "\\begin_layout %s" % (layout_name), i)
@ -868,24 +858,29 @@ def revert_verbatim(document, starred = False):
break break
m = find_end_of_inset(document.body, n) m = find_end_of_inset(document.body, n)
del(document.body[m:m+1]) del(document.body[m:m+1])
document.body[n:n+1] = ['\end_layout', '', '\\begin_layout Plain Layout'] document.body[n:n+1] = ['\\end_layout', '', '\\begin_layout Plain Layout']
l += 1 l += 1
# we deleted a line, so the end of the inset moved forward. # we deleted a line, so the end of the inset moved forward.
# FIXME But we also added some lines, didn't we? I think this
# should be j += 1.
j -= 1 j -= 1
# consecutive verbatim environments need to be connected # consecutive verbatim environments need to be connected
k = find_token(document.body, "\\begin_layout Verbatim", j) k = find_token(document.body, "\\begin_layout %s" % (layout_name), j)
if k == j + 2 and consecutive == False: if k == j + 2 and consecutive == False:
consecutive = True consecutive = True
document.body[j:j+1] = ['\end_layout', '', '\\begin_layout Plain Layout'] document.body[j:j+1] = ['\\end_layout', '', '\\begin_layout Plain Layout']
document.body[i:i+1] = subst_begin document.body[i:i+1] = subst_begin
continue continue
if k == j + 2 and consecutive == True: if k == j + 2 and consecutive == True:
document.body[j:j+1] = ['\end_layout', '', '\\begin_layout Plain Layout'] document.body[j:j+1] = ['\\end_layout', '', '\\begin_layout Plain Layout']
del(document.body[i:i+1]) del(document.body[i:i+1])
continue continue
if k != j + 2 and consecutive == True: if k != j + 2 and consecutive == True:
document.body[j:j+1] = subst_end document.body[j:j+1] = subst_end
# the next paragraph must not be indented # the next paragraph must not be indented
# FIXME This seems to be causing problems, because of the
# hardcoded use of 19. We should figure out exactly where
# this needs to go by searching for the right tag.
document.body[j+19:j+19] = ['\\noindent'] document.body[j+19:j+19] = ['\\noindent']
del(document.body[i:i+1]) del(document.body[i:i+1])
consecutive = False consecutive = False
@ -893,6 +888,9 @@ def revert_verbatim(document, starred = False):
else: else:
document.body[j:j+1] = subst_end document.body[j:j+1] = subst_end
# the next paragraph must not be indented # the next paragraph must not be indented
# FIXME This seems to be causing problems, because of the
# hardcoded use of 19. We should figure out exactly where
# this needs to go by searching for the right tag.
document.body[j+19:j+19] = ['\\noindent'] document.body[j+19:j+19] = ['\\noindent']
document.body[i:i+1] = subst_begin document.body[i:i+1] = subst_begin