]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_2_0.py
Fix bug #6113: Customized font color in footnote is not rendered in LyX.
[lyx.git] / lib / lyx2lyx / lyx_2_0.py
index 5a946be30393f26bc424928c39cf1ce059f77ced..d668bfa745ac4dc8ecb95b6e534d539776f0f48a 100644 (file)
@@ -179,141 +179,51 @@ def latex_length(string):
     'Convert lengths to their LaTeX representation.'
     i = 0
     percent = False
-    # the string has always the form
-    # ValueUnit+ValueUnit-ValueUnit
-    # the + and - lengths are optional
-    # the + is always before the -
-    i = string.find("text%")
-    if i > -1:
-        percent = True
-        minus = string.rfind("-", 0, i)
-        plus = string.rfind("+", 0, i)
-        if plus == -1 and minus == -1:
-            value = string[:i]
-            value = str(float(value)/100)
-            end = string[i+5:]
-            string = value + "\\textwidth" + end
-        if plus > minus:
-            value = string[plus+1:i]
-            value = str(float(value)/100)
-            begin = string[:plus+1]
-            end = string[i+5:]
-            string = begin + value + "\\textwidth" + end
-        if plus < minus:
-            value = string[minus+1:i]
-            value = str(float(value)/100)
-            begin = string[:minus+1]
-            string = begin + value + "\\textwidth"
-    i = string.find("col%")
-    if i > -1:
-        percent = True
-        minus = string.rfind("-", 0, i)
-        plus = string.rfind("+", 0, i)
-        if plus == -1 and minus == -1:
-            value = string[:i]
-            value = str(float(value)/100)
-            end = string[i+4:]
-            string = value + "\\columnwidth" + end
-        if plus > minus:
-            value = string[plus+1:i]
-            value = str(float(value)/100)
-            begin = string[:plus+1]
-            end = string[i+4:]
-            string = begin + value + "\\columnwidth" + end
-        if plus < minus:
-            value = string[minus+1:i]
-            value = str(float(value)/100)
-            begin = string[:minus+1]
-            string = begin + value + "\\columnwidth"
-    i = string.find("page%")
-    if i > -1:
-        percent = True
-        minus = string.rfind("-", 0, i)
-        plus = string.rfind("+", 0, i)
-        if plus == -1 and minus == -1:
-            value = string[:i]
-            value = str(float(value)/100)
-            end = string[i+5:]
-            string = value + "\\paperwidth" + end
-        if plus > minus:
-            value = string[plus+1:i]
-            value = str(float(value)/100)
-            begin = string[:plus+1]
-            end = string[i+5:]
-            string = begin + value + "\\paperwidth" + end
-        if plus < minus:
-            value = string[minus+1:i]
-            value = str(float(value)/100)
-            begin = string[:minus+1]
-            string = begin + value + "\\paperwidth"
-    i = string.find("line%")
-    if i > -1:
-        percent = True
-        minus = string.rfind("-", 0, i)
-        plus = string.rfind("+", 0, i)
-        if plus == -1 and minus == -1:
-            value = string[:i]
-            value = str(float(value)/100)
-            end = string[i+5:]
-            string = value + "\\linewidth" + end
-        if plus > minus:
-            value = string[plus+1:i]
-            value = str(float(value)/100)
-            begin = string[:plus+1]
-            end = string[i+5:]
-            string = begin + value + "\\linewidth" + end
-        if plus < minus:
-            value = string[minus+1:i]
-            value = str(float(value)/100)
-            begin = string[:minus+1]
-            string = begin + value + "\\linewidth"
-    i = string.find("theight%")
-    if i > -1:
-        percent = True
-        minus = string.rfind("-", 0, i)
-        plus = string.rfind("+", 0, i)
-        if plus == -1 and minus == -1:
-            value = string[:i]
-            value = str(float(value)/100)
-            end = string[i+8:]
-            string = value + "\\textheight" + end
-        if plus > minus:
-            value = string[plus+1:i]
-            value = str(float(value)/100)
-            begin = string[:plus+1]
-            end = string[i+8:]
-            string = begin + value + "\\textheight" + end
-        if plus < minus:
-            value = string[minus+1:i]
-            value = str(float(value)/100)
-            begin = string[:minus+1]
-            string = begin + value + "\\textheight"
-    i = string.find("pheight%")
-    if i > -1:
-        percent = True
-        minus = string.rfind("-", 0, i)
-        plus = string.rfind("+", 0, i)
-        if plus == -1 and minus == -1:
-            value = string[:i]
-            value = str(float(value)/100)
-            end = string[i+8:]
-            string = value + "\\paperheight" + end
-        if plus > minus:
-            value = string[plus+1:i]
-            value = str(float(value)/100)
-            begin = string[:plus+1]
-            end = string[i+8:]
-            string = begin + value + "\\paperheight" + end
-        if plus < minus:
-            value = string[minus+1:i]
-            value = str(float(value)/100)
-            begin = string[:minus+1]
-            string = begin + value + "\\paperheight"
+    # the string has the form
+    # ValueUnit+ValueUnit-ValueUnit or
+    # ValueUnit+-ValueUnit
+    # the + and - (glue lengths) are optional
+    # the + always precedes the -
+
+    # Convert relative lengths to LaTeX units
+    units = {"text%":"\\textwidth", "col%":"\\columnwidth",
+             "page%":"\\pagewidth", "line%":"\\linewidth",
+             "theight%":"\\textheight", "pheight%":"\\pageheight"}
+    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
+
+    # replace + and -, but only if the - is not the first character
+    string = string[0] + string[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:
-        string = string.replace("+", " plus ")
-        string = string.replace("-", " minus ")
         return "True," + string
         
 
@@ -909,21 +819,16 @@ def revert_paragraph_indentation(document):
       i = find_token(document.header, "\\paragraph_indentation", i)
       if i == -1:
           break
-      # only remove the preamble line when default
-      # otherwise also write the value to the preamble  
-      j = document.header[i].find("default")
-      if j > -1:
+      # 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:
-          # search for the beginning of the value via the space
-          j = document.header[i].find(" ")
-          length = document.header[i][j+1:]
           # handle percent lengths
-          length = latex_length(length)
           # latex_length returns "bool,length"
-          k = length.find(",")
-          length = length[k+1:]
+          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]
@@ -937,23 +842,15 @@ def revert_percent_skip_lengths(document):
       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
