mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-12-22 13:18:28 +00:00
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:
parent
e3cd6db4a7
commit
c76d91bb47
@ -52,6 +52,11 @@ lyx2latex(document, lines):
|
||||
to LaTeX. We do the best we can and return a string containing
|
||||
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):
|
||||
Convert lengths (in LyX form) to their LaTeX representation. Returns
|
||||
(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
|
||||
|
||||
|
||||
def get_ert(lines, i):
|
||||
def get_ert(lines, i, verbatim = False):
|
||||
'Convert an ERT inset into LaTeX.'
|
||||
if not lines[i].startswith("\\begin_inset ERT"):
|
||||
return ""
|
||||
@ -156,7 +161,7 @@ def get_ert(lines, i):
|
||||
elif lines[i] == "\\end_layout":
|
||||
while i + 1 < j and lines[i+1] == "":
|
||||
i = i + 1
|
||||
elif lines[i] == "\\backslash":
|
||||
elif lines[i] == "\\backslash" and not verbatim:
|
||||
ret = ret + "\\"
|
||||
else:
|
||||
ret = ret + lines[i]
|
||||
@ -270,6 +275,15 @@ def lyx2latex(document, lines):
|
||||
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):
|
||||
'''
|
||||
Convert lengths to their LaTeX representation. Returns (bool, length),
|
||||
|
@ -30,8 +30,8 @@ import sys, os
|
||||
# find_token_backwards, is_in_inset, get_value, get_quoted_value, \
|
||||
# del_token, check_token, get_option_value
|
||||
|
||||
from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert, lyx2latex, \
|
||||
length_in_bp
|
||||
from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert, get_ert, lyx2latex, \
|
||||
lyx2verbatim, length_in_bp
|
||||
# insert_to_preamble, latex_length, revert_flex_inset, \
|
||||
# revert_font_attrs, hex2ratio, str2bool
|
||||
|
||||
@ -842,11 +842,7 @@ def revert_forest(document):
|
||||
|
||||
add_to_preamble(document, ["\\usepackage{forest}"])
|
||||
|
||||
document.body[i:j + 1] = ["\\begin_inset ERT", "status collapsed", "",
|
||||
"\\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"]
|
||||
document.body[i:j + 1] = put_cmd_in_ert("\\begin{forest}" + content + "\\end{forest}")
|
||||
# no need to reset i
|
||||
|
||||
|
||||
@ -869,7 +865,7 @@ def revert_glossgroup(document):
|
||||
|
||||
beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
|
||||
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, "", "}"]
|
||||
# no need to reset i
|
||||
@ -904,7 +900,7 @@ def revert_newgloss(document):
|
||||
i += 1
|
||||
continue
|
||||
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 ",
|
||||
argcontent, "\\end_layout"]
|
||||
@ -917,11 +913,27 @@ def revert_newgloss(document):
|
||||
|
||||
beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
|
||||
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]
|
||||
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):
|
||||
" Converts Glosse insets (Linguistics module) to the new format "
|
||||
|
Loading…
Reference in New Issue
Block a user