In my opinion, the problems when reading old files (bugs #374,#375,#376)
are unacceptable.

I suggest a different approach for this problem:
the conversion should be done by an outside script and not in LyX source code.
Attached is a proof of concept script that converts minipages & float from
old format to new format.
The script correctly handle the test case of bugs #374,#375,#376.
Comments?
#!/usr/bin/python
import sys,string

def check_token(lines, token, i):
    if lines[i][:len(token)] == token:
        return 1
    return 0

def find_token(lines, token, start):
    n = len(lines)
    m = len(token)
    i = start
    while i < n:
        line = lines[i]
        if line[:m] == token:
            return i
        i = i+1
    return -1

def find_token2(lines, token1, token2, start):
    n = len(lines)
    m1 = len(token1)
    m2 = len(token2)
    i = start
    while i < n:
        line = lines[i]
        if line[:m1] == token1 or line[:m2] == token2:
            return i
        i = i+1
    return -1

def find_token_backwards(lines, token, start):
    n = len(lines)
    m = len(token)
    i = start
    while i >= 0:
        line = lines[i]
        if line[:m] == token:
            return i
        i = i-1
    return -1

floats = {
    "footnote": ["\\begin_inset Foot\n",
                 "collapsed true\n"],
    "margin":   ["\\begin_inset Marginal\n",
                 "collapsed true\n"],
    "fig":      ["\\begin_inset Float figure\n",
                 "placement htbp\n",
                 "wide false\n",
                 "collapsed false\n"],
    "tab":      ["\\begin_inset Float table\n",
                 "placement htbp\n",
                 "wide false\n",
                 "collapsed false\n"],
    "alg":      ["\\begin_inset Float algorithm\n",
                 "placement htbp\n",
                 "wide false\n",
                 "collapsed false\n"],
    "wide-fig": ["\\begin_inset Float figure\n",
                 "placement htbp\n",
                 "wide true\n",
                 "collapsed false\n"],
    "wide-tab": ["\\begin_inset Float table\n",
                 "placement htbp\n",
                 "wide true\n",
                 "collapsed false\n"]
}

def remove_oldfloat(lines):
    i = 0
    while 1:
        i = find_token(lines, "\\begin_float", i)
        if i == -1:
            break
        j = find_token(lines, "\\end_float", i+1)
        floattype = string.split(lines[i])[1]
        #print floattype
        start = floats[floattype]+["\n"]
        mid = lines[i+1:j]
        lines[i:j+1]= start+mid+["\\end_inset \n"]
        i = i+1

def remove_oldminipage(lines):
    i = 0
    flag = 0
    while 1:
        i = find_token(lines, "\\pextra_type 2", i)
        if i == -1:
            break
        hfill = 0
        line = string.split(lines[i])
        if line[4] == "\\pextra_hfill":
            line[4:6] = []
            hfill = 1
        if line[4] == "\\pextra_start_minipage":
            line[4:6] = []
        width = line[5]
        if line[4] == "\\pextra_widthp":
            width = line[5]+"col%"

        start = ["\\begin_inset Minipage\n",
                 "position %s\n" % line[3],
                 "inner_position 0\n",
                 'height "0pt"\n',
                 'width "%s"\n' % width,
                 "collapsed false\n"
                 ]
        if flag:
            flag = 0
            if hfill:
                start = ["\n","\hfill\n","\n"]+start
        else:
            start = ["\\layout Standard\n"] + start

        j = find_token_backwards(lines,"\\layout", i-1)
        j0 = j
        mid = lines[j:i]

        j = find_token2(lines,"\\layout", "\\end_float", i+1)
        mid = mid+lines[i+1:j]

        while 1:
            i = find_token2(lines,"\\layout", "\\pextra_type", j+1)
            if i == -1 or not check_token(lines,  "\\pextra_type", i):
                break
            line = string.split(lines[i])
            if line[4] == "\\pextra_hfill":
                line[4:6] = []
            if line[4] == "\\pextra_start_minipage" and line[5] == "1":
                flag = 1
                break
            j = find_token_backwards(lines,"\\layout", i-1)
            mid = mid+lines[j:i]
            j = find_token2(lines,"\\layout", "\\end_float", i+1)
            mid = mid+lines[i+1:j]

        end = ["\\end_inset \n"]

        lines[j0:j] = start+mid+end
        i = i+1


in_file = sys.argv[1]
out_file = sys.argv[2]

fh = open(in_file)
lines = fh.readlines()
fh.close()

remove_oldminipage(lines)
remove_oldfloat(lines)

fh = open(out_file, "w")
for line in lines:
    fh.write(line)
fh.close()

Reply via email to