marktable 0.0.4s → 0.0.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 +4 -4
- data/spec/support/matchers/markdown_matchers.rb +2 -75
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3e8c1a76daf4c3d03f628200baabad28e31008e444522fb2d6c7337f623bdd8
|
4
|
+
data.tar.gz: c83d7b0a83b21c2c292d5a76963f626ddeae5bebca42363f227f6bf65fed6dc2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0f766d34db37402b6d4cbb52e37a1e33172175c5ba622051ce9899ded65de3c9fa045f7d21a234c9fc03856bfaefcf7b9b8d5f1fb346bd0c47efbee87e86236
|
7
|
+
data.tar.gz: 1a34281c031a0c47da3ff074f6206304fc569663a04ffab9246bd12a137eb7fd53a6745415991d6530d384f67e05eca480db02bdd9c49122687a9ae820475037
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'capybara'
|
4
|
+
require 'nokogiri'
|
4
5
|
|
5
6
|
RSpec::Matchers.define :match_markdown do |expected_markdown|
|
6
7
|
match do |actual|
|
@@ -74,21 +75,8 @@ RSpec::Matchers.define :match_markdown do |expected_markdown|
|
|
74
75
|
Marktable.table(data).to_s
|
75
76
|
end
|
76
77
|
|
77
|
-
# Parse HTML table into rows of data
|
78
|
+
# Parse HTML table into rows of data using Nokogiri
|
78
79
|
def parse_html_table(html)
|
79
|
-
if defined?(Nokogiri)
|
80
|
-
parse_html_with_nokogiri(html)
|
81
|
-
else
|
82
|
-
begin
|
83
|
-
require('nokogiri')
|
84
|
-
parse_html_with_nokogiri(html)
|
85
|
-
rescue LoadError
|
86
|
-
parse_html_without_nokogiri(html)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def parse_html_with_nokogiri(html)
|
92
80
|
doc = Nokogiri::HTML(html)
|
93
81
|
|
94
82
|
# Extract headers
|
@@ -122,67 +110,6 @@ RSpec::Matchers.define :match_markdown do |expected_markdown|
|
|
122
110
|
tbody_rows
|
123
111
|
end
|
124
112
|
|
125
|
-
def parse_html_without_nokogiri(html)
|
126
|
-
# Extract headers
|
127
|
-
headers = extract_headers_without_nokogiri(html)
|
128
|
-
|
129
|
-
# Extract body rows
|
130
|
-
body_rows = extract_body_rows_without_nokogiri(html, headers)
|
131
|
-
|
132
|
-
body_rows
|
133
|
-
end
|
134
|
-
|
135
|
-
def extract_headers_without_nokogiri(html)
|
136
|
-
headers = []
|
137
|
-
|
138
|
-
if html.include?('<thead')
|
139
|
-
# Extract headers from thead
|
140
|
-
thead_html = html[html.index('<thead')...(html.index('</thead>') + 8)]
|
141
|
-
headers = thead_html.scan(/<t[hd].*?>(.*?)<\/t[hd]>/im).map { |cell| cell[0].strip }
|
142
|
-
else
|
143
|
-
# No thead, get headers from first tr
|
144
|
-
first_tr = html.match(/<tr.*?>(.*?)<\/tr>/im)
|
145
|
-
if first_tr
|
146
|
-
headers = first_tr[1].scan(/<t[hd].*?>(.*?)<\/t[hd]>/im).map { |cell| cell[0].strip }
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
headers
|
151
|
-
end
|
152
|
-
|
153
|
-
def extract_body_rows_without_nokogiri(html, headers)
|
154
|
-
rows = []
|
155
|
-
has_thead = html.include?('<thead')
|
156
|
-
has_tbody = html.include?('<tbody')
|
157
|
-
in_tbody = false
|
158
|
-
|
159
|
-
html.scan(/<tr.*?>(.*?)<\/tr>/im).each_with_index do |tr_content, index|
|
160
|
-
# Skip header rows
|
161
|
-
next if should_skip_header_row?(html, tr_content[0], index, has_thead, has_tbody)
|
162
|
-
|
163
|
-
# For tables with thead/tbody, only include tbody rows
|
164
|
-
if has_thead && has_tbody
|
165
|
-
in_tbody = html[0..html.index(tr_content[0])].include?('<tbody') unless in_tbody
|
166
|
-
in_tbody = false if html[0..html.index(tr_content[0])].include?('</tbody')
|
167
|
-
next unless in_tbody
|
168
|
-
end
|
169
|
-
|
170
|
-
cells = tr_content[0].scan(/<t[hd].*?>(.*?)<\/t[hd]>/im).map { |cell_content| cell_content[0].strip }
|
171
|
-
|
172
|
-
if cells.any? && headers.any?
|
173
|
-
rows << row_to_hash(cells, headers)
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
rows
|
178
|
-
end
|
179
|
-
|
180
|
-
def should_skip_header_row?(html, tr_content, index, has_thead, has_tbody)
|
181
|
-
(has_thead && html[0..html.index(tr_content)].include?('<thead') &&
|
182
|
-
!html[0..html.index(tr_content)].include?('</thead')) ||
|
183
|
-
(!has_thead && !has_tbody && index == 0)
|
184
|
-
end
|
185
|
-
|
186
113
|
def row_to_hash(cells, headers)
|
187
114
|
row_hash = {}
|
188
115
|
headers.each_with_index do |header, i|
|
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.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Francois Gaspard
|
@@ -23,6 +23,20 @@ dependencies:
|
|
23
23
|
- - "~>"
|
24
24
|
- !ruby/object:Gem::Version
|
25
25
|
version: '13.0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: nokogiri
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '1.14'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.14'
|
26
40
|
description: Provides a row-based object model and utility methods for creating, parsing,
|
27
41
|
transforming, and exporting Markdown tables in Ruby.
|
28
42
|
email:
|