]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyxconvert_218.py
re-add stuff to math menu
[lyx.git] / lib / lyx2lyx / lyxconvert_218.py
index 7484aa6349ebde88716f546f1a0c943389c4dd46..4b94a5388a0a65de25a0e7070fc81f4240f63aca 100644 (file)
@@ -25,23 +25,18 @@ floats = {
     "margin":   ["\\begin_inset Marginal",
                 "collapsed true"],
     "fig":      ["\\begin_inset Float figure",
-                "placement htbp",
                 "wide false",
                 "collapsed false"],
     "tab":      ["\\begin_inset Float table",
-                "placement htbp",
                 "wide false",
                 "collapsed false"],
     "alg":      ["\\begin_inset Float algorithm",
-                "placement htbp",
                 "wide false",
                 "collapsed false"],
     "wide-fig": ["\\begin_inset Float figure",
-                "placement htbp",
                 "wide true",
                 "collapsed false"],
     "wide-tab": ["\\begin_inset Float table",
-                "placement htbp",
                 "wide true",
                 "collapsed false"]
 }
@@ -49,6 +44,22 @@ floats = {
 font_tokens = ["\\family", "\\series", "\\shape", "\\size", "\\emph",
               "\\bar", "\\noun", "\\color", "\\lang", "\\latex"]
 
+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+))?"+\
+                        r"(\s+\\pextra_hfill\s+(\S+))?"+\
+                        r"(\s+\\pextra_start_minipage\s+(\S+))?"+\
+                        r"(\s+(\\pextra_widthp?)\s+(\S*))?")
+
+def get_width(mo):
+    if mo.group(10):
+       if mo.group(9) == "\\pextra_widthp":
+           return mo.group(10)+"col%"
+       else:
+           return mo.group(10)
+    else:
+       return "100col%"
+
 #
 # Change \begin_float .. \end_float into \begin_inset Float .. \end_inset
 #
@@ -76,9 +87,21 @@ def remove_oldfloat(lines, language):
            lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1))
 
        new = floats[floattype]+[""]
+
+       # Check if the float is floatingfigure
+       k = find_re(lines, pextra_type3_rexp, i, j)
+       if k != -1:
+           mo = pextra_rexp.search(lines[k])
+           width = get_width(mo)
+           lines[k] = re.sub(pextra_rexp, "", lines[k])
+           new = ["\\begin_inset Wrap figure",
+                  'width "%s"' % width,
+                  "collapsed false",
+                  ""]
+
        new = new+lines[i2:j]+["\\end_inset ", ""]
 
-       # After a float, all font attribute are reseted.
+       # After a float, all font attributes are reseted.
        # We need to output '\foo default' for every attribute foo
        # whose value is not default before the float.
        # The check here is not accurate, but it doesn't matter
@@ -102,27 +125,30 @@ def remove_oldfloat(lines, language):
        lines[i:j+1] = new
        i = i+1
 
-def remove_oldminipage(lines):
+pextra_type2_rexp = re.compile(r".*\\pextra_type\s+[12]")
+pextra_type2_rexp2 = re.compile(r".*(\\layout|\\pextra_type\s+2)")
+
+def remove_pextra(lines):
     i = 0
     flag = 0
     while 1:
-       i = find_token(lines, "\\pextra_type 2", i)
+       i = find_re(lines, pextra_type2_rexp, i)
        if i == -1:
            break
