+ # first line of our insets
+ inset_start = i
+ # last line of our insets
+ inset_end = inset_start
+ # Are we at the end of a paragraph?
+ paragraph_end = 1
+ # start and end line numbers to delete if we convert this inset
+ del_lines = list()
+ # is this inset a lyxline above a paragraph?
+ top = list()
+ # raw inset information
+ lines = list()
+ # name of this inset
+ insets = list()
+ # font size of this inset
+ sizes = list()
+
+ # Detect subsequent lyxline, vspace and pagebreak insets created by convert_breaks()
+ n = 0
+ k = inset_start
+ while k < next_par:
+ if find_tokens(document.body, tokens, k) == k:
+ # inset to convert
+ lines.append(document.body[k].split())
+ insets.append(keys[lines[n][0]])
+ del_lines.append([k, k])
+ top.append(0)
+ sizes.append(size)
+ n = n + 1
+ inset_end = k
+ elif document.body[k].find("\\size") != -1:
+ # store font size
+ size = document.body[k].split()[1]
+ elif find_token(document.body, "\\begin_inset ERT", k) == k:
+ ert_begin = find_token(document.body, "\\layout", k) + 1
+ if ert_begin == 0:
+ document.warning("Malformed LyX document: Missing '\\layout'.")
+ continue
+ ert_end = find_end_of_inset(document.body, k)
+ if ert_end == -1:
+ document.warning("Malformed LyX document: Missing '\\end_inset'.")
+ continue
+ ert = ert2latex(document.body[ert_begin:ert_end], document.format - 1)
+ if (n > 0 and insets[n - 1] == "lyxline" and
+ ert == '\\vspace{-1\\parskip}\n'):
+ # vspace ERT created by convert_breaks() for top lyxline
+ top[n - 1] = 1
+ del_lines[n - 1][1] = ert_end
+ inset_end = ert_end
+ k = ert_end
+ else:
+ paragraph_end = 0
+ break
+ elif (n > 0 and insets[n - 1] == "vspace" and
+ find_token(document.body, "\\end_inset", k) == k):
+ # ignore end of vspace inset
+ del_lines[n - 1][1] = k
+ inset_end = k
+ elif is_nonempty_line(document.body[k]):
+ paragraph_end = 0
+ break
+ k = k + 1
+
+ # Determine space amount for vspace insets
+ spaceamount = list()
+ arguments = list()
+ for k in range(n):
+ if insets[k] == "vspace":
+ spaceamount.append(lines[k][2])
+ arguments.append(' ' + spaceamount[k] + ' ')
+ else:
+ spaceamount.append('')
+ arguments.append(' ')
+
+ # Can we convert to top paragraph parameters?
+ before = 0
+ if ((n == 3 and insets[0] == "newpage" and insets[1] == "vspace" and
+ insets[2] == "lyxline" and top[2]) or
+ (n == 2 and
+ ((insets[0] == "newpage" and insets[1] == "vspace") or
+ (insets[0] == "newpage" and insets[1] == "lyxline" and top[1]) or
+ (insets[0] == "vspace" and insets[1] == "lyxline" and top[1]))) or
+ (n == 1 and insets[0] == "lyxline" and top[0])):
+ # These insets have been created before a paragraph by
+ # convert_breaks()
+ before = 1
+
+ # Can we convert to bottom paragraph parameters?
+ after = 0
+ if ((n == 3 and insets[0] == "lyxline" and not top[0] and
+ insets[1] == "vspace" and insets[2] == "newpage") or
+ (n == 2 and
+ ((insets[0] == "lyxline" and not top[0] and insets[1] == "vspace") or
+ (insets[0] == "lyxline" and not top[0] and insets[1] == "newpage") or
+ (insets[0] == "vspace" and insets[1] == "newpage"))) or
+ (n == 1 and insets[0] == "lyxline" and not top[0])):
+ # These insets have been created after a paragraph by
+ # convert_breaks()
+ after = 1
+
+ if paragraph_start and paragraph_end:
+ # We are in a paragraph of our own.
+ # We must not delete this paragraph if it has parameters
+ if params == '':
+ # First try to merge with the previous paragraph.
+ # We try the previous paragraph first because we would
+ # otherwise need ERT for two subsequent vspaces.
+ prev_par = get_paragraph(document.body, this_par - 1, document.format - 1) + 1
+ if prev_par > 0 and not before:
+ prev_params = get_par_params(document.body, prev_par + 1)
+ ert = 0
+ # determine font size
+ prev_size = "normal"
+ k = prev_par + 1
+ while document.body[k][:1] == '\\' and document.body[k].split()[0] in prev_params:
+ k = k + 1
+ while k < this_par:
+ if document.body[k].find("\\size") != -1:
+ prev_size = document.body[k].split()[1]
+ break
+ elif document.body[k].find("\\begin_inset") != -1:
+ # skip insets
+ k = find_end_of_inset(document.body, k)
+ elif is_nonempty_line(document.body[k]):
+ break
+ k = k + 1
+ for k in range(n):
+ if (keywords_bot[insets[k]] in prev_params or
+ (insets[k] == "lyxline" and sizes[k] != prev_size)):
+ ert = 1
+ break
+ if not ert:
+ for k in range(n):
+ document.body.insert(prev_par + 1,
+ keywords_bot[insets[k]] + arguments[k])
+ del document.body[this_par+n:next_par-1+n]
+ i = this_par + n
+ continue
+ # Then try next paragraph
+ if next_par > 0 and not after:
+ next_params = get_par_params(document.body, next_par + 1)
+ ert = 0
+ while document.body[k][:1] == '\\' and document.body[k].split()[0] in next_params:
+ k = k + 1
+ # determine font size
+ next_size = "normal"
+ k = next_par + 1
+ while k < this_par:
+ if document.body[k].find("\\size") != -1:
+ next_size = document.body[k].split()[1]
+ break
+ elif is_nonempty_line(document.body[k]):
+ break
+ k = k + 1
+ for k in range(n):
+ if (keywords_top[insets[k]] in next_params or
+ (insets[k] == "lyxline" and sizes[k] != next_size)):
+ ert = 1
+ break
+ if not ert:
+ for k in range(n):
+ document.body.insert(next_par + 1,
+ keywords_top[insets[k]] + arguments[k])
+ del document.body[this_par:next_par-1]
+ i = this_par
+ continue
+ elif paragraph_start or paragraph_end:
+ # Convert to paragraph formatting if we are at the beginning or end
+ # of a paragraph and the resulting paragraph would not be empty
+ # The order is important: del and insert invalidate some indices
+ if paragraph_start:
+ keywords = keywords_top
+ else:
+ keywords = keywords_bot
+ ert = 0
+ for k in range(n):
+ if keywords[insets[k]] in params:
+ ert = 1
+ break
+ if not ert:
+ for k in range(n):
+ document.body.insert(this_par + 1,
+ keywords[insets[k]] + arguments[k])
+ for j in range(k, n):
+ del_lines[j][0] = del_lines[j][0] + 1
+ del_lines[j][1] = del_lines[j][1] + 1
+ del document.body[del_lines[k][0]:del_lines[k][1]+1]
+ deleted = del_lines[k][1] - del_lines[k][0] + 1
+ for j in range(k + 1, n):
+ del_lines[j][0] = del_lines[j][0] - deleted
+ del_lines[j][1] = del_lines[j][1] - deleted
+ i = this_par
+ continue
+
+ # Convert the first inset to ERT.
+ # The others are converted in the next loop runs (if they exist)
+ if insets[0] == "vspace":
+ document.body[i:i+1] = ['\\begin_inset ERT', 'status Collapsed', '',
+ '\\layout %s' % document.default_layout, '', '\\backslash ']
+ i = i + 6
+ if spaceamount[0][-1] == '*':
+ spaceamount[0] = spaceamount[0][:-1]
+ keep = 1
+ else:
+ keep = 0
+
+ # Replace defskip by the actual value
+ if spaceamount[0] == 'defskip':
+ spaceamount[0] = defskipamount
+
+ # LaTeX does not know \\smallskip* etc
+ if keep:
+ if spaceamount[0] == 'smallskip':
+ spaceamount[0] = '\\smallskipamount'
+ elif spaceamount[0] == 'medskip':
+ spaceamount[0] = '\\medskipamount'
+ elif spaceamount[0] == 'bigskip':
+ spaceamount[0] = '\\bigskipamount'
+ elif spaceamount[0] == 'vfill':
+ spaceamount[0] = '\\fill'
+
+ # Finally output the LaTeX code
+ if (spaceamount[0] == 'smallskip' or spaceamount[0] == 'medskip' or
+ spaceamount[0] == 'bigskip' or spaceamount[0] == 'vfill'):
+ document.body.insert(i, spaceamount[0] + '{}')
+ else :
+ if keep:
+ document.body.insert(i, 'vspace*{')
+ else:
+ document.body.insert(i, 'vspace{')
+ i = convert_ertbackslash(document.body, i, spaceamount[0], document.format - 1, document.default_layout)
+ document.body[i] = document.body[i] + '}'
+ i = i + 1
+ elif insets[0] == "lyxline":
+ document.body[i] = ''
+ latexsize = lyxsize2latexsize(size)
+ if latexsize == '':
+ document.warning("Could not convert LyX fontsize '%s' to LaTeX font size." % size)
+ latexsize = '\\normalsize'
+ i = insert_ert(document.body, i, 'Collapsed',
+ '\\lyxline{%s}' % latexsize,
+ document.format - 1, document.default_layout)
+ # We use \providecommand so that we don't get an error if native
+ # lyxlines are used (LyX writes first its own preamble and then
+ # the user specified one)
+ add_to_preamble(document,
+ ['% Commands inserted by lyx2lyx for lyxlines',
+ '\\providecommand{\\lyxline}[1]{',
+ ' {#1 \\vspace{1ex} \\hrule width \\columnwidth \\vspace{1ex}}'
+ '}'])
+ elif insets[0] == "newpage":
+ document.body[i] = ''
+ i = insert_ert(document.body, i, 'Collapsed', '\\newpage{}',
+ document.format - 1, document.default_layout)
+
+
+# Convert a LyX length into a LaTeX length
+def convert_len(len, special):