Commit a1dbd013 authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Some fixes for converting grid tables

parent 132f4fce
Loading
Loading
Loading
Loading
+44 −26
Original line number Diff line number Diff line
@@ -482,7 +482,7 @@ def parse_pandoc_table_with_spans(pandoc_table):
			self.colspan = 0
			self.colspan_adjusted = False
			self.alignment = "align=\"center\""
			self.position = 0
			self.position = None
			self.list_flag = False
			self.auxiliar_index = None

@@ -553,13 +553,17 @@ def parse_pandoc_table_with_spans(pandoc_table):

	def adjust_colspan(row, column_index, number_of_parts, line, number_of_columns, delimiter_positions):
		for j in range(column_index, number_of_parts):
			delimiter_start = row[j - 1].position if j != 0 else 0
			delimiter_start = None
			col_i= column_index
			while delimiter_start == None:
				delimiter_start = row[col_i - 1].position if col_i > 0 else 0
				col_i -= 1
			positions = [line.find(delimiter, delimiter_start + 1) for delimiter in "|+" if delimiter in line[delimiter_start + 1:]]
			position = min(positions) if positions else -1
			if position > delimiter_positions[j]:  # Colspan to be increased
				row[i].colspan += 1
				row[column_index].colspan += 1
				if position == delimiter_positions[len(delimiter_positions) - 1]:  # last cell in row, adjust colspan to get max number columns
					colspan_allocated = row[i].colspan
					colspan_allocated = row[column_index].colspan
					#for cell_index in range(number_of_parts):
					#	colspan_allocated += row[cell_index].colspan
					row[column_index].colspan += number_of_columns - colspan_allocated - column_index
@@ -637,15 +641,19 @@ def parse_pandoc_table_with_spans(pandoc_table):
					#	else:
					#		alignments.append("align=\"center\"")
					header_delimiter_index = 0
					table_row = Row(number_of_columns_row)
					for i in range(number_of_columns_row):
						delimiter_index += len(parts[i]) + 1
						table_row[i].alignment = default_alignments[i] if i == 0 else "align=\"center\""
					table_row = Row(number_of_columns)
					i = 0
					j = 0
					while i in range(number_of_columns) and j in range(len(parts)):
						delimiter_index += len(parts[j]) + 1
						#table_row[i].alignment = default_alignments[i] if i == 0 else "align=\"center\""
						table_row[i].position = delimiter_index # Position of cell delimiter +

						#Set alignment as defined by header separator line
						table_row[i].set_alignment()

						while delimiter_index > delimiter_positions[i]:
							i += 1
						i += 1
						j += 1
				elif in_data_row:
					# Regular data row or partial separator
					if _matchGridTableBodySeparator.match(line): # Partial separator
@@ -657,9 +665,10 @@ def parse_pandoc_table_with_spans(pandoc_table):
						for auxiliar_cell_index in range(number_of_columns):
							aux_delimiter_index += len(cells[auxiliar_cell_index]) + 1
							auxiliar_rows[-1][auxiliar_cell_index].position = aux_delimiter_index  # Position of cell delimiter +
						auxiliar_rows[-1][i].set_alignment()
							auxiliar_rows[-1][auxiliar_cell_index].set_alignment()

						if len(cells) <= number_of_columns: # Colspan: Positions of | with respect to + need to be determined
							table_row_index = 0
							for i in range(len(cells)):
								if _matchGridTableBodySeparatorLine.match(cells[i]):  # A new row is to be added
									#auxiliar_rows[-1]['use_auxiliar_row'][i] = True