-       hfill = 0
-       line = string.split(lines[i])
-       if line[4] == "\\pextra_hfill":
-           hfill = 1
-           line[4:6] = []
-       if line[4] == "\\pextra_start_minipage":
-           # We just ignore this
-           line[4:6] = []
-
-       position = line[3]
-       width = line[5]
-       if line[4] == "\\pextra_widthp":
-           width = line[5]+"col%"
-       
+
+       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)
+       lines[i] = re.sub(pextra_rexp, "", lines[i])
 
        start = ["\\begin_inset Minipage",
                 "position " + position,
@@ -140,11 +166,9 @@ def remove_oldminipage(lines):
 
        j = find_token_backwards(lines,"\\layout", i-1)
        j0 = j
-       mid = lines[j:i]
 
        j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
-       # j can be -1, but this is still ok
-       mid = mid+lines[i+1:j]
+       # j can be -1
 
        count = 0
        while 1:
@@ -152,20 +176,19 @@ def remove_oldminipage(lines):
            count = count+1
            if j == -1 or not check_token(lines[j], "\\layout"):
                break
-           i = find_tokens(lines, ["\\layout", "\\pextra_type"], j+1)
-           if i == -1 or not check_token(lines[i], "\\pextra_type"):
+           i = find_re(lines, pextra_type2_rexp2, j+1)
+           if i == -1:
+               break
+           mo = pextra_rexp.search(lines[i])
+           if not mo:
                break
-           line = string.split(lines[i])
-           if line[4] == "\\pextra_hfill":
-               line[4:6] = []
-           if line[4] == "\\pextra_start_minipage" and line[5] == "1":
+           if mo.group(7) == "1":
                flag = 1
                break
-           j = find_token_backwards(lines,"\\layout", i-1)
-           mid = mid+lines[j:i]
+           lines[i] = re.sub(pextra_rexp, "", lines[i])
            j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
-           mid = mid+lines[i+1:j]
 
+       mid = lines[j0:j]
        end = ["\\end_inset "]
 
        lines[j0:j] = start+mid+end
@@ -174,7 +197,7 @@ def remove_oldminipage(lines):
 def is_empty(lines):
     return filter(is_nonempty_line, lines) == []
 
-font_rexp =  re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun)")
+move_rexp =  re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun|end_deeper)")
 ert_rexp = re.compile(r"\\begin_inset|.*\\SpecialChar")
 spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)")
 ert_begin = ["\\begin_inset ERT",
@@ -190,10 +213,12 @@ def remove_oldert(lines):
            break
        j = i+1
        while 1:
-           j = find_tokens(lines, ["\\latex default", "\\begin_inset", "\\layout", "\\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
 
@@ -225,7 +250,8 @@ def remove_oldert(lines):
 
            tmp = []
            for line in lines[k:k2]:
-               if font_rexp.match(line):
+                # Move some lines outside the ERT inset:
+               if move_rexp.match(line):
                    if new2 == []:
                        # This is not necessary, but we want the output to be
                        # as similar as posible to the lyx format
@@ -271,6 +297,7 @@ def remove_oldert(lines):
        lines[i:j+1] = new
        i = i+1
 
+    # Delete remaining "\latex xxx" tokens
     i = 0
     while 1:
        i = find_token(lines, "\\latex ", i)
@@ -278,7 +305,7 @@ def remove_oldert(lines):
            break
        del lines[i]
 
-
+# ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future.
 def remove_oldertinset(lines):
     i = 0
     while 1:
@@ -295,9 +322,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")
@@ -308,7 +339,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):
@@ -336,7 +367,7 @@ def get_length(lines, name, start, end):
     x = string.split(lines[i])
     return x[2]+oldunits[int(x[1])]
 
-def append(x, token, value):
+def write_attribute(x, token, value):
     if value != "":
        x.append("\t"+token+" "+value)
 
@@ -381,21 +412,21 @@ def remove_figinset(lines):
            subcaption = 1
 
        new = ["\\begin_inset Graphics FormatVersion 1"]
-       append(new, "filename", filename)
-       append(new, "display", display)
+       write_attribute(new, "filename", filename)
+       write_attribute(new, "display", display)
        if subcaption:
            new.append("\tsubcaption")
-       append(new, "subcaptionText", subcaptionText)
-       append(new, "size_type", size_type)
-       append(new, "width", width)
-       append(new, "height", height)
+       write_attribute(new, "subcaptionText", subcaptionText)
+       write_attribute(new, "size_type", size_type)
+       write_attribute(new, "width", width)
+       write_attribute(new, "height", height)
        if rotateAngle != "":
            new.append("\trotate")
-           append(new, "rotateAngle", rotateAngle)
-       new.append("\trotateOrigin center")
-       new.append("\tlyxsize_type 1")
-       append(new, "lyxwidth", lyxwidth)
-       append(new, "lyxheight", lyxheight)
+           write_attribute(new, "rotateAngle", rotateAngle)
+       write_attribute(new, "rotateOrigin", "leftBaseline")
+       write_attribute(new, "lyxsize_type", "1")
+       write_attribute(new, "lyxwidth", lyxwidth)
+       write_attribute(new, "lyxheight", lyxheight)
        new = new + ["\end_inset"]
        lines[i:j+1] = new
 
@@ -409,21 +440,39 @@ def update_tabular(lines):
         if i == -1:
             break
 
-        # scan table header meta-info
-        lines[i+1] = string.replace(lines[i+1], 'version="2"', 'version="3"')
-
-        j = find_token(lines, '</lyxtabular>', i)
-        if j == -1:
-            break
+       for k in get_tabular_lines(lines, i):
+           if check_token(lines[k], "<lyxtabular"):
+               lines[k] = string.replace(lines[k], 'version="2"', 'version="3"')
+           elif check_token(lines[k], "<column"):
+               lines[k] = string.replace(lines[k], 'width=""', 'width="0pt"')
 
-       for k in xrange(i+2,j):
-           if check_token(lines[k], "<column"):
-                lines[k] = string.replace(lines[k], 'width=""', 'width="0pt"')
            if line_re.match(lines[k]):
                lines[k] = re.sub(attr_re, "", lines[k])
 
        i = i+1
 
+# Figure insert are hidden feature of lyx 1.1.6. This might be removed in the future.
+def fix_oldfloatinset(lines):
+    i = 0
+    while 1:
+       i = find_token(lines, "\\begin_inset Float ", i)
+       if i == -1:
+           break
+        j = find_token(lines, "collapsed", i)
+        if j != -1:
+            lines[j:j] = ["wide false"]
+        i = i+1
+
+def change_listof(lines):
+    i = 0
+    while 1:
+       i = find_token(lines, "\\begin_inset LatexCommand \\listof", i)
+       if i == -1:
+           break
+        type = lines[i][33:-3]
+        lines[i] = "\\begin_inset FloatList "+type
+        i = i+1
+
 def change_preamble(lines):
     i = find_token(lines, "\\use_amsmath", 0)
     if i == -1:
@@ -437,8 +486,10 @@ def convert(header, body):
        language = "english"
 
     change_preamble(header)
+    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)