+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 += 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 += 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 += 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_end_of_layout(document.body, BeginLayout)
+ # replace if width is ""
+ if (width == '""'):
+ 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 += 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 += 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
+
+
+def revert_aa1(document):
+ " Reverts InsetArguments of aa to TeX-code "
+ if document.textclass == "aa":
+ i = 0
+ while True:
+ if i != -1:
+ i = find_token(document.body, "\\begin_layout Abstract (structured)", i)
+ if i != -1:
+ revert_Argument_to_TeX_brace(document, i, 0, 1, 4, False, False)
+ i += 1
+ if i == -1:
+ return
+
+
+def revert_aa2(document):
+ " Reverts InsetArguments of aa to TeX-code "
+ if document.textclass == "aa":
+ i = 0
+ while True:
+ if i != -1:
+ i = find_token(document.body, "\\begin_layout Abstract (structured)", i)
+ if i != -1:
+ document.body[i] = "\\begin_layout Abstract"
+ i += 1
+ if i == -1:
+ return
+
+
+def revert_tibetan(document):
+ "Set the document language for Tibetan to English"
+
+ if document.language == "tibetan":
+ document.language = "english"
+ i = find_token(document.header, "\\language", 0)
+ if i != -1:
+ document.header[i] = "\\language english"
+ j = 0
+ while j < len(document.body):
+ j = find_token(document.body, "\\lang tibetan", j)
+ if j != -1:
+ document.body[j] = document.body[j].replace("\\lang tibetan", "\\lang english")
+ j += 1
+ else:
+ j = len(document.body)
+
+
+#############
+#
+# Chunk stuff
+#
+#############
+
+# The idea here is that we will have a sequence of chunk paragraphs.
+# We want to convert them to paragraphs in one or several chunk insets.
+# Individual chunks are terminated by the character @ on the last line.
+# This line will be discarded, and following lines are treated as new
+# chunks, which go into their own insets.
+# The first line of a chunk should look like: <<CONTENT>>=
+# We will discard the delimiters, and put the CONTENT into the
+# optional argument of the inset, if the CONTENT is non-empty.
+def convert_chunks(document):
+ first_re = re.compile(r'<<(.*)>>=(.*)')
+ file_pos = 0
+ while True:
+ # find start of a block of chunks
+ i = find_token(document.body, "\\begin_layout Chunk", file_pos)
+ if i == -1:
+ return
+ start = i
+ end = -1
+ contents = []
+ chunk_started = False
+
+ while True:
+ # process the one we just found
+ j = find_end_of_layout(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX documents. Can't find end of Chunk layout!")
+ # there is no point continuing, as we will run into the same error again.
+ return
+ this_chunk = "".join(document.body[i + 1:j])
+
+ # there may be empty lines between chunks
+ # we just skip them.
+ if not chunk_started:
+ if this_chunk != "":
+ # new chunk starts
+ chunk_started = True
+
+ if chunk_started:
+ contents.append(document.body[i + 1:j])
+
+ # look for potential chunk terminator
+ # on the last line of the chunk paragraph
+ if document.body[j - 1] == "@":
+ break
+
+ # look for subsequent chunk paragraph
+ i = find_token(document.body, "\\begin_layout", j)
+ if i == -1:
+ break
+
+ if get_value(document.body, "\\begin_layout", i) != "Chunk":
+ break
+
+ file_pos = end = j + 1
+
+ # The last chunk should simply have an "@" in it
+ # or at least end with "@" (can happen if @ is
+ # preceded by a newline)
+ lastpar = ''
+ if len(contents) > 0:
+ lastpar = ''.join(contents[-1])
+ if not lastpar.endswith("@"):
+ document.warning("Unexpected chunk content: chunk not terminated by '@'!")
+ if len(contents) == 0:
+ # convert empty chunk layouts to Standard
+ document.body[start] = "\\begin_layout Standard"
+ continue
+
+ if lastpar == "@":
+ # chunk par only contains "@". Just drop it.
+ contents.pop()
+ else:
+ # chunk par contains more. Only drop the "@".
+ contents[-1].pop()
+
+ # The first line should look like: <<CONTENT>>=
+ # We want the CONTENT
+ optarg = ' '.join(contents[0])
+ optarg.strip()
+ # We can already have real chunk content in
+ # the first par (separated from the options by a newline).
+ # We collect such stuff to re-insert it later.
+ postoptstuff = []
+
+ match = first_re.search(optarg)
+ if match:
+ optarg = match.groups()[0]
+ if match.groups()[1] != "":
+ postopt = False
+ for c in contents[0]:
+ if c.endswith(">>="):
+ postopt = True
+ continue
+ if postopt:
+ postoptstuff.append(c)
+ # We have stripped everything. This can be deleted.
+ contents.pop(0)
+
+ newstuff = ['\\begin_layout Standard']
+
+ # Maintain paragraph parameters
+ par_params = ["\\noindent", "\\indent", "\\indent-toggle", "\\leftindent",
+ "\\start_of_appendix", "\\paragraph_spacing", "\\align",
+ "\\labelwidthstring"]
+ parms = start + 1
+ while True:
+ if document.body[parms].split(' ', 1)[0] not in par_params:
+ break
+ newstuff.extend([document.body[parms]])
+ parms += 1
+
+ newstuff.extend(
+ ['\\begin_inset Flex Chunk',
+ 'status open', '',
+ '\\begin_layout Plain Layout', ''])
+
+ # If we have a non-empty optional argument, insert it.
+ if match and optarg != "":
+ newstuff.extend(
+ ['\\begin_inset Argument 1',
+ 'status open', '',
+ '\\begin_layout Plain Layout',
+ optarg,
+ '\\end_layout', '',
+ '\\end_inset', ''])
+
+ # Since we already opened a Plain layout, the first paragraph
+ # does not need to do that.
+ did_one_par = False
+ if postoptstuff:
+ # we need to replace newlines with new layouts
+ start_newline = -1
+ started_text = False
+ for lno in range(0,len(postoptstuff)):
+ if postoptstuff[lno].startswith("\\begin_inset Newline newline"):
+ start_newline = lno
+ elif start_newline != -1:
+ if postoptstuff[lno].startswith("\\end_inset"):
+ # replace that bit, but only if we already have some text
+ # and we're not at the end except for a blank line
+ if started_text and \
+ (lno != len(postoptstuff) - 2 or postoptstuff[-1] != ""):
+ newstuff.extend(['\\end_layout', '\n', '\\begin_layout Plain Layout', '\n'])
+ start_newline = -1
+ started_text = True
+ else:
+ newstuff.extend([postoptstuff[lno]])
+ newstuff.append('\\end_layout')
+ did_one_par = True
+ for c in contents:
+ if did_one_par:
+ newstuff.extend(['', '\\begin_layout Plain Layout', ''])
+ else:
+ did_one_par = True
+ newstuff.extend(c)
+ newstuff.append('\\end_layout')
+
+ newstuff.extend(['', '\\end_inset', '', '\\end_layout', ''])
+
+ document.body[start:end] = newstuff
+
+ file_pos += len(newstuff) - (end - start)
+
+
+def revert_chunks(document):
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset Flex Chunk", i)
+ if i == -1:
+ return
+
+ iend = find_end_of_inset(document.body, i)
+ if iend == -1:
+ document.warning("Can't find end of Chunk!")
+ i += 1
+ continue
+
+ # Look for optional argument
+ optarg = ""
+ ostart = find_token(document.body, "\\begin_inset Argument 1", i, iend)
+ if ostart != -1:
+ oend = find_end_of_inset(document.body, ostart)
+ k = find_token(document.body, "\\begin_layout Plain Layout", ostart, oend)
+ if k == -1:
+ document.warning("Malformed LyX document: Can't find argument contents!")
+ else:
+ m = find_end_of_layout(document.body, k)
+ optarg = "".join(document.body[k+1:m])
+
+ # We now remove the optional argument, so we have something
+ # uniform on which to work
+ document.body[ostart : oend + 1] = []
+ # iend is now invalid
+ iend = find_end_of_inset(document.body, i)
+
+ retval = get_containing_layout(document.body, i)
+ if not retval:
+ document.warning("Can't find containing layout for Chunk!")
+ i = iend
+ continue
+ (lname, lstart, lend, pstart) = retval
+ # we now want to work through the various paragraphs, and collect their contents
+ parlist = []
+ k = i
+ while True:
+ k = find_token(document.body, "\\begin_layout Plain Layout", k, lend)
+ if k == -1:
+ break
+ j = find_end_of_layout(document.body, k)
+ if j == -1:
+ document.warning("Can't find end of layout inside chunk!")
+ break
+ parlist.append(document.body[k+1:j])
+ k = j
+ # we now need to wrap all of these paragraphs in chunks
+ newlines = []
+ newlines.extend(["\\begin_layout Chunk", "", "<<" + optarg + ">>=", "\\end_layout", ""])
+ for stuff in parlist:
+ newlines.extend(["\\begin_layout Chunk"] + stuff + ["\\end_layout", ""])
+ newlines.extend(["\\begin_layout Chunk", "", "@", "\\end_layout", ""])
+ # replace old content with new content
+ document.body[lstart : lend + 1] = newlines
+ i = lstart + len(newlines)
+
+