-      j = document.header[i].find("smallskip")
-      k = document.header[i].find("medskip")
-      l = document.header[i].find("bigskip")
-      if (j > -1) or (k > -1) or (l > -1):
-          break
-      else:
-          # search for the beginning of the value via the space
-          j = document.header[i].find(" ")
-          length = document.header[i][j+1:]
+      if length not in ('smallskip', 'medskip', 'bigskip'):
           # handle percent lengths
           length = latex_length(length)
           # latex_length returns "bool,length"
-          l = length.find(",")
-          percent = length[:l]
-          length = length[l+1:]
+          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 + "}"])
@@ -969,32 +866,25 @@ def revert_percent_vspace_lengths(document):
       i = find_token(document.body, "\\begin_inset VSpace", i)
       if i == -1:
           break
-      # only revert when a custom length was set and when
+      # only revert if a custom length was set and if
       # it used a percent length
-      j = document.body[i].find("defskip")
-      k = document.body[i].find("smallskip")
-      l = document.body[i].find("medskip")
-      m = document.body[i].find("bigskip")
-      n = document.body[i].find("vfill")
-      if (j > -1) or (k > -1) or (l > -1) or (m > -1) or (n > -1):
-          break
-      else:
-          # search for the beginning of the value via the last space
-          o = document.body[i].rfind(" ")
-          length = document.body[i][o+1:]
+      line = document.body[i]
+      r = re.compile(r'\\begin_inset VSpace (.*)$')
+      m = r.match(line)
+      length = m.group(1)
+      if length not in ('defskip', 'smallskip', 'medskip', 'bigskip', 'vfill'):
           # check if the space has a star (protected space)
-          p = document.body[i].rfind("*")
-          if p > -1:
-              length = length[:-1]
+          protected = (document.body[i].rfind("*") != -1)
+          if protected:
+              length = length.rstrip('*')
           # handle percent lengths
           length = latex_length(length)
           # latex_length returns "bool,length"
