def revert_flex_inset(document, name, LaTeXname, position):
" Convert flex insets to TeX code "
- i = 0
- z = 0
+ i = position
while True:
- i = find_token(document.body, '\\begin_inset Flex ' + name, position)
+ i = find_token(document.body, '\\begin_inset Flex ' + name, i)
if i == -1:
return
- else:
- z = find_end_of_inset(document.body, i)
- if z == -1:
- document.warning("Malformed LyX document: Can't find end of Flex " + name + " inset.")
- return
- # remove the \end_inset
- document.body[z - 2:z + 1] = put_cmd_in_ert("}")
- # we need to reset character layouts if necessary
- j = find_token(document.body, '\\emph on', i)
- k = find_token(document.body, '\\noun on', i)
- l = find_token(document.body, '\\series', i)
- m = find_token(document.body, '\\family', i)
- n = find_token(document.body, '\\shape', i)
- o = find_token(document.body, '\\color', i)
- p = find_token(document.body, '\\size', i)
- q = find_token(document.body, '\\bar under', i)
- r = find_token(document.body, '\\uuline on', i)
- s = find_token(document.body, '\\uwave on', i)
- t = find_token(document.body, '\\strikeout on', i)
- if j != -1 and j < z:
- document.body.insert(z-2, "\\emph default")
- if k != -1 and k < z:
- document.body.insert(z-2, "\\noun default")
- if l != -1 and l < z:
- document.body.insert(z-2, "\\series default")
- if m != -1 and m < z:
- document.body.insert(z-2, "\\family default")
- if n != -1 and n < z:
- document.body.insert(z-2, "\\shape default")
- if o != -1 and o < z:
- document.body.insert(z-2, "\\color inherit")
- if p != -1 and p < z:
- document.body.insert(z-2, "\\size default")
- if q != -1 and q < z:
- document.body.insert(z-2, "\\bar default")
- if r != -1 and r < z:
- document.body.insert(z-2, "\\uuline default")
- if s != -1 and s < z:
- document.body.insert(z-2, "\\uwave default")
- if t != -1 and t < z:
- document.body.insert(z-2, "\\strikeout default")
- document.body[i:i+4] = put_cmd_in_ert(LaTeXname + "{")
+ z = find_end_of_inset(document.body, i)
+ if z == -1:
+ document.warning("Malformed LyX document: Can't find end of Flex " + name + " inset.")
+ return
+ # remove the \end_inset
+ document.body[z - 2:z + 1] = put_cmd_in_ert("}")
+ # we need to reset character layouts if necessary
+ j = find_token(document.body, '\\emph on', i)
+ k = find_token(document.body, '\\noun on', i)
+ l = find_token(document.body, '\\series', i)
+ m = find_token(document.body, '\\family', i)
+ n = find_token(document.body, '\\shape', i)
+ o = find_token(document.body, '\\color', i)
+ p = find_token(document.body, '\\size', i)
+ q = find_token(document.body, '\\bar under', i)
+ r = find_token(document.body, '\\uuline on', i)
+ s = find_token(document.body, '\\uwave on', i)
+ t = find_token(document.body, '\\strikeout on', i)
+ if j != -1 and j < z:
+ document.body.insert(z-2, "\\emph default")
+ if k != -1 and k < z:
+ document.body.insert(z-2, "\\noun default")
+ if l != -1 and l < z:
+ document.body.insert(z-2, "\\series default")
+ if m != -1 and m < z:
+ document.body.insert(z-2, "\\family default")
+ if n != -1 and n < z:
+ document.body.insert(z-2, "\\shape default")
+ if o != -1 and o < z:
+ document.body.insert(z-2, "\\color inherit")
+ if p != -1 and p < z:
+ document.body.insert(z-2, "\\size default")
+ if q != -1 and q < z:
+ document.body.insert(z-2, "\\bar default")
+ if r != -1 and r < z:
+ document.body.insert(z-2, "\\uuline default")
+ if s != -1 and s < z:
+ document.body.insert(z-2, "\\uwave default")
+ if t != -1 and t < z:
+ document.body.insert(z-2, "\\strikeout default")
+ document.body[i:i + 4] = put_cmd_in_ert(LaTeXname + "{")
i += 1
i = find_token(document.body, name + ' on', i)
if i == -1:
return changed
+ j = find_token(document.body, name + ' default', i)
+ k = find_token(document.body, name + ' on', i + 1)
+ # if there is no default set, the style ends with the layout
+ # assure hereby that we found the correct layout end
+ if j != -1 and (j < k or k == -1):
+ document.body[j:j+1] = put_cmd_in_ert("}")
else:
- j = find_token(document.body, name + ' default', i)
- k = find_token(document.body, name + ' on', i + 1)
- # if there is no default set, the style ends with the layout
- # assure hereby that we found the correct layout end
- if j != -1 and (j < k or k ==-1):
- document.body[j:j+1] = put_cmd_in_ert("}")
+ j = find_token(document.body, '\\end_layout', i)
+ document.body[j:j] = put_cmd_in_ert("}")
+ document.body[i:i + 1] = put_cmd_in_ert(LaTeXname + "{")
+ changed = True
+ i += 1
+
+
+def revert_layout_command(document, name, LaTeXname, position):
+ " Reverts a command from a layout to TeX code "
+ i = position
+ while True:
+ i = find_token(document.body, '\\begin_layout ' + name, i)
+ if i == -1:
+ return
+ k = -1
+ # find the next layout
+ j = i + 1
+ while k == -1:
+ j = find_token(document.body, '\\begin_layout', j)
+ l = len(document.body)
+ # if nothing was found it was the last layout of the document
+ if j == -1:
+ document.body[l - 4:l - 4] = put_cmd_in_ert("}")
+ k = 0
+ # exclude plain layout because this can be TeX code or another inset
+ elif document.body[j] != '\\begin_layout Plain Layout':
+ document.body[j - 2:j - 2] = put_cmd_in_ert("}")
+ k = 0
else:
- j = find_token(document.body, '\\end_layout', i)
- document.body[j:j] = put_cmd_in_ert("}")
- document.body[i:i+1] = put_cmd_in_ert(LaTeXname + "{")
- changed = True
+ j += 1
+ document.body[i] = '\\begin_layout Standard'
+ document.body[i + 1:i + 1] = put_cmd_in_ert(LaTeXname + "{")
i += 1
i = find_token(document.body, '\\begin_inset Argument', i)
if i == -1:
return
- document.body[i] = "\\begin_inset OptArg"
- i += 1
+ document.body[i] = "\\begin_inset OptArg"
+ i += 1
def revert_makebox(document):
i = find_token(document.body, '\\begin_inset Box Frameless', i)
if i == -1:
return
- else:
- z = find_end_of_inset(document.body, i)
- if z == -1:
- document.warning("Malformed LyX document: Can't find end of box inset.")
- return
- j = find_token(document.body, 'use_makebox 1', i)
- # assure we found the makebox of the current box
- if j > i + 7 or j == -1:
- return
- else:
- # remove the \end_inset
- document.body[z - 2:z + 1] = put_cmd_in_ert("}")
- # determine the alignment
- k = find_token(document.body, 'hor_pos', j - 4)
- align = document.body[k][9]
- # determine the width
- l = find_token(document.body, 'width "', j + 1)
- length = document.body[l][7:]
- # remove trailing '"'
- length = length[:-1]
- # latex_length returns "bool,length"
- length = latex_length(length).split(",")[1]
- subst = "\\makebox[" + length + "][" \
- + align + "]{"
- document.body[i:i+13] = put_cmd_in_ert(subst)
+ z = find_end_of_inset(document.body, i)
+ if z == -1:
+ document.warning("Malformed LyX document: Can't find end of box inset.")
+ return
+ j = find_token(document.body, 'use_makebox 1', i)
+ # assure we found the makebox of the current box
+ if j > i + 7 or j == -1:
+ return
+ # remove the \end_inset
+ document.body[z - 2:z + 1] = put_cmd_in_ert("}")
+ # determine the alignment
+ k = find_token(document.body, 'hor_pos', j - 4)
+ align = document.body[k][9]
+ # determine the width
+ l = find_token(document.body, 'width "', j + 1)
+ length = document.body[l][7:]
+ # remove trailing '"'
+ length = length[:-1]
+ # latex_length returns "bool,length"
+ length = latex_length(length).split(",")[1]
+ subst = "\\makebox[" + length + "][" \
+ + align + "]{"
+ document.body[i:i + 13] = put_cmd_in_ert(subst)
i += 1
def revert_IEEEtran(document):
" Convert IEEEtran layouts and styles to TeX code "
+ if document.textclass != "IEEEtran":
+ return
+
revert_flex_inset(document, "IEEE membership", "\\IEEEmembership", 0)
revert_flex_inset(document, "Lowercase", "\\MakeLowercase", 0)
+ revert_layout_command(document, "Special Paper Notice", "\\IEEEspecialpapernotice", 0)
+ revert_layout_command(document, "After Title Text", "\\IEEEaftertitletext", 0)
+ revert_layout_command(document, "Page headings", "\\markboth", 0)
+ revert_layout_command(document, "Publication ID", "\\IEEEpubid", 0)
+ note = '\\begin_layout Standard\n' \
+ + '\\begin_inset Note Note\n' \
+ + 'status open\n\n' \
+ + '\\begin_layout Plain Layout\n\n' \
+ + '\series bold\n' \
+ + 'IMPORTANT NOTICE!!!:\n' \
+ + '\\series default\n' \
+ + ' \n' \
+ + '\\color red\n' \
+ + 'This document was created from a newer LyX version.\n' \
+ + ' To be able to view/export it with LyX 1.6.x or earlier, the title and author\n' \
+ + ' must be specified by using TeX code.\n' \
+ + ' Do not use then the standard title and author environment!\n' \
+ + '\\end_layout\n\n' \
+ + '\\end_inset\n\n\n' \
+ + '\\end_layout\n\n'
+ # insert a note that title and author must be given in TeX code
+ document.body.insert(0, note)
+ # we need to revert title and author to TeX code to set \maketitle correctly
+ revert_layout_command(document, "Title", "\\title", 0)
+ revert_layout_command(document, "Author", "\\author", 0)
+ # write \markboth code to the preamble
+ insert_to_preamble(0, document,
+ '% Commands inserted by lyx2lyx\n'
+ + '% protect \\markboth against an old bug reintroduced in babel >= 3.8g\n'
+ + '\\let\\oldforeign@language\\foreign@language\n'
+ + '\\DeclareRobustCommand{\\foreign@language}[1]{%\n'
+ + ' \\lowercase{\\oldforeign@language{#1}}}\n')
+ # set maketitle
+ y = find_token(document.body, '\\begin_layout Abstract', 0)
+ if y == -1:
+ document.warning("Malformed LyX document: Can't find abstract of IEEEtran paper.")
+ return
+ maketitle = ['\\begin_layout Standard\n', \
+ '\\begin_inset ERT\n', \
+ 'status collapsed\n', \
+ '\\begin_layout Plain Layout\n', \
+ '\\backslash\n', \
+ 'maketitle \n', \
+ '\\end_layout\n', \
+ '\\end_inset\n', \
+ '\\end_layout\n']
+ document.body[y:y] = maketitle
##