to_md 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +15 -1
  3. data/lib/to_md.rb +43 -9
  4. data/lib/to_md/version.rb +1 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dd3f538d346aff27a74df1fe62eaee10bafa574f
4
- data.tar.gz: 1866c6ec639c4bdb6b91cec63b328af486af8265
3
+ metadata.gz: 6688ac7f100f6aa7f2c479ab6dd3bf1ec259ba2f
4
+ data.tar.gz: 499f870ef0013d92bde40162c68772639bdda8fa
5
5
  SHA512:
6
- metadata.gz: d993c662f0dd7d091d885726af441184ec8a1c6e1e9d46ce6d57c991b2768f3b51d3e4433b299bcebdde36ab837b1026612ab0c9db0c573e9bd72ba237a10f37
7
- data.tar.gz: 8f42d6772b1f9e063be8b20582a0414328203537f571233106b8b2ef8bc0c3bcb142b3926ad170d13eb703c97e2424f898cd89ea1c96ba70833108b508e5d073
6
+ metadata.gz: 65c833144df7a80e3d7e295262ce98c1028a5cde11459cd3eb628860c4e08ccca9bc4c8dda042ee76636302fa61f802e1b138619dad39c1deba5c78e2a8c7940
7
+ data.tar.gz: c7c10416e7a550a638c4a72fd3c9d86249d859a195de541133e09c108c0495a84b934f9855837799122917cc2772e6bc78805c50e8d0b449eaa25eedb0c6d1d9
data/README.md CHANGED
@@ -36,6 +36,17 @@ puts [['#', 'japanese'], [1, '一']].to_md
36
36
  # | # | japanese |
37
37
  # | --- | --- |
38
38
  # | 1 | 一 |
39
+
40
+ puts [{id: 1, name: 'John'}, {id: 2, name: 'David'}].to_md
41
+ # | id | name |
42
+ # | --- | --- |
43
+ # | 1 | John |
44
+ # | 2 | David |
45
+
46
+ puts [[:name, :age], {id: 3, name: 'Robert', age: 20}].to_md
47
+ # | name | age |
48
+ # | --- | --- |
49
+ # | Robert | 20 |
39
50
  ```
40
51
 
41
52
  ## Development
@@ -46,9 +57,12 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
46
57
 
47
58
  ## Contributing
48
59
 
49
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/to_md. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yasslab/to_md. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org/) code of conduct.
50
61
 
51
62
 
52
63
  ## License
53
64
 
65
+ Copyright © 2015 [YassLab](http://yasslab.jp)
66
+
54
67
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
68
+
@@ -1,19 +1,53 @@
1
1
  require "to_md/version"
2
2
 
3
+ require "to_md/version"
4
+
3
5
  module ToMd
4
6
  refine(Array) do
5
7
  def to_md
6
- # list
7
- unless all? {|e| e.is_a?(Array) }
8
- return map {|s| "- #{s}"}.join($/)
8
+ if first.is_a? Array
9
+ TableBuilder.build first, drop(1)
10
+ elsif all?{|e|e.is_a? Hash}
11
+ TableBuilder.build_with_hash self
12
+ else
13
+ #list
14
+ map {|s| "- #{s}"}.join($/)
15
+ end
16
+ end
17
+ end
18
+
19
+ module TableBuilder
20
+ class << self
21
+
22
+ def build_with_hash items
23
+ keys = items.map(&:keys).inject(:|)
24
+ build keys, items
9
25
  end
10
26
 
11
- # table
12
- [
13
- "| #{first.join(' | ')} |",
14
- "|#{' --- |' * first.size}",
15
- drop(1).map {|s| "| #{s.join(' | ')} |"}.join($/)
16
- ].join($/) + $/
27
+ def build header, items
28
+ [
29
+ row(header),
30
+ row(header.map{'---'}),
31
+ *items.map{|item|row item_to_array(header, item)}
32
+ ].join($/)+$/
33
+ end
34
+
35
+ def item_to_array header, item
36
+ if Hash === item
37
+ header.map{|key|item[key]}
38
+ else
39
+ item = [*item] unless Array === item
40
+ header.zip(item).map(&:last)
41
+ end
42
+ end
43
+
44
+ def row array
45
+ '| '+array.map{|c|escape_cell c}.join(' | ')+' |'
46
+ end
47
+
48
+ def escape_cell text
49
+ text.to_s.gsub('|', '&#124;').gsub($/, '&#10;')
50
+ end
17
51
  end
18
52
  end
19
53
  end
@@ -1,3 +1,3 @@
1
1
  module ToMd
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_md
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seiei Miyagi