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

Allow + and | delimiters as part of cell content

parent 24845228
Loading
Loading
Loading
Loading
+51 −21
Original line number Original line Diff line number Diff line
@@ -37,10 +37,11 @@ module Banzai
      require 'logger'
      require 'logger'


      # Add these regex constants at the top of the file, after the require statement
      # Add these regex constants at the top of the file, after the require statement
      GRID_TABLE_SEPARATOR = /^\s*\+([-:=]+\+)+\s*$/
      GRID_TABLE_SEPARATOR = /^\+([-:=]+\+)+$/
      GRID_TABLE_HEADER_SEPARATOR = /^\s*\+([=:]+\+)+\s*$/
      GRID_TABLE_HEADER_SEPARATOR = /^\+([=:]+\+)+$/
      GRID_TABLE_BODY_SEPARATOR = /[^\n]*\+([:-]+\+)+[^\n]*$/
      GRID_TABLE_BODY_SEPARATOR = /^[+|][^\n]*(\+[:-]+\+($|[^\n]*[+|]$))/
      GRID_TABLE_BODY_SEPARATOR_LINE = /^[-:]+$/
      GRID_TABLE_BODY_SEPARATOR_LINE = /^:?-+:?$/
      GRID_TABLE_DELIMITER = /(?<!\\)\||(?=\+:?[-=]+:?\+)\+|(?<![^-=:])\+/


      NEXT_ELEMENT_LIST_MARK = "∆"
      NEXT_ELEMENT_LIST_MARK = "∆"


@@ -119,6 +120,7 @@ module Banzai
      # 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.strip
        modified_content = modified_content.gsub(/\\\|/, '|')
        if cell.content.nil?
        if cell.content.nil?
          cell.rowspan += 1
          cell.rowspan += 1
          cell.colspan += 1
          cell.colspan += 1
@@ -172,11 +174,13 @@ module Banzai
            col_i -= 1
            col_i -= 1
          end
          end


          delimiters = ['|', '+']
          positions = []
          positions = delimiters.filter_map do |delimiter|
          line.scan(GRID_TABLE_DELIMITER) do
            pos = line[delimiter_start + 1..]&.index(delimiter)
            pos = $~.offset(0)[0]
            pos ? pos + delimiter_start + 1 : nil
            if pos > delimiter_start
          end.compact
              positions << pos
            end
          end
          
          
          position = positions.min
          position = positions.min


@@ -207,12 +211,12 @@ module Banzai
        current_positions = []
        current_positions = []
        start_pos = 1
        start_pos = 1


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

          if pos > 0
            current_positions << pos
            current_positions << pos
          start_pos = pos + 1  # Move to the next character after the found delimiter
          end
        end
        end
          
          
        # puts "Current positions: #{current_positions}"
        # puts "Current positions: #{current_positions}"
@@ -230,7 +234,7 @@ module Banzai
      # rubocop:disable Metrics/CyclomaticComplexity -- PoC
      # rubocop:disable Metrics/CyclomaticComplexity -- PoC
      # rubocop:disable Metrics/PerceivedComplexity -- PoC
      # rubocop:disable Metrics/PerceivedComplexity -- PoC
      def parse_pandoc_table_with_spans(pandoc_table)
      def parse_pandoc_table_with_spans(pandoc_table)
        # Split the input into lines
        # Split the input into lines and remove trailing spaces
        lines = pandoc_table.rstrip.split("\n").map(&:rstrip)
        lines = pandoc_table.rstrip.split("\n").map(&:rstrip)


        # Retrieve separator indices
        # Retrieve separator indices
@@ -328,7 +332,16 @@ module Banzai
              raise "Misaligned delimiters in separator row: #{line}" unless check_delimiter_alignment(line,
              raise "Misaligned delimiters in separator row: #{line}" unless check_delimiter_alignment(line,
                delimiter_positions)
                delimiter_positions)


              parts = line.strip.delete_prefix("+").split("+")
              parts = []
              pos = 0
              line.scan(GRID_TABLE_DELIMITER) do
                match_pos = $~.offset(0)[0]
                if match_pos > pos + 1
                  parts << line[(pos + 1)..match_pos-1]
                end
                pos = match_pos
              end
        
              delimiter_index = 0
              delimiter_index = 0
              rows << Row.new(number_of_columns)
              rows << Row.new(number_of_columns)
              rows_tracker = RowTracker.new(number_of_columns)
              rows_tracker = RowTracker.new(number_of_columns)
@@ -353,7 +366,15 @@ module Banzai
                raise "Misaligned delimiters in partial separator: #{line}" unless check_delimiter_alignment(line,
                raise "Misaligned delimiters in partial separator: #{line}" unless check_delimiter_alignment(line,
                  delimiter_positions)
                  delimiter_positions)


                parts = line.strip.gsub(/^(\+|\|)/, '').split(/[\|\+]/)
                parts = []
                pos = 0
                line.scan(GRID_TABLE_DELIMITER) do
                  match_pos = $~.offset(0)[0]
                  if match_pos > pos + 1
                    parts << line[(pos + 1)..match_pos-1]
                  end
                  pos = match_pos
                end


                rows << Row.new(number_of_columns)
                rows << Row.new(number_of_columns)
                aux_delimiter_index = 0
                aux_delimiter_index = 0
@@ -414,7 +435,16 @@ module Banzai
                  end
                  end
                end
                end
              else # Data row
              else # Data row
                cells_content = line.strip.delete_prefix("|").split("|")
                cells_content = []
                pos = 0
                line.scan(GRID_TABLE_DELIMITER) do
                  match_pos = $~.offset(0)[0]
                  if match_pos > pos + 1
                    cells_content << line[(pos + 1)..match_pos-1]
                  end
                  pos = match_pos
                end

                # Check end table delimiter alignment (not checked during the lines processing)
                # Check end table delimiter alignment (not checked during the lines processing)
                raise "Misaligned delimiters in row: #{line}" unless check_delimiter_alignment(
                raise "Misaligned delimiters in row: #{line}" unless check_delimiter_alignment(
                  line, delimiter_positions)
                  line, delimiter_positions)