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

Grid tables - remove any table cell markdown content parsing (Gitlab parser or...

Grid tables - remove any table cell markdown content parsing (Gitlab parser or Pandoc will handle that)
parent adddb837
Loading
Loading
Loading
Loading
Loading
+9 −2
Original line number Original line Diff line number Diff line
@@ -43,6 +43,7 @@ class GridCell:
		self.position:Optional[int] = None
		self.position:Optional[int] = None
		self.listFlag:bool = False
		self.listFlag:bool = False
		self.auxiliarIndex:int = 0
		self.auxiliarIndex:int = 0
		self.leadingSpaces:int = 0




	def calculateAndSetAlignment(self, 
	def calculateAndSetAlignment(self, 
@@ -159,14 +160,20 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR


	# Set content on the cell - concatenating multilines, flagging lists
	# Set content on the cell - concatenating multilines, flagging lists
	def handleCellContent(cell:GridCell, content:str) -> None:
	def handleCellContent(cell:GridCell, content:str) -> None:
		_c = content.strip()
		_c = content.rstrip()
		# Remove backslash from escaped pipes
		# Remove backslash from escaped pipes
		_c = _c.replace('\|', '|')
		_c = _c.replace('\|', '|')
		cell.leadingSpaces = len(_c) - len(_c.lstrip())
		_c = _c[cell.leadingSpaces:]
		
		
		if cell.content is None:	# Previous empty cell
		if cell.content is None:	# Previous empty cell
			cell.rowspan += 1
			cell.rowspan += 1
			cell.colspan += 1
			cell.colspan += 1
			if _c.startswith('- '):  # List in a cell
			
			cell.leadingSpaces = len(_c) - len(_c.lstrip())
			_c = _c[cell.leadingSpaces:]
			
			if re.match(r'^(?:>?\s*(?:[-*+]|\d+\.|\d+\)\s).*)', _c): # 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		
+34 −24
Original line number Original line Diff line number Diff line
@@ -47,7 +47,7 @@ module Banzai


      class Cell
      class Cell
        attr_accessor :content, :rowspan, :colspan, :colspan_adjusted, :alignment, :position_start, :position,
        attr_accessor :content, :rowspan, :colspan, :colspan_adjusted, :alignment, :position_start, :position,
          :list_flag
          :list_flag, :leading_spaces


          def initialize
          def initialize
            @content = nil
            @content = nil
@@ -58,6 +58,7 @@ module Banzai
            @position_start = nil
            @position_start = nil
            @position = nil
            @position = nil
            @list_flag = false
            @list_flag = false
            @leading_spaces = 0
          end
          end


        def calculate_and_set_alignment(header_delimiter_positions, default_alignments)
        def calculate_and_set_alignment(header_delimiter_positions, default_alignments)
@@ -119,12 +120,18 @@ module Banzai
      # Helper method to handle content in cells
      # Helper method to handle content in cells
      # rubocop:disable Metrics/PerceivedComplexity -- PoC
      # rubocop:disable Metrics/PerceivedComplexity -- PoC
      def handling_content(cell, content)
      def handling_content(cell, content)
        modified_content = content.strip
        modified_content = content.rstrip
        modified_content = modified_content.gsub(/\\\|/, '|')
        modified_content = modified_content.gsub(/\\\|/, '|')
        if cell.leading_spaces <= modified_content[/^\s*/].size
            modified_content = modified_content[cell.leading_spaces..]
        end
        if cell.content.nil?
        if cell.content.nil?
          cell.rowspan += 1
          cell.rowspan += 1
          cell.colspan += 1
          cell.colspan += 1
          if modified_content.start_with?("- ") # List

          cell.leading_spaces = modified_content[/^\s*/].size
          modified_content = modified_content[cell.leading_spaces..]
          if modified_content.lstrip.match?(/^(?:>?\s*(?:[-*+]|\d+\.|\d+\)\s).*)/) # List
            cell.list_flag = true
            cell.list_flag = true
            modified_content = modified_content.gsub(/\\\s*$/, '\n')
            modified_content = modified_content.gsub(/\\\s*$/, '\n')


@@ -138,7 +145,7 @@ module Banzai
          else
          else
            cell.content = modified_content.gsub(/\\\s*$/, '\n')
            cell.content = modified_content.gsub(/\\\s*$/, '\n')
          end
          end
        elsif modified_content.start_with?("- ")
        elsif modified_content.lstrip.match?(/^(?:>?\s*(?:[-*+]|\d+\.|\d+\)\s).*)/) # List
          cell.content += '\n' unless cell.list_flag
          cell.content += '\n' unless cell.list_flag
          cell.list_flag = true
          cell.list_flag = true
          modified_content = modified_content.gsub(/\\\s*$/, '\n')
          modified_content = modified_content.gsub(/\\\s*$/, '\n')
@@ -146,7 +153,7 @@ module Banzai
        elsif cell.list_flag && !modified_content.empty?
        elsif cell.list_flag && !modified_content.empty?
          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.strip.chomp(NEXT_ELEMENT_LIST_MARK.to_s)
          cell.content = cell.content.chomp(NEXT_ELEMENT_LIST_MARK.to_s)
          cell.content += "#{modified_content}#{NEXT_ELEMENT_LIST_MARK}"
          cell.content += "#{modified_content}#{NEXT_ELEMENT_LIST_MARK}"
        elsif modified_content.empty?
        elsif modified_content.empty?
          if cell.list_flag
          if cell.list_flag
@@ -611,26 +618,29 @@ module Banzai
              lists = cell.content&.scan(
              lists = cell.content&.scan(
                /(?:(?:^|\s*<br\s*\/?>)\s*(?:(?:[-*+]|\d+\.)\s+[^#{NEXT_ELEMENT_LIST_MARK}]+#{NEXT_ELEMENT_LIST_MARK})+(?:\s*<br\s*\/?>\s*<br\s*\/?>|$))/o)
                /(?:(?:^|\s*<br\s*\/?>)\s*(?:(?:[-*+]|\d+\.)\s+[^#{NEXT_ELEMENT_LIST_MARK}]+#{NEXT_ELEMENT_LIST_MARK})+(?:\s*<br\s*\/?>\s*<br\s*\/?>|$))/o)


              if lists
              #if lists
                lists.each do |list|
              #  lists.each do |list|
                  matches = list&.scan(
              #    matches = list&.scan(
                  /\s*([-*+]|\d+\.)\s+([^#{NEXT_ELEMENT_LIST_MARK}]+?)#{NEXT_ELEMENT_LIST_MARK}/o)
              #    /\s*([-*+]|\d+\.)\s+([^#{NEXT_ELEMENT_LIST_MARK}]+?)#{NEXT_ELEMENT_LIST_MARK}/o)
                  if matches
              #    if matches
                    sub_list = "<ul>"
              #      sub_list = "<ul>"
                    matches.each do |match|
              #      matches.each do |match|
                      sub_list += "<li>#{match[1]}</li>"
              #        sub_list += "<li>#{match[1]}</li>"
                    end
              #      end
                    sub_list += "</ul><br />"
              #     sub_list += "</ul><br />"
                    cell.content = cell.content.gsub(list.to_s, sub_list.to_s)
              #      cell.content = cell.content.gsub(list.to_s, sub_list.to_s)
                    # Enforce left alignment when cell contains a list
              #      # Enforce left alignment when cell contains a list
                    cell.alignment = 'left'
              #      cell.alignment = 'left'
                  end
              #    end
                end
              #  end
              end
              #end


              rowspan = cell.rowspan > 1 ? %( rowspan="#{cell.rowspan}") : ""
              rowspan = cell.rowspan > 1 ? %( rowspan="#{cell.rowspan}") : ""
              colspan = cell.colspan > 1 ? %( colspan="#{cell.colspan}") : ""
              colspan = cell.colspan > 1 ? %( colspan="#{cell.colspan}") : ""
              html += %(<td#{rowspan}#{colspan} markdown="1" style="text-align: #{cell.alignment}">\n\n#{cell.content}\n\n</td>)
              content_with_breaklines = cell.content
              content_with_breaklines = cell.content&.gsub("<br />", "\n")
              content_with_breaklines = content_with_breaklines&.gsub(NEXT_ELEMENT_LIST_MARK.to_s, "\n")
              html += %(<td#{rowspan}#{colspan} markdown="1" style="text-align: #{cell.alignment}">\n\n#{content_with_breaklines}\n\n</td>)
            end
            end
            html += '</tr>'
            html += '</tr>'
          end
          end