]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_2_0.py
Remove comment.
[lyx.git] / lib / lyx2lyx / lyx_2_0.py
index 1faed1612f57c3e48801717fd9e45808aa93eb9b..109b0b083084ed6c9030a84f77e1b86db332e4f5 100644 (file)
@@ -209,8 +209,7 @@ def lyx2latex(document, lines):
               line = "\\,"
       elif hspace != "":
           # The LyX length is in line[8:], after the \length keyword
-          # latex_length returns "bool,length"
-          length = latex_length(line[8:]).split(",")[1]
+          length = latex_length(line[8:])[1]
           line = hspace + "{" + length + "}"
           hspace = ""
       elif line.isspace() or \
@@ -259,11 +258,15 @@ def lyx2latex(document, lines):
     return content
 
 
-def latex_length(string):
-    'Convert lengths to their LaTeX representation.'
+def latex_length(slen):
+    ''' 
+    Convert lengths to their LaTeX representation. Returns (bool, length),
+    where the bool tells us if it was a percentage, and the length is the
+    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
@@ -274,41 +277,39 @@ def latex_length(string):
              "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 percent ==  False:
-        return "False," + string
-    else:
-        return "True," + string
+    if slen.find("plus  minus"):
+        lastvaluepos = slen.rfind(" ")
+        lastvalue = slen[lastvaluepos:]
+        slen = slen.replace("  ", lastvalue + " ")
+    return (percent, slen)
 
 
 def revert_flex_inset(document, name, LaTeXname, position):
@@ -445,12 +446,12 @@ def revert_tabularvalign(document):
       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:
@@ -460,7 +461,7 @@ def revert_tabularvalign(document):
               # This seems wrong: It removes everything after 
               # tabularvalignment, too.
               document.body[fline] = document.body[fline][:q - 1] + '>'
-          i = end
+          i += 1
           continue
 
        # no longtable
@@ -501,7 +502,9 @@ def revert_tabularvalign(document):
           '',
           '\\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):
@@ -934,18 +937,30 @@ def revert_applemac(document):
 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):
@@ -960,49 +975,36 @@ 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", 0)
+    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
+      length = latex_length(length)[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", 0)
+    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
+    percent, length = latex_length(length)
+    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):
@@ -1014,28 +1016,26 @@ def revert_percent_vspace_lengths(document):
           break
       # only revert if a custom length was set and if
       # it used a percent length
-      line = document.body[i]
       r = re.compile(r'\\begin_inset VSpace (.*)$')
-      m = r.match(line)
+      m = r.match(document.body[i])
       length = m.group(1)
-      if length not in ('defskip', 'smallskip', 'medskip', 'bigskip', 'vfill'):
-          # check if the space has a star (protected space)
-          protected = (document.body[i].rfind("*") != -1)
+      if length in ('defskip', 'smallskip', 'medskip', 'bigskip', 'vfill'):
+         i += 1
+         continue
+      # check if the space has a star (protected space)
+      protected = (document.body[i].rfind("*") != -1)
+      if protected:
+          length = length.rstrip('*')
+      # handle percent lengths
+      percent, length = latex_length(length)
+      # revert the VSpace inset to ERT
+      if percent == "True":
           if protected:
-              length = length.rstrip('*')
-          # handle percent lengths
-          length = latex_length(length)
-          # latex_length returns "bool,length"
-          percent = length.split(",")[0]
-          length = length.split(",")[1]
-          # revert the VSpace inset to ERT
-          if percent == "True":
-              if protected:
-                  subst = [old_put_cmd_in_ert("\\vspace*{" + length + "}")]
-              else:
-                  subst = [old_put_cmd_in_ert("\\vspace{" + length + "}")]
-              document.body[i:i + 2] = subst
-      i = i + 1
+              subst = put_cmd_in_ert("\\vspace*{" + length + "}")
+          else:
+              subst = put_cmd_in_ert("\\vspace{" + length + "}")
+          document.body[i:i + 2] = subst
+      i += 1
 
 
 def revert_percent_hspace_lengths(document):
@@ -1045,26 +1045,29 @@ def revert_percent_hspace_lengths(document):
       i = find_token(document.body, "\\begin_inset space \\hspace", i)
       if i == -1:
           break
-      protected = (document.body[i].find("\\hspace*{}") != -1)
-      # only revert if a custom length was set and if
-      # it used a percent length
-      length = get_value(document.body, '\\length', i + 1)
+      j = find_end_of_inset(document.body, i)
+      if j == -1:
+          document.warning("Can't find end of inset at line " + str(i))
+          i += 1
+          continue
+      # only revert if a custom length was set...
+      length = get_value(document.body, '\\length', i + 1, j)
       if length == '':
           document.warning("Malformed lyx document: Missing '\\length' in Space inset.")
-          return
-      # handle percent lengths
-      length = latex_length(length)
-      # latex_length returns "bool,length"
-      percent = length.split(",")[0]
-      length = length.split(",")[1]
+          i = j
+          continue
+      protected = (document.body[i].find("\\hspace*{}") != -1)
+      # ...and if it used a percent length
+      percent, length = latex_length(length)
       # revert the HSpace inset to ERT
       if percent == "True":
           if protected:
-              subst = [old_put_cmd_in_ert("\\hspace*{" + length + "}")]
+              subst = put_cmd_in_ert("\\hspace*{" + length + "}")
           else:
-              subst = [old_put_cmd_in_ert("\\hspace{" + length + "}")]
-          document.body[i:i + 3] = subst
-      i = i + 2
+              subst = put_cmd_in_ert("\\hspace{" + length + "}")
+          document.body[i:j + 1] = subst
+      # if we did a substitution, this will still be ok
+      i = j
 
 
 def revert_hspace_glue_lengths(document):
@@ -1083,8 +1086,7 @@ def revert_hspace_glue_lengths(document):
       glue  = re.compile(r'.+[\+-]')
       if glue.search(length):
           # handle percent lengths
-          # latex_length returns "bool,length"
-          length = latex_length(length).split(",")[1]
+          length = latex_length(length)[1]
           # revert the HSpace inset to ERT
           if protected:
               subst = [old_put_cmd_in_ert("\\hspace*{" + length + "}")]
@@ -1864,8 +1866,7 @@ def revert_makebox(document):
       length = document.body[l][7:]
       # remove trailing '"'
       length = length[:-1]
-      # latex_length returns "bool,length"
-      length = latex_length(length).split(",")[1]
+      length = latex_length(length)[1]
       subst = "\\makebox[" + length + "][" \
         + align + "]{"
       document.body[i:y + 1] = put_cmd_in_ert(subst)