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

Merge branch 'fixesGridtables' into 'master'

Fixes for gridtables

See merge request tools/scripts!2
parents 0af4d248 b5dfe5a8
Loading
Loading
Loading
Loading
+44 −18
Original line number Diff line number Diff line
@@ -371,6 +371,8 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR
							# Set alignment as defined by header separator line
							cell.calculateAndSetAlignment(headerDelimiterPositions, delimiterPositions, defaultAlignments, hasHeader)

							#adjustColspan(rows[-1], columnIndex, numberOfColumns, line, numberOfColumns, delimiterPositions)

							while delimiterIndex > delimiterPositions[columnIndex]:
								columnIndex += 1
							columnIndex += 1
@@ -382,13 +384,14 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR
						if not checkDelimiterAlignment(line, delimiterPositions):
							raise ValueError(f'Misaligned delimiters in partial separator: {line}')

						cellsContent = re.split(r'[\|\+]', line.strip('|').strip('+'))  # (?<!\\)[\|\+]
						parts = re.split(r'[\|\+]', line.strip('|').strip('+'))  # (?<!\\)[\|\+]

						#Add another row, set delimiters for each cell
						rows.append(GridRow(numberOfColumns))
						auxDelimiterIndex = 0
						auxiliarCellIndex = 0

						for columnIndex, content in enumerate(cellsContent):
						for columnIndex, content in enumerate(parts):
							if auxiliarCellIndex < numberOfColumns:
								auxDelimiterIndex += len(content) + 1
								cell = rows[-1][auxiliarCellIndex]
@@ -399,13 +402,13 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR
									auxiliarCellIndex += 1
								auxiliarCellIndex += 1

						if len(cellsContent) <= numberOfColumns: # Colspan: Positions of | with respect to + need to be determined
						if len(parts) <= numberOfColumns: # Colspan: Positions of | with respect to + need to be determined
							columnCellIndex = 0

							# Put the value in a variable here because we need the initial value
							maxRowsTracker = rowsTracker.max()
							# Go through all cells in a columnt
							for columnIndex, content in enumerate(cellsContent):
							for columnIndex, content in enumerate(parts):
								rowIndex = rowsTracker[columnCellIndex]
								cell = rows[rowIndex][columnCellIndex]

@@ -429,7 +432,7 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR

								else:
									# Handle content of the cell
									handleCellContent(cell, cellsContent[columnIndex])
									handleCellContent(cell, parts[columnIndex])
									cell.rowspan += 1
									if not cell.colspanAdjusted:
										# TO BE CHECKED Most probably the code below is never executed, colspan should be already adjusted when dealing with a partial separator
@@ -440,7 +443,7 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR
									continue

						else:
							raise ValueError(f'More cells than columns found ({len(cellsContent)} {numberOfColumns})')
							raise ValueError(f'More cells than columns found ({len(parts)} {numberOfColumns})')
						
					else: # Data row
						cellsContent = re.split(r'\|', line.strip('|'))
@@ -449,6 +452,16 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR
						if not checkDelimiterAlignment(line, delimiterPositions):
							raise ValueError(f'Misaligned delimiters in row: {line}')

						#Check correctness of columns as defined by the separator
						if len(parts) < len(cellsContent): #Something is wrong, either separator line missing delimiters or actual line with many cells
							#Determine which cells to allocate to each part
							raise ValueError(f'Missing delimiters in previous separator line')
						#elif len(parts) > len(cellsContent): #Missing delimiters in actual line
						#	raise ValueError(f'Missing delimiters in row: {line}: delimiters = {len(cellsContent)}, expected delimiters = {len(parts)}')
						else:
							printDebug(f'\nProcessing line: "{line}"')
							printDebug(f'\Parameters: "Number of cells: {len(cellsContent)}, Expected number of cells: {len(parts)} "')	
							
						columnCellIndex = 0
						if len(cellsContent) < numberOfColumns: # Colspan: Positions of | with respect to + need to be determined
							for columnIndex, content in enumerate(cellsContent):
@@ -462,10 +475,13 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR
								if cell.position >= delimiterPositions[columnCellIndex]:
									columnCellIndex += cell.colspan  # Move forward index i
								
								printDebug(f'\nCell: "{cell}"')

						elif len(cellsContent) == numberOfColumns: # Simple row
							for columnIndex, content in enumerate(cellsContent):
								rowIndex = rowsTracker[columnIndex]
								handleCellContent(rows[rowIndex][columnIndex], content)
								printDebug(f'\nCell: "{rows[rowIndex][columnIndex]}"')
						else:
							raise ValueError(f'More cells than columns found ({len(cellsContent)} {numberOfColumns})')
				else:
@@ -523,14 +539,14 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR
		if len(forwardRowspan) == 0:
			forwardRowspan = [0] * len(headerRows[idx])
		sum = 0

		rowForwardRowspan=forwardRowspan[:]
		for cellIndex, cell in enumerate(headerRow):
			sum += cell.colspan
			if idx > 0 and cell.colspan == 0:
				if forwardRowspan[cellIndex] > 0:
			if cell.colspan == 0:
				if rowForwardRowspan[cellIndex] > 0:
					sum += 1
					forwardRowspan[cellIndex] -= 1
			if forwardRowspan[cellIndex] == 0 and cell.rowspan > 1:
			if rowForwardRowspan[cellIndex] == 0 and cell.rowspan > 1:
				forwardRowspan[cellIndex] = cell.rowspan -1
				colspan=1
				while cell.colspan > colspan:
@@ -538,31 +554,41 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR
					colspan += 1

		if not sum == numberOfColumns:
			printDebug(f'\nChecking line: "{headerRow}"')
			raise ValueError('Grid table not converted properly')

		else:
			printDebug(f'\Correct line: "{headerRow}"')
	# Checking the data rows
	forwardRowspan = []
	#use_forwardRowspan = False
	for idx, dataRow in enumerate(dataRows):
		if len(forwardRowspan) == 0:
			forwardRowspan = [0] * len(dataRows[idx])
		sum = 0

		rowForwardRowspan=forwardRowspan[:]
		for cellIndex, cell in enumerate(dataRows[idx]):
			sum += cell.colspan
			if idx > 0 and cell.colspan == 0:
				if forwardRowspan[cellIndex] > 0:
			if cell.colspan == 0:
				if rowForwardRowspan[cellIndex] > 0:
					sum += 1
					forwardRowspan[cellIndex] -= 1
			if forwardRowspan[cellIndex] == 0 and cell.rowspan > 1:
			if rowForwardRowspan[cellIndex] == 0 and cell.rowspan > 1:
				forwardRowspan[cellIndex] = cell.rowspan - 1
				colspan=1
				while cell.colspan > colspan:
					forwardRowspan[cellIndex + colspan] = cell.rowspan - 1
					colspan += 1
			printDebug(f'\nrowForwardRowspan: {rowForwardRowspan}')
			printDebug(f'\nForwardRowspan: {forwardRowspan}')

		printDebug(f'\nForwardRowspan after row processing: {forwardRowspan}')
		#use_forwardRowspan = True		
		if not sum == numberOfColumns:
			printDebug(f'\nSum: {sum}')
			printDebug(f'\nIncorrect line: "{dataRow}"')
			raise ValueError('Grid table not converted properly')

		else:
			printDebug(f'\Correct line: "{dataRow}"')
	return headerRows, dataRows