" Revert the tabular valign option "
i = 0
while True:
- i = find_token(document.body, "\\begin_inset Tabular", i)
- if i == -1:
- return
- j = find_token(document.body, "</cell>", i)
- if j == -1:
- document.warning("Malformed LyX document: Could not find end of tabular cell.")
- i += 1
- continue
- # don't set a box for longtables, only delete tabularvalignment
- # the alignment is 2 lines below \\begin_inset Tabular
- p = document.body[i + 2].find("islongtable")
- if p > -1:
- q = document.body[i + 2].find("tabularvalignment")
- if q > -1:
- document.body[i + 2] = document.body[i + 2][:q - 1]
- document.body[i + 2] = document.body[i + 2] + '>'
- i = i + 1
-
- # when no longtable
- if p == -1:
- tabularvalignment = 'c'
- # which valignment is specified?
- m = document.body[i + 2].find('tabularvalignment="top"')
- if m > -1:
- tabularvalignment = 't'
- m = document.body[ i+ 2].find('tabularvalignment="bottom"')
- if m > -1:
- tabularvalignment = 'b'
- # delete tabularvalignment
- q = document.body[i + 2].find("tabularvalignment")
- if q > -1:
- document.body[i + 2] = document.body[i + 2][:q - 1]
- document.body[i + 2] = document.body[i + 2] + '>'
-
- # don't add a box when centered
- if tabularvalignment == 'c':
- i = j
- continue
- subst = ['\\end_layout', '\\end_inset']
- document.body[j:j] = subst # just inserts those lines
- subst = ['\\begin_inset Box Frameless',
- 'position "' + tabularvalignment +'"',
- 'hor_pos "c"',
- 'has_inner_box 1',
- 'inner_pos "c"',
- 'use_parbox 0',
- # we don't know the width, assume 50%
- 'width "50col%"',
- 'special "none"',
- 'height "1in"',
- 'height_special "totalheight"',
- 'status open',
- '',
- '\\begin_layout Plain Layout']
- document.body[i:i] = subst # this just inserts the array at i
- i += len(subst) + 2 # adjust i to save a few cycles
+ i = find_token(document.body, "\\begin_inset Tabular", i)
+ if i == -1:
+ return
+ end = find_end_of_inset(document.body, i)
+ if end == -1:
+ document.warning("Can't find end of inset at line " + str(i))
+ i = j
+ continue
+ fline = find_token(document.body, "<features", i, end)
+ if fline == -1:
+ document.warning("Can't find features for inset at line " + str(i))
+ i = end
+ continue
+ p = document.body[fline].find("islongtable")
+ if p != -1:
+ q = document.body[fline].find("tabularvalignment")
+ if q != -1:
+ # FIXME
+ # This seems wrong: It removes everything after
+ # tabularvalignment, too.
+ document.body[fline] = document.body[fline][:q - 1] + '>'
+ i = end
+ continue
+ # no longtable
+ tabularvalignment = 'c'
+ # which valignment is specified?
+ m = document.body[fline].find('tabularvalignment="top"')
+ if m != -1:
+ tabularvalignment = 't'
+ m = document.body[fline].find('tabularvalignment="bottom"')
+ if m != -1:
+ tabularvalignment = 'b'
+ # delete tabularvalignment
+ q = document.body[fline].find("tabularvalignment")
+ if q != -1:
+ # FIXME
+ # This seems wrong: It removes everything after
+ # tabularvalignment, too.
+ document.body[fline] = document.body[fline][:q - 1] + '>'
+
+ # don't add a box when centered
+ if tabularvalignment == 'c':
+ i = end
+ continue
+ subst = ['\\end_layout', '\\end_inset']
+ document.body[end:end] = subst # just inserts those lines
+ subst = ['\\begin_inset Box Frameless',
+ 'position "' + tabularvalignment +'"',
+ 'hor_pos "c"',
+ 'has_inner_box 1',
+ 'inner_pos "c"',
+ 'use_parbox 0',
+ # we don't know the width, assume 50%
+ 'width "50col%"',
+ 'special "none"',
+ 'height "1in"',
+ 'height_special "totalheight"',
+ 'status open',
+ '',
+ '\\begin_layout Plain Layout']
+ document.body[i:i] = subst # this just inserts the array at i
+ i = end + len(subst) # adjust i to save a few cycles
-def revert_phantom(document):
+
+def revert_phantom_types(document, ptype, cmd):
" Reverts phantom to ERT "
i = 0
- j = 0
while True:
- i = find_token(document.body, "\\begin_inset Phantom Phantom", i)
+ i = find_token(document.body, "\\begin_inset Phantom " + ptype, i)
if i == -1:
return
- substi = document.body[i].replace('\\begin_inset Phantom Phantom', \
- '\\begin_inset ERT\nstatus collapsed\n\n' \
- '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
- 'phantom{\n\\end_layout\n\n\\end_inset\n')
- substi = substi.split('\n')
- document.body[i:i + 4] = substi
- i += len(substi)
- j = find_token(document.body, "\\end_layout", i)
- if j == -1:
- document.warning("Malformed LyX document: Could not find end of Phantom inset.")
- return
- substj = document.body[j].replace('\\end_layout', \
- '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
- '\\begin_layout Plain Layout\n\n' \
- '}\n\\end_layout\n\n\\end_inset\n')
- substj = substj.split('\n')
- document.body[j:j + 4] = substj
- i += len(substj)
+ end = find_end_of_inset(document.body, i)
+ if end == -1:
+ document.warning("Can't find end of inset at line " + str(i))
+ i += 1
+ continue
+ blay = find_token(document.body, "\\begin_layout Plain Layout", i, end)
+ if blay == -1:
+ document.warning("Can't find layout for inset at line " + str(i))
+ i = end
+ continue
+ bend = find_token(document.body, "\\end_layout", blay, end)
+ if bend == -1:
+ document.warning("Malformed LyX document: Could not find end of Phantom inset's layout.")
+ i = end
+ continue
+ substi = ["\\begin_inset ERT", "status collapsed", "",
+ "\\begin_layout Plain Layout", "", "", "\\backslash",
+ cmd + "{", "\\end_layout", "", "\\end_inset"]
+ substj = ["\\size default", "", "\\begin_inset ERT", "status collapsed", "",
+ "\\begin_layout Plain Layout", "", "}", "\\end_layout", "", "\\end_inset"]
+ # do the later one first so as not to mess up the numbering
+ document.body[bend:end + 1] = substj
+ document.body[i:blay + 1] = substi
+ i = end + len(substi) + len(substj) - (end - bend) - (blay - i) - 2
+def revert_phantom(document):
+ revert_phantom_types(document, "Phantom", "phantom")
+
def revert_hphantom(document):
- " Reverts hphantom to ERT "
- i = 0
- j = 0
- while True:
- i = find_token(document.body, "\\begin_inset Phantom HPhantom", i)
- if i == -1:
- return
- substi = document.body[i].replace('\\begin_inset Phantom HPhantom', \
- '\\begin_inset ERT\nstatus collapsed\n\n' \
- '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
- 'hphantom{\n\\end_layout\n\n\\end_inset\n')
- substi = substi.split('\n')
- document.body[i:i + 4] = substi
- i += len(substi)
- j = find_token(document.body, "\\end_layout", i)
- if j == -1:
- document.warning("Malformed LyX document: Could not find end of HPhantom inset.")
- return
- substj = document.body[j].replace('\\end_layout', \
- '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
- '\\begin_layout Plain Layout\n\n' \
- '}\n\\end_layout\n\n\\end_inset\n')
- substj = substj.split('\n')
- document.body[j:j + 4] = substj
- i += len(substj)
-
+ revert_phantom_types(document, "HPhantom", "hphantom")
def revert_vphantom(document):
- " Reverts vphantom to ERT "
- i = 0
- j = 0
- while True:
- i = find_token(document.body, "\\begin_inset Phantom VPhantom", i)
- if i == -1:
- return
- substi = document.body[i].replace('\\begin_inset Phantom VPhantom', \
- '\\begin_inset ERT\nstatus collapsed\n\n' \
- '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
- 'vphantom{\n\\end_layout\n\n\\end_inset\n')
- substi = substi.split('\n')
- document.body[i:i + 4] = substi
- i += len(substi)
- j = find_token(document.body, "\\end_layout", i)
- if j == -1:
- document.warning("Malformed LyX document: Could not find end of VPhantom inset.")
- return
- substj = document.body[j].replace('\\end_layout', \
- '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
- '\\begin_layout Plain Layout\n\n' \
- '}\n\\end_layout\n\n\\end_inset\n')
- substj = substj.split('\n')
- document.body[j:j + 4] = substj
- i += len(substj)
+ revert_phantom_types(document, "VPhantom", "vphantom")
def revert_xetex(document):
del document.header[i]
+def hex2ratio(s):
+ val = string.atoi(s, 16)
+ if val != 0:
+ val += 1
+ return str(val / 256.0)
+
+
def revert_backgroundcolor(document):
" Reverts background color to preamble code "
- i = 0
- colorcode = ""
- while True:
- i = find_token(document.header, "\\backgroundcolor", i)
- if i == -1:
- return
- colorcode = get_value(document.header, '\\backgroundcolor', 0)
- del document.header[i]
- # don't clutter the preamble if backgroundcolor is not set
- if colorcode == "#ffffff":
- continue
- # the color code is in the form #rrggbb where every character denotes a hex number
- # convert the string to an int
- red = string.atoi(colorcode[1:3],16)
- # we want the output "0.5" for the value "127" therefore add here
- if red != 0:
- red = red + 1
- redout = float(red) / 256
- green = string.atoi(colorcode[3:5],16)
- if green != 0:
- green = green + 1
- greenout = float(green) / 256
- blue = string.atoi(colorcode[5:7],16)
- if blue != 0:
- blue = blue + 1
- blueout = float(blue) / 256
- # write the preamble
- insert_to_preamble(0, document,
- '% Commands inserted by lyx2lyx to set the background color\n'
- + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
- + '\\definecolor{page_backgroundcolor}{rgb}{'
- + str(redout) + ', ' + str(greenout)
- + ', ' + str(blueout) + '}\n'
- + '\\pagecolor{page_backgroundcolor}\n')
+ i = find_token(document.header, "\\backgroundcolor", 0)
+ if i == -1:
+ return
+ colorcode = get_value(document.header, '\\backgroundcolor', 0)
+ del document.header[i]
+ # don't clutter the preamble if backgroundcolor is not set
+ if colorcode == "#ffffff":
+ return
+ red = hex2ratio(colorcode[1:3])
+ green = hex2ratio(colorcode[3:5])
+ blue = hex2ratio(colorcode[5:7])
+ insert_to_preamble(0, document,
+ '% Commands inserted by lyx2lyx to set the background color\n'
+ + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
+ + '\\definecolor{page_backgroundcolor}{rgb}{'
+ + red + ',' + green + ',' + blue + '}\n'
+ + '\\pagecolor{page_backgroundcolor}\n')
def revert_splitindex(document):