marktable 0.1.0 โ†’ 0.1.1

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: f4dc7dda731d7cbac0c2b155c3c572c44954ca2f65281f3cf976b05ba9cd1532
4
- data.tar.gz: 1261f5a0afd0bbf45a16d8e9d369ef61b8e9313e83788a1b5e10be8871fac962
3
+ metadata.gz: 776f59a6e0c0ee0b8e52f3fd4b9dd7a8ae8e1acbb93ff98504bb382dfecc93e9
4
+ data.tar.gz: 8df1d4b723562ced9f83e172165ed07d934e95fe2d9fe7c39411371b960dadfb
5
5
  SHA512:
6
- metadata.gz: 8127ab236b08a55f8d8d2fafcb116996d347527085af2dc1e71632b55930d326eae6e6b8bbac2e55c33965effa4d14863b4741e80c70297b2079bf715153dab6
7
- data.tar.gz: 8400c3fbb70a9330d2db6c36f23599161ae35e4766c614597f773f38305ae9bf62708d053477d5e727d7cf51dee76490bb23743c0c77aca76a4a87997795e9be
6
+ metadata.gz: ed5b2e94ca382771d81d5c6423fa43e38dded1cbfe315c6fedd098b0f77efc66d18b7e62372f8ac2e16884a3d3495f5631fee13b96623d8575276e8b6827b619
7
+ data.tar.gz: 28d859a6e6c1709ee0be5c3ff7f63fd91f87688d6c5169d0193790889e590ce4b0602afa7c1a5f48693227bf963cca9396a26900f037899351d4c54c69783833
data/README.md CHANGED
@@ -1,27 +1,26 @@
1
1
  # Marktable
2
2
 
3
+ Marktable is a Ruby gem for easily converting between different table formats and testing table content in your specs.
3
4
 
4
- A powerful Ruby library for parsing, manipulating, and generating Markdown tables with an elegant and intuitive API.
5
+ ## Table of Contents
5
6
 
