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.
- checksums.yaml +4 -4
- data/README.md +15 -1
- data/lib/to_md.rb +43 -9
- data/lib/to_md/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6688ac7f100f6aa7f2c479ab6dd3bf1ec259ba2f
|
4
|
+
data.tar.gz: 499f870ef0013d92bde40162c68772639bdda8fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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/
|
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
|
+
|
data/lib/to_md.rb
CHANGED
@@ -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
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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('|', '|').gsub($/, ' ')
|
50
|
+
end
|
17
51
|
end
|
18
52
|
end
|
19
53
|
end
|
data/lib/to_md/version.rb
CHANGED