@@ -678,33 +687,42 @@ def parse_pandoc_table_with_spans(pandoc_table):
										if not auxiliar_rows[table_row[i].auxiliar_index][i].colspan_adjusted:
											auxiliar_rows[table_row[i].auxiliar_index][i].colspan_adjusted = True
											# TO BE CHECKED Most probably the code below is never executed, colspan should be already adjusted when dealing with a partial separator
											auxiliar_rows[table_row[i].auxiliar_index][i] = adjust_colspan(auxiliar_rows[table_row[i].auxiliar_index], i, len(cells), line, number_of_columns, delimiter_positions)
											auxiliar_rows[table_row[i].auxiliar_index][i] = adjust_colspan(auxiliar_rows[table_row[i].auxiliar_index], i, number_of_columns, line, number_of_columns, delimiter_positions)
											table_row_index += auxiliar_rows[table_row[table_row_index].auxiliar_index][i].colspan - 1
									else:
										table_row[i] = handling_content(table_row[i], cells[i])
										table_row[table_row_index] = handling_content(table_row[table_row_index], cells[i])
										# Cell which is not separator
										table_row[i].rowspan += 1
										if not table_row.cells[i].colspan_adjusted:
											table_row[i].colspan_adjusted = True
										table_row[table_row_index].rowspan += 1
										if not table_row.cells[table_row_index].colspan_adjusted:
											table_row[table_row_index].colspan_adjusted = True
											#TO BE CHECKED Most probably the code below is never executed, colspan should be already adjusted when dealing with a partial separator
											table_row[i] = adjust_colspan(table_row, i, len(cells), line, number_of_columns, delimiter_positions)
											table_row[table_row_index] = adjust_colspan(table_row, table_row_index, number_of_columns, line, number_of_columns, delimiter_positions)
											#table_row_index += table_row[i].colspan - 1 #Move forward index i
								if table_row[table_row_index].position == delimiter_positions[i]:
									table_row_index += table_row[table_row_index].colspan if table_row[table_row_index].colspan != 0 else 1
						else:
							raise ValueError("More cells than columns found")
					else: # Data row
						cells = re.split(r"\s*\|\s*", line.strip("|"))
						if len(cells) < number_of_columns: # Colspan: Positions of | with respect to + need to be determined
							table_row_index = 0
							for i in range(len(cells)):
								# Handle content of the cell
								if table_row[i].auxiliar_index is not None:# and auxiliar_rows[table_row[i]['auxiliar_index']]['use_auxiliar_row'][i]:
									auxiliar_rows[table_row.cells[i].auxiliar_index][i] = handling_content(auxiliar_rows[table_row[i].auxiliar_index][i], cells[i])
									if not auxiliar_rows[table_row.cells[i].auxiliar_index].cells[i].colspan_adjusted:
										auxiliar_rows[table_row.cells[i].auxiliar_index].cells[i].colspan_adjusted = True
								if table_row[table_row_index].auxiliar_index is not None:# and auxiliar_rows[table_row[i]['auxiliar_index']]['use_auxiliar_row'][i]:
									auxiliar_rows[table_row.cells[table_row_index].auxiliar_index][i] = handling_content(auxiliar_rows[table_row[table_row_index].auxiliar_index][i], cells[i])
									if not auxiliar_rows[table_row[table_row_index].auxiliar_index].cells[i].colspan_adjusted:
										auxiliar_rows[table_row[table_row_index].auxiliar_index].cells[i].colspan_adjusted = True
										#TO BE CHECKED Most probably the code below is never executed, colspan should be already adjusted when dealing with a partial separator
										auxiliar_rows[table_row[i].auxiliar_index][i] = adjust_colspan(auxiliar_rows[table_row[i].auxiliar_index].cells, i, len(cells), line, number_of_columns, delimiter_positions)
										auxiliar_rows[table_row[table_row_index].auxiliar_index][i] = adjust_colspan(auxiliar_rows[table_row[table_row_index].auxiliar_index].cells, i, number_of_columns, line, number_of_columns, delimiter_positions)
										table_row_index += auxiliar_rows[table_row[table_row_index].auxiliar_index][i].colspan - 1  # Move forward index i
								else:
									table_row[i] = handling_content(table_row[i], cells[i])
									if not table_row.cells[i].colspan_adjusted:
										table_row[i].colspan_adjusted = True
										table_row[i] = adjust_colspan(table_row.cells, i, len(cells), line, number_of_columns, delimiter_positions)
									table_row[table_row_index] = handling_content(table_row[table_row_index], cells[i])
									if not table_row.cells[table_row_index].colspan_adjusted:
										table_row[table_row_index].colspan_adjusted = True
										table_row[table_row_index] = adjust_colspan(table_row.cells, table_row_index, number_of_columns, line, number_of_columns, delimiter_positions)
										table_row_index += table_row[table_row_index].colspan - 1  # Move forward index i

								table_row_index += 1
						elif len(cells) == number_of_columns: # Simple row
							for i in range(len(cells)):
								if table_row[i].auxiliar_index is not None:# and auxiliar_rows[table_row[i]['auxiliar_index']]['use_auxiliar_row'][i]: