Commit 01141186 authored by Naum Spaseski's avatar Naum Spaseski Committed by Miguel Angel Reina Ortega
Browse files

If header cell is merged, generate grid table without headers

parent a93e5329
Loading
Loading
Loading
Loading
Loading
+51 −14
Original line number Diff line number Diff line
@@ -57,12 +57,37 @@ def markdownToGrid(markdownLines:list[str]) -> list[str]:
		while len(row) < maxCols:
			row.append('')
	
	# Check if header row has vertical merges with content rows
	# If the first data row (row 2, index 2) contains rowspan markers,
	# it means header cells are merged vertically with content cells
	hasHeaderVerticalMerge = False
	if len(rows) > 2:
		# Check if any cell in the first data row (index 2) is a rowspan marker
		# This indicates that a header cell from row 0 spans down
		hasHeaderVerticalMerge = any(
			cell.strip() == rowspanMarker 
			for cell in rows[2] 
			if cell is not None
		)
	
	# Generate grid table
	result = []
	
	# Helper function to detect separator rows (markdown table separator with dashes)
	def isSeparatorRow(row: list[str]) -> bool:
		"""Check if a row is a markdown table separator row (all dashes)."""
		return all(cell.strip() == '-' or cell.strip() == '' for cell in row)
	
	# Top border
	result.append('+' + '+'.join('-' * (w + 2) for w in colWidths) + '+')
	
	# If header has vertical merges, treat all rows as content rows
	# Otherwise, create a header row and separator
	if hasHeaderVerticalMerge:
		# No header row - all rows are content rows
		# Start processing from row 0 (the original header row), but skip row 1 (separator row)
		dataRowsStart = 0
	else:
		# Header row
		result.append('|' + '|'.join(
			f'{rows[0][i]:<{colWidths[i]}}' for i in range(len(rows[0])) if rows[0][i] is not None
@@ -71,24 +96,36 @@ def markdownToGrid(markdownLines:list[str]) -> list[str]:
		# Header separator
		result.append('+:' + '+:'.join('=' * (w + 1) for w in colWidths) + '+')
		
		# Data rows start from row 2 (after header and separator)
		dataRowsStart = 2
	
	# Data rows
	for rowIndex, row in enumerate(rows[2:]):
	for rowIndex, row in enumerate(rows[dataRowsStart:]):
		# Skip separator rows when processing content rows from the start (hasHeaderVerticalMerge case)
		if hasHeaderVerticalMerge and isSeparatorRow(row):
			continue

		# The following code detects if cells in the next row have rowspan marker(s)
		# If so, it will merge the cells with the current one and remove the rowspan marker
		# from that cell
		nextRowCellsMerged:list[bool] = []
		
		if rowIndex < len(rows)-3:
			for cellIndex, cell in enumerate(rows[rowIndex+3]):
				if cell.strip() == rowspanMarker:
		# Calculate the actual index in the rows array
		currentRowIdx = dataRowsStart + rowIndex
		nextRowIdx = currentRowIdx + 1

		if nextRowIdx < len(rows):
			for cellIndex, cell in enumerate(rows[nextRowIdx]):
				if cellIndex < len(row) and cell.strip() == rowspanMarker:
					nextRowCellsMerged.append(True)
					rows[rowIndex+3][cellIndex] = cell.replace(rowspanMarker, ' '*len(rowspanMarker))
					rows[nextRowIdx][cellIndex] = cell.replace(rowspanMarker, ' '*len(rowspanMarker))
				else:	
					nextRowCellsMerged.append(False)
			# nextRowCellsMerged = [ cell.strip() == rowspanMarker for cell in rows[rowIndex+3] ]
			# Ensure we have the right number of entries (match number of columns)
			while len(nextRowCellsMerged) < len(colWidths):
				nextRowCellsMerged.append(False)
		else:
			nextRowCellsMerged = [ False for _ in rows[rowIndex+2] ]
			nextRowCellsMerged = [ False for _ in colWidths ]

		result.append('|' + '|'.join(
			f'{row[i]:<{colWidths[i]}}'