#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
""" Convert files to the file format generated by lyx 1.4"""
from os import access, F_OK
import os.path
from parser_tools import check_token, find_token, \
- get_value, del_token, is_nonempty_line, \
+ get_value, is_nonempty_line, \
find_tokens, find_end_of, find_beginning_of, find_token_exact, find_tokens_exact, \
find_re, find_tokens_backwards
from sys import stdin
"Finds the matching \end_inset"
return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
+def del_token(lines, token, start, end):
+ """ del_token(lines, token, start, end) -> int
+
+ Find the lower line in lines where token is the first element and
+ delete that line.
+
+ Returns the number of lines remaining."""
+
+ k = find_token_exact(lines, token, start, end)
+ if k == -1:
+ return end
+ else:
+ del lines[k]
+ return end - 1
+
# End of helper functions
####################################################################
def remove_color_default(document):
" Remove \color default"
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\color default", i)
if i == -1:
return
" \InsetSpace ~ -> \SpecialChar ~"
regexp = re.compile(r'(.*)(\\InsetSpace\s+)(\S+)')
i = 0
- while 1:
+ while True:
i = find_re(document.body, regexp, i)
if i == -1:
break
def lyx_support_escape(lab):
- " Equivalent to lyx::support::escape()"
+ " Equivalent to pre-unicode lyx::support::escape()"
hexdigit = ['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
enc = ""
"\\begin_inset LatexCommand \\eqref -> ERT"
regexp = re.compile(r'^\\begin_inset\s+LatexCommand\s+\\eqref')
i = 0
- while 1:
+ while True:
i = find_re(document.body, regexp, i)
if i == -1:
break
def remove_insetparent(document):
" Remove \lyxparent"
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_inset LatexCommand \\lyxparent", i)
if i == -1:
break
external_rexp = re.compile(r'\\begin_inset External ([^,]*),"([^"]*)",')
external_header = "\\begin_inset External"
i = 0
- while 1:
+ while True:
i = find_token(document.body, external_header, i)
if i == -1:
break
" Revert inset External."
external_header = "\\begin_inset External"
i = 0
- while 1:
+ while True:
i = find_token(document.body, external_header, i)
if i == -1:
break
" Revert inset External. (part II)"
draft_token = '\tdraft'
i = 0
- while 1:
+ while True:
i = find_token(document.body, '\\begin_inset External', i)
if i == -1:
break
" Convert \\layout comment"
i = 0
comment = "\\layout Comment"
- while 1:
+ while True:
i = find_token(document.body, comment, i)
if i == -1:
return
'\\layout %s' % document.default_layout]
i = i + 7
- while 1:
+ while True:
old_i = i
i = find_token(document.body, "\\layout", i)
if i == -1:
#but if this happens deal with it greacefully adding
#the missing \end_deeper.
i = len(document.body) - 1
- document.body[i:i] = ["\end_deeper",""]
+ document.body[i:i] = ["\\end_deeper",""]
return
else:
del document.body[i]
def revert_comment(document):
" Revert comments"
i = 0
- while 1:
+ while True:
i = find_tokens(document.body, ["\\begin_inset Comment", "\\begin_inset Greyedout"], i)
if i == -1:
i = i + 1
struct_stack = ["\\layout"]
- while 1:
+ while True:
i = find_tokens(document.body, ["\\begin_inset", "\\end_inset", "\\layout",
"\\begin_deeper", "\\end_deeper", "\\the_end"], i)
document.body.insert(i,"")
document.body.insert(i,"\\end_layout")
i = i + 3
+ # consecutive begin_deeper only insert one end_layout
+ while document.body[i].startswith('\\begin_deeper'):
+ i += 1
struct_stack.append(token)
continue
def rm_end_layout(document):
" Remove \end_layout"
i = 0
- while 1:
+ while True:
i = find_token(document.body, '\\end_layout', i)
if i == -1:
def rm_body_changes(document):
" Remove body changes."
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\change_", i)
if i == -1:
return
def layout2begin_layout(document):
" \layout -> \begin_layout "
i = 0
- while 1:
+ while True:
i = find_token(document.body, '\\layout', i)
if i == -1:
return
def begin_layout2layout(document):
" \begin_layout -> \layout "
i = 0
- while 1:
+ while True:
i = find_token(document.body, '\\begin_layout', i)
if i == -1:
return
" Convert table valignment, center -> middle"
regexp = re.compile(r'^\\begin_inset\s+Tabular')
i = 0
- while 1:
+ while True:
i = find_re(document.body, regexp, i)
if i == -1:
return
" Convert table valignment, middle -> center"
regexp = re.compile(r'^\\begin_inset\s+Tabular')
i = 0
- while 1:
+ while True:
i = find_re(document.body, regexp, i)
if i == -1:
return
attribute_values = ['default', 'default', 'default', 'default',
'default', 'default', 'default', 'none', document.language]
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_layout", i)
if i == -1:
return
def convert_note(document):
" Convert Notes. "
i = 0
- while 1:
+ while True:
i = find_tokens(document.body, ["\\begin_inset Note",
"\\begin_inset Comment",
"\\begin_inset Greyedout"], i)
" Revert Notes. "
note_header = "\\begin_inset Note "
i = 0
- while 1:
+ while True:
i = find_token(document.body, note_header, i)
if i == -1:
break
def convert_box(document):
" Convert Boxes. "
i = 0
- while 1:
+ while True:
i = find_tokens(document.body, ["\\begin_inset Boxed",
"\\begin_inset Doublebox",
"\\begin_inset Frameless",
" Revert Boxes."
box_header = "\\begin_inset Box "
i = 0
- while 1:
+ while True:
i = find_token(document.body, box_header, i)
if i == -1:
break
i = i + 1
-def convert_collapsable(document):
+def convert_collapsible(document):
" Convert collapsed insets. "
i = 0
- while 1:
+ while True:
i = find_tokens_exact(document.body, ["\\begin_inset Box",
"\\begin_inset Branch",
"\\begin_inset CharStyle",
# If, however, we find a line starting '\begin_layout'
# (_always_ present) then break with a warning message
i = i + 1
- while 1:
+ while True:
if (document.body[i] == "collapsed false"):
document.body[i] = "status open"
break
i = i + 1
-def revert_collapsable(document):
+def revert_collapsible(document):
" Revert collapsed insets. "
i = 0
- while 1:
+ while True:
i = find_tokens_exact(document.body, ["\\begin_inset Box",
"\\begin_inset Branch",
"\\begin_inset CharStyle",
# If, however, we find a line starting '\begin_layout'
# (_always_ present) then break with a warning message
i = i + 1
- while 1:
+ while True:
if (document.body[i] == "status open"):
document.body[i] = "collapsed false"
break
def convert_ert(document):
" Convert ERT. "
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_inset ERT", i)
if i == -1:
break
# If, however, we find a line starting '\begin_layout'
# (_always_ present) then break with a warning message
i = i + 1
- while 1:
+ while True:
if (document.body[i] == "status Open"):
document.body[i] = "status open"
break
def revert_ert(document):
" Revert ERT. "
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_inset ERT", i)
if i == -1:
break
# If, however, we find a line starting '\begin_layout'
# (_always_ present) then break with a warning message
i = i + 1
- while 1:
+ while True:
if (document.body[i] == "status open"):
document.body[i] = "status Open"
break
inner_pos = ["c","t","b","s"]
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_inset Minipage", i)
if i == -1:
return
# Convert the insets
i = 0
- while 1:
+ while True:
i = find_tokens(document.body, tokens, i)
if i == -1:
return
len = '%f\\' % len2value(len) + special
# Convert LyX units to LaTeX units
- for unit in units.keys():
+ for unit in list(units.keys()):
if len.find(unit) != -1:
len = '%f' % (len2value(len) / 100) + units[unit]
break
pos = ['t', 'c', 'b']
inner_pos = ['c', 't', 'b', 's']
i = 0
- while 1:
+ while True:
i = find_token(document.body, '\\begin_inset Frameless', i)
if i == -1:
return
'inner_pos':1, 'use_parbox':'0', 'width':'100col%',
'special':'none', 'height':'1in',
'height_special':'totalheight', 'collapsed':'false'}
- for key in params.keys():
+ for key in list(params.keys()):
value = get_value(document.body, key, i, j).replace('"', '')
if value != "":
if key == 'position':
def remove_branches(document):
" Remove branches. "
i = 0
- while 1:
+ while True:
i = find_token(document.header, "\\branch", i)
if i == -1:
break
del document.header[i:j+1]
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_inset Branch", i)
if i == -1:
return
# If, however, we find a line starting '\layout'
# (_always_ present) then break with a warning message
collapsed_found = 0
- while 1:
+ while True:
if (document.body[i][:9] == "collapsed"):
del document.body[i]
collapsed_found = 1
def convert_float(document):
" Convert sideway floats. "
i = 0
- while 1:
+ while True:
i = find_token_exact(document.body, '\\begin_inset Float', i)
if i == -1:
return
# If, however, we find a line starting '\begin_layout'
# (_always_ present) then break with a warning message
i = i + 1
- while 1:
+ while True:
if (document.body[i][:4] == "wide"):
document.body.insert(i + 1, 'sideways false')
break
def revert_float(document):
- " Revert sideway floats. "
+ " Revert sideways floats. "
i = 0
- while 1:
+ while True:
i = find_token_exact(document.body, '\\begin_inset Float', i)
if i == -1:
return
+ line = document.body[i]
+ r = re.compile(r'\\begin_inset Float (.*)$')
+ m = r.match(line)
+ floattype = m.group(1)
+ if floattype != "figure" and floattype != "table":
+ i = i + 1
+ continue
j = find_end_of_inset(document.body, i)
if j == -1:
document.warning("Malformed lyx document: Missing '\\end_inset'.")
i = i + 1
continue
if get_value(document.body, 'sideways', i, j) != "false":
- document.warning("Conversion of 'sideways true' not yet implemented.")
- # Don't remove 'sideways' so that people will get warnings by lyx
+ l = find_token(document.body, "\\begin_layout Standard", i + 1, j)
+ if l == -1:
+ document.warning("Malformed LyX document: Missing `\\begin_layout Standard' in Float inset.")
+ return
+ document.body[j] = '\\layout Standard\n\\begin_inset ERT\nstatus Collapsed\n\n' \
+ '\\layout Standard\n\n\n\\backslash\n' \
+ 'end{sideways' + floattype + '}\n\n\\end_inset\n'
+ del document.body[i+1:l-1]
+ document.body[i] = '\\begin_inset ERT\nstatus Collapsed\n\n' \
+ '\\layout Standard\n\n\n\\backslash\n' \
+ 'begin{sideways' + floattype + '}\n\n\\end_inset\n\n'
+ add_to_preamble(document,
+ ['\\usepackage{rotfloat}\n'])
i = i + 1
continue
del_token(document.body, 'sideways', i, j)
""" Add extension to documentnames of insetgraphics if necessary.
"""
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_inset Graphics", i)
if i == -1:
return
return
i = i + 1
filename = document.body[j].split()[1]
- absname = os.path.normpath(os.path.join(document.dir, filename))
- if document.input == stdin and not os.path.isabs(filename):
+ if document.dir == u'' and not os.path.isabs(filename):
# We don't know the directory and cannot check the document.
# We could use a heuristic and take the current directory,
# and we could try to find out if documentname has an extension,
# but that would be just guesses and could be wrong.
- document.warning("""Warning: Can not determine whether document
+ document.warning("""Warning: Cannot determine whether document
%s
needs an extension when reading from standard input.
You may need to correct the document manually or run
lyx2lyx again with the .lyx document as commandline argument.""" % filename)
continue
+ absname = os.path.normpath(os.path.join(document.dir, filename))
# This needs to be the same algorithm as in pre 233 insetgraphics
if access(absname, F_OK):
continue
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_layout Author", i)
if i == -1:
return
def convert_bullets(document):
" Convert bullets. "
i = 0
- while 1:
+ while True:
i = find_token(document.header, "\\bullet", i)
if i == -1:
return
def revert_bullets(document):
" Revert bullets. "
i = 0
- while 1:
+ while True:
i = find_token(document.header, "\\bullet", i)
if i == -1:
return
"\\leftindent"
i = 0
- while 1:
+ while True:
i = find_token(document.body, '\\begin_layout', i)
if i == -1:
return
i = i + 1
- while 1:
+ while True:
if body[i].strip() and body[i].split()[0] not in allowed_parameters:
break
'\\emph', '\\numeric', '\\bar', '\\noun',
'\\color', '\\lang']
i = 0
- while 1:
+ while True:
i = find_token(document.body, '\\begin_inset ERT', i)
if i == -1:
return
# convert non-standard paragraphs to standard
k = i
- while 1:
+ while True:
k = find_token(document.body, "\\begin_layout", k, j)
if k == -1:
break
# insert an empty paragraph before each paragraph but the first
k = i
first_pagraph = 1
- while 1:
+ while True:
k = find_token(document.body, "\\begin_layout", k, j)
if k == -1:
break
# convert \\newline to new paragraph
k = i
- while 1:
+ while True:
k = find_token(document.body, "\\newline", k, j)
if k == -1:
break
document.body[k:k+1] = ["\\end_layout", "", '\\begin_layout %s' % document.default_layout]
- k = k + 4
- j = j + 3
+ k = k + 3
+ j = j + 2
# We need an empty line if document.default_layout == ''
- if document.body[k-1] != '':
- document.body.insert(k-1, '')
+ if document.body[k] != '':
+ document.body.insert(k, '')
k = k + 1
j = j + 1
i = i + 1
def revert_ert_paragraphs(document):
" Remove double paragraph breaks. "
i = 0
- while 1:
+ while True:
i = find_token(document.body, '\\begin_inset ERT', i)
if i == -1:
return
# replace paragraph breaks with \newline
k = i
- while 1:
+ while True:
k = find_token(document.body, "\\end_layout", k, j)
l = find_token(document.body, "\\begin_layout", k, j)
if k == -1 or l == -1:
# replace double \newlines with paragraph breaks
k = i
- while 1:
+ while True:
k = find_token(document.body, "\\newline", k, j)
if k == -1:
break
# Change language in the document body
regexp = re.compile(r'^\\lang\s+frenchb')
i = 0
- while 1:
+ while True:
i = find_re(document.body, regexp, i)
if i == -1:
break
return
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_layout SGML", i)
if i == -1:
i = i + 10
##
-# Convertion hub
+# Conversion hub
#
supported_versions = ["1.4.%d" % i for i in range(3)] + ["1.4"]
convert_table_valignment_middle, convert_breaks]],
[226, [convert_note]],
[227, [convert_box]],
- [228, [convert_collapsable, convert_ert]],
+ [228, [convert_collapsible, convert_ert]],
[229, [convert_minipage]],
[230, [convert_jurabib]],
[231, [convert_float]],
[230, [revert_float]],
[229, [revert_jurabib]],
[228, []],
- [227, [revert_collapsable, revert_ert]],
+ [227, [revert_collapsible, revert_ert]],
[226, [revert_box, revert_external_2]],
[225, [revert_note]],
[224, [rm_end_layout, begin_layout2layout, revert_end_document,