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

Support for only body tables

parent 5f9a76d5
Loading
Loading
Loading
Loading
+53 −32
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ module Banzai
      GRID_TABLE_BODY_SEPARATOR_LINE = /^[-:]+$/

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

          def initialize
            @content = nil
@@ -49,24 +49,25 @@ module Banzai
            @colspan = 0
            @colspan_adjusted = false
            @alignment = 'align="center"'
            @position_start = nil
            @position = nil
            @list_flag = false
          end

          def set_alignment(default_alignments, header_delimiter_positions)
          def calculateAndSetAlignment(header_delimiter_positions, default_alignments )
            
            raise "Cell position must be set before calculating alignment" if @position.nil? || @position_start.nil?
            
            header_delimiter_index = 0
            while header_delimiter_index < default_alignments.length && 
                @position > header_delimiter_positions[header_delimiter_index]
                @position_start > header_delimiter_positions[header_delimiter_index]
              header_delimiter_index += 1
            end

            raise "Invalid table formatting" unless header_delimiter_index < default_alignments.length
            if @position < header_delimiter_positions[header_delimiter_index]
                @alignment = default_alignments[header_delimiter_index]
            elsif @position == header_delimiter_positions[header_delimiter_index]
            
            @alignment = default_alignments[header_delimiter_index]
                header_delimiter_index += 1
            end
            
          end
      end # end of class Cell

@@ -237,6 +238,26 @@ module Banzai
          break
        end

        unless has_header
          # Set default alignments from the first separator which takes the role of header
          header_separator_index = 0
          parts = lines[0].strip.delete_prefix("+").split("+")
    
          parts.each_with_index do |part, part_index|
            default_alignments << if part.start_with?(":") && !part.end_with?(":")
                                      'align="left"'
                                    elsif !part.start_with?(":") && part.end_with?(":")
                                      'align="right"'
                                    else
                                      'align="center"'
                                    end
    
            start_pos = part_index == 0 ? 0 : header_delimiter_positions[part_index - 1]
            pos = lines[0][start_pos + 1..-1]&.index("+")
            header_delimiter_positions << (pos ? pos + start_pos + 1 : -1)
          end
        end

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

@@ -264,8 +285,9 @@ module Banzai
                next unless i < number_of_columns

                delimiter_index += parts[j].length + 1
                rows[-1][i].position_start = delimiter_index - parts[j].length
                rows[-1][i].position = delimiter_index
                rows[-1][i].set_alignment(default_alignments, header_delimiter_positions)
                rows[-1][i].calculateAndSetAlignment(header_delimiter_positions, default_alignments )

                i += 1 while delimiter_index > delimiter_positions[i]
                i += 1
@@ -274,10 +296,7 @@ module Banzai
            elsif in_data_row
              # Regular data row or partial separator
              if GRID_TABLE_BODY_SEPARATOR.match?(line) # Partial separator
                cells_content = line.strip
                                   .delete_prefix("|")
                                   .delete_prefix("+")
                                   .split(/[\|\+]/)
                cells_content = line.strip.gsub(/^(\+|\|)/, '').split(/[\|\+]/)

                rows << Row.new(number_of_columns)
                aux_delimiter_index = 0
@@ -287,8 +306,9 @@ module Banzai
                  next unless auxiliar_cell_index < number_of_columns

                  aux_delimiter_index += cells_content[i].length + 1
                  rows[-1][auxiliar_cell_index].position_start = aux_delimiter_index - cells_content[i].length
                  rows[-1][auxiliar_cell_index].position = aux_delimiter_index
                  rows[-1][auxiliar_cell_index].set_alignment(default_alignments, header_delimiter_positions)
                  rows[-1][auxiliar_cell_index].calculateAndSetAlignment(header_delimiter_positions, default_alignments )

                  auxiliar_cell_index += 1 while aux_delimiter_index > delimiter_positions[auxiliar_cell_index]

@@ -299,6 +319,7 @@ module Banzai

                column_index = 0
                maxRowTracker = rows_tracker.maxValue
                
                cells_content.each_with_index do |content, _i|
                  if GRID_TABLE_BODY_SEPARATOR_LINE.match?(content) # Separator - split row
                    rows_tracker[column_index] = maxRowTracker + 1
@@ -308,7 +329,6 @@ module Banzai
                    (column_index...delimiter_positions.length).each do |del_index|
                      if rows[rows_tracker[column_index]][column_index].position >= delimiter_positions[del_index]
                        column_forward += 1
                        #rows_tracker[column_index + column_forward - 1] += 1 if column_forward > 1
                      end
                    end

@@ -335,7 +355,6 @@ module Banzai
                    end
                  end
                end

              else # Data row
                cells_content = line.strip.delete_prefix("|").split(/\|/)
                column_index = 0
@@ -377,10 +396,12 @@ module Banzai
            rows.each { |body_row| data_rows << body_row.cells }
          elsif has_header && start < header_separator_index
            rows.each { |header_row| header_rows << header_row.cells }
          end
          else
            rows.each { |body_row| data_rows << body_row.cells }
          end
          
          raise "No valid rows found in the provided Pandoc table." if data_rows.empty? && header_rows.empty?
        end

        # Format text (bold and italic)
        [header_rows, data_rows].each do |rows|