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

Adding separators alignment checking

parent 9cfb8d0d
Loading
Loading
Loading
Loading
+39 −1
Original line number Diff line number Diff line
@@ -190,6 +190,32 @@ module Banzai
        row[column_index]
      end

      def checkDelimiterAlignment(line, delimiterPositions)
        return false if line.empty? || delimiterPositions.empty?
        
        #puts "\nChecking line: #{line}"
        #puts "Expected delimiter positions: #{delimiterPositions}"
          
        # For any row (only +, only |, mix of + and |)
        currentPositions = []
        start_pos = 1

        while start_pos < line.length
          pos = line.index(/[|+]/, start_pos)  # Find the next occurrence of | or + starting from start_pos
          break if pos.nil?  # Exit if no more delimiters are found

          currentPositions << pos
          start_pos = pos + 1  # Move to the next character after the found delimiter
        end

        #puts "Current positions: #{currentPositions}"

        # Check if the last expected delimiter position is found in currentPositions
        return currentPositions.include?(delimiterPositions[-1]) &&
               line.match?(/\A[|+]/) &&  # Check if the line starts with | or +
               currentPositions.all? { |pos| delimiterPositions.include?(pos) }  # Ensure all current positions are in delimiterPositions
      end

      # rubocop:disable Metrics/AbcSize -- PoC
      # rubocop:disable Metrics/CyclomaticComplexity -- PoC
      # rubocop:disable Metrics/PerceivedComplexity -- PoC
@@ -266,6 +292,10 @@ module Banzai
          end
        end

        #Check end table delimiter alignment (not checked during the lines processing)
	      raise "Misaligned delimiters in table separators: #{lines[-1]}" unless checkDelimiterAlignment(lines[-1], delimiter_positions)


        # Process table body (including rows belonging to header as they are processed in the same way)
        data_rows = []

@@ -284,6 +314,9 @@ module Banzai
            # First line (normally a separator) of each block
            if separator?(line) && !in_data_row
              in_data_row = true
              #Check end table delimiter alignment (not checked during the lines processing)
	          raise "Misaligned delimiters in separator row: #{line}" unless checkDelimiterAlignment(line, delimiter_positions)
              
              parts = line.strip.delete_prefix("+").split("+")
              delimiter_index = 0
              rows << Row.new(number_of_columns)
@@ -305,6 +338,9 @@ module Banzai
            elsif in_data_row
              # Regular data row or partial separator
              if GRID_TABLE_BODY_SEPARATOR.match?(line) # Partial separator
                #Check end table delimiter alignment (not checked during the lines processing)
                raise "Misaligned delimiters in partial separator: #{line}" unless checkDelimiterAlignment(line, delimiter_positions)

                cells_content = line.strip.gsub(/^(\+|\|)/, '').split(/[\|\+]/)

                rows << Row.new(number_of_columns)
@@ -366,6 +402,9 @@ module Banzai
                end
              else # Data row
                cells_content = line.strip.delete_prefix("|").split(/\|/)
                #Check end table delimiter alignment (not checked during the lines processing)
	              raise "Misaligned delimiters in row: #{line}" unless checkDelimiterAlignment(line, delimiter_positions)

                column_index = 0

                if cells_content.length < number_of_columns
@@ -516,7 +555,6 @@ module Banzai
                matches.each do |match|
                  list += "<li>#{match[1]}</li>"
                end
                puts "List:  #{list}"
                cell.content = cell.content.gsub(/(\s*([-*+]|\d+\.)\s+([^#{NEXT_ELEMENT_LIST_MARK}]+#{NEXT_ELEMENT_LIST_MARK}\n?))+/, list)
                # Enforce left alignment if cell contains a list
                cell.alignment = 'align="left"'