# Uncomment only what you need to import, please.
-from parser_tools import (count_pars_in_inset, del_token, find_end_of_inset,
- find_end_of_layout, find_token, find_token_backwards, find_token_exact,
- find_re, get_bool_value,
- get_containing_layout, get_option_value, get_value, get_quoted_value)
-# del_value, del_complete_lines,
-# find_complete_lines, find_end_of,
+from parser_tools import (count_pars_in_inset, del_complete_lines, del_token,
+ find_end_of, find_end_of_inset, find_end_of_layout, find_token,
+ find_token_backwards, find_token_exact, find_re, get_bool_value,
+ get_containing_inset, get_containing_layout, get_option_value, get_value,
+ get_quoted_value)
+# del_value,
+# find_complete_lines,
# find_re, find_substring,
-# get_containing_inset,
# is_in_inset, set_bool_value
# find_tokens, check_token
endcell = find_token(document.body, "</cell>", begcell)
vcand = False
if find_token(document.body, "\\begin_inset Newline", begcell, endcell) != -1:
- vcand = not fixedwidth and not multirow
+ vcand = not fixedwidth
elif count_pars_in_inset(document.body, begcell + 2) > 1:
- vcand = not fixedwidth and not multirow
+ vcand = not fixedwidth
elif get_value(document.body, "\\begin_layout", begcell) != "Plain Layout":
- vcand = not fixedwidth and not multirow
+ vcand = not fixedwidth
colalignment = col_info[col][2]
colvalignment = col_info[col][3]
if vcand:
document.body[col_line] = document.body[col_line][:-1] + " special=\"" + vval + "\">"
else:
alarg = ""
- if multicolumn:
+ if multicolumn or multirow:
if cellvalign == "middle":
alarg = "[m]"
elif cellvalign == "bottom":
alarg = "[b]"
else:
- document.warning("col: %i, alignment: %s" % (col, colvalignment))
if colvalignment == "middle":
alarg = "[m]"
elif colvalignment == "bottom":
flt = find_token(document.body, "\\begin_layout", begcell, endcell)
elt = find_token_backwards(document.body, "\\end_layout", endcell)
if flt != -1 and elt != -1:
- document.body[elt:elt+1] = put_cmd_in_ert("\\end{cellvarwidth}")
- document.body[flt+1:flt+1] = put_cmd_in_ert("\\begin{cellvarwidth}" + alarg)
+ extralines = []
+ # we need to reset character layouts if necessary
+ el = find_token(document.body, '\\emph on', flt, elt)
+ if el != -1:
+ extralines.append("\\emph default")
+ el = find_token(document.body, '\\noun on', flt, elt)
+ if el != -1:
+ extralines.append("\\noun default")
+ el = find_token(document.body, '\\series', flt, elt)
+ if el != -1:
+ extralines.append("\\series default")
+ el = find_token(document.body, '\\family', flt, elt)
+ if el != -1:
+ extralines.append("\\family default")
+ el = find_token(document.body, '\\shape', flt, elt)
+ if el != -1:
+ extralines.append("\\shape default")
+ el = find_token(document.body, '\\color', flt, elt)
+ if el != -1:
+ extralines.append("\\color inherit")
+ el = find_token(document.body, '\\size', flt, elt)
+ if el != -1:
+ extralines.append("\\size default")
+ el = find_token(document.body, '\\bar under', flt, elt)
+ if el != -1:
+ extralines.append("\\bar default")
+ el = find_token(document.body, '\\uuline on', flt, elt)
+ if el != -1:
+ extralines.append("\\uuline default")
+ el = find_token(document.body, '\\uwave on', flt, elt)
+ if el != -1:
+ extralines.append("\\uwave default")
+ el = find_token(document.body, '\\strikeout on', flt, elt)
+ if el != -1:
+ extralines.append("\\strikeout default")
+ document.body[elt:elt+1] = extralines + put_cmd_in_ert("\\end{cellvarwidth}") + ["\end_layout"]
+ parlang = -1
+ for q in range(flt, elt):
+ if document.body[q] != "" and document.body[q][0] != "\\":
+ break
+ if document.body[q][:5] == "\\lang":
+ parlang = q
+ break
+ if parlang != -1:
+ document.body[parlang+1:parlang+1] = put_cmd_in_ert("\\begin{cellvarwidth}" + alarg)
+ else:
+ document.body[flt+1:flt+1] = put_cmd_in_ert("\\begin{cellvarwidth}" + alarg)
needcellvarwidth = True
needvarwidth = True
# ERT newlines and linebreaks (since LyX < 2.4 automatically inserts parboxes
document.body[nl:nl+1] = put_cmd_in_ert("\\linebreak{}")
else:
document.body[nl:nl+1] = put_cmd_in_ert("\\\\")
+ # Replace parbreaks in multirow with \\endgraf
+ if multirow == True:
+ flt = find_token(document.body, "\\begin_layout", begcell, endcell)
+ if flt != -1:
+ while True:
+ elt = find_end_of_layout(document.body, flt)
+ if elt == -1:
+ document.warning("Malformed LyX document! Missing layout end.")
+ break
+ endcell = find_token(document.body, "</cell>", begcell)
+ flt = find_token(document.body, "\\begin_layout", elt, endcell)
+ if flt == -1:
+ break
+ document.body[elt : flt + 1] = put_cmd_in_ert("\\endgraf{}")
m += 1
i = j
add_to_preamble(document, ["\\usepackage{varwidth}"])
+def convert_vcolumns2(document):
+ """Convert varwidth ERT to native"""
+ i = 0
+ try:
+ while True:
+ i = find_token(document.body, "\\begin_inset Tabular", i+1)
+ if i == -1:
+ return
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Could not find end of tabular.")
+ continue
+
+ # Parse cells
+ nrows = int(document.body[i+1].split('"')[3])
+ ncols = int(document.body[i+1].split('"')[5])
+ m = i + 1
+ lines = []
+ for row in range(nrows):
+ for col in range(ncols):
+ m = find_token(document.body, "<cell", m)
+ multirow = get_option_value(document.body[m], 'multirow') != ""
+ begcell = m
+ endcell = find_token(document.body, "</cell>", begcell)
+ vcand = False
+ cvw = find_token(document.body, "begin{cellvarwidth}", begcell, endcell)
+ if cvw != -1:
+ vcand = document.body[cvw - 1] == "\\backslash" and get_containing_inset(document.body, cvw)[0] == "ERT"
+ if vcand:
+ # Remove ERTs with cellvarwidth env
+ ecvw = find_token(document.body, "end{cellvarwidth}", begcell, endcell)
+ if ecvw != -1:
+ if document.body[ecvw - 1] == "\\backslash":
+ eertins = get_containing_inset(document.body, ecvw)
+ if eertins and eertins[0] == "ERT":
+ del document.body[eertins[1] : eertins[2] + 1]
+
+ cvw = find_token(document.body, "begin{cellvarwidth}", begcell, endcell)
+ ertins = get_containing_inset(document.body, cvw)
+ if ertins and ertins[0] == "ERT":
+ del(document.body[ertins[1] : ertins[2] + 1])
+
+ # Convert ERT newlines (as cellvarwidth detection relies on that)
+ while True:
+ endcell = find_token(document.body, "</cell>", begcell)
+ nl = find_token(document.body, "\\backslash", begcell, endcell)
+ if nl == -1 or document.body[nl + 2] != "\\backslash":
+ break
+ ertins = get_containing_inset(document.body, nl)
+ if ertins and ertins[0] == "ERT":
+ document.body[ertins[1] : ertins[2] + 1] = ["\\begin_inset Newline newline", "", "\\end_inset"]
+
+ # Same for linebreaks
+ while True:
+ endcell = find_token(document.body, "</cell>", begcell)
+ nl = find_token(document.body, "linebreak", begcell, endcell)
+ if nl == -1 or document.body[nl - 1] != "\\backslash":
+ break
+ ertins = get_containing_inset(document.body, nl)
+ if ertins and ertins[0] == "ERT":
+ document.body[ertins[1] : ertins[2] + 1] = ["\\begin_inset Newline linebreak", "", "\\end_inset"]
+
+ # And \\endgraf
+ if multirow == True:
+ endcell = find_token(document.body, "</cell>", begcell)
+ nl = find_token(document.body, "endgraf{}", begcell, endcell)
+ if nl == -1 or document.body[nl - 1] != "\\backslash":
+ break
+ ertins = get_containing_inset(document.body, nl)
+ if ertins and ertins[0] == "ERT":
+ document.body[ertins[1] : ertins[2] + 1] = ["\\end_layout", "", "\\begin_layout Plain Layout"]
+ m += 1
+
+ i += 1
+
+ finally:
+ del_complete_lines(document.preamble,
+ ['% Added by lyx2lyx',
+ '%% Variable width box for table cells',
+ r'\newenvironment{cellvarwidth}[1][t]',
+ r' {\begin{varwidth}[#1]{\linewidth}}',
+ r' {\@finalstrut\@arstrutbox\end{varwidth}}'])
+ del_complete_lines(document.preamble,
+ ['% Added by lyx2lyx',
+ r'\usepackage{varwidth}'])
+
+
+frontispiece_def = [
+ r'### Inserted by lyx2lyx (frontispiece layout) ###',
+ r'Style Frontispiece',
+ r' CopyStyle Titlehead',
+ r' LatexName frontispiece',
+ r'End',
+]
+
+
+def convert_koma_frontispiece(document):
+ """Remove local KOMA frontispiece definition"""
+ if document.textclass[:3] != "scr":
+ return
+
+ if document.del_local_layout(frontispiece_def):
+ document.add_module("ruby")
+
+
+def revert_koma_frontispiece(document):
+ """Add local KOMA frontispiece definition"""
+ if document.textclass[:3] != "scr":
+ return
+
+ if find_token(document.body, "\\begin_layout Frontispiece", 0) != -1:
+ document.append_local_layout(frontispiece_def)
+
+
+def revert_spellchecker_ignore(document):
+ """Revert document spellchecker dictionary"""
+ while True:
+ i = find_token(document.header, "\\spellchecker_ignore")
+ if i == -1:
+ return
+ del document.header[i]
+
+
+def revert_docbook_mathml_prefix(document):
+ """Revert the DocBook parameter to choose the prefix for the MathML name space"""
+ while True:
+ i = find_token(document.header, "\\docbook_mathml_prefix")
+ if i == -1:
+ return
+ del document.header[i]
+
+def revert_document_metadata(document):
+ """Revert document metadata"""
+ i = 0
+ while True:
+ i = find_token(document.header, "\\begin_metadata", i)
+ if i == -1:
+ return
+ j = find_end_of(document.header, i, "\\begin_metadata", "\\end_metadata")
+ if j == -1:
+ # this should not happen
+ break
+ document.header[i : j + 1] = []
+
##
# Conversion hub
#
[602, [convert_branch_colors]],
[603, []],
[604, []],
- [605, []]
+ [605, [convert_vcolumns2]],
+ [606, [convert_koma_frontispiece]],
+ [607, []],
+ [608, []],
+ [609, []]
]
-revert = [[604, [revert_vcolumns2]],
+revert = [[608, [revert_document_metadata]],
+ [607, [revert_docbook_mathml_prefix]],
+ [606, [revert_spellchecker_ignore]],
+ [605, [revert_koma_frontispiece]],
+ [604, [revert_vcolumns2]],
[603, [revert_branch_darkcols]],
[602, [revert_darkmode_graphics]],
[601, [revert_branch_colors]],