]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_2_0.py
whitespace.
[lyx.git] / lib / lyx2lyx / lyx_2_0.py
index 848621f16c4b830e8c8373d8f67a7fbc6299ffea..243cbb8958b4240cc0aab12196ab737e067ada96 100644 (file)
@@ -89,10 +89,7 @@ def revert_tabularvalign(document):
       if p != -1:
           q = document.body[fline].find("tabularvalignment")
           if q != -1:
-              # FIXME
-              # This seems wrong: It removes everything after 
-              # tabularvalignment, too.
-              document.body[fline] = document.body[fline][:q - 1] + '>'
+              document.body[fline] = re.sub(r' tabularvalignment=\"[a-z]+\"', "", document.body[fline])
           i += 1
           continue
 
@@ -108,16 +105,13 @@ def revert_tabularvalign(document):
       # delete tabularvalignment
       q = document.body[fline].find("tabularvalignment")
       if q != -1:
-          # FIXME
-          # This seems wrong: It removes everything after 
-          # tabularvalignment, too.
-          document.body[fline] = document.body[fline][:q - 1] + '>'
+          document.body[fline] = re.sub(r' tabularvalignment=\"[a-z]+\"', "", document.body[fline])
 
       # don't add a box when centered
       if tabularvalignment == 'c':
           i = end
           continue
-      subst = ['\\end_layout', '\\end_inset']
+      subst = ['\\end_inset', '\\end_layout']
       document.body[end:end] = subst # just inserts those lines
       subst = ['\\begin_inset Box Frameless',
           'position "' + tabularvalignment +'"',
@@ -285,8 +279,8 @@ def revert_xetex(document):
         pretext.append(tw)
     if osf:
         pretext.append('\\defaultfontfeatures{Numbers=OldStyle}')
-    pretext.append('\usepackage{xunicode}')
-    pretext.append('\usepackage{xltxtra}')
+    pretext.append('\\usepackage{xunicode}')
+    pretext.append('\\usepackage{xltxtra}')
     insert_to_preamble(document, pretext)
 
 
@@ -317,11 +311,33 @@ def revert_backgroundcolor(document):
         '\\pagecolor{page_backgroundcolor}'])
 
 
-def revert_splitindex(document):
-    " Reverts splitindex-aware documents "
+def add_use_indices(document):
+    " Add \\use_indices if it is missing "
     i = find_token(document.header, '\\use_indices', 0)
+    if i != -1:
+        return i
+    i = find_token(document.header, '\\use_bibtopic', 0)
+    if i == -1:
+        i = find_token(document.header, '\\cite_engine', 0)
+    if i == -1:
+        i = find_token(document.header, '\\use_mathdots', 0)
+    if i == -1:
+        i = find_token(document.header, '\\use_mhchem', 0)
+    if i == -1:
+        i = find_token(document.header, '\\use_esint', 0)
+    if i == -1:
+        i = find_token(document.header, '\\use_amsmath', 0)
     if i == -1:
         document.warning("Malformed LyX document: Missing \\use_indices.")
+        return -1
+    document.header.insert(i + 1, '\\use_indices 0')
+    return i + 1
+
+
+def revert_splitindex(document):
+    " Reverts splitindex-aware documents "
+    i = add_use_indices(document)
+    if i == -1:
         return
     useindices = str2bool(get_value(document.header, "\\use_indices", i))
     del document.header[i]
@@ -398,6 +414,7 @@ def revert_splitindex(document):
 
 def convert_splitindex(document):
     " Converts index and printindex insets to splitindex-aware format "
+    add_use_indices(document)
     i = 0
     while True:
         i = find_token(document.body, "\\begin_inset Index", i)
@@ -422,9 +439,8 @@ def convert_splitindex(document):
 
 def revert_subindex(document):
     " Reverts \\printsubindex CommandInset types "
-    i = find_token(document.header, '\\use_indices', 0)
+    i = add_use_indices(document)
     if i == -1:
-        document.warning("Malformed LyX document: Missing \\use_indices.")
         return
     useindices = str2bool(get_value(document.header, "\\use_indices", i))
     i = 0
@@ -448,9 +464,8 @@ def revert_subindex(document):
 
 def revert_printindexall(document):
     " Reverts \\print[sub]index* CommandInset types "
-    i = find_token(document.header, '\\use_indices', 0)
+    i = add_use_indices(document)
     if i == -1:
-        document.warning("Malformed LyX document: Missing \\use_indices.")
         return
     useindices = str2bool(get_value(document.header, "\\use_indices", i))
     i = 0
@@ -542,7 +557,6 @@ def revert_nomencl_cwidth(document):
       j = find_end_of_inset(document.body, i)
       l = find_token(document.body, "width", i, j)
       if l == -1:
-        document.warning("Can't find width option for nomencl_print!")
         i = j
         continue
       width = get_quoted_value(document.body, "width", i, j)
@@ -902,7 +916,7 @@ def merge_gbrief(document):
                     "Verteiler":       "cc",
                     "Gruss":           "Closing"}
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, "\\begin_layout", i)
         if i == -1:
             break
@@ -1318,7 +1332,7 @@ def revert_lyx_version(document):
         pass
 
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, '\\begin_inset Info', i)
         if i == -1:
             return
@@ -1467,7 +1481,7 @@ def revert_align_decimal(document):
 def convert_optarg(document):
   " Convert \\begin_inset OptArg to \\begin_inset Argument "
   i = 0
