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 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 = ""
params.reverse()
if document.body[i+1]: del document.body[i+1]
- document.body[i] = document.body[i] + " " + template[0]+ ', "' + filename[0] + '", " '+ "".join(params[1:]) + '"'
+ document.body[i] = document.body[i] + " " + template[0]+ ', "' + filename[0] + '", " '+ " ".join(params[1:]) + '"'
i = 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]
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
vspace_top = document.body[i].find("\\added_space_top")
tmp_list = document.body[i][vspace_top:].split()
vspace_top_value = tmp_list[1]
- document.body[i] = document.body[i][:vspace_top] + "".join(tmp_list[2:])
+ document.body[i] = document.body[i][:vspace_top] + " ".join(tmp_list[2:])
if vspace_bot != -1:
# the position could be change because of the removal of other
vspace_bot = document.body[i].find("\\added_space_bottom")
tmp_list = document.body[i][vspace_bot:].split()
vspace_bot_value = tmp_list[1]
- document.body[i] = document.body[i][:vspace_bot] + "".join(tmp_list[2:])
+ document.body[i] = document.body[i][:vspace_bot] + " ".join(tmp_list[2:])
document.body[i] = document.body[i].strip()
i = i + 1
def revert_float(document):
- " Revert sideway floats. "
+ " Revert sideways floats. "
i = 0
while 1:
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)
# 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
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
i = i + 10
##
-# Convertion hub
+# Conversion hub
#
supported_versions = ["1.4.%d" % i for i in range(3)] + ["1.4"]