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

Some fixes for grid tables:

- grid table filter: add spaces when no explicit \. If explicit \, user shall take care of spaces
- grid table converter to html (for mkdocs): it needs to still parse lists
parent e99210f1
Loading
Loading
Loading
Loading
Loading
+17 −4
Original line number Original line Diff line number Diff line
@@ -173,7 +173,8 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR
			cell.leadingSpaces = len(_c) - len(_c.lstrip())
			cell.leadingSpaces = len(_c) - len(_c.lstrip())
			_c = _c[cell.leadingSpaces:]
			_c = _c[cell.leadingSpaces:]
			
			
			if re.match(r'^(?:>?\s*(?:[-*+]|\d+\.|\d+\)\s).*)', _c): # List in a cell
			if _c.startswith('- '):  # List
			#if re.match(r'^(?:>?\s*(?:[-*+]|\d+\.|\d+\)\s).*)', _c.lstrip()): # List in a cell
				cell.listFlag = True
				cell.listFlag = True
				_c = re.sub(r'\\\s*$', '\n', _c)
				_c = re.sub(r'\\\s*$', '\n', _c)
				cell.content = _c + _nextListElementMark  # Add list element end mark to know when the list element ends		
				cell.content = _c + _nextListElementMark  # Add list element end mark to know when the list element ends		
@@ -187,6 +188,7 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR
				cell.content = re.sub(r'\\\s*$', '\n', _c)
				cell.content = re.sub(r'\\\s*$', '\n', _c)
		else: # Cell has content
		else: # Cell has content
			if _c.startswith('- '):  # List
			if _c.startswith('- '):  # List
			#if re.match(r'^(?:>?\s*(?:[-*+]|\d+\.|\d+\)\s).*)', _c.lstrip()): # List in a cell
				if not cell.listFlag:
				if not cell.listFlag:
					cell.content += '\n'
					cell.content += '\n'
					#cell['content'] = cell['content'].strip("\n")
					#cell['content'] = cell['content'].strip("\n")
@@ -194,9 +196,13 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR
				_c = re.sub(r'\\\s*$', '\n', _c)
				_c = re.sub(r'\\\s*$', '\n', _c)
				cell.content += _c + _nextListElementMark  # Add list element end mark to know when the list element ends
				cell.content += _c + _nextListElementMark  # Add list element end mark to know when the list element ends
			elif cell.listFlag and len(_c) > 0:  # any other content when handling list is concatenated to the last list element
			elif cell.listFlag and len(_c) > 0:  # any other content when handling list is concatenated to the last list element
				
				_c = re.sub(r'\\\s*$', '\n', _c)
				_c = re.sub(r'\\\s*$', '\n', _c)
				# cell.content = cell.content.strip(nextListElementMark) #remove list element end mark
				# cell.content = cell.content.strip(nextListElementMark) #remove list element end mark
				cell.content = cell.content.removesuffix(_nextListElementMark) #remove list element end mark
				cell.content = cell.content.removesuffix(_nextListElementMark) #remove list element end mark
				if cell.content.endswith('\n'): # a force linebreak is present
					cell.content += _c + _nextListElementMark
				else: # Multiline content is concatenated to the last line with a space
					cell.content += ' ' +  _c + _nextListElementMark #add list element end mark
					cell.content += ' ' +  _c + _nextListElementMark #add list element end mark
			elif len(_c) == 0:  # separation between list and other paragraph
			elif len(_c) == 0:  # separation between list and other paragraph
				if cell.listFlag:
				if cell.listFlag:
@@ -205,6 +211,9 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR
				#content = re.sub(r'\\\s*$', "\n", content.strip())
				#content = re.sub(r'\\\s*$', "\n", content.strip())
				cell.content += '\n' if not cell.content.endswith('\n') else ''
				cell.content += '\n' if not cell.content.endswith('\n') else ''
			else:
			else:
				if cell.content.endswith('\n'): # a force linebreak is present
					cell.content += re.sub(r'\\\s*$', '\n', _c)
				else: # Multiline content is concatenated to the last line with a space
					cell.content += ' ' + re.sub(r'\\\s*$', '\n', _c)
					cell.content += ' ' + re.sub(r'\\\s*$', '\n', _c)


	# Adjust colspan of a cell
	# Adjust colspan of a cell
@@ -750,9 +759,13 @@ def generateHtmlTableWithSpans(gridTable:str) -> str:
						# Enforce left alignment if cell contains a list
						# Enforce left alignment if cell contains a list
						cell.alignment = _alignLeft
						cell.alignment = _alignLeft


				#contentWithBreaklines = cell.content
				#contentWithBreaklines = contentWithBreaklines.replace(_nextListElementMark, "<br />")
				#multilineContent = '\n'.join(contentWithBreaklines.split("<br />"))
				rowspan = f' rowspan="{cell.rowspan}"' if cell.rowspan > 1 else ''
				rowspan = f' rowspan="{cell.rowspan}"' if cell.rowspan > 1 else ''
				colspan = f' colspan="{cell.colspan}"' if cell.colspan > 1 else ''
				colspan = f' colspan="{cell.colspan}"' if cell.colspan > 1 else ''
				html +=   f'<td{rowspan}{colspan} {cell.alignment} markdown="1">{cell.content}</td>\n'
				#html = '\n'.join([html, f'<td{rowspan}{colspan} {cell.alignment} markdown="1">','','', multilineContent,'','', '</td>',''])
				html +=   f'<td{rowspan}{colspan} {cell.alignment} markdown="1">\n\n{cell.content}\n\n</td>\n'
		html += '</tr>\n'
		html += '</tr>\n'


	html += '</tbody>\n'
	html += '</tbody>\n'
+10 −2
Original line number Original line Diff line number Diff line
@@ -154,7 +154,11 @@ module Banzai
          modified_content = modified_content.gsub(/\\\s*$/, '\n')
          modified_content = modified_content.gsub(/\\\s*$/, '\n')
          # cell.content = cell.content.strip(nextListElementMark) #remove list element end mark
          # cell.content = cell.content.strip(nextListElementMark) #remove list element end mark
          cell.content = cell.content.chomp(NEXT_ELEMENT_LIST_MARK.to_s)
          cell.content = cell.content.chomp(NEXT_ELEMENT_LIST_MARK.to_s)
          if cell.content.end_with?('\n') # a force linebreak is present
            cell.content += "#{modified_content}#{NEXT_ELEMENT_LIST_MARK}"
            cell.content += "#{modified_content}#{NEXT_ELEMENT_LIST_MARK}"
          else # Multiline content is concatenated to the last line with a space
            cell.content += " #{modified_content}#{NEXT_ELEMENT_LIST_MARK}"
          end 
        elsif modified_content.empty?
        elsif modified_content.empty?
          if cell.list_flag
          if cell.list_flag
            cell.list_flag = false
            cell.list_flag = false
@@ -164,8 +168,12 @@ module Banzai
          cell.content += cell.content.end_with?('\n') ? "" : '\n'
          cell.content += cell.content.end_with?('\n') ? "" : '\n'
        else
        else
          modified_content = modified_content.gsub(/\\\s*$/, '\n')
          modified_content = modified_content.gsub(/\\\s*$/, '\n')
          if cell.content.end_with?('\n') # a force linebreak is present
            cell.content += modified_content
          else # Multiline content is concatenated to the last line with a space
            cell.content += " #{modified_content}"
            cell.content += " #{modified_content}"
          end
          end
        end


        cell
        cell
      end
      end