- # find begin/end of table
- begin_table = find_token(document.body, '<lyxtabular version=', begin_table)
- if begin_table == -1:
- break
- end_table = find_token(document.body, '</lyxtabular>', begin_table)
- if end_table == -1:
- document.warning("Malformed LyX document: Could not find end of table.")
- break
- # store the number of rows
- begin = document.body[begin_table].find('rows="')
- end = document.body[begin_table].find('" ', begin)
- rows = document.body[begin_table][begin + 6:end]
- # store the number of columns
- begin = document.body[begin_table].find('columns="')
- end = document.body[begin_table].find('">', begin)
- columns = document.body[begin_table][begin + 9:end]
- # find column number of the multirows with offset
- begin_row = begin_table
- for row in range(int(rows)):
- begin_row = find_token(document.body, '<row>', begin_row)
- begin_cell = begin_row
- for column in range(int(columns)):
- begin_cell = find_token(document.body, '<cell ', begin_cell)
- i = document.body[begin_cell].find('multirow="3" mroffset=')
- if i <> -1:
- # we found a multirow in column number column - 1
- # store column width
- begin = document.body[begin_table + 2 + column].find('width="')
- end = document.body[begin_table + 2 + column].find('">', begin)
- col_width = document.body[begin_table + 2 + column][begin + 7:end]
+ # find begin/end of table
+ begin_table = find_token(document.body, '<lyxtabular version=', begin_table)
+ if begin_table == -1:
+ break
+ end_table = find_end_of(document.body, begin_table, '<lyxtabular', '</lyxtabular>')
+ if end_table == -1:
+ document.warning("Malformed LyX document: Could not find end of table.")
+ begin_table += 1
+ continue
+ # does this table have multirow?
+ i = find_token(document.body, '<cell multirow="3"', begin_table, end_table)
+ if i == -1:
+ begin_table = end_table
+ continue
+
+ # store the number of rows and columns
+ numrows = get_option_value(document.body[begin_table], "rows")
+ numcols = get_option_value(document.body[begin_table], "columns")
+ try:
+ numrows = int(numrows)
+ numcols = int(numcols)
+ except:
+ document.warning("Unable to determine rows and columns!")
+ begin_table = end_table
+ continue
+
+ mrstarts = []
+ multirows = []
+ # collect info on rows and columns of this table.
+ begin_row = begin_table
+ for row in range(numrows):
+ begin_row = find_token(document.body, '<row>', begin_row, end_table)
+ if begin_row == -1:
+ document.warning("Can't find row " + str(row + 1))
+ break
+ end_row = find_end_of(document.body, begin_row, '<row>', '</row>')
+ if end_row == -1:
+ document.warning("Can't find end of row " + str(row + 1))
+ break
+ begin_cell = begin_row
+ multirows.append([])
+ for column in range(numcols):
+ begin_cell = find_token(document.body, '<cell ', begin_cell, end_row)
+ if begin_cell == -1:
+ document.warning("Can't find column " + str(column + 1) + \
+ "in row " + str(row + 1))
+ break
+ # NOTE
+ # this will fail if someone puts "</cell>" in a cell, but
+ # that seems fairly unlikely.
+ end_cell = find_end_of(document.body, begin_cell, '<cell', '</cell>')
+ if end_cell == -1:
+ document.warning("Can't find end of column " + str(column + 1) + \
+ "in row " + str(row + 1))
+ break
+ multirows[row].append([begin_cell, end_cell, 0])
+ if document.body[begin_cell].find('multirow="3" mroffset=') != -1:
+ multirows[row][column][2] = 3 # begin multirow
+ mrstarts.append([row, column])
+ elif document.body[begin_cell].find('multirow="4"') != -1:
+ multirows[row][column][2] = 4 # in multirow
+ begin_cell = end_cell
+ begin_row = end_row
+ # end of table info collection
+
+ # work from the back to avoid messing up numbering
+ mrstarts.reverse()
+ for m in mrstarts:
+ row = m[0]
+ col = m[1]
+ # get column width
+ col_width = get_option_value(document.body[begin_table + 2 + col], "width")