- j = find_end_of_inset(file.body, i)
- if j == -1:
- file.warning("Malformed LyX file: Missing '\\end_inset'.")
- i = i + 1
- continue
- del file.body[i]
-
- # Gather parameters
- params = {'position':'0', 'hor_pos':'c', 'has_inner_box':'1',
- 'inner_pos':'1', 'use_parbox':'0', 'width':'100col%',
- 'special':'none', 'height':'1in',
- 'height_special':'totalheight', 'collapsed':'false'}
- for key in params.keys():
- value = replace(get_value(file.body, key, i, j), '"', '')
- if value != "":
- if key == 'position':
- # convert new to old position: 'position "t"' -> 0
- value = find_token(pos, value, 0)
- if value != -1:
- params[key] = value
- elif key == 'inner_pos':
- # convert inner position
- value = find_token(inner_pos, value, 0)
- if value != -1:
- params[key] = value
- else:
- params[key] = value
- j = del_token(file.body, key, i, j)
- i = i + 1
-
- # Convert to minipage or ERT?
- # Note that the inner_position and height parameters of a minipage
- # inset are ignored and not accessible for the user, although they
- # are present in the file format and correctly read in and written.
- # Therefore we convert to ERT if they do not have their LaTeX
- # defaults. These are:
- # - the value of "position" for "inner_pos"
- # - "\totalheight" for "height"
- if (params['use_parbox'] != '0' or
- params['has_inner_box'] != '1' or
- params['special'] != 'none' or
- inner_pos[params['inner_pos']] != pos[params['position']] or
- params['height_special'] != 'totalheight' or
- len2value(params['height']) != 1.0):
-
- # Convert to ERT
- if params['collapsed'] == 'true':
- params['collapsed'] = 'Collapsed'
- else:
- params['collapsed'] = 'Open'
- file.body[i : i] = ['\\begin_inset ERT', 'status ' + params['collapsed'],
- '', '\\layout Standard', '', '\\backslash ']
- i = i + 6
- if params['use_parbox'] == '1':
- file.body.insert(i, 'parbox')
- else:
- file.body.insert(i, 'begin{minipage}')
- file.body[i] = file.body[i] + '[' + pos[params['position']] + ']['
- i = convert_ertlen(file.body, i, params['height'], params['height_special'])
- file.body[i] = file.body[i] + '][' + inner_pos[params['inner_pos']] + ']{'
- i = convert_ertlen(file.body, i, params['width'], params['special'])
- if params['use_parbox'] == '1':
- file.body[i] = file.body[i] + '}{'
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Missing '\\end_inset'.")
+ i = i + 1
+ continue
+ del document.body[i]
+ j = j - 1
+
+ # Gather parameters
+ params = {'position':0, 'hor_pos':'c', 'has_inner_box':'1',
+ 'inner_pos':1, 'use_parbox':'0', 'width':'100col%',
+ 'special':'none', 'height':'1in',
+ 'height_special':'totalheight', 'collapsed':'false'}
+ for key in list(params.keys()):
+ value = get_value(document.body, key, i, j).replace('"', '')
+ if value != "":
+ if key == 'position':
+ # convert new to old position: 'position "t"' -> 0
+ value = find_token(pos, value, 0)
+ if value != -1:
+ params[key] = value
+ elif key == 'inner_pos':
+ # convert inner position
+ value = find_token(inner_pos, value, 0)
+ if value != -1:
+ params[key] = value
+ else:
+ params[key] = value
+ j = del_token(document.body, key, i, j)
+ i = i + 1
+
+ # Convert to minipage or ERT?
+ # Note that the inner_position and height parameters of a minipage
+ # inset are ignored and not accessible for the user, although they
+ # are present in the file format and correctly read in and written.
+ # Therefore we convert to ERT if they do not have their LaTeX
+ # defaults. These are:
+ # - the value of "position" for "inner_pos"
+ # - "\totalheight" for "height"
+ if (params['use_parbox'] != '0' or
+ params['has_inner_box'] != '1' or
+ params['special'] != 'none' or
+ params['height_special'] != 'totalheight' or
+ len2value(params['height']) != 1.0):
+
+ # Here we know that this box is not supported in file format 224.
+ # Therefore we need to convert it to ERT. We can't simply convert
+ # the beginning and end of the box to ERT, because the
+ # box inset may contain layouts that are different from the
+ # surrounding layout. After the conversion the contents of the
+ # box inset is on the same level as the surrounding text, and
+ # paragraph layouts and align parameters can get mixed up.
+
+ # A possible solution for this problem:
+ # Convert the box to a minipage and redefine the minipage
+ # environment in ERT so that the original box is simulated.
+ # For minipages we could do this in a way that the width and
+ # position can still be set from LyX, but this did not work well.
+ # This is not possible for parboxes either, so we convert the
+ # original box to ERT, put the minipage inset inside the box
+ # and redefine the minipage environment to be empty.
+
+ # Commands that are independant of a particular box can go to
+ # the preamble.
+ # We need to define lyxtolyxrealminipage with 3 optional
+ # arguments although LyX 1.3 uses only the first one.
+ # Otherwise we will get LaTeX errors if this document is
+ # converted to format 225 or above again (LyX 1.4 uses all
+ # optional arguments).
+ add_to_preamble(document,
+ ['% Commands inserted by lyx2lyx for frameless boxes',
+ '% Save the original minipage environment',
+ '\\let\\lyxtolyxrealminipage\\minipage',
+ '\\let\\endlyxtolyxrealminipage\\endminipage',
+ '% Define an empty lyxtolyximinipage environment',
+ '% with 3 optional arguments',
+ '\\newenvironment{lyxtolyxiiiminipage}[4]{}{}',
+ '\\newenvironment{lyxtolyxiiminipage}[2][\\lyxtolyxargi]%',
+ ' {\\begin{lyxtolyxiiiminipage}{\\lyxtolyxargi}{\\lyxtolyxargii}{#1}{#2}}%',
+ ' {\\end{lyxtolyxiiiminipage}}',
+ '\\newenvironment{lyxtolyximinipage}[1][\\totalheight]%',
+ ' {\\def\\lyxtolyxargii{{#1}}\\begin{lyxtolyxiiminipage}}%',
+ ' {\\end{lyxtolyxiiminipage}}',
+ '\\newenvironment{lyxtolyxminipage}[1][c]%',
+ ' {\\def\\lyxtolyxargi{{#1}}\\begin{lyxtolyximinipage}}',
+ ' {\\end{lyxtolyximinipage}}'])
+
+ if params['use_parbox'] != '0':
+ ert = '\\parbox'
+ else:
+ ert = '\\begin{lyxtolyxrealminipage}'
+
+ # convert optional arguments only if not latex default
+ if (pos[params['position']] != 'c' or
+ inner_pos[params['inner_pos']] != pos[params['position']] or
+ params['height_special'] != 'totalheight' or
+ len2value(params['height']) != 1.0):
+ ert = ert + '[' + pos[params['position']] + ']'
+ if (inner_pos[params['inner_pos']] != pos[params['position']] or
+ params['height_special'] != 'totalheight' or
+ len2value(params['height']) != 1.0):
+ ert = ert + '[' + convert_len(params['height'],
+ params['height_special']) + ']'
+ if inner_pos[params['inner_pos']] != pos[params['position']]:
+ ert = ert + '[' + inner_pos[params['inner_pos']] + ']'
+
+ ert = ert + '{' + convert_len(params['width'],
+ params['special']) + '}'
+
+ if params['use_parbox'] != '0':
+ ert = ert + '{'
+ ert = ert + '\\let\\minipage\\lyxtolyxminipage%\n'
+ ert = ert + '\\let\\endminipage\\endlyxtolyxminipage%\n'
+
+ old_i = i
+ i = insert_ert(document.body, i, 'Collapsed', ert, document.format - 1, document.default_layout)
+ j = j + i - old_i - 1
+
+ document.body[i:i] = ['\\begin_inset Minipage',
+ 'position %d' % params['position'],
+ 'inner_position 1',
+ 'height "1in"',
+ 'width "' + params['width'] + '"',
+ 'collapsed ' + params['collapsed']]
+ i = i + 6
+ j = j + 6
+
+ # Restore the original minipage environment since we may have
+ # minipages inside this box.
+ # Start a new paragraph because the following may be nonstandard
+ document.body[i:i] = ['\\layout %s' % document.default_layout, '', '']
+ i = i + 2
+ j = j + 3
+ ert = '\\let\\minipage\\lyxtolyxrealminipage%\n'
+ ert = ert + '\\let\\endminipage\\lyxtolyxrealendminipage%'
+ old_i = i
+ i = insert_ert(document.body, i, 'Collapsed', ert, document.format - 1, document.default_layout)
+ j = j + i - old_i - 1
+
+ # Redefine the minipage end before the inset end.
+ # Start a new paragraph because the previous may be nonstandard
+ document.body[j:j] = ['\\layout %s' % document.default_layout, '', '']
+ j = j + 2
+ ert = '\\let\\endminipage\\endlyxtolyxminipage'
+ j = insert_ert(document.body, j, 'Collapsed', ert, document.format - 1, document.default_layout)
+ j = j + 1
+ document.body.insert(j, '')
+ j = j + 1
+
+ # LyX writes '%\n' after each box. Therefore we need to end our
+ # ERT with '%\n', too, since this may swallow a following space.
+ if params['use_parbox'] != '0':
+ ert = '}%\n'