]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyxconvert_218.py
Handle Michael's changes to InsetCollapsable.
[lyx.git] / lib / lyx2lyx / lyxconvert_218.py
index 08e7f4f4366453dad846687560a06ded3ef6903d..78d245f6fe7a4a8c5ac28b7ffd9f35d4b35ec886 100644 (file)
@@ -44,10 +44,6 @@ floats = {
 font_tokens = ["\\family", "\\series", "\\shape", "\\size", "\\emph",
               "\\bar", "\\noun", "\\color", "\\lang", "\\latex"]
 
-#
-# Change \begin_float .. \end_float into \begin_inset Float .. \end_inset
-#
-
 pextra_type3_rexp = re.compile(r".*\\pextra_type\s+3")
 pextra_rexp = re.compile(r"\\pextra_type\s+(\S+)"+\
                         r"(\s+\\pextra_alignment\s+(\S+))?"+\
@@ -64,6 +60,10 @@ def get_width(mo):
     else:
        return "100col%"
 
+#
+# Change \begin_float .. \end_float into \begin_inset Float .. \end_inset
+#
+
 def remove_oldfloat(lines, language):
     i = 0
     while 1:
@@ -106,7 +106,7 @@ def remove_oldfloat(lines, language):
        # whose value is not default before the float.
        # The check here is not accurate, but it doesn't matter
        # as extra '\foo default' commands are ignored.
-       # In fact, it might be safer to output '\foo default' for all 
+       # In fact, it might be safer to output '\foo default' for all
        # font attributes.
        k = get_paragraph(lines, i)
        flag = 0
@@ -125,21 +125,29 @@ def remove_oldfloat(lines, language):
        lines[i:j+1] = new
        i = i+1
 
-pextra_type2_rexp = re.compile(r".*\\pextra_type\s+2")
+pextra_type2_rexp = re.compile(r".*\\pextra_type\s+[12]")
 pextra_type2_rexp2 = re.compile(r".*(\\layout|\\pextra_type\s+2)")
 
-def remove_oldminipage(lines):
+def remove_pextra(lines):
     i = 0
     flag = 0
     while 1:
        i = find_re(lines, pextra_type2_rexp, i)
        if i == -1:
            break
-       
+
        mo = pextra_rexp.search(lines[i])
+        width = get_width(mo)
+
+        if mo.group(1) == "1":
+            # handle \pextra_type 1 (indented paragraph)
+            lines[i] = re.sub(pextra_rexp, "\\leftindent "+width+" ", lines[i])
+            i = i+1
+            continue
+
+        # handle \pextra_type 2 (minipage)
        position = mo.group(3)
        hfill = mo.group(5)
-       width = get_width(mo)
        lines[i] = re.sub(pextra_rexp, "", lines[i])
 
        start = ["\\begin_inset Minipage",
@@ -156,11 +164,8 @@ def remove_oldminipage(lines):
        else:
            start = ["\\layout Standard"] + start
 
-       j = find_token_backwards(lines,"\\layout", i-1)
-       j0 = j
-
-       j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
-       # j can be -1
+       j0 = find_token_backwards(lines,"\\layout", i-1)
+       j = get_next_paragraph(lines, i)
 
        count = 0
        while 1:
@@ -190,7 +195,7 @@ def is_empty(lines):
     return filter(is_nonempty_line, lines) == []
 
 move_rexp =  re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun|end_deeper)")
-ert_rexp = re.compile(r"\\begin_inset|.*\\SpecialChar")
+ert_rexp = re.compile(r"\\begin_inset|\\hfill|.*\\SpecialChar")
 spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)")
 ert_begin = ["\\begin_inset ERT",
             "status Collapsed",
@@ -205,10 +210,12 @@ def remove_oldert(lines):
            break
        j = i+1
        while 1:
-           j = find_tokens(lines, ["\\latex default", "\\begin_inset", "\\layout", "\\end_inset", "\\end_float", "\\the_end"],
+            # \end_inset is for ert inside a tabular cell. The other tokens
+            # are obvious.
+           j = find_tokens(lines, ["\\latex default", "\\layout", "\\begin_inset", "\\end_inset", "\\end_float", "\\the_end"],
                            j)
            if check_token(lines[j], "\\begin_inset"):
-               j = find_end_of_inset(lines, j)
+               j = find_end_of_inset(lines, j)+1
            else:
                break
 
@@ -226,11 +233,15 @@ def remove_oldert(lines):
        k = i+1
        while 1:
            k2 = find_re(lines, ert_rexp, k, j)
-           inset = specialchar = 0
+           inset = hfill = specialchar = 0
            if k2 == -1:
                k2 = j
            elif check_token(lines[k2], "\\begin_inset"):
                inset = 1
+            elif check_token(lines[k2], "\\hfill"):
+                hfill = 1
+                del lines[k2]
+                j = j-1
            else:
                specialchar = 1
                mo = spchar_rexp.match(lines[k2])
@@ -269,6 +280,9 @@ def remove_oldert(lines):
                if not is_nonempty_line(lines[k]):
                    k = k+1
                    new.append("")
+            elif hfill:
+                new = new+["\hfill", ""]
+                k = k2
            elif specialchar:
                if new == []:
                    # This is not necessary, but we want the output to be
@@ -312,9 +326,13 @@ def remove_oldertinset(lines):
        i = i+1
 
 def is_ert_paragraph(lines, i):
+    if not check_token(lines[i], "\\layout Standard"):
+        return 0
+
     i = find_nonempty_line(lines, i+1)
     if not check_token(lines[i], "\\begin_inset ERT"):
        return 0
+
     j = find_end_of_inset(lines, i)
     k = find_nonempty_line(lines, j+1)
     return check_token(lines[k], "\\layout")
@@ -325,7 +343,7 @@ def combine_ert(lines):
        i = find_token(lines, "\\begin_inset ERT", i)
        if i == -1:
            break
-       j = find_token_backwards(lines,"\\layout", i-1)
+       j = get_paragraph(lines, i)
        count = 0
        text = []
        while is_ert_paragraph(lines, j):
@@ -343,7 +361,7 @@ def combine_ert(lines):
            lines[j:k] = text
 
        i = i+1
-       
+
 oldunits = ["pt", "cm", "in", "text%", "col%"]
 
 def get_length(lines, name, start, end):
@@ -365,8 +383,12 @@ def remove_figinset(lines):
            break
        j = find_end_of_inset(lines, i)
 
-       lyxwidth = string.split(lines[i])[3]+"pt"
-       lyxheight = string.split(lines[i])[4]+"pt"
+       if ( len(string.split(lines[i])) > 2 ):
+           lyxwidth = string.split(lines[i])[3]+"pt"
+           lyxheight = string.split(lines[i])[4]+"pt"
+       else:
+           lyxwidth = ""
+           lyxheight = ""
 
        filename = get_value(lines, "file", i+1, j)
 
@@ -388,9 +410,13 @@ def remove_figinset(lines):
        else:
            display = "color"
 
-       subcaptionText = get_value(lines, "subcaption", i+1, j)
-       if subcaptionText != "":
-           subcaptionText = '"'+subcaptionText+'"'
+       subcaptionText = ""
+       subcaptionLine = find_token(lines, "subcaption", i+1, j)
+       if subcaptionLine != -1:
+            subcaptionText = lines[subcaptionLine][11:]
+           if subcaptionText != "":
+               subcaptionText = '"'+subcaptionText+'"'
+
        k = find_token(lines, "subfigure", i+1,j)
        if k == -1:
            subcaption = 0
@@ -455,10 +481,35 @@ def change_listof(lines):
        i = find_token(lines, "\\begin_inset LatexCommand \\listof", i)
        if i == -1:
            break
-        type = lines[i][33:-3]
+        type = re.search(r"listof(\w*)", lines[i]).group(1)[:-1]
         lines[i] = "\\begin_inset FloatList "+type
         i = i+1
 
+def change_infoinset(lines):
+    i = 0
+    while 1:
+        i = find_token(lines, "\\begin_inset Info", i)
+        if i == -1:
+            break
+        txt = string.lstrip(lines[i][18:])
+        new = ["\\begin_inset Note", "collapsed true", ""]
+        j = find_token(lines, "\\end_inset", i)
+        if j == -1:
+            break
+
+        note_lines = lines[i+1:j]
+        if len(txt) > 0:
+            note_lines = [txt]+note_lines
+
+        for line in note_lines:
+            new = new + ["\layout Standard", ""]
+            tmp = string.split(line, '\\')
+            new = new + [tmp[0]]
+            for x in tmp[1:]:
+                new = new + ["\\backslash ", x]
+        lines[i:j] = new
+        i = i+5
+
 def change_preamble(lines):
     i = find_token(lines, "\\use_amsmath", 0)
     if i == -1:
@@ -475,12 +526,13 @@ def convert(header, body):
     change_listof(body)
     fix_oldfloatinset(body)
     update_tabular(body)
-    remove_oldminipage(body)
+    remove_pextra(body)
     remove_oldfloat(body, language)
     remove_figinset(body)
     remove_oldertinset(body)
     remove_oldert(body)
     combine_ert(body)
+    change_infoinset(body)
 
 if __name__ == "__main__":
     pass