#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
#
# 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.2"""
- find_tokens, find_tokens_backwards, find_beginning_of, find_end_of, find_re, \
+ find_tokens, find_tokens_backwards, \
+ find_beginning_of, find_end_of, find_re, \
# Private helper functions
def get_layout(line, default_layout):
# Private helper functions
def get_layout(line, default_layout):
tokens = ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"]
while i != -1:
tokens = ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"]
while i != -1:
return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset")
return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset")
return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
def find_end_of_tabular(lines, i):
return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
def find_end_of_tabular(lines, i):
return find_end_of(lines, i, "<lyxtabular", "</lyxtabular")
def get_tabular_lines(lines, i):
return find_end_of(lines, i, "<lyxtabular", "</lyxtabular")
def get_tabular_lines(lines, i):
-#
-# Change \begin_float .. \end_float into \begin_inset Float .. \end_inset
-#
-def remove_oldfloat(file):
- lines = file.body
+def remove_oldfloat(document):
+ " Change \begin_float .. \end_float into \begin_inset Float .. \end_inset"
+ lines = document.body
i = find_token(lines, "\\begin_float", i)
if i == -1:
break
# There are no nested floats, so finding the end of the float is simple
j = find_token(lines, "\\end_float", i+1)
i = find_token(lines, "\\begin_float", i)
if i == -1:
break
# There are no nested floats, so finding the end of the float is simple
j = find_token(lines, "\\end_float", i+1)
- floattype = string.split(lines[i])[1]
- if not floats.has_key(floattype):
- file.warning("Error! Unknown float type " + floattype)
+ floattype = lines[i].split()[1]
+ if floattype not in floats:
+ document.warning("Error! Unknown float type " + floattype)
# as extra '\foo default' commands are ignored.
# In fact, it might be safer to output '\foo default' for all
# font attributes.
# as extra '\foo default' commands are ignored.
# In fact, it might be safer to output '\foo default' for all
# font attributes.
flag = 0
for token in font_tokens:
if find_token(lines, token, k, i) != -1:
flag = 0
for token in font_tokens:
if find_token(lines, token, k, i) != -1:
pextra_type2_rexp2 = re.compile(r".*(\\layout|\\pextra_type\s+2)")
pextra_widthp = re.compile(r"\\pextra_widthp")
pextra_type2_rexp2 = re.compile(r".*(\\layout|\\pextra_type\s+2)")
pextra_widthp = re.compile(r"\\pextra_widthp")
# collect more paragraphs to the minipage
count = count+1
if j == -1 or not check_token(lines[j], "\\layout"):
# collect more paragraphs to the minipage
count = count+1
if j == -1 or not check_token(lines[j], "\\layout"):
i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
if i == -1:
break
j = i+1
i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
if i == -1:
break
j = i+1
# \end_inset is for ert inside a tabular cell. The other tokens
# are obvious.
j = find_tokens(lines, ["\\latex default", "\\layout", "\\begin_inset", "\\end_inset", "\\end_float", "\\the_end"],
# \end_inset is for ert inside a tabular cell. The other tokens
# are obvious.
j = find_tokens(lines, ["\\latex default", "\\layout", "\\begin_inset", "\\end_inset", "\\end_float", "\\the_end"],
k2 = find_re(lines, ert_rexp, k, j)
inset = hfill = specialchar = 0
if k2 == -1:
k2 = find_re(lines, ert_rexp, k, j)
inset = hfill = specialchar = 0
if k2 == -1:
if new == []:
# This is not necessary, but we want the output to be
# as similar as posible to the lyx format
if new == []:
# This is not necessary, but we want the output to be
# as similar as posible to the lyx format
-# ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future.
-def remove_oldertinset(file):
- lines = file.body
+def remove_oldertinset(document):
+ " ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future."
+ lines = document.body
i = find_token(lines, "\\begin_inset ERT", i)
if i == -1:
break
j = find_end_of_inset(lines, i)
k = find_token(lines, "\\layout", i+1)
i = find_token(lines, "\\begin_inset ERT", i)
if i == -1:
break
j = find_end_of_inset(lines, i)
k = find_token(lines, "\\layout", i+1)
oldunits = ["pt", "cm", "in", "text%", "col%"]
def get_length(lines, name, start, end):
oldunits = ["pt", "cm", "in", "text%", "col%"]
def get_length(lines, name, start, end):
i = find_token(lines, name, start, end)
if i == -1:
return ""
i = find_token(lines, name, start, end)
if i == -1:
return ""
return x[2]+oldunits[int(x[1])]
def write_attribute(x, token, value):
return x[2]+oldunits[int(x[1])]
def write_attribute(x, token, value):
i = find_token(lines, "\\begin_inset Figure", i)
if i == -1:
break
j = find_end_of_inset(lines, i)
i = find_token(lines, "\\begin_inset Figure", i)
if i == -1:
break
j = find_end_of_inset(lines, i)
- if ( len(string.split(lines[i])) > 2 ):
- lyxwidth = string.split(lines[i])[3]+"pt"
- lyxheight = string.split(lines[i])[4]+"pt"
+ if ( len(lines[i].split()) > 2 ):
+ lyxwidth = lines[i].split()[3]+"pt"
+ lyxheight = lines[i].split()[4]+"pt"
i = find_re(lines, regexp, i)
if i == -1:
break
for k in get_tabular_lines(lines, i):
if check_token(lines[k], "<lyxtabular"):
i = find_re(lines, regexp, i)
if i == -1:
break
for k in get_tabular_lines(lines, i):
if check_token(lines[k], "<lyxtabular"):
if line_re.match(lines[k]):
lines[k] = re.sub(attr_re, "", lines[k])
if line_re.match(lines[k]):
lines[k] = re.sub(attr_re, "", lines[k])
def setHeaderFooterRows(hr, fhr, fr, lfr, rows_, row_info):
def setHeaderFooterRows(hr, fhr, fr, lfr, rows_, row_info):
longtable_re = re.compile(r'islongtable="(\w)"')
ltvalues_re = re.compile(r'endhead="(-?\d*)" endfirsthead="(-?\d*)" endfoot="(-?\d*)" endlastfoot="(-?\d*)"')
lt_features_re = re.compile(r'(endhead="-?\d*" endfirsthead="-?\d*" endfoot="-?\d*" endlastfoot="-?\d*")')
longtable_re = re.compile(r'islongtable="(\w)"')
ltvalues_re = re.compile(r'endhead="(-?\d*)" endfirsthead="(-?\d*)" endfoot="(-?\d*)" endlastfoot="(-?\d*)"')
lt_features_re = re.compile(r'(endhead="-?\d*" endfirsthead="-?\d*" endfoot="-?\d*" endlastfoot="-?\d*")')
-# Figure insert are hidden feature of lyx 1.1.6. This might be removed in the future.
-def fix_oldfloatinset(file):
- lines = file.body
+def fix_oldfloatinset(document):
+ " Figure insert are hidden feature of lyx 1.1.6. This might be removed in the future."
+ lines = document.body
new = ["\\begin_inset Note", "collapsed true", ""]
j = find_token(lines, "\\end_inset", i)
if j == -1:
new = ["\\begin_inset Note", "collapsed true", ""]
j = find_token(lines, "\\end_inset", i)
if j == -1:
- new = new + ['\layout %s' % file.default_layout, ""]
- tmp = string.split(line, '\\')
+ new = new + ['\layout %s' % document.default_layout, ""]
+ tmp = line.split('\\')
i = find_token(lines, "\\use_amsmath", 0)
if i == -1:
return
lines[i+1:i+1] = ["\\use_natbib 0",
i = find_token(lines, "\\use_amsmath", 0)
if i == -1:
return
lines[i+1:i+1] = ["\\use_natbib 0",
convert = [[220, [change_header, change_listof, fix_oldfloatinset,
update_tabular, update_longtables, remove_pextra,
remove_oldfloat, remove_figinset, remove_oldertinset,
convert = [[220, [change_header, change_listof, fix_oldfloatinset,
update_tabular, update_longtables, remove_pextra,
remove_oldfloat, remove_figinset, remove_oldertinset,