marktable 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1784dd519b087a312b16b62a5b562b4e96c6d9b2b8d2120fbedfbd26610c9b08
4
- data.tar.gz: dbdd8c041ff3601a869bd1645ff040b3727272475742573d31c9e9b0f4be0c3c
3
+ metadata.gz: 7b7b5feb35551a6a59edde7ca07f5d40d42db64a59db7ab63ec9c30b3bd91639
4
+ data.tar.gz: efb35fdc7273544d43abf282401e8468c4952c515d89d0a51cea5097efffbe05
5
5
  SHA512:
6
- metadata.gz: 80378878aca4a4aac40969c76c007ba82073bf6ddb4a3ec601fe2b73f904c0d09efef2212b7a4e396dbedb215402e71563f1de84ca6f9c8861fe3bb7ce6eb282
7
- data.tar.gz: 3bc02122e8896440a1fa5c959e344db546dedcca04355e142509b39ffd605e2d71ecb00b6d0d4d9b868088c2b85e8b1fb3ac00da63a4fcedfc2a2efbc83c1938
6
+ metadata.gz: 076bedb8bcc98ee8b02f1b253037ad8e35b43d235b8406a6e79bd08ed0ca9826894d9e6f3bafbd26c6c27e54e4b15bc0366962758343604b83e929e854556e01
7
+ data.tar.gz: 9fcfd90ad2851a656670c7a7e73ab55943925db60223fc37bf947d7ffca91c8cdec1384ec035c226681eaf038712f58e5cb2f559eba9ba9efdfa471fba45c470
@@ -6,15 +6,17 @@ require 'marktable'
6
6
 
7
7
  # Register the matchers if RSpec is defined
8
8
  if defined?(RSpec)
9
- RSpec::Matchers.define :match_markdown do |expected_markdown|
9
+ RSpec::Matchers.define :match_markdown do |expected_markdown, ignore_headers: false|
10
+
10
11
  chain :with_format do |format|
11
12
  @format = format
12
13
  end
13
14
 
14
15
  match do |actual|
15
- @expected_data = parse_input(expected_markdown, :markdown)
16
+ @ignore_headers = ignore_headers
16
17
  @format ||= infer_format(actual)
17
18
  @actual_data = parse_input(actual, @format)
19
+ @expected_data = parse_input(expected_markdown, :markdown)
18
20
 
19
21
  # Compare data using to_a for consistent comparison
20
22
  @actual_data.to_a == @expected_data.to_a
@@ -36,8 +38,7 @@ if defined?(RSpec)
36
38
 
37
39
  # Handle both Capybara::Node::Element and Capybara::Node::Simple
38
40
  if capybara?(input)
39
- # Convert Capybara element to Nokogiri element or HTML
40
- input = input.native
41
+ input = parse_capybara_html(input)
41
42
  format = :html
42
43
  end
43
44
 
@@ -47,13 +48,28 @@ if defined?(RSpec)
47
48
  format = :html
48
49
  end
49
50
 
50
- Marktable::Table.new(input, type: format)
51
+ # Pass headers option if it was specified in the chain
52
+ options = { type: format }
53
+ options[:headers] = false if @ignore_headers
54
+ Marktable::Table.new(input, **options)
51
55
  end
52
56
 
53
57
  def html?(data)
54
58
  nokogiri?(data) || capybara?(data)
55
59
  end
56
60
 
61
+ def parse_capybara_html(data)
62
+ rows = data.all('tr').map do |row|
63
+ cells = row.all('th,td').map do |cell|
64
+ cell_text = cell.text.tr("\n", ' ').strip
65
+ cell_type = cell.tag_name
66
+ "<#{cell_type}>#{cell_text}</#{cell_type}>"
67
+ end
68
+ "<tr>#{cells.join}</tr>"
69
+ end
70
+ "<table>#{rows.join}</table>"
71
+ end
72
+
57
73
  def capybara?(data)
58
74
  defined?(Capybara::Node::Base) && data.is_a?(Capybara::Node::Base) ||
