Small improvement to pre-historic file format conversion.

This commit is contained in:
José Matos 2018-04-28 14:57:57 +01:00
parent 1fe328666c
commit 91ded82b52

View File

@ -32,18 +32,17 @@ def regularise_header(document):
def find_next_space(line, j): def find_next_space(line, j):
""" Return position of next space or backslash, which one comes """ Return position of next space or backslash, which one comes
first, starting from position k, if not existing return last first, starting from position j, if none exists returns last
position in line.""" position in line (+1)."""
l = line.find(' ', j) space_pos = line.find(' ', j)
if l == -1: if space_pos == -1:
l = len(line) space_pos = len(line)
k = line.find('\\', j)
if k == -1:
k = len(line)
if k < l: bksl_pos = line.find('\\', j)
return k if bksl_pos == -1:
return l bksl_pos = len(line)
return min(space_pos, bksl_pos)
def regularise_body(document): def regularise_body(document):
@ -65,36 +64,38 @@ def regularise_body(document):
while i < len(document.body): while i < len(document.body):
line = document.body[i] line = document.body[i]
j = 0 j = 0
tmp = [] new_block = []
while j < len(line): while j < len(line):
k = line.find('\\', j) k = line.find('\\', j)
if k == -1: if k == -1:
tmp += [line[j:]] new_block += [line[j:]]
break break
if k != j: if k != j:
tmp += [line[j: k]] #document.warning("j=%d\tk=%d\t#%s#%s#" % (j,k,line,line[j: k]))
new_block += [line[j: k]]
j = k j = k
k = find_next_space(line, j+1) k = find_next_space(line, j+1)
# These tokens take the rest of the line
token = line[j+1:k] token = line[j+1:k]
# These tokens take the rest of the line
if token in getline_tokens: if token in getline_tokens:
tmp += [line[j:]] #document.warning("getline_token:%s\tj=%d\t\t#%s#%s#" % (token,j,line,line[j:]))
new_block += [line[j:]]
break break
# These tokens take no arguments # These tokens take no arguments
if token in noargs_tokens: if token in noargs_tokens:
tmp += [line[j:k]] new_block += [line[j:k]]
j = k j = k
continue continue
# These tokens take one argument # These tokens take one argument
if token in onearg_tokens: if token in onearg_tokens:
k = find_next_space(line, k + 1) k = find_next_space(line, k + 1)
tmp += [line[j:k]] new_block += [line[j:k]]
j = k j = k
continue continue
@ -104,29 +105,30 @@ def regularise_body(document):
inset = line[k+1: l] inset = line[k+1: l]
if inset == "Latex": if inset == "Latex":
tmp += [line[j:l]] new_block += [line[j:l]]
j = l j = l
continue continue
if inset in ["LatexCommand", "LatexDel"]: if inset in ["LatexCommand", "LatexDel", "Label", "Figure",
tmp += [line[j:]] "Formula"]:
new_block += [line[j:]]
break break
if inset == "Quotes": if inset == "Quotes":
l = find_next_space(line, l + 1) l = find_next_space(line, l + 1)
tmp += [line[j:l]] new_block += [line[j:l]]
j = l j = l
continue continue
document.warning("unkown inset %s" % line) document.warning("unkown inset %s" % inset)
assert(False) assert(False)
# We are inside a latex inset, pass the text verbatim # We are inside a latex inset, pass the text verbatim
tmp += [line[j:]] new_block += [line[j:]]
break break
document.body[i: i+1] = tmp document.body[i: i+1] = new_block
i += len(tmp) i += len(new_block)
supported_versions = ["0.10.%d" % i for i in range(8)] + ["0.10"] supported_versions = ["0.10.%d" % i for i in range(8)] + ["0.10"]