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 +4 -4
- data/README.md +46 -331
- data/lib/marktable/formatters/html.rb +1 -1
- data/lib/marktable/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 776f59a6e0c0ee0b8e52f3fd4b9dd7a8ae8e1acbb93ff98504bb382dfecc93e9
|
4
|
+
data.tar.gz: 8df1d4b723562ced9f83e172165ed07d934e95fe2d9fe7c39411371b960dadfb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
5
|
+
## Table of Contents
|
5
6
|
|
6
|
-
|
7
|
+
- [Features](#features)
|
8
|
+
- [Installation](#installation)
|
9
|
+
- [Basic Usage](#basic-usage)
|
10
|
+
- [Documentation](#documentation)
|
11
|
+
- [License](#license)
|
7
12
|
|
8
|
-
|
13
|
+
## Features
|
9
14
|
|
10
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
43
|
+
## Basic Usage
|
161
44
|
|
162
|
-
|
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
|
-
|
51
|
+
# In your specs - compare tables across formats
|
52
|
+
expect(markdown_table).to match_markdown(expected_markdown)
|
243
53
|
|
244
|
-
|
245
|
-
|
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
|
-
|
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
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
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
|
-
|
69
|
+
table = Marktable.from_markdown(markdown)
|
300
70
|
|
301
|
-
|
71
|
+
table.to_a # Array of hashes
|
72
|
+
table.to_csv # CSV string
|
73
|
+
table.to_html # HTML table
|
302
74
|
|
303
|
-
|
304
|
-
|
305
|
-
|
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
|
-
|
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
|
-
|
366
|
-
|
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
|
-
##
|
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
|
+
`
|
data/lib/marktable/version.rb
CHANGED