-  while 1:
+  while True:
     i = find_token(document.body, '\\begin_inset OptArg', i)
     if i == -1:
       return
@@ -1478,7 +1492,7 @@ def convert_optarg(document):
 def revert_argument(document):
   " Convert \\begin_inset Argument to \\begin_inset OptArg "
   i = 0
-  while 1:
+  while True:
     i = find_token(document.body, '\\begin_inset Argument', i)
     if i == -1:
       return
@@ -1489,7 +1503,7 @@ def revert_argument(document):
 def revert_makebox(document):
   " Convert \\makebox to TeX code "
   i = 0
-  while 1:
+  while True:
     i = find_token(document.body, '\\begin_inset Box', i)
     if i == -1:
       break
@@ -1536,7 +1550,7 @@ def revert_makebox(document):
 def convert_use_makebox(document):
   " Adds use_makebox option for boxes "
   i = 0
-  while 1:
+  while True:
     i = find_token(document.body, '\\begin_inset Box', i)
     if i == -1:
       return
@@ -1564,10 +1578,13 @@ def convert_use_makebox(document):
 
 def revert_IEEEtran(document):
   " Convert IEEEtran layouts and styles to TeX code "
+
   if document.textclass != "IEEEtran":
     return
+
   revert_flex_inset(document.body, "IEEE membership", "\\IEEEmembership")
   revert_flex_inset(document.body, "Lowercase", "\\MakeLowercase")
+
   layouts = ("Special Paper Notice", "After Title Text", "Publication ID",
              "Page headings", "Biography without photo")
   latexcmd = {"Special Paper Notice": "\\IEEEspecialpapernotice",
@@ -1575,6 +1592,7 @@ def revert_IEEEtran(document):
               "Publication ID":       "\\IEEEpubid"}
   obsoletedby = {"Page headings":            "MarkBoth",
                  "Biography without photo":  "BiographyNoPhoto"}
+
   for layout in layouts:
     i = 0
     while True:
@@ -1586,7 +1604,7 @@ def revert_IEEEtran(document):
           document.warning("Malformed LyX document: Can't find end of " + layout + " layout.")
           i += 1
           continue
-        if layout in obsoletedby:
+        if layout in list(obsoletedby.keys()):
           document.body[i] = "\\begin_layout " + obsoletedby[layout]
           i = j
           continue
@@ -1650,7 +1668,7 @@ def revert_nameref(document):
   for cmd in cmds:
     i = 0
     oldcmd = "LatexCommand " + cmd
-    while 1:
+    while True:
       # It seems better to look for this, as most of the reference
       # insets won't be ones we care about.
       i = find_token(document.body, oldcmd, i)
@@ -1681,13 +1699,13 @@ def revert_nameref(document):
       document.body[stins:endins + 1] = newcontent
 
   if foundone:
-    add_to_preamble(document, ["\usepackage{nameref}"])
+    add_to_preamble(document, ["\\usepackage{nameref}"])
 
 
 def remove_Nameref(document):
   " Convert Nameref commands to nameref commands "
   i = 0
-  while 1:
+  while True:
     # It seems better to look for this, as most of the reference
     # insets won't be ones we care about.
     i = find_token(document.body, "LatexCommand Nameref" , i)
@@ -1817,9 +1835,16 @@ def convert_mathdots(document):
     " Load mathdots automatically "
     i = find_token(document.header, "\\use_mhchem" , 0)
     if i == -1:
-      i = find_token(document.header, "\\use_esint" , 0)
-    if i != -1:
-      document.header.insert(i + 1, "\\use_mathdots 1")
+        i = find_token(document.header, "\\use_esint" , 0)
+    if i == -1:
+        document.warning("Malformed LyX document: Can't find \\use_mhchem.")
+        return;
+    j = find_token(document.preamble, "\\usepackage{mathdots}", 0)
+    if j == -1:
+        document.header.insert(i + 1, "\\use_mathdots 0")
+    else:
+        document.header.insert(i + 1, "\\use_mathdots 2")
+        del document.preamble[j]
 
 
 def revert_mathdots(document):
@@ -1922,7 +1947,7 @@ def convert_rule(document):
 def revert_rule(document):
     " Revert line insets to Tex code "
     i = 0
-    while 1:
+    while True:
       i = find_token(document.body, "\\begin_inset CommandInset line" , i)
       if i == -1:
         return
@@ -2324,7 +2349,7 @@ def revert_script(document):
     " Convert subscript/superscript inset to TeX code "
     i = 0
     foundsubscript = False
-    while 1:
+    while True:
         i = find_token(document.body, '\\begin_inset script', i)
         if i == -1:
             break
@@ -2366,13 +2391,12 @@ def revert_script(document):
 
 def convert_use_xetex(document):
     " convert \\use_xetex to \\use_non_tex_fonts "
-    i = 0
     i = find_token(document.header, "\\use_xetex", 0)
     if i == -1:
-        return
-    
-    val = get_value(document.header, "\\use_xetex", 0)
-    document.header[i] = "\\use_non_tex_fonts " + val
+        document.header.insert(-1, "\\use_non_tex_fonts 0")
+    else:
+        val = get_value(document.header, "\\use_xetex", 0)
+        document.header[i] = "\\use_non_tex_fonts " + val
 
 
 def revert_use_xetex(document):