-          q = length.find(",")
-          percent = length[:q]
-          length = length[q+1:]
+          percent = length.split(",")[0]
+          length = length.split(",")[1]
           # revert the VSpace inset to ERT
           if percent == "True":
-              if p > -1:
+              if protected:
                   subst = [put_cmd_in_ert("\\vspace*{" + length + "}")]
               else:
                   subst = [put_cmd_in_ert("\\vspace{" + length + "}")]
@@ -1005,87 +895,178 @@ def revert_percent_vspace_lengths(document):
 def revert_percent_hspace_lengths(document):
     " Revert relative HSpace lengths to ERT "
     i = 0
-    j = 0
     while True:
-      i = find_token(document.body, "\\begin_inset space \hspace{}", i)
+      i = find_token(document.body, "\\begin_inset space \\hspace", i)
       if i == -1:
-          j = find_token(document.body, "\\begin_inset space \hspace*{}", j)
-          if j == -1:
-              break
-          else:
-              star = True
-              i = j
-      else:
-          star = False
-      # only revert when a custom length was set and when
-      # it used a percent length
-      o = document.body[i+1].find("\\length")
-      if o == -1:
-          document.warning("Error: Cannot find lenght for \\hspace!")
           break
-      # search for the beginning of the value via the space
-      k = document.body[i+1].find(" ")
-      length = document.body[i+1][k+1:]
+      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)
+      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"
-      m = length.find(",")
-      percent = length[:m]
-      length = length[m+1:]
+      percent = length.split(",")[0]
+      length = length.split(",")[1]
       # revert the HSpace inset to ERT
       if percent == "True":
-          if star == True:
+          if protected:
               subst = [put_cmd_in_ert("\\hspace*{" + length + "}")]
           else:
               subst = [put_cmd_in_ert("\\hspace{" + length + "}")]
           document.body[i:i+3] = subst
       i = i + 2
-      j = i
 
 
 def revert_hspace_glue_lengths(document):
     " Revert HSpace glue lengths to ERT "
     i = 0
-    j = 0
     while True:
-      i = find_token(document.body, "\\begin_inset space \hspace{}", i)
+      i = find_token(document.body, "\\begin_inset space \\hspace", i)
       if i == -1:
-          j = find_token(document.body, "\\begin_inset space \hspace*{}", j)
-          if j == -1:
-              break
-          else:
-              star = True
-              i = j
-      else:
-          star = False
-      o = document.body[i+1].find("\\length")
-      if o == -1:
-          document.warning("Error: Cannot find lenght for \\hspace!")
           break
-      # search for the beginning of the value via the space
-      k = document.body[i+1].find(" ")
-      length = document.body[i+1][k+1:]
-      # only revert when the length contains a plus or minus
-      l = length.find("+")
-      if l == -1:
-          l = length.find("-")
-          if l == -1:
-              break
-      # handle percent lengths
-      length = latex_length(length)
-      # latex_length returns "bool,length"
-      m = length.find(",")
-      length = length[m+1:]
-      # revert the HSpace inset to ERT
-      # allow leading -
-      if length.rfind("-") <> 0 or (length.rfind("-") == 0 and length.rfind("+") > -1):
-          if star == True:
+      protected = (document.body[i].find("\\hspace*{}") != -1)
+      length = get_value(document.body, '\\length', i+1)
+      if length == '':
+          document.warning("Malformed lyx document: Missing '\\length' in Space inset.")
+          return
+      # only revert if the length contains a plus or minus at pos != 0
+      glue  = re.compile(r'.+[\+-]')
+      if glue.search(length):
+          # handle percent lengths
+          # latex_length returns "bool,length"
+          length = latex_length(length).split(",")[1]
+          # revert the HSpace inset to ERT
+          if protected:
               subst = [put_cmd_in_ert("\\hspace*{" + length + "}")]
           else:
               subst = [put_cmd_in_ert("\\hspace{" + length + "}")]
           document.body[i:i+3] = subst
       i = i + 2
