]> git.lyx.org Git - features.git/blobdiff - lib/lyx2lyx/lyx_2_2.py
Try to fix bug #9587 correctly.
[features.git] / lib / lyx2lyx / lyx_2_2.py
index 77394af99bdf347330a95d96b7ecdaf99797c7e8..cd69744ea9fb985d87d52d5b0d8bfdd703f5df2b 100644 (file)
@@ -1094,43 +1094,64 @@ def convert_colorbox(document):
 def revert_colorbox(document):
     " outputs color settings for boxes as TeX code "
 
-    i = 0
+    binset = 0
     defaultframecolor = "black"
     defaultbackcolor = "none"
     while True:
-        i = find_token(document.body, "framecolor", i)
-        if i == -1:
+        binset = find_token(document.body, "\\begin_inset Box", binset)
+        if binset == -1:
             return
-        # read out the values
-        beg = document.body[i].find('"');
-        end = document.body[i].rfind('"');
-        framecolor = document.body[i][beg+1:end];
-        beg = document.body[i + 1].find('"');
-        end = document.body[i + 1].rfind('"');
-        backcolor = document.body[i+1][beg+1:end];
-        j = find_end_of_inset(document.body, i)
-        if j == -1:
+
+        einset = find_end_of_inset(document.body, binset)
+        if einset == -1:
             document.warning("Malformed LyX document: Can't find end of box inset!")
-            i += 1
+            binset += 1
             continue
-        # delete the specification
-        del document.body[i : i + 2]
+
+        blay = find_token(document.body, "\\begin_layout", binset, einset)
+        if blay == -1:
+            document.warning("Malformed LyX document: Can't find start of layout!")
+            binset = einset
+            continue
+
+        # doing it this way, we make sure only to find a framecolor option
+        frame = find_token(document.body, "framecolor", binset, blay)
+        if frame == -1:
+            binset = einset
+            continue
+
+        beg = document.body[frame].find('"')
+        end = document.body[frame].rfind('"')
+        framecolor = document.body[frame][beg+1:end]
+
+        # this should be on the next line
+        bgcolor = frame + 1
+        beg = document.body[bgcolor].find('"')
+        end = document.body[bgcolor].rfind('"')
+        backcolor = document.body[bgcolor][beg+1:end]
+
+        # delete those bits
+        del document.body[frame:frame+2]
+        # adjust end of inset
+        einset -= 2
+
+        if document.body[binset] == "\\begin_inset Box Boxed" and \
+            framecolor != defaultframecolor:
+          document.body[binset] = "\\begin_inset Box Frameless"
+
         # output TeX code
         # first output the closing brace
-        if framecolor != defaultframecolor or backcolor != defaultbackcolor:
-            document.body[j + 1 : j + 1] = put_cmd_in_ert("}")
-        # now output the box commands
-        if framecolor != defaultframecolor or backcolor != defaultbackcolor:
-            document.body[i - 14 : i - 14] = put_cmd_in_ert("{")
-        if framecolor != defaultframecolor:
-            document.body[i - 9 : i - 8] = ["\\backslash fcolorbox{" + framecolor + "}{" + backcolor + "}{"]
-            k = find_token(document.body, "\\begin_inset Box Boxed", i - 5)
-            if k == i - 5:
-                # \fcolorbox is already framed box thus remove the frame
-                document.body[k : k + 1] = ["\\begin_inset Box Frameless"]
-        if backcolor != defaultbackcolor and framecolor == defaultframecolor:
-            document.body[i - 9 : i - 8] = ["\\backslash colorbox{" + backcolor + "}{"]
-        i = i + 11
+        if framecolor == defaultframecolor and backcolor == defaultbackcolor:
+            # nothing needed
+            pass
+        else:
+            document.body[einset + 1 : einset + 1] = put_cmd_in_ert("}")
+            if framecolor != defaultframecolor:
+                document.body[binset:binset] = put_cmd_in_ert("\\backslash fcolorbox{" + framecolor + "}{" + backcolor + "}{")
+            else:
+              document.body[binset:binset] = put_cmd_in_ert("\\backslash colorbox{" + backcolor + "}{")
+
+        binset = einset
 
 
 def revert_mathmulticol(document):