Fix gloss, glossgroup and forest reversion routines.

These were using lyx2latex, assuming the result can be used in verbatim
insets as is, which is not the case (mostly due to \\backslash).

A new lyx2verbatim function is now used instead. Also, ERT insets are
dissolved in the gloss reversion process.
This commit is contained in:
Juergen Spitzmueller 2015-12-25 12:00:56 +01:00
parent e3cd6db4a7
commit c76d91bb47
2 changed files with 41 additions and 15 deletions

View File

@ -52,6 +52,11 @@ lyx2latex(document, lines):
to LaTeX. We do the best we can and return a string containing to LaTeX. We do the best we can and return a string containing
the translated material. the translated material.
lyx2verbatim(document, lines):
Here, lines is a list of lines of LyX material we want to convert
to verbatim material (used in ERT an the like). We do the best we
can and return a string containing the translated material.
latex_length(slen): latex_length(slen):
Convert lengths (in LyX form) to their LaTeX representation. Returns Convert lengths (in LyX form) to their LaTeX representation. Returns
(bool, length), where the bool tells us if it was a percentage, and (bool, length), where the bool tells us if it was a percentage, and
@ -133,7 +138,7 @@ def put_cmd_in_ert(arg):
return ret return ret
def get_ert(lines, i): def get_ert(lines, i, verbatim = False):
'Convert an ERT inset into LaTeX.' 'Convert an ERT inset into LaTeX.'
if not lines[i].startswith("\\begin_inset ERT"): if not lines[i].startswith("\\begin_inset ERT"):
return "" return ""
@ -156,7 +161,7 @@ def get_ert(lines, i):
elif lines[i] == "\\end_layout": elif lines[i] == "\\end_layout":
while i + 1 < j and lines[i+1] == "": while i + 1 < j and lines[i+1] == "":
i = i + 1 i = i + 1
elif lines[i] == "\\backslash": elif lines[i] == "\\backslash" and not verbatim:
ret = ret + "\\" ret = ret + "\\"
else: else:
ret = ret + lines[i] ret = ret + lines[i]
@ -270,6 +275,15 @@ def lyx2latex(document, lines):
return content return content
def lyx2verbatim(document, lines):
'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)
return content
def latex_length(slen): def latex_length(slen):
''' '''
Convert lengths to their LaTeX representation. Returns (bool, length), Convert lengths to their LaTeX representation. Returns (bool, length),

View File

@ -30,8 +30,8 @@ import sys, os
# find_token_backwards, is_in_inset, get_value, get_quoted_value, \ # find_token_backwards, is_in_inset, get_value, get_quoted_value, \
# del_token, check_token, get_option_value # del_token, check_token, get_option_value
from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert, lyx2latex, \ from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert, get_ert, lyx2latex, \
length_in_bp lyx2verbatim, length_in_bp
# 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
@ -842,11 +842,7 @@ def revert_forest(document):
add_to_preamble(document, ["\\usepackage{forest}"]) add_to_preamble(document, ["\\usepackage{forest}"])
document.body[i:j + 1] = ["\\begin_inset ERT", "status collapsed", "", document.body[i:j + 1] = put_cmd_in_ert("\\begin{forest}" + content + "\\end{forest}")
"\\begin_layout Plain Layout", "", "\\backslash",
"begin{forest}", "\\end_layout", "", "\\begin_layout Plain Layout",
content, "\\end_layout", "", "\\begin_layout Plain Layout",
"\\backslash", "end{forest}", "", "\\end_layout", "", "\\end_inset"]
# no need to reset i # no need to reset i
@ -869,7 +865,7 @@ def revert_glossgroup(document):
beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i) beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
endPlain = find_end_of_layout(document.body, beginPlain) endPlain = find_end_of_layout(document.body, beginPlain)
content = lyx2latex(document, document.body[beginPlain : endPlain]) content = lyx2verbatim(document, document.body[beginPlain : endPlain])
document.body[i:j + 1] = ["{", "", content, "", "}"] document.body[i:j + 1] = ["{", "", content, "", "}"]
# no need to reset i # no need to reset i
@ -904,7 +900,7 @@ def revert_newgloss(document):
i += 1 i += 1
continue continue
argendPlain = find_end_of_inset(document.body, argbeginPlain) argendPlain = find_end_of_inset(document.body, argbeginPlain)
argcontent = lyx2latex(document, document.body[argbeginPlain : argendPlain - 2]) argcontent = lyx2verbatim(document, document.body[argbeginPlain : argendPlain - 2])
document.body[j:j] = ["", "\\begin_layout Plain Layout","\\backslash", "glt ", document.body[j:j] = ["", "\\begin_layout Plain Layout","\\backslash", "glt ",
argcontent, "\\end_layout"] argcontent, "\\end_layout"]
@ -917,11 +913,27 @@ def revert_newgloss(document):
beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i) beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
endPlain = find_end_of_layout(document.body, beginPlain) endPlain = find_end_of_layout(document.body, beginPlain)
content = lyx2latex(document, document.body[beginPlain : endPlain]) content = lyx2verbatim(document, document.body[beginPlain : endPlain])
document.body[beginPlain + 1:endPlain] = [content] document.body[beginPlain + 1:endPlain] = [content]
i = beginPlain + 1 i = beginPlain + 1
# Dissolve ERT insets
for glosse in glosses:
i = 0
while True:
i = find_token(document.body, "\\begin_inset ERT", i)
if i == -1:
return
j = find_end_of_inset(document.body, i)
if j == -1:
document.warning("Malformed LyX document: Can't find end of ERT inset")
i += 1
continue
ert = get_ert(document.body, i, True)
document.body[i:j+1] = [ert]
i = i + 1
def convert_newgloss(document): def convert_newgloss(document):
" Converts Glosse insets (Linguistics module) to the new format " " Converts Glosse insets (Linguistics module) to the new format "