return content
-def latex_length(string):
+def latex_length(slen):
'Convert lengths to their LaTeX representation.'
i = 0
percent = False
- # the string has the form
+ # the slen has the form
# ValueUnit+ValueUnit-ValueUnit or
# ValueUnit+-ValueUnit
# the + and - (glue lengths) are optional
"page%":"\\paperwidth", "line%":"\\linewidth",
"theight%":"\\textheight", "pheight%":"\\paperheight"}
for unit in units.keys():
- i = string.find(unit)
- if i != -1:
- percent = True
- minus = string.rfind("-", 1, i)
- plus = string.rfind("+", 0, i)
- latex_unit = units[unit]
- if plus == -1 and minus == -1:
- value = string[:i]
- value = str(float(value)/100)
- end = string[i + len(unit):]
- string = value + latex_unit + end
- if plus > minus:
- value = string[plus + 1:i]
- value = str(float(value)/100)
- begin = string[:plus + 1]
- end = string[i+len(unit):]
- string = begin + value + latex_unit + end
- if plus < minus:
- value = string[minus + 1:i]
- value = str(float(value)/100)
- begin = string[:minus + 1]
- string = begin + value + latex_unit
+ i = slen.find(unit)
+ if i == -1:
+ continue
+ percent = True
+ minus = slen.rfind("-", 1, i)
+ plus = slen.rfind("+", 0, i)
+ latex_unit = units[unit]
+ if plus == -1 and minus == -1:
+ value = slen[:i]
+ value = str(float(value)/100)
+ end = slen[i + len(unit):]
+ slen = value + latex_unit + end
+ if plus > minus:
+ value = slen[plus + 1:i]
+ value = str(float(value)/100)
+ begin = slen[:plus + 1]
+ end = slen[i+len(unit):]
+ slen = begin + value + latex_unit + end
+ if plus < minus:
+ value = slen[minus + 1:i]
+ value = str(float(value)/100)
+ begin = slen[:minus + 1]
+ slen = begin + value + latex_unit
# replace + and -, but only if the - is not the first character
- string = string[0] + string[1:].replace("+", " plus ").replace("-", " minus ")
+ slen = slen[0] + slen[1:].replace("+", " plus ").replace("-", " minus ")
# handle the case where "+-1mm" was used, because LaTeX only understands
# "plus 1mm minus 1mm"
- if string.find("plus minus"):
- lastvaluepos = string.rfind(" ")
- lastvalue = string[lastvaluepos:]
- string = string.replace(" ", lastvalue + " ")
+ if slen.find("plus minus"):
+ lastvaluepos = slen.rfind(" ")
+ lastvalue = slen[lastvaluepos:]
+ slen = slen.replace(" ", lastvalue + " ")
if percent == False:
- return "False," + string
+ return "False," + slen
else:
- return "True," + string
+ return "True," + slen
def revert_flex_inset(document, name, LaTeXname, position):
end = find_end_of_inset(document.body, i)
if end == -1:
document.warning("Can't find end of inset at line " + str(i))
- i = j
+ i += 1
continue
fline = find_token(document.body, "<features", i, end)
if fline == -1:
document.warning("Can't find features for inset at line " + str(i))
- i = end
+ i += 1
continue
p = document.body[fline].find("islongtable")
if p != -1:
# This seems wrong: It removes everything after
# tabularvalignment, too.
document.body[fline] = document.body[fline][:q - 1] + '>'
- i = end
+ i += 1
continue
# no longtable
'',
'\\begin_layout Plain Layout']
document.body[i:i] = subst # this just inserts the array at i
- i = end + len(subst) # adjust i to save a few cycles
+ # since there could be a tabular inside a tabular, we cannot
+ # jump to end
+ i += len(subst)
def revert_phantom_types(document, ptype, cmd):
def revert_strikeout(document):
- " Reverts \\strikeout character style "
+ " Reverts \\strikeout font attribute "
changed = revert_font_attrs(document, "\\uuline", "\\uuline")
changed = revert_font_attrs(document, "\\uwave", "\\uwave") or changed
changed = revert_font_attrs(document, "\\strikeout", "\\sout") or changed
def revert_ulinelatex(document):
- " Reverts \\uline character style "
+ " Reverts \\uline font attribute "
i = find_token(document.body, '\\bar under', 0)
if i == -1:
return
i = find_token(document.header, '\\bibtex_command', 0)
if i == -1:
document.warning("Malformed LyX document: Missing \\bibtex_command.")
- return
- del document.header[i]
+ else:
+ del document.header[i]
i = find_token(document.header, '\\index_command', 0)
if i == -1:
document.warning("Malformed LyX document: Missing \\index_command.")
- return
- del document.header[i]
+ else:
+ del document.header[i]
def convert_nomencl_width(document):
i = j
continue
del document.body[l]
- i = i + 1
+ i = j - 1
def revert_nomencl_cwidth(document):
j = find_end_of_inset(document.body, i)
l = find_token(document.body, "width", i, j)
if l == -1:
- #Can't find width option for nomencl_print
- i = j
- continue
+ document.warning("Can't find width option for nomencl_print!")
+ i = j
+ continue
width = get_value(document.body, "width", i, j).strip('"')
del document.body[l]
add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
add_to_preamble(document, ["\\setlength{\\nomlabelwidth}{" + width + "}"])
- i = i + 1
+ i = j - 1
def revert_applemac(document):
" Revert applemac encoding to auto "
- i = 0
- if document.encoding == "applemac":
- document.encoding = "auto"
- i = find_token(document.header, "\\encoding", 0)
- if i != -1:
- document.header[i] = "\\encoding auto"
+ if document.encoding != "applemac":
+ return
+ document.encoding = "auto"
+ i = find_token(document.header, "\\encoding", 0)
+ if i != -1:
+ document.header[i] = "\\encoding auto"
def revert_longtable_align(document):
" Remove longtable alignment setting "
i = 0
- j = 0
while True:
i = find_token(document.body, "\\begin_inset Tabular", i)
if i == -1:
break
- # the alignment is 2 lines below \\begin_inset Tabular
- j = document.body[i + 2].find("longtabularalignment")
+ end = find_end_of_inset(document.body, i)
+ if end == -1:
+ document.warning("Can't find end of inset at line " + str(i))
+ i += 1
+ continue
+ fline = find_token(document.body, "<features", i, end)
+ if fline == -1:
+ document.warning("Can't find features for inset at line " + str(i))
+ i += 1
+ continue
+ j = document.body[fline].find("longtabularalignment")
if j == -1:
- break
- document.body[i + 2] = document.body[i + 2][:j - 1]
- document.body[i + 2] = document.body[i + 2] + '>'
- i = i + 1
+ i += 1
+ continue
+ # FIXME Is this correct? It wipes out everything after the
+ # one we found.
+ document.body[fline] = document.body[fline][:j - 1] + '>'
+ # since there could be a tabular inside this one, we
+ # cannot jump to end.
+ i += 1
def revert_branch_filename(document):
def revert_paragraph_indentation(document):
" Revert custom paragraph indentation to preamble code "
- i = 0
- while True:
- i = find_token(document.header, "\\paragraph_indentation", i)
- if i == -1:
- break
- # only remove the preamble line if default
- # otherwise also write the value to the preamble
- length = get_value(document.header, "\\paragraph_indentation", i)
- if length == "default":
- del document.header[i]
- break
- else:
- # handle percent lengths
- # latex_length returns "bool,length"
- length = latex_length(length).split(",")[1]
- add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
- add_to_preamble(document, ["\\setlength{\\parindent}{" + length + "}"])
- del document.header[i]
- i = i + 1
+ i = find_token(document.header, "\\paragraph_indentation", i)
+ if i == -1:
+ return
+ length = get_value(document.header, "\\paragraph_indentation", i)
+ # we need only remove the line if indentation is default
+ if length != "default":
+ # handle percent lengths
+ # latex_length returns "bool,length"
+ length = latex_length(length).split(",")[1]
+ add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
+ add_to_preamble(document, ["\\setlength{\\parindent}{" + length + "}"])
+ del document.header[i]
def revert_percent_skip_lengths(document):
" Revert relative lengths for paragraph skip separation to preamble code "
- i = 0
- while True:
- i = find_token(document.header, "\\defskip", i)
- if i == -1:
- break
- length = get_value(document.header, "\\defskip", i)
- # only revert when a custom length was set and when
- # it used a percent length
- if length not in ('smallskip', 'medskip', 'bigskip'):
- # handle percent lengths
- length = latex_length(length)
- # latex_length returns "bool,length"
- percent = length.split(",")[0]
- length = length.split(",")[1]
- if percent == "True":
- add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
- add_to_preamble(document, ["\\setlength{\\parskip}{" + length + "}"])
- # set defskip to medskip as default
- document.header[i] = "\\defskip medskip"
- i = i + 1
+ i = find_token(document.header, "\\defskip", i)
+ if i == -1:
+ return
+ length = get_value(document.header, "\\defskip", i)
+ # only revert when a custom length was set and when
+ # it used a percent length
+ if length in ('smallskip', 'medskip', 'bigskip'):
+ return
+ # handle percent lengths
+ length = latex_length(length)
+ # latex_length returns "bool,length"
+ percent = length.split(",")[0]
+ length = length.split(",")[1]
+ if percent == "True":
+ add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
+ add_to_preamble(document, ["\\setlength{\\parskip}{" + length + "}"])
+ # set defskip to medskip as default
+ document.header[i] = "\\defskip medskip"
def revert_percent_vspace_lengths(document):