59
75
  defined?(Capybara::Node::Simple) && data.is_a?(Capybara::Node::Simple) ||
@@ -13,8 +13,8 @@ module Marktable
13
13
  result = parser.parse
14
14
  @rows = result.rows
15
15
  @headers = result.headers
16
- # Validate headers if present
17
- validate_headers if @headers
16
+ # Validate headers if present and not explicitly disabled
17
+ validate_headers if @headers && headers != false
18
18
  # Fix: Initialize @has_headers based on whether @headers is present
19
19
  @has_headers = !@headers.nil?
20
20
  end
@@ -44,7 +44,7 @@ module Marktable
44
44
 
45
45
  csv_table.each do |csv_row|
46
46
  # Convert CSV::Row to hash then to our Row
47
- row_data = csv_row.to_h
47
+ row_data = csv_row.to_h.transform_values { |v| v.nil? ? '' : v }
48
48
  rows << Row.new(row_data, headers: headers)
49
49
  end
50
50
 
@@ -56,10 +56,12 @@ module Marktable
56
56
 
57
57
  if csv_table.is_a?(::CSV::Table)
58
58
  csv_table.each do |csv_row|
59
- rows << Row.new(csv_row.fields)
59
+ fields = csv_row.fields.map { |v| v.nil? ? '' : v }
60
+ rows << Row.new(fields)
60
61
  end
61
62
  else
62
63
  csv_table.each do |fields|
64
+ fields = fields.map { |v| v.nil? ? '' : v }
63
65
  rows << Row.new(fields)
64
66
  end
65
67
  end
@@ -47,44 +47,24 @@ module Marktable
47
47
  end
48
48
  end
49
49
 
50
- def extract_row_cells(row)
51
- row.css('th, td').map do |cell|
52
- # Get text content
53
- text = cell.text
54
-
55
- # Handle JSON content specially to preserve important whitespace
56
- if text.include?('{') && text.include?('}')
57
- # Process JSON content more carefully
58
- # 1. Remove newlines
59
- # 2. Preserve spaces between JSON delimiters
60
- # 3. Normalize other whitespace
61
- json_content = text.gsub(/\r?\n/, ' ') # Replace newlines with spaces
62
- .gsub(/(\{)\s*/, '\1 ') # Ensure exactly one space after opening brace
63
- .gsub(/\s*(\})/, ' \1') # Ensure exactly one space before closing brace
64
- .gsub(/\s+/, ' ') # Collapse other consecutive whitespace
65
- .gsub(/\A\s+|\s+\z/, '') # Trim leading/trailing whitespace
66
- .gsub(/\u00A0/, ' ') # Replace &nbsp; with spaces
67
- json_content
68
- else
69
- # For regular content
70
- text.gsub(/\r?\n/, '') # Remove all newlines completely
71
- .gsub(/\s+/, ' ') # Collapse consecutive whitespace
72
- .gsub(/\A\s+|\s+\z/, '') # Trim leading/trailing whitespace
73
- .gsub(/\u00A0/, ' ') # Replace &nbsp; with spaces
74
- end
75
- end
50
+ def rows
51
+ @rows ||= table.css('tr')
76
52
  end
77
53
 
78
54
  def first_row
79
- @first_row ||= rows.first
55
+ rows.first
80
56
  end
81
57
 
82
58
  def has_headers?
83
- @has_headers ||= first_row.css('th').any? || @headers_flag
59
+ return false if @headers_flag == false
60
+ return true if @headers_flag == true
61
+
62
+ # Auto-detect headers if not explicitly specified
63
+ first_row&.css('th').any?
84
64
  end
85
65
 
86
- def rows
87
- @rows ||= table.css('tr')
66
+ def extract_row_cells(row)
67
+ row.css('th, td').map { |cell| cell.text.tr("\n", ' ').strip }
88
68
  end
89
69
  end
90
70
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Marktable
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.5'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marktable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francois Gaspard