6
- ## ๐Ÿ“š Overview
7
+ - [Features](#features)
8
+ - [Installation](#installation)
9
+ - [Basic Usage](#basic-usage)
10
+ - [Documentation](#documentation)
11
+ - [License](#license)
7
12
 
8
- Marktable allows you to seamlessly work with Markdown tables using familiar Ruby data structures. Whether you're parsing tables from Markdown documents, generating tables for documentation, or manipulating tabular data, Marktable provides simple yet powerful tools for all your Markdown table needs.
13
+ ## Features
9
14
 
10
- ## โœจ Features
15
+ * RSpec matchers to compare tables across formats
16
+ * Convert between multiple table formats:
17
+ - Markdown tables
18
+ - Arrays of arrays
19
+ - Arrays of hashes
20
+ - CSV
21
+ - HTML tables
11
22
 
12
- - **Custom RSpec matcher** for testing Markdown-like data structures (including html tables) against expected Markdown output
13
- - **Parse** Markdown tables into Ruby data structures (arrays or hashes)
14
- - **Generate** beautifully formatted Markdown tables from Ruby objects
15
- - **Filter** and **transform** table data with Ruby's familiar block syntax
16
- - **Auto-detect** headers from properly formatted Markdown tables
17
- - **Handle** tables with or without headers
18
- - **Support** for mismatched columns and keys in data
19
- - **Convert** between array-based and hash-based representations
20
-
21
- ## Not supported
22
- - Non-string values (E.g. complex objects) in the table rows.
23
-
24
- ## ๐Ÿ“ฆ Installation
23
+ ## Installation
25
24
 
26
25
  Add this line to your application's Gemfile:
27
26
 
@@ -31,343 +30,59 @@ gem 'marktable'
31
30
 
32
31
  And then execute:
33
32
 
34
- ```bash
35
- bundle install
36
- ```
37
-
38
- Or install it yourself as:
39
-
40
- ```bash
41
- gem install marktable
42
- ```
43
-
44
- ## ๐Ÿš€ Quick Start
45
-
46
- ### Main use case (Rspec only at this time)
47
-
48
- This gem started as a pet project aiming at:
49
- - Simplifying the process of testing html table content
50
- - Making these specs more developer-friendly, with a very readable format
51
-
52
-
53
- ```ruby
54
- visit my_path
55
- actual_table = page.find('#my-table')
56
- expected_table = <<~MARKDOWN
57
- | Name | Age | City |
58
- | ----- | --- | -------- |
59
- | John | 30 | New York |
60
- | Jane | 25 | Boston |
61
- | Bob | 17 | Chicago |
62
- MARKDOWN
63
-
64
- expect(actual_table).to match_markdown(expected_table)
65
- ```
66
-
67
- In case of semantical mismatch, the matcher will show an easy to verify markdown representation of the tables:
68
-
69
- ```markdown
70
- Expected markdown table to match:
71
-
72
- Expected:
73
- | Name | Age | City |
74
- | ----- | --- | -------- |
75
- | John | 30 | New York |
76
- | Jane | 25 | Boston |
77
- | Bob | 17 | Chicago |
78
-
79
- Actual:
80
- | Name | Age |
81
- | ---- | --- |
82
- | John | 31 |
83
- | Jane | 25 |
84
-
85
- Parsed expected data: [{"Name" => "John", "Age" => "30"}, {"Name" => "Jane", "Age" => "25"}, {"Name" => "Bob", "Age" => "17"}]
86
- Parsed actual data: [{"Name" => "John", "Age" => "31"}, {"Name" => "Jane", "Age" => "25"}]
87
33
  ```
88
-
89
-
90
-
91
- ### Parsing a Markdown Table
92
-
93
- ```ruby
94
- require 'marktable'
95
-
96
- markdown_table = <<~MARKDOWN
97
- | Name | Age | City |
98
- | ----- | --- | -------- |
99
- | John | 30 | New York |
100
- | Jane | 25 | Boston |
101
- MARKDOWN
102
-
103
- # Parse into an array of hashes (with auto-detected headers)
104
- data = Marktable.parse(markdown_table)
105
- # => [
106
- # {"Name"=>"John", "Age"=>"30", "City"=>"New York"},
107
- # {"Name"=>"Jane", "Age"=>"25", "City"=>"Boston"}
108
- # ]
109
-
110
- # Access data easily
111
- puts data.first["Name"] # => "John"
34
+ $ bundle install
112
35
  ```
113
36
 
114
- ### Generating a Markdown Table
115
-
116
- ```ruby
117
- # Create a new table and add rows
118
- table = Marktable.generate do |t|
119
- t << {"Name" => "John", "Age" => "30", "City" => "New York"}
120
- t << {"Name" => "Jane", "Age" => "25", "City" => "Boston"}
121
- t << {"Name" => "Bob", "Age" => "17", "City" => "Chicago"}
122
- end
37
+ Or install it yourself:
123
38
 
124
- puts table
125
- # | Name | Age | City |
126
- # | ---- | --- | -------- |
127
- # | John | 30 | New York |
128
- # | Jane | 25 | Boston |
129
- # | Bob | 17 | Chicago |
130
39
  ```
131
-
132
- ## ๐Ÿ“– Usage Guide
133
-
134
- ### Working with Table Objects
135
-
136
- Create a table object for more advanced operations:
137
-
138
- ```ruby
139
- # Create from a markdown string
140
- table = Marktable.new(markdown_table)
141
-
142
- # Or create from array data
143
- array_data = [
144
- ["Name", "Age", "City"],
145
- ["John", "30", "New York"],
146
- ["Jane", "25", "Boston"],
147
- ["Bob", "17", "Chicago"]
148
- ]
149
- table = Marktable.new(array_data, headers: true)
150
-
151
- # Or with auto-detected headers
152
- array_data = [
153
- { "Name" => "John", "Age" => "30", "City" => "New York" },
154
- { "Name" => "Jane", "Age" => "25", "City" => "Boston" },
155
- { "Name" => "Bob", "Age" => "17", "City" => "Chicago" }
156
- ]
157
- table = Marktable.new(array_data)
40
+ $ gem install marktable
158
41
  ```
159
42
 
160
- ### Filtering Rows
43
+ ## Basic Usage
161
44
 
162
- Filter rows using pattern matching or blocks:
163
-
164
- ```ruby
165
- # Filter with a regex pattern
166
- nyc_residents = table.filter(/New York/)
167
-
168
- # Filter with a block
169
- adults = table.filter do |row|
170
- row["Age"].to_i >= 18
171
- end
172
-
173
- puts adults
174
- # | Name | Age | City |
175
- # | ---- | --- | -------- |
176
- # | John | 30 | New York |
177
- # | Jane | 25 | Boston |
178
- ```
179
-
180
- ### Transforming Data
181
-
182
- Transform table data with the map method:
183
-
184
- ```ruby
185
- # Add 5 years to everyone's age
186
- older = table.map do |row|
187
- row.merge("Age" => (row["Age"].to_i + 5).to_s)
188
- end
189
-
190
- puts older
191
- # | Name | Age | City |
192
- # | ---- | --- | -------- |
193
- # | John | 35 | New York |
194
- # | Jane | 30 | Boston |
195
- # | Bob | 22 | Chicago |
196
- ```
197
-
198
- ### Working with Arrays
199
-
200
- Use arrays instead of hashes for row data:
201
-
202
- ```ruby
203
- # Create a table with array rows
204
- table = Marktable.new([], headers: false)
205
- table << ["John", "30", "New York"]
206
- table << ["Jane", "25", "Boston"]
207
-
208
- puts table
209
- # | John | 30 | New York |
210
- # | Jane | 25 | Boston |
211
- ```
212
-
213
- ### Mixed Row Types
214
-
215
- Marktable can handle mixed row types (arrays and hashes):
216
-
217
- ```ruby
218
- table = Marktable.generate do |t|
219
- t << {"Name" => "John", "Age" => "30", "City" => "New York"}
220
- t << ["Jane", "25", "Boston"] # Array with same column count
221
- end
222
-
223
- puts table
224
- # | Name | Age | City |
225
- # | ---- | --- | -------- |
226
- # | John | 30 | New York |
227
- # | Jane | 25 | Boston |
228
- ```
229
-
230
- ## ๐Ÿงช Testing with Custom Matchers
231
-
232
- Marktable includes a custom RSpec matcher, `match_markdown`, to make testing Markdown-compatible data structures simple and reliable.
233
-
234
- ### Using the `match_markdown` Matcher
235
-
236
- First, require the matcher in your spec_helper.rb or in the specific test file:
45
+ ### RSpec Matchers
237
46
 
238
47
  ```ruby
48
+ # In your spec_helper.rb
239
49
  require 'marktable/rspec'
240
- ```
241
50
 
242
- #### Testing Markdown Table Output
51
+ # In your specs - compare tables across formats
52
+ expect(markdown_table).to match_markdown(expected_markdown)
243
53
 
244
- ```ruby
245
- RSpec.describe ReportGenerator do
246
- describe "#generate_customer_table" do
247
- it "generates the expected customer table" do
248
- actual = ReportGenerator.new(customers).generate_table
249
- # Presume actual contains:
250
- # [
251
- # { id: 1, name: "John Smith", status: "Active" },
252
- # { id: 2, name: "Jane Doe", status: "Pending" }
253
- # ]
254
-
255
- expected = <<~MARKDOWN
256
- | ID | Name | Status |
257
- | -- | ---------- | ------- |
258
- | 1 | John Smith | Active |
259
- | 2 | Jane Doe | Pending |
260
- MARKDOWN
261
-
262
- expect(result).to match_markdown(expected)
263
- end
264
- end
265
- end
54
+ html_table = page.find('#users-table')
55
+ expect(html_table).to match_markdown(expected_markdown)
266
56
  ```
267
57
 
268
- #### Testing HTML Table Output
269
-
270
- The `match_markdown` matcher can also compare HTML tables by extracting their semantic content:
58
+ ### Converting Between Formats
271
59
 
272
60
  ```ruby
273
- RSpec.describe HtmlReportGenerator do
274
- describe "#generate_sales_report" do
275
- it "generates the expected HTML table" do
276
- sales_data = [
277
- { product: "Widget A", quantity: 150, revenue: "$3,000" },
278
- { product: "Widget B", quantity: 75, revenue: "$1,875" }
279
- ]
280
-
281
- generator = HtmlReportGenerator.new(sales_data)
282
- html_output = generator.generate_sales_report
283
-
284
- # The matcher extracts the data structure from both the HTML and expected markdown
285
- expected = <<~MARKDOWN
286
- | Product | Quantity | Revenue |
287
- | -------- | -------- | ------- |
288
- | Widget A | 150 | $3,000 |
289
- | Widget B | 75 | $1,875 |
290
- MARKDOWN
291
-
292
- # This will pass even though one is HTML and one is Markdown!
293
- expect(html_output).to match_markdown(expected)
294
- end
295
- end
296
- end
297
- ```
61
+ # From markdown to other formats
62
+ markdown = <<~MARKDOWN
63
+ | Name | Age |
64
+ |------|----- |
65
+ | Alice | 30 |
66
+ | Bob | 25 |
67
+ MARKDOWN
298
68
 
299
- #### Testing API JSON Responses
69
+ table = Marktable.from_markdown(markdown)
300
70
 
301
- The matcher is also helpful when testing APIs that return tabular data:
71
+ table.to_a # Array of hashes
72
+ table.to_csv # CSV string
73
+ table.to_html # HTML table
302
74
 
303
- ```ruby
304
- RSpec.describe "Products API" do
305
- describe "GET /api/products" do
306
- it "returns products in the expected format" do
307
- # Setup test data and make request
308
- get "/api/products"
309
-
310
- # Parse JSON response
311
- json_response = JSON.parse(response.body)
312
-
313
- # Convert API response to a table
314
- table = Marktable.new(json_response)
315
-
316
- expected = <<~MARKDOWN
317
- | id | name | category | price |
318
- | -- | ----------- | -------- | ------ |
319
- | 1 | Product One | Books | $19.99 |
320
- | 2 | Product Two | Games | $59.99 |
321
- MARKDOWN
322
-
323
- expect(table).to match_markdown(expected)
324
- end
325
- end
326
- end
75
+ # From arrays or hashes to markdown
76
+ data = [{ 'Name' => 'Alice', 'Age' => '30' }]
77
+ Marktable.from_array(data).to_md # Markdown table
327
78
  ```
328
79
 
329
- The `match_markdown` matcher compares tables semantically rather than character-by-character, which means:
330
-
331
- - It ignores differences in whitespace padding
332
- - It handles different column ordering in hash-based tables
333
- - It works with both HTML and Markdown table formats
334
- - It provides clear error messages showing the differences between tables
335
-
336
- ## ๐Ÿ“‹ API Reference
337
-
338
- ### Class Methods
339
-
340
- - `Marktable.parse(table, headers: nil)` - Parse table string or array into an array of hashes/arrays
341
- - `Marktable.new(table, headers: nil)` - Create a new Table object
342
- - `Marktable.parse_line(row)` - Parse a single markdown row into an array
343
- - `Marktable.generate { |t| ... }` - Generate a table with a block
344
-
345
- ### Instance Methods
346
-
347
- - `table.to_a` - Convert table to array of hashes/arrays
348
- - `table.to_s` / `table.generate` - Generate markdown representation
349
- - `table.empty?` - Check if table is empty
350
- - `table.column_count` - Get column count
351
- - `table << row_data` - Add a row to the table
352
- - `table.filter(pattern = nil) { |row| ... }` - Filter rows by pattern or block
353
- - `table.map { |row| ... }` - Transform rows with a block
354
-
355
- ## ๐Ÿงช Development
356
-
357
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
358
-
359
- To install this gem onto your local machine, run `bundle exec rake install`.
360
-
361
- ## ๐Ÿค Contributing
362
-
363
- Bug reports and pull requests are welcome on GitHub at https://github.com/yourusername/marktable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/yourusername/marktable/blob/main/CODE_OF_CONDUCT.md).
80
+ ## Documentation
364
81
 
365
- 1. Fork the repository
366
- 2. Create your feature branch: `git checkout -b my-new-feature`
367
- 3. Commit your changes: `git commit -am 'Add some feature'`
368
- 4. Push to the branch: `git push origin my-new-feature`
369
- 5. Submit a pull request
82
+ * [Full Examples](docs/examples.md) - Detailed usage examples
83
+ * [API Documentation](docs/api_documentation.md) - Complete API reference
370
84
 
371
- ## ๐Ÿ“„ License
85
+ ## License
372
86
 
373
87
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
88
+ `
@@ -23,7 +23,7 @@ module Marktable
23
23
  doc.tbody do
24
24
  rows.each do |row|
25
25
  doc.tr do
26
- row.each_value do |cell|
26
+ row.values.each do |cell|
27
27
  doc.td do
28
28
  cell_text = cell.to_s
29
29
  if cell_text.include?('\n')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Marktable
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francois Gaspard