-      j = i
+
+def convert_author_id(document):
+    " Add the author_id to the \\author definition and make sure 0 is not used"
+    i = 0
+    j = 1
+    while True:
+        i = find_token(document.header, "\\author", i)
+        if i == -1:
+            break
+        
+        r = re.compile(r'(\\author) (\".*\")\s?(.*)$')
+        m = r.match(document.header[i])
+        if m != None:
+            name = m.group(2)
+            
+            email = ''
+            if m.lastindex == 3:
+                email = m.group(3)
+            document.header[i] = "\\author %i %s %s" % (j, name, email)
+        j = j + 1
+        i = i + 1
+        
+    k = 0
+    while True:
+        k = find_token(document.body, "\\change_", k)
+        if k == -1:
+            break
+
+        change = document.body[k].split(' ');
+        if len(change) == 3:
+            type = change[0]
+            author_id = int(change[1])
+            time = change[2]
+            document.body[k] = "%s %i %s" % (type, author_id + 1, time)
+        k = k + 1
+
+def revert_author_id(document):
+    " Remove the author_id from the \\author definition "
+    i = 0
+    j = 0
+    idmap = dict()
+    while True:
+        i = find_token(document.header, "\\author", i)
+        if i == -1:
+            break
+        
+        r = re.compile(r'(\\author) (\d+) (\".*\")\s?(.*)$')
+        m = r.match(document.header[i])
+        if m != None:
+            author_id = int(m.group(2))
+            idmap[author_id] = j
+            name = m.group(3)
+            
+            email = ''
+            if m.lastindex == 4:
+                email = m.group(4)
+            document.header[i] = "\\author %s %s" % (name, email)
+        i = i + 1
+        j = j + 1
+
+    k = 0
+    while True:
+        k = find_token(document.body, "\\change_", k)
+        if k == -1:
+            break
+
+        change = document.body[k].split(' ');
+        if len(change) == 3:
+            type = change[0]
+            author_id = int(change[1])
+            time = change[2]
+            document.body[k] = "%s %i %s" % (type, idmap[author_id], time)
+        k = k + 1
+
+
+def revert_suppress_date(document):
+    " Revert suppressing of default document date to preamble code "
+    i = 0
+    while True:
+      i = find_token(document.header, "\\suppress_date", i)
+      if i == -1:
+          break
+      # remove the preamble line and write to the preamble
+      # when suppress_date was true
+      date = get_value(document.header, "\\suppress_date", i)
+      if date == "true":
+          add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
+          add_to_preamble(document, ["\\date{}"])
+      del document.header[i]
+      i = i + 1
+
+
+def revert_mhchem(document):
+    "Revert mhchem loading to preamble code"
+    i = 0
+    j = 0
+    k = 0
+    i = find_token(document.header, "\\use_mhchem 1", 0)
+    if i != -1:
+        mhchem = "auto"
+    else:
+        i = find_token(document.header, "\\use_mhchem 2", 0)
+        if i != -1:
+            mhchem = "on"
+    if mhchem == "auto":
+        j = find_token(document.body, "\\cf{", 0)
+        if j != -1:
+            mhchem = "on"
+        else:
+            j = find_token(document.body, "\\ce{", 0)
+            if j != -1:
+                mhchem = "on"
+    if mhchem == "on":
+        add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
+        add_to_preamble(document, ["\\PassOptionsToPackage{version=3}{mhchem}"])
+        add_to_preamble(document, ["\\usepackage{mhchem}"])
+    k = find_token(document.header, "\\use_mhchem", 0)
+    if k == -1:
+        document.warning("Malformed LyX document: Could not find mhchem setting.")
+        return
+    del document.header[k]
 
 
 ##
@@ -1115,10 +1096,16 @@ convert = [[346, []],
            [365, []],
            [366, []],
            [367, []],
-           [368, []]
+           [368, []],
+           [369, [convert_author_id]],
+           [370, []],
+           [371, []]
           ]
 
-revert =  [[367, [revert_hspace_glue_lengths]],
+revert =  [[370, [revert_mhchem]],
+           [369, [revert_suppress_date]],
+           [368, [revert_author_id]],
+           [367, [revert_hspace_glue_lengths]],
            [366, [revert_percent_vspace_lengths, revert_percent_hspace_lengths]],
            [365, [revert_percent_skip_lengths]],
            [364, [revert_paragraph_indentation]],