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

Corrected grid table generation

parent c7254b09
Loading
Loading
Loading
Loading
+13 −7
Original line number Original line Diff line number Diff line
@@ -322,7 +322,7 @@ def processDocuments(documents:list[str],
			headers.append( (level, replaceNL(text)))
			headers.append( (level, replaceNL(text)))


			
			
			return [ '', f'{"#" * level} {nr}{replaceNL(text)}' if text else '' ]
			return [ '', f'{"#" * level} {nr}{replaceNL(text)}' if text else '', '' ]




		def strippedTag(tag:str) -> str:
		def strippedTag(tag:str) -> str:
@@ -772,6 +772,7 @@ def processDocuments(documents:list[str],
						rows:list[list[str]] = []
						rows:list[list[str]] = []
						nrRows = 0
						nrRows = 0
						colSpanDetected = False
						colSpanDetected = False
						rowSpanDetected = False
						for row in elem.rows:
						for row in elem.rows:
							_row = ET.fromstring(row._tr.xml)
							_row = ET.fromstring(row._tr.xml)
							cells:list[str] = []
							cells:list[str] = []
@@ -787,6 +788,7 @@ def processDocuments(documents:list[str],
								gridspanElem = cell.find('.//w:tcPr/w:vMerge', namespaces={'w': wns})
								gridspanElem = cell.find('.//w:tcPr/w:vMerge', namespaces={'w': wns})
								if gridspanElem is not None and _val not in gridspanElem.attrib:
								if gridspanElem is not None and _val not in gridspanElem.attrib:
									cells.append(rowspanMarker)
									cells.append(rowspanMarker)
									rowSpanDetected = True  # Set flag that rowspan was found
								
								
								else:
								else:


@@ -817,10 +819,11 @@ def processDocuments(documents:list[str],
						if nrRows == 1:
						if nrRows == 1:
							_print(f'[red]({linenumber(len(lines)+2)}) Single-row table found. Such tables cannot be converted to markdown.[/red]Consider to change the following table in the original document:\n[grey39]{rows[0]}', highlight = False)
							_print(f'[red]({linenumber(len(lines)+2)}) Single-row table found. Such tables cannot be converted to markdown.[/red]Consider to change the following table in the original document:\n[grey39]{rows[0]}', highlight = False)


						# Warning if a table with colspans is detected
						# Warning if a table with colspans or rowspans is detected
						if colSpanDetected:
						if colSpanDetected or rowSpanDetected:
							if forceMarkdownTables:
							if forceMarkdownTables:
								_print(f'[yellow]({linenumber(len(lines)+2)}) Table with colspans found: [/yellow][grey39]{richString(lastTableCaption)}[/grey39]\nConsider to convert it manually to a grid table', highlight = False)
								spanType = "colspans" if colSpanDetected else "rowspans"
								_print(f'[yellow]({linenumber(len(lines)+2)}) Table with {spanType} found: [/yellow][grey39]{richString(lastTableCaption)}[/grey39]\nConsider to convert it manually to a grid table', highlight = False)


						tableLines:list[str] = []
						tableLines:list[str] = []


@@ -841,10 +844,13 @@ def processDocuments(documents:list[str],
							tableLines.append(f'|{"|".join(row)}|'
							tableLines.append(f'|{"|".join(row)}|'
										 .replace('\n', _linebreak))	# replace line breaks in cells
										 .replace('\n', _linebreak))	# replace line breaks in cells
						
						
						# if colSpanDetected and gridTableForColspan then convert to grid table
						# if colSpanDetected or rowSpanDetected then convert to grid table
						if colSpanDetected and not forceMarkdownTables and not errorDetected:
						if (colSpanDetected or rowSpanDetected) and not forceMarkdownTables and not errorDetected:
							lines.append('')	# Add an empty line before a table
							lines.append('')	# Add an empty line before a table
							lines.append('<mark>Table with colspans converted to grid table. Please check and adjust manually if necessary.</mark>')
							spanType = "colspans" if colSpanDetected else ("rowspans" if rowSpanDetected else "merged cells")
							if colSpanDetected and rowSpanDetected:
								spanType = "colspans and rowspans"
							lines.append(f'<mark>Table with {spanType} converted to grid table. Please check and adjust manually if necessary.</mark>')
							tableLines = markdownToGrid(tableLines)
							tableLines = markdownToGrid(tableLines)
						
						
						lines.append('')	# Add an empty line before a table
						lines.append('')	# Add an empty line before a table