# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-""" Convert files to the file format generated by lyx 2.1"""
+""" Convert files to the file format generated by LyX 2.1"""
import re, string
import unicodedata
j = j + 1
-def convert_use_mathtools(document):
- "insert use_package mathtools"
+def convert_use_package(document, pkg):
i = find_token(document.header, "\\use_package", 0)
if i == -1:
document.warning("Malformed LyX document: Can't find \\use_package.")
return;
- j = find_token(document.preamble, "\\usepackage{mathtools}", 0)
+ j = find_token(document.preamble, "\\usepackage{" + pkg + "}", 0)
if j == -1:
- document.header.insert(i + 1, "\\use_package mathtools 0")
+ document.header.insert(i + 1, "\\use_package " + pkg + " 0")
else:
- document.header.insert(i + 1, "\\use_package mathtools 2")
+ document.header.insert(i + 1, "\\use_package " + pkg + " 2")
del document.preamble[j]
-def revert_use_mathtools(document):
- "remove use_package mathtools"
- regexp = re.compile(r'(\\use_package\s+mathtools)')
+def revert_use_package(document, pkg, commands, oldauto):
+ # oldauto defines how the version we are reverting to behaves:
+ # if it is true, the old version uses the package automatically.
+ # if it is false, the old version never uses the package.
+ regexp = re.compile(r'(\\use_package\s+%s)' % pkg)
i = find_re(document.header, regexp, 0)
value = "1" # default is auto
if i != -1:
value = get_value(document.header, "\\use_package" , i).split()[1]
del document.header[i]
if value == "2": # on
- add_to_preamble(document, ["\\usepackage{mathtools}"])
- elif value == "1": # auto
- commands = ["mathclap", "mathllap", "mathrlap", \
- "lgathered", "rgathered", "vcentcolon", "dblcolon", \
- "coloneqq", "Coloneqq", "coloneq", "Coloneq", "eqqcolon", \
- "Eqqcolon", "eqcolon", "Eqcolon", "colonapprox", \
- "Colonapprox", "colonsim", "Colonsim"]
+ add_to_preamble(document, ["\\usepackage{" + pkg + "}"])
+ elif value == "1" and not oldauto: # auto
i = 0
while True:
i = find_token(document.body, '\\begin_inset Formula', i)
code = "\n".join(document.body[i:j])
for c in commands:
if code.find("\\%s" % c) != -1:
- add_to_preamble(document, ["\\usepackage{mathtools}"])
+ add_to_preamble(document, ["\\usepackage{" + pkg + "}"])
return
i = j
+def convert_use_mathtools(document):
+ "insert use_package mathtools"
+ convert_use_package(document, "mathtools")
+
+
+def revert_use_mathtools(document):
+ "remove use_package mathtools"
+ commands = ["mathclap", "mathllap", "mathrlap", \
+ "lgathered", "rgathered", "vcentcolon", "dblcolon", \
+ "coloneqq", "Coloneqq", "coloneq", "Coloneq", "eqqcolon", \
+ "Eqqcolon", "eqcolon", "Eqcolon", "colonapprox", \
+ "Colonapprox", "colonsim", "Colonsim"]
+ revert_use_package(document, "mathtools", commands, False)
+
+
def convert_use_stmaryrd(document):
"insert use_package stmaryrd"
- i = find_token(document.header, "\\use_package", 0)
- if i == -1:
- document.warning("Malformed LyX document: Can't find \\use_package.")
- return;
- j = find_token(document.preamble, "\\usepackage{stmaryrd}", 0)
- if j == -1:
- document.header.insert(i + 1, "\\use_package stmaryrd 0")
- else:
- document.header.insert(i + 1, "\\use_package stmaryrd 2")
- del document.preamble[j]
+ convert_use_package(document, "stmaryrd")
def revert_use_stmaryrd(document):
"remove use_package stmaryrd"
- regexp = re.compile(r'(\\use_package\s+stmaryrd)')
- i = find_re(document.header, regexp, 0)
- value = "1" # default is auto
- if i != -1:
- value = get_value(document.header, "\\use_package" , i).split()[1]
- del document.header[i]
- if value == "2": # on
- add_to_preamble(document, ["\\usepackage{stmaryrd}"])
- elif value == "1": # auto
- commands = ["shortleftarrow", "shortrightarrow", "shortuparrow", \
+ # commands provided by stmaryrd.sty but LyX uses other packages:
+ # boxdot lightning, bigtriangledown, bigtriangleup
+ commands = ["shortleftarrow", "shortrightarrow", "shortuparrow", \
"shortdownarrow", "Yup", "Ydown", "Yleft", "Yright", \
"varcurlyvee", "varcurlywedge", "minuso", "baro", \
"sslash", "bbslash", "moo", "varotimes", "varoast", \
"varcopyright", "longarrownot", "Longarrownot", \
"Mapsto", "mapsfrom", "Mapsfrom" "Longmapsto", \
"longmapsfrom", "Longmapsfrom"]
- # commands provided by stmaryrd.sty but LyX uses other packages:
- # boxdot lightning, bigtriangledown, bigtriangleup
+ revert_use_package(document, "stmaryrd", commands, False)
- i = 0
- while True:
- i = find_token(document.body, '\\begin_inset Formula', 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 Formula inset at line " + str(i))
- i += 1
- continue
- code = "\n".join(document.body[i:j])
- for c in commands:
- if code.find("\\%s" % c) != -1:
- add_to_preamble(document, ["\\usepackage{stmaryrd}"])
- return
- i = j
def convert_use_stackrel(document):
"insert use_package stackrel"
- i = find_token(document.header, "\\use_package", 0)
- if i == -1:
- document.warning("Malformed LyX document: Can't find \\use_package.")
- return;
- j = find_token(document.preamble, "\\usepackage{stackrel}", 0)
- if j == -1:
- document.header.insert(i + 1, "\\use_package stackrel 0")
- else:
- document.header.insert(i + 1, "\\use_package stackrel 2")
- del document.preamble[j]
+ convert_use_package(document, "stackrel")
def revert_use_stackrel(document):
"remove use_package stackrel"
- regexp = re.compile(r'(\\use_package\s+stackrel)')
- i = find_re(document.header, regexp, 0)
- value = "1" # default is auto
- if i != -1:
- value = get_value(document.header, "\\use_package" , i).split()[1]
- del document.header[i]
- if value == "2": # on
- add_to_preamble(document, ["\\usepackage{stackrel}"])
- elif value == "1": # auto
- regcmd = re.compile(r'.*\\stackrel\s*\[')
- i = 0
- while True:
- i = find_token(document.body, '\\begin_inset Formula', 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 Formula inset at line " + str(i))
- i += 1
- continue
- code = "\n".join(document.body[i:j])
- if regcmd.match(code):
- add_to_preamble(document, ["\\usepackage{stackrel}"])
- return
- i = j
+ commands = ["stackrel"]
+ revert_use_package(document, "stackrel", commands, False)
def convert_cite_engine_type(document):
document.header[i] = "\\cite_engine natbib_" + engine_type
+# this is the same, as revert_use_cancel() except for the default
def revert_cancel(document):
"add cancel to the preamble if necessary"
commands = ["cancelto", "cancel", "bcancel", "xcancel"]
- i = 0
- while True:
- i = find_token(document.body, '\\begin_inset Formula', 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 Formula inset at line " + str(i))
- i += 1
- continue
- code = "\n".join(document.body[i:j])
- for c in commands:
- if code.find("\\%s" % c) != -1:
- add_to_preamble(document, ["\\usepackage{cancel}"])
- return
- i = j
+ revert_use_package(document, "cancel", commands, False)
def revert_verbatim(document):
return
j = find_end_of_layout(document.body, i)
if j == -1:
- document.warning("Malformed lyx document: Can't find end of Verbatim layout")
+ document.warning("Malformed LyX document: Can't find end of Verbatim layout")
i += 1
continue
# delete all line breaks insets (there are no other insets)
return
j = find_end_of_inset(document.body, i)
if j == -1:
- document.warning("Malformed lyx document: Can't find end of IPA inset")
+ document.warning("Malformed LyX document: Can't find end of IPA inset")
i += 1
continue
Multipar = False
n = find_token(document.body, "\\begin_layout", i, j)
if n == -1:
- document.warning("Malformed lyx document: IPA inset has no embedded layout")
+ document.warning("Malformed LyX document: IPA inset has no embedded layout")
i += 1
continue
m = find_end_of_layout(document.body, n)
if m == -1:
- document.warning("Malformed lyx document: Can't find end of embedded layout")
+ document.warning("Malformed LyX document: Can't find end of embedded layout")
i += 1
continue
content = document.body[n+1:m]
return
j = find_end_of_inset(document.body, i)
if j == -1:
- document.warning("Malformed lyx document: Can't find end of ERT inset")
+ document.warning("Malformed LyX document: Can't find end of ERT inset")
i += 1
continue
ert = get_ert(document.body, i)
if document.body[i+1] == "LatexCommand lstlistoflistings":
j = find_end_of_inset(document.body, i)
if j == -1:
- document.warning("Malformed lyx document: Can't find end of TOC inset")
+ document.warning("Malformed LyX document: Can't find end of TOC inset")
i += 1
continue
subst = put_cmd_in_ert("\\lstlistoflistings{}")
add_to_preamble(document, ["\\usepackage{amssymb}"])
+def convert_use_cancel(document):
+ "insert use_package cancel"
+ convert_use_package(document, "cancel")
+
+
+def revert_use_cancel(document):
+ "remove use_package cancel"
+ commands = ["cancel", "bcancel", "xcancel", "cancelto"]
+ revert_use_package(document, "cancel", commands, True)
+
+
def revert_ancientgreek(document):
"Set the document language for ancientgreek to greek"
# Find containing paragraph layout
parent = get_containing_layout(document.body, i)
if parent == False:
- document.warning("Malformed lyx document: Can't find parent paragraph layout")
+ document.warning("Malformed LyX document: Can't find parent paragraph layout")
i = i + 1
continue
parbeg = parent[1]
# Find containing paragraph layout
parent = get_containing_layout(document.body, i)
if parent == False:
- document.warning("Malformed lyx document: Can't find parent paragraph layout")
+ document.warning("Malformed LyX document: Can't find parent paragraph layout")
i = i + 1
continue
parbeg = parent[1]
# Revert to old syntax
document.body[p] = "\\begin_inset Argument"
if j == -1:
- document.warning("Malformed lyx document: Can't find end of Argument inset")
+ document.warning("Malformed LyX document: Can't find end of Argument inset")
continue
if val > 0:
args[val] = document.body[p : j + 1]
# Find containing paragraph layout
parent = get_containing_layout(document.body, i)
if parent == False:
- document.warning("Malformed lyx document: Can't find parent paragraph layout")
+ document.warning("Malformed LyX document: Can't find parent paragraph layout")
i = i + 1
continue
parbeg = parent[3]
# Find containing paragraph layout
parent = get_containing_layout(document.body, i)
if parent == False:
- document.warning("Malformed lyx document: Can't find parent paragraph layout")
+ document.warning("Malformed LyX document: Can't find parent paragraph layout")
i = i + 1
continue
parbeg = parent[1]
# Find containing paragraph layout
parent = get_containing_layout(document.body, i)
if parent == False:
- document.warning("Malformed lyx document: Can't find parent paragraph layout")
+ document.warning("Malformed LyX document: Can't find parent paragraph layout")
i = i + 1
continue
parbeg = parent[1]
# Find containing paragraph layout
parent = get_containing_layout(document.body, i)
if parent == False:
- document.warning("Malformed lyx document: Can't find parent paragraph layout")
+ document.warning("Malformed LyX document: Can't find parent paragraph layout")
i = i + 1
continue
parbeg = parent[1]
# Find containing paragraph layout
parent = get_containing_layout(document.body, i)
if parent == False:
- document.warning("Malformed lyx document: Can't find parent paragraph layout")
+ document.warning("Malformed LyX document: Can't find parent paragraph layout")
i = i + 1
continue
parbeg = parent[1]
# Find containing paragraph layout
parent = get_containing_layout(document.body, i)
if parent == False:
- document.warning("Malformed lyx document: Can't find parent paragraph layout")
+ document.warning("Malformed LyX document: Can't find parent paragraph layout")
i = i + 1
continue
parbeg = parent[1]
# Find end of sequence
j = find_end_of_sequence(document.body, i)
if j == -1:
- document.warning("Malformed lyx document. Cannot find end of Overprint sequence!")
+ document.warning("Malformed LyX document. Cannot find end of Overprint sequence!")
i = i + 1
continue
endseq = j
if argbeg != -1:
argend = find_end_of_layout(document.body, argbeg)
if argend == -1:
- document.warning("Malformed lyx document. Cannot find end of Overprint argument!")
+ document.warning("Malformed LyX document. Cannot find end of Overprint argument!")
i = i + 1
continue
beginPlain = find_token(document.body, "\\begin_layout Plain Layout", argbeg)
# Find end of sequence
j = find_end_of_sequence(document.body, i)
if j == -1:
- document.warning("Malformed lyx document. Cannot find end of Overprint sequence!")
+ document.warning("Malformed LyX document. Cannot find end of Overprint sequence!")
i = i + 1
continue
endseq = j
if argbeg != -1:
argend = find_end_of_inset(document.body, argbeg)
if argend == -1:
- document.warning("Malformed lyx document. Cannot find end of Overprint argument!")
+ document.warning("Malformed LyX document. Cannot find end of Overprint argument!")
i = i + 1
continue
beginPlain = find_token(document.body, "\\begin_layout Plain Layout", argbeg)
if document.body[p] == "\\begin_layout Overprint":
q = find_end_of_layout(document.body, p)
if q == -1:
- document.warning("Malformed lyx document. Cannot find end of Overprint layout!")
+ document.warning("Malformed LyX document. Cannot find end of Overprint layout!")
p += 1
continue
subst = ["\\begin_layout Standard"] + put_cmd_in_ert("\\onslide")
if argbeg != -1:
argend = find_end_of_inset(document.body, argbeg)
if argend == -1:
- document.warning("Malformed lyx document. Cannot find end of Overprint item argument!")
+ document.warning("Malformed LyX document. Cannot find end of Overprint item argument!")
p += 1
continue
beginPlain = find_token(document.body, "\\begin_layout Plain Layout", argbeg)
return
j = find_end_of_layout(document.body, i)
if j == -1:
- document.warning("Malformed lyx document: Can't find end of FrameTitle layout")
+ document.warning("Malformed LyX document: Can't find end of FrameTitle layout")
i = i + 1
continue
endlay = j
return
j = find_end_of_layout(document.body, i)
if j == -1:
- document.warning("Malformed lyx document: Can't find end of Epigraph layout")
+ document.warning("Malformed LyX document: Can't find end of Epigraph layout")
i = i + 1
continue
endlay = j
return
j = find_end_of_layout(document.body, i)
if j == -1:
- document.warning("Malformed lyx document: Can't find end of Epigraph layout")
+ document.warning("Malformed LyX document: Can't find end of Epigraph layout")
i = i + 1
continue
endlay = j
return
document.body[i] = "\\begin_inset Caption Standard"
i = i + 1
-
def revert_captioninsets(document):
def convert_captionlayouts(document):
" Convert caption layouts to caption insets. "
-
+
caption_dict = {
"Captionabove": "Above",
"Captionbelow": "Below",
"CenteredCaption" : "Centered",
"Bicaption" : "Bicaption",
}
-
+
i = 0
while True:
i = find_token(document.body, "\\begin_layout", i)
# Find end of sequence
j = find_end_of_sequence(document.body, i)
if j == -1:
- document.warning("Malformed lyx document. Cannot find end of FragileFrame sequence!")
+ document.warning("Malformed LyX document. Cannot find end of FragileFrame sequence!")
i = i + 1
continue
endseq = j
# Find end of sequence
j = find_end_of_sequence(document.body, i)
if j == -1:
- document.warning("Malformed lyx document. Cannot find end of Frame sequence!")
+ document.warning("Malformed LyX document. Cannot find end of Frame sequence!")
i = i + 1
continue
endseq = j
add_to_preamble(document, "\\renewcommand{\\sfdefault}{%s}" % sf)
document.header[k] = "\\font_sans default"
+def revert_iwona_fonts(document):
+ " Revert iwona font definition to LaTeX "
+
+ i = find_token(document.header, "\\font_math", 0)
+ if i != -1:
+ if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
+ val = get_value(document.header, "\\font_math", i)
+ if val == "iwona-math":
+ add_to_preamble(document, "\\let\\Myrmdefault\\rmdefault\n" \
+ "\\usepackage[math]{iwona}\n" \
+ "\\renewcommand{\\rmdefault}{\\Myrmdefault}")
+ document.header[i] = "\\font_math auto"
+
+ if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
+ iwona_fonts = ["iwona", "iwonac", "iwonal", "iwonalc"]
+ k = find_token(document.header, "\\font_sans iwona", 0)
+ if k != -1:
+ sf = get_value(document.header, "\\font_sans", k)
+ if sf in iwona_fonts:
+ add_to_preamble(document, "\\renewcommand{\\sfdefault}{%s}" % sf)
+ document.header[k] = "\\font_sans default"
+
def revert_new_libertines(document):
" Revert new libertine font definition to LaTeX "
document.header[k] = "\\font_sans default"
+def convert_lyxframes(document):
+ " Converts old beamer frames to new style "
+
+ beamer_classes = ["beamer", "article-beamer", "scrarticle-beamer"]
+ if document.textclass not in beamer_classes:
+ return
+
+ framebeg = ["BeginFrame", "BeginPlainFrame"]
+ frameend = ["EndFrame", "BeginFrame", "BeginPlainFrame", "AgainFrame", "Section", "Section*",
+ "Subsection", "Subsection*", "Subsubsection", "Subsubsection*"]
+ for lay in framebeg:
+ i = 0
+ while True:
+ i = find_token_exact(document.body, "\\begin_layout " + lay, i)
+ if i == -1:
+ break
+ parent = get_containing_layout(document.body, i)
+ if parent == False or parent[1] != i:
+ document.warning("Wrong parent layout!")
+ i += 1
+ continue
+ frametype = parent[0]
+ j = parent[2]
+ parbeg = parent[3]
+ if i != -1:
+ # Step I: Convert ERT arguments
+ # FIXME: This currently only works if the arguments are in one single ERT
+ ertend = i
+ if document.body[parbeg] == "\\begin_inset ERT":
+ ertend = find_end_of_inset(document.body, parbeg)
+ if ertend == -1:
+ document.warning("Malformed LyX document: missing ERT \\end_inset")
+ continue
+ ertcont = parbeg + 5
+ if document.body[ertcont].startswith("[<"):
+ # This is a default overlay specification
+ # strip off the [<
+ document.body[ertcont] = document.body[ertcont][2:]
+ if document.body[ertcont].endswith(">]"):
+ # strip off the >]
+ document.body[ertcont] = document.body[ertcont][:-2]
+ elif document.body[ertcont].endswith("]"):
+ # divide the args
+ tok = document.body[ertcont].find('>][')
+ if tok != -1:
+ subst = [document.body[ertcont][:tok],
+ '\\end_layout', '', '\\end_inset', '', '', '\\begin_inset Argument 3',
+ 'status collapsed', '', '\\begin_layout Plain Layout',
+ document.body[ertcont][tok + 3:-1]]
+ document.body[ertcont : ertcont + 1] = subst
+ ertend += 11
+ # Convert to ArgInset
+ document.body[parbeg] = "\\begin_inset Argument 2"
+ elif document.body[ertcont].startswith("<"):
+ # This is an overlay specification
+ # strip off the <
+ document.body[ertcont] = document.body[ertcont][1:]
+ if document.body[ertcont].endswith(">"):
+ # strip off the >
+ document.body[ertcont] = document.body[ertcont][:-1]
+ # Convert to ArgInset
+ document.body[parbeg] = "\\begin_inset Argument 1"
+ elif document.body[ertcont].endswith(">]"):
+ # divide the args
+ tok = document.body[ertcont].find('>[<')
+ if tok != -1:
+ document.body[ertcont : ertcont + 1] = [document.body[ertcont][:tok],
+ '\\end_layout', '', '\\end_inset', '', '', '\\begin_inset Argument 2',
+ 'status collapsed', '', '\\begin_layout Plain Layout',
+ document.body[ertcont][tok + 3:-2]]
+ # Convert to ArgInset
+ document.body[parbeg] = "\\begin_inset Argument 1"
+ ertend += 11
+ elif document.body[ertcont].endswith("]"):
+ # divide the args
+ tok = document.body[ertcont].find('>[<')
+ if tok != -1:
+ # divide the args
+ tokk = document.body[ertcont].find('>][')
+ if tokk != -1:
+ document.body[ertcont : ertcont + 1] = [document.body[ertcont][:tok],
+ '\\end_layout', '', '\\end_inset', '', '', '\\begin_inset Argument 2',
+ 'status collapsed', '', '\\begin_layout Plain Layout',
+ document.body[ertcont][tok + 3:tokk],
+ '\\end_layout', '', '\\end_inset', '', '', '\\begin_inset Argument 3',
+ 'status collapsed', '', '\\begin_layout Plain Layout',
+ document.body[ertcont][tokk + 3:-1]]
+ ertend += 22
+ else:
+ tokk = document.body[ertcont].find('>[')
+ if tokk != -1:
+ document.body[ertcont : ertcont + 1] = [document.body[ertcont][:tokk],
+ '\\end_layout', '', '\\end_inset', '', '', '\\begin_inset Argument 3',
+ 'status collapsed', '', '\\begin_layout Plain Layout',
+ document.body[ertcont][tokk + 2:-1]]
+ ertend += 11
+ # Convert to ArgInset
+ document.body[parbeg] = "\\begin_inset Argument 1"
+ elif document.body[ertcont].startswith("["):
+ # This is an ERT option
+ # strip off the [
+ document.body[ertcont] = document.body[ertcont][1:]
+ if document.body[ertcont].endswith("]"):
+ # strip off the ]
+ document.body[ertcont] = document.body[ertcont][:-1]
+ # Convert to ArgInset
+ document.body[parbeg] = "\\begin_inset Argument 3"
+ # End of argument conversion
+ # Step II: Now rename the layout and convert the title to an argument
+ j = find_end_of_layout(document.body, i)
+ document.body[j : j + 1] = ['\\end_layout', '', '\\end_inset', '', '\\end_layout']
+ if lay == "BeginFrame":
+ document.body[i] = "\\begin_layout Frame"
+ else:
+ document.body[i] = "\\begin_layout PlainFrame"
+ document.body[ertend + 1 : ertend + 1] = ['\\begin_inset Argument 4',
+ 'status open', '', '\\begin_layout Plain Layout']
+ # Step III: find real frame end
+ j = j + 8
+ jj = j
+ while True:
+ fend = find_token(document.body, "\\begin_layout", jj)
+ if fend == -1:
+ document.warning("Malformed LyX document: No real frame end!")
+ return
+ val = get_value(document.body, "\\begin_layout", fend)
+ if val not in frameend:
+ jj = fend + 1
+ continue
+ old = document.body[fend]
+ if val == frametype:
+ document.body[fend : fend] = ['\\end_deeper', '', '\\begin_layout Separator', '', '\\end_layout']
+ else:
+ document.body[fend : fend] = ['\\end_deeper']
+ document.body[j + 1 : j + 1] = ['', '\\begin_deeper']
+ break
+ i = j
+
+
+def remove_endframes(document):
+ " Remove deprecated beamer endframes "
+
+ beamer_classes = ["beamer", "article-beamer", "scrarticle-beamer"]
+ if document.textclass not in beamer_classes:
+ return
+
+ i = 0
+ while True:
+ i = find_token_exact(document.body, "\\begin_layout EndFrame", i)
+ if i == -1:
+ break
+ j = find_end_of_layout(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Missing \\end_layout to EndFrame")
+ i = i + 1
+ continue
+ del document.body[i : j + 1]
+
+
+def revert_powerdot_flexes(document):
+ " Reverts powerdot flex insets "
+
+ if document.textclass != "powerdot":
+ return
+
+ flexes = {"Onslide" : "\\onslide",
+ "Onslide*" : "\\onslide*",
+ "Onslide+" : "\\onslide+"}
+ rx = re.compile(r'^\\begin_inset Flex (.+)$')
+
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset Flex", i)
+ if i == -1:
+ return
+ m = rx.match(document.body[i])
+ if m:
+ flextype = m.group(1)
+ z = find_end_of_inset(document.body, i)
+ if z == -1:
+ document.warning("Can't find end of Flex " + flextype + " inset.")
+ i += 1
+ continue
+ if flextype in flexes:
+ pre = put_cmd_in_ert(flexes[flextype])
+ arg = find_token(document.body, "\\begin_inset Argument 1", i, z)
+ if arg != -1:
+ argend = find_end_of_inset(document.body, arg)
+ if argend == -1:
+ document.warning("Can't find end of Argument!")
+ i += 1
+ continue
+ # Find containing paragraph layout
+ beginPlain = find_token(document.body, "\\begin_layout Plain Layout", arg)
+ endPlain = find_end_of_layout(document.body, beginPlain)
+ argcontent = document.body[beginPlain + 1 : endPlain]
+ # Adjust range end
+ z = z - len(document.body[arg : argend + 1])
+ # Remove arg inset
+ del document.body[arg : argend + 1]
+ pre += put_cmd_in_ert("{") + argcontent + put_cmd_in_ert("}")
+ pre += put_cmd_in_ert("{")
+ beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
+ endPlain = find_end_of_layout(document.body, beginPlain)
+ # Adjust range end
+ z = z - len(document.body[i : beginPlain + 1])
+ z += len(pre)
+ document.body[i : beginPlain + 1] = pre
+ post = put_cmd_in_ert("}")
+ document.body[z - 2 : z + 1] = post
+ i += 1
+
+
+def revert_powerdot_pause(document):
+ " Reverts powerdot pause layout to ERT "
+
+ if document.textclass != "powerdot":
+ return
+
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_layout Pause", i)
+ if i == -1:
+ return
+ j = find_end_of_layout(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of Pause layout")
+ i = i + 1
+ continue
+ endlay = j
+ subst = ["\\begin_layout Standard"] + put_cmd_in_ert("\\pause")
+ for p in range(i, j):
+ if p >= endlay:
+ break
+ arg = find_token(document.body, "\\begin_inset Argument 1", i, j)
+ if arg != -1:
+ beginPlain = find_token(document.body, "\\begin_layout Plain Layout", p)
+ endPlain = find_end_of_layout(document.body, beginPlain)
+ endInset = find_end_of_inset(document.body, p)
+ content = document.body[beginPlain + 1 : endPlain]
+ # Adjust range end
+ endlay = endlay - len(document.body[p : endInset + 1])
+ # Remove arg inset
+ del document.body[p : endInset + 1]
+ subst += put_cmd_in_ert("[") + content + put_cmd_in_ert("]")
+
+ document.body[i : i + 1] = subst
+ i = endlay
+
+
+def revert_powerdot_itemargs(document):
+ " Reverts powerdot item arguments to ERT "
+
+ if document.textclass != "powerdot":
+ return
+
+ i = 0
+ list_layouts = ["Itemize", "ItemizeType1", "Enumerate", "EnumerateType1"]
+ rx = re.compile(r'^\\begin_inset Argument (\S+)$')
+
+ while True:
+ i = find_token(document.body, "\\begin_inset Argument", i)
+ if i == -1:
+ return
+ # Find containing paragraph layout
+ parent = get_containing_layout(document.body, i)
+ if parent == False:
+ document.warning("Malformed LyX document: Can't find parent paragraph layout")
+ i = i + 1
+ continue
+ parbeg = parent[1]
+ parend = parent[2]
+ realparbeg = parent[3]
+ layoutname = parent[0]
+ realparend = parend
+ for p in range(parbeg, parend):
+ if p >= realparend:
+ i = realparend
+ break
+ if layoutname in list_layouts:
+ m = rx.match(document.body[p])
+ if m:
+ argnr = m.group(1)
+ if argnr == "item:1":
+ j = find_end_of_inset(document.body, i)
+ # Find containing paragraph layout
+ beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
+ endPlain = find_end_of_layout(document.body, beginPlain)
+ content = document.body[beginPlain + 1 : endPlain]
+ del document.body[i:j+1]
+ subst = put_cmd_in_ert("[") + content + put_cmd_in_ert("]")
+ document.body[realparbeg : realparbeg] = subst
+ elif argnr == "item:2":
+ j = find_end_of_inset(document.body, i)
+ # Find containing paragraph layout
+ beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
+ endPlain = find_end_of_layout(document.body, beginPlain)
+ content = document.body[beginPlain + 1 : endPlain]
+ del document.body[i:j+1]
+ subst = put_cmd_in_ert("<") + content + put_cmd_in_ert(">")
+ document.body[realparbeg : realparbeg] = subst
+
+ i = realparend
+
+
+def revert_powerdot_columns(document):
+ " Reverts powerdot twocolumn to TeX-code "
+ if document.textclass != "powerdot":
+ return
+
+ rx = re.compile(r'^\\begin_inset Argument (\S+)$')
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_layout Twocolumn", i)
+ if i == -1:
+ return
+ j = find_end_of_layout(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of Twocolumn layout")
+ i = i + 1
+ continue
+ endlay = j
+ document.body[j : j] = put_cmd_in_ert("}") + document.body[j : j]
+ endlay += len(put_cmd_in_ert("}"))
+ subst = ["\\begin_layout Standard"] + put_cmd_in_ert("\\twocolumn")
+ for p in range(i, j):
+ if p >= endlay:
+ break
+ m = rx.match(document.body[p])
+ if m:
+ argnr = m.group(1)
+ if argnr == "1":
+ beginPlain = find_token(document.body, "\\begin_layout Plain Layout", p)
+ endPlain = find_end_of_layout(document.body, beginPlain)
+ endInset = find_end_of_inset(document.body, p)
+ content = document.body[beginPlain + 1 : endPlain]
+ # Adjust range end
+ endlay = endlay - len(document.body[p : endInset + 1])
+ # Remove arg inset
+ del document.body[p : endInset + 1]
+ subst += put_cmd_in_ert("[") + content + put_cmd_in_ert("]")
+ elif argnr == "2":
+ beginPlain = find_token(document.body, "\\begin_layout Plain Layout", p)
+ endPlain = find_end_of_layout(document.body, beginPlain)
+ endInset = find_end_of_inset(document.body, p)
+ content = document.body[beginPlain + 1 : endPlain]
+ # Adjust range end
+ endlay = endlay - len(document.body[p : endInset + 1])
+ # Remove arg inset
+ del document.body[p : endInset + 1]
+ subst += put_cmd_in_ert("{") + content + put_cmd_in_ert("}")
+
+ subst += put_cmd_in_ert("{")
+ document.body[i : i + 1] = subst
+ i = endlay
+
+
+def revert_mbox_fbox(document):
+ 'Convert revert mbox/fbox boxes to TeX-code'
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset Box", i)
+ if i == -1:
+ return
+ j = find_token(document.body, "width", i)
+ if j != i + 7:
+ document.warning("Malformed LyX document: Can't find box width")
+ return
+ width = get_value(document.body, "width", j)
+ k = find_end_of_inset(document.body, j)
+ if k == -1:
+ document.warning("Malformed LyX document: Can't find end of box inset")
+ i += 1
+ continue
+ BeginLayout = find_token(document.body, "\\begin_layout Plain Layout", j)
+ EndLayout = find_token(document.body, "\\end_layout", BeginLayout)
+ # replace if width is "-999col%"
+ if (width == '"-999col%"'):
+ document.body[EndLayout:k + 1] = put_cmd_in_ert("}")
+ if document.body[i] == "\\begin_inset Box Frameless":
+ document.body[i:BeginLayout + 1] = put_cmd_in_ert("\\mbox{")
+ if document.body[i] == "\\begin_inset Box Boxed":
+ document.body[i:BeginLayout + 1] = put_cmd_in_ert("\\fbox{")
+ i = i + 1
+
+
+def revert_starred_caption(document):
+ " Reverts unnumbered longtable caption insets "
+
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset Caption LongTableNoNumber", i)
+ if i == -1:
+ return
+ # This is not equivalent, but since the caption inset is a full blown
+ # text inset a true conversion to ERT is too difficult.
+ document.body[i] = "\\begin_inset Caption Standard"
+ i = i + 1
+
+
+def revert_forced_local_layout(document):
+ i = 0
+ while True:
+ i = find_token(document.header, "\\begin_forced_local_layout", i)
+ if i == -1:
+ return
+ j = find_end_of(document.header, i, "\\begin_forced_local_layout", "\\end_forced_local_layout")
+ if j == -1:
+ # this should not happen
+ break
+ regexp = re.compile(r'\s*forcelocal', re.IGNORECASE)
+ k = find_re(document.header, regexp, i, j)
+ while k != -1:
+ del document.header[k]
+ j = j - 1
+ k = find_re(document.header, regexp, i, j)
+ k = find_token(document.header, "\\begin_local_layout", 0)
+ if k == -1:
+ document.header[i] = "\\begin_local_layout"
+ document.header[j] = "\\end_local_layout"
+ else:
+ l = find_end_of(document.header, k, "\\begin_local_layout", "\\end_local_layout")
+ if j == -1:
+ # this should not happen
+ break
+ lines = document.header[i+1 : j]
+ if k > i:
+ document.header[k+1 : k+1] = lines
+ document.header[i : j ] = []
+ else:
+ document.header[i : j ] = []
+ document.header[k+1 : k+1] = lines
+
+
##
# Conversion hub
#
[461, []],
[462, []],
[463, [convert_encodings]],
+ [464, [convert_use_cancel]],
+ [465, [convert_lyxframes, remove_endframes]],
+ [466, []],
+ [467, []],
+ [468, []],
+ [469, []],
+ [470, []]
]
revert = [
+ [469, [revert_forced_local_layout]],
+ [468, [revert_starred_caption]],
+ [467, [revert_mbox_fbox]],
+ [466, [revert_iwona_fonts]],
+ [465, [revert_powerdot_flexes, revert_powerdot_pause, revert_powerdot_itemargs, revert_powerdot_columns]],
+ [464, []],
+ [463, [revert_use_cancel]],
[462, [revert_encodings]],
[461, [revert_new_libertines]],
[460, [revert_kurier_fonts]],